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

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