source: trunk/MagicSoft/Cosy/catalog/StarCatalog.cc@ 3664

Last change on this file since 3664 was 2407, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 15.8 KB
Line 
1#include "StarCatalog.h"
2
3#include <iomanip.h> // cout
4#include <iostream.h> // cout
5
6#include <TSystem.h>
7
8#include "slalib.h"
9#include "slamac.h"
10#include "File.h"
11
12#include "MStarList.h"
13
14ClassImp(StarCatalog);
15
16StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fSao(NULL), fSrt(NULL), fEntries(0), fSinAngle(0), fCosAngle(1)
17{
18 // p = pointer to MainFrame (not owner)
19
20 //
21 // read index file
22 //
23 File idx("sao/sao-sort.idx", "r");
24 if (!idx)
25 return;
26
27 while (!idx.Eof())
28 {
29 idx.Newline();
30 fEntries++;
31 }
32
33 idx.Reset();
34
35 fSrt = new sort_t[fEntries];
36
37 for (int i=0; i<fEntries; i++)
38 {
39 fSrt[i].ra = idx.Geti(4);
40 fSrt[i].dec = idx.Geti(4);
41 fSrt[i].nr = idx.Geti(10);
42 idx.Newline();
43 }
44
45 //
46 // open catalog
47 //
48 fSao = new SaoFile("sao/sao-sort.cmp");
49}
50
51StarCatalog::~StarCatalog()
52{
53 if (fSrt)
54 delete fSrt;
55 if (fSao)
56 delete fSao;
57}
58
59void StarCatalog::SetPixSize(const double pixsize)
60{
61 fPixSize = D2PI/360.0 * pixsize;
62
63 fWidth = fPixSize * 768/2;
64 fHeight = fPixSize * 576/2;
65}
66
67double StarCatalog::GetPixSize() const
68{
69 return fPixSize*3600*360.0/D2PI;
70}
71
72
73void StarCatalog::SetAltAz(const AltAz &altaz)
74{
75 fAltAz = altaz * D2PI/360.0;
76
77 cout << "Set --> Alt: " << 360.0/D2PI*fAltAz.Alt();
78 cout << " Az: " << fAltAz.Az() << endl;
79
80 fRaDec = CalcRaDec(fAltAz);
81
82 cout << "Ra: " << 360.0/D2PI*fRaDec.Ra();
83 cout << " Dec: " << 360.0/D2PI*fRaDec.Dec() << endl;
84
85 CalcAltAzRange();
86 CalcRaDecRange();
87}
88
89void StarCatalog::SetRaDec(const RaDec &radec)
90{
91 fRaDec = radec;
92 fRaDec *= D2PI/360.0;
93
94 fAltAz = CalcAltAz(fRaDec);
95
96 //cout << "Alt: " << 360.0/D2PI*fAltAz.Alt() << " ";
97 //cout << "Az: " << 360.0/D2PI*fAltAz.Az() << endl;
98
99 CalcRaDecRange();
100 CalcAltAzRange();
101}
102
103void StarCatalog::CalcAltAzRange()
104{
105 byte fAlt0[180];
106
107 for (int h=0; h<180; h++)
108 fAlt0[h] = kFALSE;
109
110 for (int h=0; h<360; h++)
111 fAz0[h] = kFALSE;
112
113 double az0, alt0;
114 double az1, alt1;
115 //
116 // scan horizontal border
117 //
118 for (int x=-768/2; x<768/2+1; x++)
119 {
120 slaDh2e(DPI+x*fPixSize, -fHeight, DPI/2-fAltAz.Alt(), &az0, &alt0);
121 slaDh2e(DPI+x*fPixSize, +fHeight, DPI/2-fAltAz.Alt(), &az1, &alt1);
122
123 const int z0 = ((int)(360.0/D2PI*(az0+fAltAz.Az()))+360)%360;
124 const int t0 = (int)(360.0/D2PI*alt0);
125
126 fAz0[z0] = kTRUE;
127
128 if (-89<=t0 && t0<=90)
129 fAlt0[90-t0] = kTRUE;
130
131 const int z1 = ((int)(360.0/D2PI*(az1+fAltAz.Az()))+360)%360;
132 const int t1 = (int)(360.0/D2PI*alt1);
133
134 fAz0[z1] = kTRUE;
135
136 if (-89<=t1 && t1<=90)
137 fAlt0[90-t1] = kTRUE;
138 }
139
140 //
141 // scan vertical border
142 //
143 for (int y=-576/2; y<576/2+1; y++)
144 {
145 slaDh2e(DPI-fWidth, y*fPixSize, DPI/2-fAltAz.Alt(), &az0, &alt0);
146 slaDh2e(DPI+fWidth, y*fPixSize, DPI/2-fAltAz.Alt(), &az1, &alt1);
147
148 const int z0 = ((int)(360.0/D2PI*(az0+fAltAz.Az()))+360)%360;
149 const int t0 = (int)(360.0/D2PI*alt0);
150
151 fAz0[z0] = kTRUE;
152
153 if (-89<=t0 && t0<=90)
154 fAlt0[90-t0] = kTRUE;
155
156 const int z1 = ((int)(360.0/D2PI*(az1+fAltAz.Az()))+360)%360;
157 const int t1 = (int)(360.0/D2PI*alt1);
158
159 fAz0[z1] = kTRUE;
160
161 if (-89<=t1 && t1<=90)
162 fAlt0[90-t1] = kTRUE;
163 }
164
165 //
166 // count degrees of azimut
167 //
168 fAzCnt=0;
169 for (int x=0; x<360; x++)
170 if (fAz0[x])
171 fAzCnt++;
172
173 //cout << "fAzCnt: " << setw(3) << fAzCnt << " " << flush;
174
175 //
176 // calculate min and max of altitude
177 //
178 fAltMin=0;
179 fAltMax=0;
180 for (int y=0; y<180; y++)
181 {
182 if (fAlt0[y])
183 fAltMax = y;
184
185 if (fAlt0[179-y])
186 fAltMin = 179-y;
187 }
188
189 fAltMin -= 90;
190 fAltMax -= 90;
191
192 //
193 // check whether altaz north- or south-pole is in the visible region
194 //
195 byte img[768*576];
196 if (DrawAltAz(0, img, 90, 0))
197 {
198 fAltMax=89;
199 cout << "Alt Az Pole1 Inside!" << endl;
200 }
201 if (DrawAltAz(0, img, -90, 0))
202 {
203 fAltMin=-90;
204 cout << "Alt Az Pole2 Inside!" << endl;
205 }
206
207 //cout << "fAltMin: " << setw(3) << fAltMin << " ";
208 //cout << "fAltMax: " << setw(3) << fAltMax << endl;
209}
210
211void StarCatalog::CalcRaDecRange()
212{
213 //
214 // calculate range to search in
215 //
216 byte fDec[180];
217
218 for (int h=0; h<180; h++)
219 fDec[h] = kFALSE;
220
221 for (int h=0; h<360; h++)
222 fRa0[h] = kFALSE;
223
224 double ha0, ha1;
225 double de0, de1;
226
227 const double phi = GetPhi();
228 const double alpha = GetAlpha();
229 //
230 // scan horizontal border
231 //
232 for (int x=-768/2; x<768/2+1; x++)
233 {
234 double dx, dy;
235 slaDh2e(DPI-x*fPixSize, -fHeight, DPI/2-fAltAz.Alt(), &dx, &dy);
236 slaDh2e(fAltAz.Az()+dx, -dy, phi, &ha0, &de0);
237
238 slaDh2e(DPI-x*fPixSize, +fHeight, DPI/2-fAltAz.Alt(), &dx, &dy);
239 slaDh2e(fAltAz.Az()+dx, -dy, phi, &ha1, &de1);
240
241 const int h0 = ((int)(360.0/D2PI*(alpha-ha0))+360)%360;
242 const int d0 = (int)(360.0/D2PI*de0);
243
244 fRa0[h0] = kTRUE;
245
246 if (-90<=d0 && d0<=89)
247 fDec[d0+90] = kTRUE;
248
249 const int h1 = ((int)(360.0/D2PI*(alpha-ha1))+360)%360;
250 const int d1 = (int)(360.0/D2PI*de1);
251
252 fRa0[h1] = kTRUE;
253
254 if (-90<=d1 && d1<=89)
255 fDec[d1+90] = kTRUE;
256 }
257
258 //
259 // scan vertical border
260 //
261 for (int y=-576/2; y<576/2+1; y++)
262 {
263 double dx, dy;
264 slaDh2e(DPI-fWidth, -y*fPixSize, DPI/2-fAltAz.Alt(), &dx, &dy);
265 slaDh2e(fAltAz.Az()+dx, -dy, phi, &ha0, &de0);
266
267 slaDh2e(DPI+fWidth, -y*fPixSize, DPI/2-fAltAz.Alt(), &dx, &dy);
268 slaDh2e(fAltAz.Az()+dx, -dy, phi, &ha1, &de1);
269
270 const int h0 = ((int)(360.0/D2PI*(alpha-ha0))+360)%360;
271 const int d0 = (int)(360.0/D2PI*de0);
272
273 fRa0[h0] = kTRUE;
274
275 if (-90<=d0 && d0<=89)
276 fDec[d0+90] = kTRUE;
277
278 const int h1 = ((int)(360.0/D2PI*(alpha-ha1))+360)%360;
279 const int d1 = (int)(360.0/D2PI*de1);
280
281 fRa0[h1] = kTRUE;
282
283 if (-90<=d1 && d1<=89)
284 fDec[d1+90] = kTRUE;
285 }
286
287 //
288 // count degrees of right ascension
289 //
290 fRaCnt=0;
291 for (int x=0; x<360; x++)
292 if (fRa0[x])
293 fRaCnt++;
294 //cout << "fRaCnt: " << setw(3) << fRaCnt << " " << flush;
295
296 //
297 // calculate min and max of declination
298 //
299 for (int y=0; y<180; y++)
300 {
301 if (fDec[y])
302 fDecMax = y;
303
304 if (fDec[179-y])
305 fDecMin = 179-y;
306 }
307
308 fDecMin -= 90;
309 fDecMax -= 90;
310
311 //
312 // check whether radec north- or south-pole is in the visible region
313 //
314 byte img[768*576];
315 if (DrawRaDec(0, img, 0, 90))
316 {
317 fDecMax=89;
318 cout << "Ra Dec Pole1 Inside!" << endl;
319 }
320 if (DrawRaDec(0, img, 0, -90))
321 {
322 fDecMin=-90;
323 cout << "Ra Dec Pole1 Inside!" << endl;
324 }
325
326 //cout << "fDecMin: " << setw(3) << fDecMin << " ";
327 //cout << "fDecMax: " << setw(3) << fDecMax << endl;
328}
329
330void StarCatalog::DrawSCAltAz(byte *img, const int color) const
331{
332 //
333 // ------------ draw az lines ---------------
334 //
335 for (int az=0; az<360; az++)
336 {
337 if (!fAz0[az])
338 continue;
339
340 for (double alt=fAltMin-1; alt<fAltMax+1; alt+=0.006*(fAltMax-fAltMin))
341 {
342 if ((alt>88 && az%5) || alt>89.5)
343 continue;
344
345 DrawAltAz(color, img, alt, az);
346 }
347 }
348
349 //
350 // ------------ draw alt lines ---------------
351 //
352 for (int alt=fAltMin; alt<fAltMax+1; alt++)
353 {
354 for (double az=0; az<360; az+=0.004*fAzCnt)
355 {
356 if (!fAz0[(int)az] && !fAz0[(int)(az+359)%360] && !fAz0[(int)(az+1)%360])
357 continue;
358
359 DrawAltAz(color, img, alt, az);
360 }
361 }
362}
363
364void StarCatalog::DrawSCRaDec(byte *img, const int color) const
365{
366 //
367 // ------------ draw ra lines ---------------
368 //
369 for (int ra=0; ra<360; ra++)
370 {
371 if (!fRa0[ra])
372 continue;
373
374 for (double dec=fDecMin-1; dec<fDecMax+1; dec+=0.005*(fDecMax-fDecMin))
375 {
376 if ((dec>88 && ra%5) || dec>89.5)
377 continue;
378
379 DrawRaDec(color, img, ra, dec, ra==0||ra==90);
380 }
381 }
382
383 //
384 // ------------ draw dec lines ---------------
385 //
386 for (int dec=fDecMin; dec<fDecMax+1; dec++)
387 {
388 for (double ra=0; ra<360; ra+=0.003*fRaCnt)
389 {
390 if (!fRa0[(int)ra])
391 continue;
392
393 DrawRaDec(color, img, ra, dec, dec==89);
394 }
395 }
396}
397
398void StarCatalog::DrawCross(byte *img, const int x, const int y)
399{
400 for (int dx=-4; dx<5; dx++)
401 if (dx) img[y*768+x+dx] = 0xff;
402
403 for (int dy=-4; dy<5; dy++)
404 if (dy) img[(y+dy)*768+x] = 0xff;
405}
406
407void StarCatalog::GetImg(byte *img, byte *cimg, MStarList &list) const
408{
409 memset(cimg, 0, 768*576);
410
411 DrawSCAltAz(cimg, 2<<4);
412 DrawSCRaDec(cimg, 2);
413
414 DrawStars(list, cimg);
415 DrawCross(img, 768/2, 576/2);
416}
417
418void StarCatalog::GetImg(byte *img, byte *cimg, const double utc,
419 const RaDec &radec)
420{
421 MStarList list;
422 GetStars(list, utc, radec);
423 GetImg(img, cimg, list);
424 /*
425 // memset(img, 0, 768*576);
426 SetMjd(utc);
427 //fAlpha = sla.GetAlpha();
428 SetRaDec(radec);
429 //CalcImg(cimg);
430 */
431}
432
433void StarCatalog::GetImg(byte *img, byte *cimg, const double utc,
434 const AltAz &altaz)
435{
436 MStarList list;
437 GetStars(list, utc, altaz);
438 GetImg(img, cimg, list);
439 /*
440 // memset(img, 0, 768*576);
441
442 SetMjd(utc);
443 //fAlpha = sla.GetAlpha();
444 SetAltAz(altaz);
445
446 CalcRaDecRange();
447
448 //CalcImg(img);
449 */
450
451}
452
453void StarCatalog::GetStars(MStarList &list, const double utc, const RaDec &radec)
454{
455 SetMjd(utc);
456 SetRaDec(radec);
457
458 CalcStars(list);
459}
460
461void StarCatalog::GetStars(MStarList &list, const double utc, const AltAz &altaz)
462{
463 SetMjd(utc);
464 SetAltAz(altaz);
465
466 CalcRaDecRange();
467 CalcStars(list);
468}
469
470void StarCatalog::DrawCircle(int color, byte *img, int xx, int yy, int size)
471{
472 for (int x=xx-size; x<xx+size+1; x++)
473 {
474 if (x<0 || x>767)
475 continue;
476
477 const float p = xx+size-x;
478 const float q = 2*size - p;
479 const int h = (int)sqrt(p*q);
480
481 const int y1 = yy-h;
482 if (y1>=0 && y1<576)
483 img[y1*768+x] = color;
484
485 const int y2 = yy+h;
486 if (y2>=0 && y2<576)
487 img[y2*768+x] = color;
488 }
489}
490
491Bool_t StarCatalog::DrawAltAz(const int color, byte *img, double alt, double az, int size) const
492{
493 //
494 // alt/az[deg] -> alt/az[rad]
495 //
496 alt *= D2PI/360.0;
497 az *= D2PI/360.0;
498
499 //
500 // alt/az[rad] -> alt/az[pix]
501 //
502 double dx, dy;
503 slaDe2h(az-fAltAz.Az(), -alt, DPI/2-fAltAz.Alt(), &dx, &dy);
504
505 //
506 // Align alt/az[pix]
507 //
508 const int xx = (int)(((dx-DPI)*fCosAngle - dy*fSinAngle + fWidth)/fPixSize);
509 const int yy = (int)(((dx-DPI)*fSinAngle + dy*fCosAngle + fHeight)/fPixSize);
510 //const int xx = 767-(int)((fWidth-dx+DPI)/fPixSize);
511 //const int yy = (int)((fHeight+dy)/fPixSize);
512
513 //
514 // Range Check
515 //
516 if (!(0<=xx && xx<768 && 0<=yy && yy<576))
517 return kFALSE;
518
519 //
520 // Draw
521 //
522 DrawCircle(color, img, xx, yy, size);
523
524 return kTRUE;
525}
526
527Bool_t StarCatalog::Draw(const int color, byte *img, const AltAz &altaz)
528{
529 return DrawAltAz(color, img, altaz.Alt(), altaz.Az());
530}
531
532/*
533Bool_t StarCatalog::Draw(const int color, byte *img, const SaoFile *sao)
534{
535 if (sao->MagV() > fLimitMag)
536 return kFALSE;
537
538 //
539 // ---- mean to observed ---
540 //
541 AltAz altaz=CalcAltAz(sao->GetRaDec()) * 360.0/D2PI;
542
543 const int mag = (10 - (sao->MagV()>1 ? (int)sao->MagV() : 1))/2;
544
545 //
546 // ---- imaging -----
547 //
548 return DrawAltAz(color, img, altaz.Alt(), altaz.Az(), mag);
549}
550*/
551
552Bool_t StarCatalog::DrawRaDec(const int color, byte *img, double ra, double dec, int size) const
553{
554 //
555 // radec[deg] -> radec[rad]
556 //
557 ra *= D2PI/360.0;
558 dec *= D2PI/360.0;
559
560 //
561 // radec[rad] -> hadec[rad]
562 //
563 const double ha = GetAlpha()-ra;
564
565 //
566 // hadec[rad] -> altaz[rad]
567 //
568 double alt, az;
569 slaDe2h(ha, dec, GetPhi(), &az, &alt);
570
571 //
572 // altaz[rad] -> altaz[deg]
573 //
574 alt *= 360.0/D2PI;
575 az *= 360.0/D2PI;
576
577 return DrawAltAz(color, img, alt, az, size);
578}
579
580Bool_t StarCatalog::Draw(const int color, byte *img, const RaDec &radec)
581{
582 return DrawRaDec(color, img, radec.Ra(), radec.Dec());
583}
584/*
585void StarCatalog::CalcImg(byte *img)
586{
587
588 //
589 // --------- search for stars in catalog ----------
590 //
591 int count = 0;
592 int deleted = 0;
593
594 int idx = 0;
595
596 while (fSrt[idx].dec<fDecMin)
597 idx++;
598
599 idx--;
600 while (++idx<fEntries && fSrt[idx].dec<fDecMax+1)
601 {
602 const int ra = fSrt[idx].ra;
603
604 if (!fRa0[ra])
605 continue;
606
607 int nr = fSrt[idx].nr;
608 do
609 {
610 //
611 // Get entry from catalog
612 //
613 fSao->GetEntry(nr++);
614
615 //
616 // Try to draw star into the image
617 // white = 0xff
618 //
619 if (!Draw(0x0f, img, fSao))
620 deleted++;
621
622 count++;
623 }
624 while ((int)(360.0/D2PI*fSao->Ra())==ra);
625 }
626
627 cout << " " << count << "-" << deleted << "=" << count-deleted << " " << flush;
628}
629*/
630void StarCatalog::DrawStars(MStarList &list, byte *img)
631{
632 MStarListIter Next(&list);
633
634 MStar *star;
635 while ((star=Next()))
636 {
637 const int mag = (10 - (star->GetMag()>1 ? (int)star->GetMag() : 1))/2;
638
639 Double_t color = 0x0f;
640
641 DrawCircle(color, img, (int)star->GetX(), (int)star->GetY(), mag);
642 }
643}
644
645void StarCatalog::CalcStars(MStarList &list) const
646{
647 //
648 // --------- search for stars in catalog ----------
649 //
650 if (fEntries==0)
651 return;
652
653 int count = 0;
654 int deleted = 0;
655
656 int idx = 0;
657
658 while (fSrt[idx].dec<fDecMin)
659 idx++;
660
661 idx--;
662 while (++idx<fEntries && fSrt[idx].dec<fDecMax+1)
663 {
664 const int ra = fSrt[idx].ra;
665
666 if (!fRa0[ra])
667 continue;
668
669 int nr = fSrt[idx].nr;
670 do
671 {
672 //
673 // Get entry from catalog
674 //
675 fSao->GetEntry(nr++);
676
677 if (fSao->MagV() > fLimitMag)
678 continue;
679
680 //
681 // ---- mean to observed ---
682 //
683 AltAz altaz=CalcAltAz(fSao->GetRaDec());
684
685 //
686 // alt/az[rad] -> alt/az[pix]
687 //
688 double dx, dy;
689 slaDe2h(altaz.Az()-fAltAz.Az(), -altaz.Alt(),
690 DPI/2-fAltAz.Alt(), &dx, &dy);
691
692 //
693 // Align and rotate alt/az[pix]
694 //
695 float xx = ((dx-DPI)*fCosAngle - dy*fSinAngle + fWidth)/fPixSize;
696 float yy = ((dx-DPI)*fSinAngle + dy*fCosAngle + fHeight)/fPixSize;
697
698 //
699 // Range Check, add stars to the list
700 //
701 if (!(0<=xx && xx<768 && 0<=yy && yy<576))
702 {
703 deleted++;
704 continue;
705 }
706
707 list.Add(xx, yy, fSao->MagV());
708 count++;
709 }
710 while ((int)(360.0/D2PI*fSao->Ra())==ra);
711 }
712
713 cout << "Showing " << count+deleted << "-" << deleted << "=" << count << " stars." << endl;
714}
715
716AltAz StarCatalog::CalcAltAzFromPix(Double_t pixx, Double_t pixy) const
717{
718 pixx *= fPixSize;
719 pixy *= fPixSize;
720
721 const double dx = (pixx-fWidth)*fCosAngle + (pixy-fHeight)*fSinAngle;
722 const double dy = -(pixx-fWidth)*fSinAngle + (pixy-fHeight)*fCosAngle;
723
724 //const double dx = (pixx-768.0)*fPixSize + fWidth+DPI;
725 //const double dy = pixy*fPixSize - fHeight;
726
727 double ha, dec;
728 slaDh2e(dx, dy, DPI/2-fAltAz.Alt(), &ha, &dec);
729
730 return AltAz(-dec, ha+fAltAz.Az());
731}
Note: See TracBrowser for help on using the repository browser.