source: trunk/MagicSoft/Cosy/gui/MGStarguider.cc@ 1676

Last change on this file since 1676 was 1531, checked in by tbretz, 22 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 17.0 KB
Line 
1#include "MGStarguider.h"
2
3#include <fstream.h> // ifstream
4#include <iostream.h> // cout
5#
6#include <TGMenu.h>
7#include <TSystem.h>
8#include <TGSplitter.h> // TGHorizontal3DLine
9#include <TGTextEntry.h>
10
11#include "MGImage.h"
12#include "MGCoordinates.h"
13
14#include "coord.h"
15
16#include "StarCatalog.h"
17
18#include "Filter.h"
19#include "Filter2.h"
20#include "Writer.h"
21#include "base/timer.h"
22
23#include "MStarList.h"
24
25ClassImp(MGStarguider);
26
27enum {
28 IDM_kFilter,
29 IDM_kCatalog,
30 IDM_kStarguider,
31 IDM_kStart,
32 IDM_kStop,
33 IDM_kFileType,
34 IDM_kPPM,
35 IDM_kPNG,
36 IDM_kOnce,
37 IDM_kUseFileRaDec,
38 IDM_kContinous,
39 IDM_kRate25ps,
40 IDM_kRate5ps,
41 IDM_kRate1s,
42 IDM_kRate5s,
43 IDM_kRate30s,
44 IDM_kRate1m,
45 IDM_kRate5m,
46 IDM_kSetup,
47 IDM_kLimMag3,
48 IDM_kLimMag4,
49 IDM_kLimMag5,
50 IDM_kLimMag6,
51 IDM_kLimMag7,
52 IDM_kLimMag8,
53 IDM_kLimMag9,
54 IDM_kPixSize,
55 IDM_kInterpol125,
56 IDM_kInterpol25,
57 IDM_kInterpol10,
58 IDM_kInterpol5,
59 IDM_kInterpol2,
60 IDM_kInterpol1
61};
62
63#define kZOOM 96
64
65MGStarguider::MGStarguider()
66: Camera(), TGMainFrame(gClient->GetRoot(), 768, 700), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2)
67{
68 gVirtualX->GrabButton(fId, kButton2, /*kButtonPressMask|kButtonReleaseMask|*/kNone, kNone, kNone, kNone);
69
70
71 fList = new MGList;
72
73 fSao = new StarCatalog;
74 fRaDec = new RaDec(180, 40);
75
76 // p = pointer to MainFrame (not owner)
77
78 const TGWindow *p=gClient->GetRoot();
79
80 //
81 // Create Menu for MGStarguider Display
82 //
83 fDisplay = new TGPopupMenu(p);
84 fDisplay->AddEntry("&Filter", IDM_kFilter);
85 fDisplay->AddEntry("Sao &Catalog", IDM_kCatalog);
86 fDisplay->AddEntry("Starguider", IDM_kStarguider);
87 fDisplay->Associate(this);
88 fList->Add(fDisplay);
89
90 fFileType = new TGPopupMenu(p);
91 fFileType->AddEntry("PP&M", IDM_kPPM);
92 fFileType->AddEntry("&PNG", IDM_kPNG);
93 fFileType->CheckEntry(IDM_kPNG);
94 fFileType->Associate(this);
95 fList->Add(fFileType);
96
97 fWriteType = new TGPopupMenu(p);
98 fWriteType->AddEntry("Once", IDM_kOnce);
99 fWriteType->AddEntry("Continous", IDM_kContinous);
100 fWriteType->CheckEntry(IDM_kOnce);
101 fWriteType->Associate(this);
102 fList->Add(fWriteType);
103
104 fWriteRate = new TGPopupMenu(p);
105 fWriteRate->AddEntry("25/s", IDM_kRate25ps);
106 fWriteRate->AddEntry("5/s", IDM_kRate5ps);
107 fWriteRate->AddEntry("1s", IDM_kRate1s);
108 fWriteRate->AddEntry("5s", IDM_kRate5s);
109 fWriteRate->AddEntry("30s", IDM_kRate30s);
110 fWriteRate->AddEntry("1min", IDM_kRate1m);
111 fWriteRate->AddEntry("5min", IDM_kRate5m);
112 fWriteRate->CheckEntry(IDM_kRate1m);
113 fWriteRate->Associate(this);
114 fList->Add(fWriteRate);
115
116 fWrtRate = 25*60;
117
118 fWrite = new TGPopupMenu(p);
119 fWrite->AddEntry("&Start", IDM_kStart);
120 fWrite->AddEntry("Sto&p", IDM_kStop);
121 fWrite->AddSeparator();
122 fWrite->AddPopup("File &Type", fFileType);
123 fWrite->AddPopup("&Write Type", fWriteType);
124 fWrite->AddPopup("Write &Rate", fWriteRate);
125 fWrite->DisableEntry(IDM_kStop);
126 fWrite->Associate(this);
127 fList->Add(fWrite);
128
129 fLimMag = new TGPopupMenu(p);
130 fLimMag->AddEntry("3", IDM_kLimMag3);
131 fLimMag->AddEntry("4", IDM_kLimMag4);
132 fLimMag->AddEntry("5", IDM_kLimMag5);
133 fLimMag->AddEntry("6", IDM_kLimMag6);
134 fLimMag->AddEntry("7", IDM_kLimMag7);
135 fLimMag->AddEntry("8", IDM_kLimMag8);
136 fLimMag->AddEntry("9", IDM_kLimMag9);
137 fLimMag->CheckEntry(IDM_kLimMag8);
138 fLimMag->Associate(this);
139 fList->Add(fLimMag);
140
141 fSao->SetLimitMag(8.0);
142
143 fInterpol = new TGPopupMenu(p);
144 fInterpol->AddEntry("125", IDM_kInterpol125);
145 fInterpol->AddEntry("25", IDM_kInterpol25);
146 fInterpol->AddEntry("10", IDM_kInterpol10);
147 fInterpol->AddEntry("5", IDM_kInterpol5);
148 fInterpol->AddEntry("2", IDM_kInterpol2);
149 fInterpol->AddEntry("Off", IDM_kInterpol1);
150 fInterpol->CheckEntry(IDM_kInterpol25);
151 fInterpol->Associate(this);
152 fList->Add(fInterpol);
153
154 fIntRate = 25;
155
156 fSetup = new TGPopupMenu(p);
157 fSetup->AddPopup("Lim. &Magnitude", fLimMag);
158 fSetup->AddPopup("Disp. &Interpolation", fInterpol);
159 fSetup->AddEntry("Use Ra/Dec from file", IDM_kUseFileRaDec);
160 fSetup->Associate(this);
161 fList->Add(fSetup);
162
163 fMenu = new TGMenuBar(this, 0, 0, kHorizontalFrame);
164 fMenu->AddPopup("&Display", fDisplay, NULL);
165 fMenu->AddPopup("&Write", fWrite, NULL);
166 fMenu->AddPopup("&Setup", fSetup, NULL);
167 fMenu->Resize(fMenu->GetDefaultSize());
168 AddFrame(fMenu); //, new TGLayoutHints (kLHintsNormal, 0, 4, 0, 0));
169 fList->Add(fMenu);
170
171 fCRaDec = new MGCoordinates(this, kETypeRaDec);
172 fCRaDec->Move(1, fMenu->GetDefaultHeight()+584);
173 AddFrame(fCRaDec);
174 fList->Add(fCRaDec);
175
176 fCZdAz = new MGCoordinates(this, kETypeZdAz);
177 fCZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+584);
178 AddFrame(fCZdAz);
179 fList->Add(fCZdAz);
180
181 const Double_t pixsize = 23.4;
182
183 fSao->SetPixSize(pixsize/3600);
184
185 TString txt;
186 txt += pixsize;
187
188 fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
189 fPixSize->SetAlignment(kTextCenterX);
190 fPixSize->Move(600, fMenu->GetDefaultHeight()+584);
191 AddFrame(fPixSize);
192 fList->Add(fPixSize);
193
194 // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
195 // AddFrame(fLineSep, new TGLayoutHints (kLHintsNormal | kLHintsExpandX));
196 // fList->Add(fLineSep);
197
198 //
199 // Create Image Display
200 //
201 fImage = new MGImage(this, 768, 576);
202 fImage->Move(0, fMenu->GetDefaultHeight());
203 AddFrame(fImage);
204 fList->Add(fImage);
205
206 fZoomImage = new MGImage(this, kZOOM, kZOOM);
207 fZoomImage->Move(768-kZOOM-2, 700-kZOOM-2);
208 AddFrame(fZoomImage);
209 fList->Add(fZoomImage);
210
211 //
212 // Make everything visible
213 //
214 SetWindowName("MGStarguider Main Window");
215 SetIconName("MGStarguider");
216
217 MapSubwindows();
218 MapWindow();
219}
220
221MGStarguider::~MGStarguider()
222{
223 delete fSao;
224 delete fRaDec;
225
226 delete fList;
227 cout << "Camera Display destroyed." << endl;
228}
229
230void MGStarguider::Layout()
231{
232 // Resize(GetDefaultSize());
233}
234
235void MGStarguider::CloseWindow()
236{
237 cout << "EventDisplay::CloseWindow: Exit Application Loop." << endl;
238
239 ExitLoop();
240
241 gSystem->ExitLoop();
242}
243
244void MGStarguider::Toggle(TGPopupMenu *p, UInt_t id)
245{
246 if (p->IsEntryChecked(id))
247 p->UnCheckEntry(id);
248 else
249 p->CheckEntry(id);
250
251}
252
253Bool_t MGStarguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
254{
255 switch (GET_MSG(msg))
256 {
257 case kC_TEXTENTRY:
258 if (GET_SUBMSG(msg)==kTE_ENTER)
259 {
260 const Float_t pixsize = atof(fPixSize->GetText());
261 cout << "Pixel Size changed to " << pixsize << "\"/pix" << endl;
262 fSao->SetPixSize(pixsize/3600);
263 }
264 return kTRUE;
265
266 case kC_COMMAND:
267 switch (GET_SUBMSG(msg))
268 {
269 case kCM_MENU:
270 switch (mp1)
271 {
272 case IDM_kCatalog:
273 Toggle(fDisplay, IDM_kCatalog);
274 if (fDisplay->IsEntryChecked(IDM_kCatalog))
275 fDisplay->EnableEntry(IDM_kStarguider);
276 else
277 {
278 fDisplay->UnCheckEntry(IDM_kStarguider);
279 fDisplay->DisableEntry(IDM_kStarguider);
280 }
281 return kTRUE;
282
283 case IDM_kStarguider:
284 Toggle(fDisplay, IDM_kStarguider);
285 return kTRUE;
286
287 case IDM_kFilter:
288 Toggle(fDisplay, IDM_kFilter);
289 if (fDisplay->IsEntryChecked(IDM_kFilter))
290 fDisplay->EnableEntry(IDM_kStarguider);
291 else
292 {
293 fDisplay->UnCheckEntry(IDM_kStarguider);
294 fDisplay->DisableEntry(IDM_kStarguider);
295 }
296 return kTRUE;
297
298 case IDM_kUseFileRaDec:
299 Toggle(fSetup, IDM_kUseFileRaDec);
300 return kTRUE;
301
302 case IDM_kStart:
303 fWrite->DisableEntry(IDM_kStart);
304 fWrite->EnableEntry(IDM_kStop);
305 return kTRUE;
306
307 case IDM_kStop:
308 fWrite->DisableEntry(IDM_kStop);
309 fWrite->EnableEntry(IDM_kStart);
310 return kTRUE;
311
312 case IDM_kPNG:
313 fFileType->CheckEntry(IDM_kPNG);
314 fFileType->UnCheckEntry(IDM_kPPM);
315 return kTRUE;
316
317 case IDM_kPPM:
318 fFileType->CheckEntry(IDM_kPPM);
319 fFileType->UnCheckEntry(IDM_kPNG);
320 return kTRUE;
321
322 case IDM_kOnce:
323 fWriteType->CheckEntry(IDM_kOnce);
324 fWriteType->UnCheckEntry(IDM_kContinous);
325 return kTRUE;
326
327 case IDM_kContinous:
328 fWriteType->CheckEntry(IDM_kContinous);
329 fWriteType->UnCheckEntry(IDM_kOnce);
330 return kTRUE;
331
332 case IDM_kRate25ps:
333 case IDM_kRate5ps:
334 case IDM_kRate1s:
335 case IDM_kRate5s:
336 case IDM_kRate30s:
337 case IDM_kRate1m:
338 case IDM_kRate5m:
339 for (int i=IDM_kRate25ps; i<=IDM_kRate5m; i++)
340 if (mp1==i)
341 fWriteRate->CheckEntry(i);
342 else
343 fWriteRate->UnCheckEntry(i);
344 switch (mp1)
345 {
346 case IDM_kRate25ps:
347 fWrtRate = 1;
348 return kTRUE;
349 case IDM_kRate5ps:
350 fWrtRate = 5;
351 return kTRUE;
352 case IDM_kRate1s:
353 fWrtRate = 25;
354 return kTRUE;
355 case IDM_kRate5s:
356 fWrtRate = 5*25;
357 return kTRUE;
358 case IDM_kRate30s:
359 fWrtRate = 30*25;
360 return kTRUE;
361 case IDM_kRate1m:
362 fWrtRate = 60*25;
363 return kTRUE;
364 case IDM_kRate5m:
365 fWrtRate = 5*60*25;
366 return kTRUE;
367 }
368 return kTRUE;
369
370 case IDM_kInterpol125:
371 case IDM_kInterpol25:
372 case IDM_kInterpol10:
373 case IDM_kInterpol5:
374 case IDM_kInterpol2:
375 case IDM_kInterpol1:
376 for (int i=IDM_kInterpol125; i<=IDM_kInterpol1; i++)
377 if (mp1==i)
378 fInterpol->CheckEntry(i);
379 else
380 fInterpol->UnCheckEntry(i);
381 switch (mp1)
382 {
383 case IDM_kInterpol1:
384 fIntRate = 1;
385 return kTRUE;
386 case IDM_kInterpol2:
387 fIntRate = 2;
388 return kTRUE;
389 case IDM_kInterpol5:
390 fIntRate = 5;
391 return kTRUE;
392 case IDM_kInterpol10:
393 fIntRate = 10;
394 return kTRUE;
395 case IDM_kInterpol25:
396 fIntRate = 25;
397 return kTRUE;
398 case IDM_kInterpol125:
399 fIntRate = 125;
400 return kTRUE;
401 }
402 return kTRUE;
403
404 case IDM_kLimMag3:
405 case IDM_kLimMag4:
406 case IDM_kLimMag5:
407 case IDM_kLimMag6:
408 case IDM_kLimMag7:
409 case IDM_kLimMag8:
410 case IDM_kLimMag9:
411 for (int i=IDM_kLimMag3; i<=IDM_kLimMag9; i++)
412 if (mp1==i)
413 fLimMag->CheckEntry(i);
414 else
415 fLimMag->UnCheckEntry(i);
416
417 fSao->SetLimitMag(mp1-IDM_kLimMag3+3);
418 return kTRUE;
419 }
420 break;
421 }
422 break;
423 }
424
425 return kTRUE;
426}
427
428void MGStarguider::GetCoordinates()
429{
430 XY xy = fCRaDec->GetCoordinates();
431
432 if (fSetup->IsEntryChecked(IDM_kUseFileRaDec))
433 {
434 ifstream fin("coordinates.txt");
435 if (!fin)
436 cout << "Error: Cannot open 'coordinates.txt' using fall back solution." << endl;
437 else
438 fin >> xy;
439 }
440
441 fCRaDec->SetCoordinates(xy);
442 fRaDec->Set(xy.X()*360/24, xy.Y());
443}
444
445void MGStarguider::CalcTrackingError(MStarList &spots, MStarList &stars)
446{
447 if (stars.GetRealEntries() < 3)
448 {
449 cout << "Sorry, less than 3 stars in FOV!" << endl;
450 return;
451 }
452
453 if (spots.GetRealEntries() < 1)
454 {
455 cout << "Sorry, less than 1 detected spot in FOV!" << endl;
456 return;
457 }
458
459 Int_t idx = 0;
460
461 MStarList sortedspots;
462
463 MStar *star;
464 MStar *spot;
465 MStarListIter NextStar(&stars);
466 MStarListIter NextSpot(&spots);
467
468 while ((spot=NextSpot()))
469 {
470 AltAz aa = fSao->CalcAltAzFromPix(spot->GetX(), spot->GetY());
471 spot->Set(aa.Az(), aa.Alt());
472 }
473
474 while ((star=NextStar()))
475 {
476 AltAz aa = fSao->CalcAltAzFromPix(star->GetX(), star->GetY());
477 star->Set(aa.Az(), aa.Alt());
478
479 const double aaz = star->GetX();
480 const double dphi2 = aaz/2.;
481 const double cos2 = cos(dphi2)*cos(dphi2);
482 const double sin2 = sin(dphi2)*sin(dphi2);
483
484 Double_t min = 800;
485
486 NextSpot.Reset();
487 while ((spot=NextSpot()))
488 {
489 const double pzd = TMath::Pi()/2-spot->GetY();
490 const double azd = TMath::Pi()/2-star->GetY();
491
492 const double d = cos(azd)*cos2 - cos(2*pzd+azd)*sin2;
493
494 const Double_t dist = acos(d);
495
496 if (dist>=min)
497 continue;
498
499 min = dist;
500 sortedspots.AddAt(idx, spot->GetX(), spot->GetY(), spot->GetMag());
501 }
502 if (min>768)
503 {
504 cout << "ERROR!!!!!!!!" << endl;
505 return;
506 }
507 idx++;
508 }
509
510 //
511 // Now we have in sortedspots the entries with the shortest distances
512 // to the corresponding ones in stars.
513 // Now calculate the tracking error.
514 //
515 NextStar.Reset();
516 MStarListIter NextSpot2(&sortedspots);
517
518 Double_t meanx=0;
519 Double_t meany=0;
520
521 while ((star=NextStar()))
522 {
523 spot = NextSpot2();
524
525 meanx += star->GetX() - spot->GetX();
526 meany += star->GetY() - spot->GetY();
527 }
528
529 meanx /= idx;
530 meany /= idx;
531
532 cout << "Tracking Error: dAlt=" << meany*180/TMath::Pi();
533 cout << "° dAz=" << meanx*180/TMath::Pi() << "° (calculated";
534 cout << " with " << idx << " stars/spots)" << endl;
535}
536
537void MGStarguider::ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
538{
539 if (!fWrite->IsEntryEnabled(IDM_kStart) &&
540 (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
541 {
542 if (fFileType->IsEntryChecked(IDM_kPNG))
543 {
544 static int num = 0;
545 char name[80];
546 sprintf(name, "pix/file%04d.png", num++);
547 Writer::Png(name, img, tm);
548 }
549
550 if (fFileType->IsEntryChecked(IDM_kPPM))
551 {
552 static int num = 0;
553 char name[80];
554 sprintf(name, "pix/file%04d.ppm", num++);
555 Writer::Ppm(name, img);
556 }
557
558 if (fWriteType->IsEntryChecked(IDM_kOnce))
559 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
560 }
561
562 static float myimg[768*576];
563
564 for (int i=0; i<768*576; i++)
565 myimg[i] += img[i];
566
567 if (n%fIntRate)
568 return;
569
570 cout << "Img: " << n << endl;
571
572 byte c[768*576];
573 for (int i=0; i<768*576; i++)
574 c[i] = (byte)(myimg[i]/fIntRate+.5);
575
576 MStarList spots;
577 if (fDisplay->IsEntryChecked(IDM_kStarguider))
578 Filter2::Execute(spots, c);
579 else
580 if (fDisplay->IsEntryChecked(IDM_kFilter))
581 Filter::Execute(c);
582
583 byte zimg[kZOOM*kZOOM];
584 for (int y=0; y<kZOOM; y++)
585 for (int x=0; x<kZOOM; x++)
586 zimg[x+y*kZOOM] = c[(fDx+(x-kZOOM/2)/2)+(fDy+(y-kZOOM/2)/2)*768];
587
588 fZoomImage->DrawImg(zimg);
589
590 if (fDisplay->IsEntryChecked(IDM_kCatalog))
591 {
592 byte cimg[768*576];
593
594 GetCoordinates();
595
596 Timer time(tm);
597
598 MStarList stars;
599 fSao->GetStars(stars, time.CalcMjd(), *fRaDec);
600 fSao->GetImg(c, cimg, stars);
601 //fSao->GetImg(c, cimg, time.CalcMjd(), *fRaDec);
602
603 fImage->DrawColImg(c, cimg);
604
605 fCZdAz->SetCoordinates(fSao->GetZdAz());
606
607 if (fDisplay->IsEntryChecked(IDM_kStarguider))
608 CalcTrackingError(spots, stars);
609 }
610 else
611 fImage->DrawImg(c);
612
613 memset(myimg, 0, 768*576*sizeof(float));
614}
615
616Bool_t MGStarguider::HandleDoubleClick(Event_t *event)
617{
618 const Int_t w = fImage->GetWidth();
619 const Int_t h = fImage->GetHeight();
620 const Int_t x = fImage->GetX();
621 const Int_t y = fImage->GetY();
622
623 if (!(event->fX>x && event->fX<x+w && event->fY>y && event->fY<y+h))
624 return kTRUE;
625
626 Int_t dx = event->fX-x;
627 Int_t dy = event->fY-y;
628
629 if (dx<kZOOM/4) dx=kZOOM/4;
630 if (dy<kZOOM/4) dy=kZOOM/4;
631 if (dx>766-kZOOM/2) dx=766-kZOOM/4;
632 if (dy>510-kZOOM/2) dy=510-kZOOM/4;
633
634 fDx = dx;
635 fDy = dy;
636
637 cout << "New coordinates for zoom: " << fDx << " " << fDy << endl;
638
639 return kTRUE;
640}
Note: See TracBrowser for help on using the repository browser.