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

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