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

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