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

Last change on this file since 1692 was 1690, checked in by tbretz, 22 years ago
*** empty log message ***
  • Property svn:executable set to *
File size: 17.3 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()
75: Camera(), TGMainFrame(gClient->GetRoot(), 768, 700), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2)
76{
77 gVirtualX->GrabButton(fId, kButton2, /*kButtonPressMask|kButtonReleaseMask|*/kNone, kNone, kNone, kNone);
78
79
80 fList = new MGList;
81
82 fSao = new StarCatalog;
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 fTimer->TurnOff();
236 delete fTimer;
237
238 delete fSao;
239 delete fRaDec;
240
241 delete fList;
242 cout << "Camera Display destroyed." << endl;
243}
244
245void MGStarguider::Layout()
246{
247 // Resize(GetDefaultSize());
248}
249
250void MGStarguider::CloseWindow()
251{
252 cout << "EventDisplay::CloseWindow: Exit Application Loop." << endl;
253
254 ExitLoop();
255
256 gSystem->ExitLoop();
257}
258
259void MGStarguider::Toggle(TGPopupMenu *p, UInt_t id)
260{
261 if (p->IsEntryChecked(id))
262 p->UnCheckEntry(id);
263 else
264 p->CheckEntry(id);
265
266}
267
268Bool_t MGStarguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
269{
270 switch (GET_MSG(msg))
271 {
272 case kC_TEXTENTRY:
273 if (GET_SUBMSG(msg)==kTE_ENTER)
274 {
275 const Float_t pixsize = atof(fPixSize->GetText());
276 cout << "Pixel Size changed to " << pixsize << "\"/pix" << endl;
277 fSao->SetPixSize(pixsize/3600);
278 }
279 return kTRUE;
280
281 case kC_COMMAND:
282 switch (GET_SUBMSG(msg))
283 {
284 case kCM_MENU:
285 switch (mp1)
286 {
287 case IDM_kCatalog:
288 Toggle(fDisplay, IDM_kCatalog);
289 if (fDisplay->IsEntryChecked(IDM_kCatalog))
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_kStarguider:
299 Toggle(fDisplay, IDM_kStarguider);
300 return kTRUE;
301
302 case IDM_kFilter:
303 Toggle(fDisplay, IDM_kFilter);
304 if (fDisplay->IsEntryChecked(IDM_kFilter))
305 fDisplay->EnableEntry(IDM_kStarguider);
306 else
307 {
308 fDisplay->UnCheckEntry(IDM_kStarguider);
309 fDisplay->DisableEntry(IDM_kStarguider);
310 }
311 return kTRUE;
312
313 case IDM_kUseFileRaDec:
314 Toggle(fSetup, IDM_kUseFileRaDec);
315 return kTRUE;
316
317 case IDM_kStart:
318 fWrite->DisableEntry(IDM_kStart);
319 fWrite->EnableEntry(IDM_kStop);
320 return kTRUE;
321
322 case IDM_kStop:
323 fWrite->DisableEntry(IDM_kStop);
324 fWrite->EnableEntry(IDM_kStart);
325 return kTRUE;
326
327 case IDM_kPNG:
328 fFileType->CheckEntry(IDM_kPNG);
329 fFileType->UnCheckEntry(IDM_kPPM);
330 return kTRUE;
331
332 case IDM_kPPM:
333 fFileType->CheckEntry(IDM_kPPM);
334 fFileType->UnCheckEntry(IDM_kPNG);
335 return kTRUE;
336
337 case IDM_kOnce:
338 fWriteType->CheckEntry(IDM_kOnce);
339 fWriteType->UnCheckEntry(IDM_kContinous);
340 return kTRUE;
341
342 case IDM_kContinous:
343 fWriteType->CheckEntry(IDM_kContinous);
344 fWriteType->UnCheckEntry(IDM_kOnce);
345 return kTRUE;
346
347 case IDM_kRate25ps:
348 case IDM_kRate5ps:
349 case IDM_kRate1s:
350 case IDM_kRate5s:
351 case IDM_kRate30s:
352 case IDM_kRate1m:
353 case IDM_kRate5m:
354 for (int i=IDM_kRate25ps; i<=IDM_kRate5m; i++)
355 if (mp1==i)
356 fWriteRate->CheckEntry(i);
357 else
358 fWriteRate->UnCheckEntry(i);
359 switch (mp1)
360 {
361 case IDM_kRate25ps:
362 fWrtRate = 1;
363 return kTRUE;
364 case IDM_kRate5ps:
365 fWrtRate = 5;
366 return kTRUE;
367 case IDM_kRate1s:
368 fWrtRate = 25;
369 return kTRUE;
370 case IDM_kRate5s:
371 fWrtRate = 5*25;
372 return kTRUE;
373 case IDM_kRate30s:
374 fWrtRate = 30*25;
375 return kTRUE;
376 case IDM_kRate1m:
377 fWrtRate = 60*25;
378 return kTRUE;
379 case IDM_kRate5m:
380 fWrtRate = 5*60*25;
381 return kTRUE;
382 }
383 return kTRUE;
384
385 case IDM_kInterpol125:
386 case IDM_kInterpol25:
387 case IDM_kInterpol10:
388 case IDM_kInterpol5:
389 case IDM_kInterpol2:
390 case IDM_kInterpol1:
391 for (int i=IDM_kInterpol125; i<=IDM_kInterpol1; i++)
392 if (mp1==i)
393 fInterpol->CheckEntry(i);
394 else
395 fInterpol->UnCheckEntry(i);
396 switch (mp1)
397 {
398 case IDM_kInterpol1:
399 fIntRate = 1;
400 return kTRUE;
401 case IDM_kInterpol2:
402 fIntRate = 2;
403 return kTRUE;
404 case IDM_kInterpol5:
405 fIntRate = 5;
406 return kTRUE;
407 case IDM_kInterpol10:
408 fIntRate = 10;
409 return kTRUE;
410 case IDM_kInterpol25:
411 fIntRate = 25;
412 return kTRUE;
413 case IDM_kInterpol125:
414 fIntRate = 125;
415 return kTRUE;
416 }
417 return kTRUE;
418
419 case IDM_kLimMag3:
420 case IDM_kLimMag4:
421 case IDM_kLimMag5:
422 case IDM_kLimMag6:
423 case IDM_kLimMag7:
424 case IDM_kLimMag8:
425 case IDM_kLimMag9:
426 for (int i=IDM_kLimMag3; i<=IDM_kLimMag9; i++)
427 if (mp1==i)
428 fLimMag->CheckEntry(i);
429 else
430 fLimMag->UnCheckEntry(i);
431
432 fSao->SetLimitMag(mp1-IDM_kLimMag3+3);
433 return kTRUE;
434 }
435 break;
436 }
437 break;
438 }
439
440 return kTRUE;
441}
442
443void MGStarguider::GetCoordinates()
444{
445 XY xy = fCRaDec->GetCoordinates();
446
447 if (fSetup->IsEntryChecked(IDM_kUseFileRaDec))
448 {
449 ifstream fin("coordinates.txt");
450 if (!fin)
451 cout << "Error: Cannot open 'coordinates.txt' using fall back solution." << endl;
452 else
453 fin >> xy;
454 }
455
456 fCRaDec->SetCoordinates(xy);
457 fRaDec->Set(xy.X()*360/24, xy.Y());
458}
459
460void MGStarguider::CalcTrackingError(MStarList &spots, MStarList &stars)
461{
462 if (stars.GetRealEntries() < 3)
463 {
464 cout << "Sorry, less than 3 stars in FOV!" << endl;
465 return;
466 }
467
468 if (spots.GetRealEntries() < 1)
469 {
470 cout << "Sorry, less than 1 detected spot in FOV!" << endl;
471 return;
472 }
473
474 Int_t idx = 0;
475
476 MStarList sortedspots;
477
478 MStar *star;
479 MStar *spot;
480 MStarListIter NextStar(&stars);
481 MStarListIter NextSpot(&spots);
482
483 while ((spot=NextSpot()))
484 {
485 AltAz aa = fSao->CalcAltAzFromPix(spot->GetX(), spot->GetY());
486 spot->Set(aa.Az(), aa.Alt());
487 }
488
489 while ((star=NextStar()))
490 {
491 AltAz aa = fSao->CalcAltAzFromPix(star->GetX(), star->GetY());
492 star->Set(aa.Az(), aa.Alt());
493
494 const double aaz = star->GetX();
495 const double dphi2 = aaz/2.;
496 const double cos2 = cos(dphi2)*cos(dphi2);
497 const double sin2 = sin(dphi2)*sin(dphi2);
498
499 Double_t min = 800;
500
501 NextSpot.Reset();
502 while ((spot=NextSpot()))
503 {
504 const double pzd = TMath::Pi()/2-spot->GetY();
505 const double azd = TMath::Pi()/2-star->GetY();
506
507 const double d = cos(azd)*cos2 - cos(2*pzd+azd)*sin2;
508
509 const Double_t dist = acos(d);
510
511 if (dist>=min)
512 continue;
513
514 min = dist;
515 sortedspots.AddAt(idx, spot->GetX(), spot->GetY(), spot->GetMag());
516 }
517 if (min>768)
518 {
519 cout << "ERROR!!!!!!!!" << endl;
520 return;
521 }
522 idx++;
523 }
524
525 //
526 // Now we have in sortedspots the entries with the shortest distances
527 // to the corresponding ones in stars.
528 // Now calculate the tracking error.
529 //
530 NextStar.Reset();
531 MStarListIter NextSpot2(&sortedspots);
532
533 Double_t meanx=0;
534 Double_t meany=0;
535
536 while ((star=NextStar()))
537 {
538 spot = NextSpot2();
539
540 meanx += star->GetX() - spot->GetX();
541 meany += star->GetY() - spot->GetY();
542 }
543
544 meanx /= idx;
545 meany /= idx;
546
547 cout << "Tracking Error: dAlt=" << meany*180/TMath::Pi();
548 cout << "° dAz=" << meanx*180/TMath::Pi() << "° (calculated";
549 cout << " with " << idx << " stars/spots)" << endl;
550}
551
552void MGStarguider::ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
553{
554 if (!fWrite->IsEntryEnabled(IDM_kStart) &&
555 (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
556 {
557 if (fFileType->IsEntryChecked(IDM_kPNG))
558 {
559 static int num = 0;
560 char name[80];
561 sprintf(name, "pix/file%04d.png", num++);
562 Writer::Png(name, img, tm);
563 }
564
565 if (fFileType->IsEntryChecked(IDM_kPPM))
566 {
567 static int num = 0;
568 char name[80];
569 sprintf(name, "pix/file%04d.ppm", num++);
570 Writer::Ppm(name, img);
571 }
572
573 if (fWriteType->IsEntryChecked(IDM_kOnce))
574 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
575 }
576
577 static float myimg[768*576];
578
579 for (int i=0; i<768*576; i++)
580 myimg[i] += img[i];
581
582 if (n%fIntRate)
583 return;
584
585 cout << "Img: " << n << endl;
586
587 byte c[768*576];
588 for (int i=0; i<768*576; i++)
589 c[i] = (byte)(myimg[i]/fIntRate+.5);
590
591 MStarList spots;
592 if (fDisplay->IsEntryChecked(IDM_kStarguider))
593 Filter2::Execute(spots, c);
594 else
595 if (fDisplay->IsEntryChecked(IDM_kFilter))
596 Filter::Execute(c);
597
598 byte zimg[kZOOM*kZOOM];
599 for (int y=0; y<kZOOM; y++)
600 for (int x=0; x<kZOOM; x++)
601 zimg[x+y*kZOOM] = c[(fDx+(x-kZOOM/2)/2)+(fDy+(y-kZOOM/2)/2)*768];
602
603 fZoomImage->DrawImg(zimg);
604
605 if (fDisplay->IsEntryChecked(IDM_kCatalog))
606 {
607 byte cimg[768*576];
608
609 GetCoordinates();
610
611 Timer time(tm);
612
613 MStarList stars;
614 fSao->GetStars(stars, time.CalcMjd(), *fRaDec);
615 fSao->GetImg(c, cimg, stars);
616 //fSao->GetImg(c, cimg, time.CalcMjd(), *fRaDec);
617
618 fImage->DrawColImg(c, cimg);
619
620 fCZdAz->SetCoordinates(fSao->GetZdAz());
621
622 if (fDisplay->IsEntryChecked(IDM_kStarguider))
623 CalcTrackingError(spots, stars);
624 }
625 else
626 fImage->DrawImg(c);
627
628 memset(myimg, 0, 768*576*sizeof(float));
629}
630
631Bool_t MGStarguider::HandleDoubleClick(Event_t *event)
632{
633 const Int_t w = fImage->GetWidth();
634 const Int_t h = fImage->GetHeight();
635 const Int_t x = fImage->GetX();
636 const Int_t y = fImage->GetY();
637
638 if (!(event->fX>x && event->fX<x+w && event->fY>y && event->fY<y+h))
639 return kTRUE;
640
641 Int_t dx = event->fX-x;
642 Int_t dy = event->fY-y;
643
644 if (dx<kZOOM/4) dx=kZOOM/4;
645 if (dy<kZOOM/4) dy=kZOOM/4;
646 if (dx>766-kZOOM/2) dx=766-kZOOM/4;
647 if (dy>510-kZOOM/2) dy=510-kZOOM/4;
648
649 fDx = dx;
650 fDy = dy;
651
652 cout << "New coordinates for zoom: " << fDx << " " << fDy << endl;
653
654 return kTRUE;
655}
Note: See TracBrowser for help on using the repository browser.