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

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