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

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