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

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