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

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