source: trunk/MagicSoft/Cosy/main/MStarguider.cc@ 2514

Last change on this file since 2514 was 2278, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 26.6 KB
Line 
1#include "MStarguider.h"
2
3#include <fstream.h> // ifstream
4#include <iostream.h> // cout
5#include <iomanip.h> // cout
6
7#include <TH2F.h>
8#include <TGraph.h>
9#include <TTimer.h>
10
11#include <TGMenu.h>
12#include <TGLabel.h>
13#include <TSystem.h>
14#include <TGSplitter.h> // TGHorizontal3DLine
15#include <TGTextEntry.h>
16
17#include "MCosy.h"
18#include "MCaos.h"
19
20#include "MGImage.h"
21#include "MGCoordinates.h"
22
23#include "coord.h"
24
25#include "Led.h"
26#include "Writer.h"
27#include "FilterLed.h"
28#include "MStarList.h"
29#include "CaosFilter.h"
30#include "StarCatalog.h"
31
32#include "MGMenu.h"
33
34ClassImp(MStarguider);
35
36enum {
37 IDM_kFilter,
38 IDM_kCaosFilter,
39 IDM_kCatalog,
40 IDM_kStarguider,
41 IDM_kStart,
42 IDM_kStop,
43 IDM_kFileType,
44 IDM_kPPM,
45 IDM_kPNG,
46 IDM_kOnce,
47 IDM_kStretch,
48// IDM_kUseFileRaDec,
49 IDM_kContinous,
50 IDM_kRate25ps,
51 IDM_kRate5ps,
52 IDM_kRate1s,
53 IDM_kRate5s,
54 IDM_kRate30s,
55 IDM_kRate1m,
56 IDM_kRate5m,
57 IDM_kSetup,
58 IDM_kLimMag3,
59 IDM_kLimMag4,
60 IDM_kLimMag5,
61 IDM_kLimMag6,
62 IDM_kLimMag7,
63 IDM_kLimMag8,
64 IDM_kLimMag9,
65 IDM_kPixSize,
66 IDM_kAngle,
67 IDM_kInterpol125,
68 IDM_kInterpol50,
69 IDM_kInterpol25,
70 IDM_kInterpol10,
71 IDM_kInterpol5,
72 IDM_kInterpol2,
73 IDM_kInterpol1,
74 IDM_kCaosPrintRings,
75 IDM_kCaosPrintLeds,
76 IDM_kCaosAnalStart,
77 IDM_kCaosAnalStop,
78 IDM_kCaosWriteStart,
79 IDM_kCaosWriteStop,
80 IDM_kResetHistograms
81};
82
83Bool_t MStarguider::HandleTimer(TTimer *t)
84{
85 fImage->DoRedraw();
86 fZoomImage->DoRedraw();
87 return kTRUE;
88}
89
90#define kZOOM 96
91
92XY MStarguider::GetCoordinates() const
93{
94 return fPZdAz->GetCoordinates();
95}
96
97void MStarguider::InitGui()
98{
99 fList = new MGList;
100
101 const TGWindow *p=gClient->GetRoot();
102
103 //
104 // Create Menu for MStarguider Display
105 //
106 fDisplay = new MGPopupMenu(p);
107 fDisplay->AddEntry("&Filter", IDM_kFilter);
108 fDisplay->AddEntry("C&aosFilter", IDM_kCaosFilter);
109 fDisplay->AddEntry("Sao &Catalog", IDM_kCatalog);
110 fDisplay->AddEntry("Starguider", IDM_kStarguider);
111 fDisplay->AddEntry("Stretch", IDM_kStretch);
112 fDisplay->DisableEntry(IDM_kStarguider);
113 fDisplay->CheckEntry(IDM_kStretch);
114 fDisplay->Associate(this);
115 fList->Add(fDisplay);
116
117 fFileType = new MGPopupMenu(p);
118 fFileType->AddEntry("PP&M", IDM_kPPM);
119 fFileType->AddEntry("&PNG", IDM_kPNG);
120 fFileType->CheckEntry(IDM_kPNG);
121 fFileType->Associate(this);
122 fList->Add(fFileType);
123
124 fWriteType = new MGPopupMenu(p);
125 fWriteType->AddEntry("&Once", IDM_kOnce);
126 fWriteType->AddEntry("&Continous", IDM_kContinous);
127 fWriteType->CheckEntry(IDM_kOnce);
128 fWriteType->Associate(this);
129 fList->Add(fWriteType);
130
131 fWriteRate = new MGPopupMenu(p);
132 fWriteRate->AddEntry("25/s", IDM_kRate25ps);
133 fWriteRate->AddEntry("5/s", IDM_kRate5ps);
134 fWriteRate->AddEntry("1s", IDM_kRate1s);
135 fWriteRate->AddEntry("5s", IDM_kRate5s);
136 fWriteRate->AddEntry("30s", IDM_kRate30s);
137 fWriteRate->AddEntry("1min", IDM_kRate1m);
138 fWriteRate->AddEntry("5min", IDM_kRate5m);
139 fWriteRate->CheckEntry(IDM_kRate1m);
140 fWriteRate->Associate(this);
141 fList->Add(fWriteRate);
142
143 fWrtRate = 25*60;
144
145 fWritePictures = new MGPopupMenu(p);
146 fWritePictures->AddEntry("&Start", IDM_kStart);
147 fWritePictures->AddEntry("Sto&p", IDM_kStop);
148 fWritePictures->AddSeparator();
149 fWritePictures->AddPopup("File &Type", fFileType);
150 fWritePictures->AddPopup("&Write Type", fWriteType);
151 fWritePictures->AddPopup("Write &Rate", fWriteRate);
152 fWritePictures->DisableEntry(IDM_kStop);
153 fWritePictures->Associate(this);
154 fList->Add(fWritePictures);
155
156 fLimMag = new MGPopupMenu(p);
157 fLimMag->AddEntry("3", IDM_kLimMag3);
158 fLimMag->AddEntry("4", IDM_kLimMag4);
159 fLimMag->AddEntry("5", IDM_kLimMag5);
160 fLimMag->AddEntry("6", IDM_kLimMag6);
161 fLimMag->AddEntry("7", IDM_kLimMag7);
162 fLimMag->AddEntry("8", IDM_kLimMag8);
163 fLimMag->AddEntry("9", IDM_kLimMag9);
164 fLimMag->CheckEntry(IDM_kLimMag8);
165 fLimMag->Associate(this);
166 fList->Add(fLimMag);
167
168 fSao->SetLimitMag(7.0);
169
170 fInterpol = new MGPopupMenu(p);
171 fInterpol->AddEntry("125", IDM_kInterpol125);
172 fInterpol->AddEntry("50", IDM_kInterpol50);
173 fInterpol->AddEntry("25", IDM_kInterpol25);
174 fInterpol->AddEntry("10", IDM_kInterpol10);
175 fInterpol->AddEntry("5", IDM_kInterpol5);
176 fInterpol->AddEntry("2", IDM_kInterpol2);
177 fInterpol->AddEntry("Off", IDM_kInterpol1);
178 fInterpol->CheckEntry(IDM_kInterpol1);
179 fInterpol->Associate(this);
180 fList->Add(fInterpol);
181
182 fIntRate = 1;
183
184 fSetup = new MGPopupMenu(p);
185 fSetup->AddPopup("Lim. &Magnitude", fLimMag);
186 fSetup->AddPopup("Disp. &Interpolation", fInterpol);
187 //fSetup->AddEntry("Use Ra/Dec from file", IDM_kUseFileRaDec);
188 fSetup->Associate(this);
189 fList->Add(fSetup);
190
191 fCaosPrint = new MGPopupMenu(p);
192 fCaosPrint->AddEntry("&Leds", IDM_kCaosPrintLeds);
193 fCaosPrint->AddEntry("&Rings", IDM_kCaosPrintRings);
194 fCaosPrint->Associate(this);
195 fList->Add(fCaosPrint);
196
197 fCaosWrite = new MGPopupMenu(p);
198 fCaosWrite->AddEntry("&Start", IDM_kCaosWriteStart);
199 fCaosWrite->AddEntry("Sto&p", IDM_kCaosWriteStop);
200 fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
201 fCaosWrite->Associate(this);
202 fList->Add(fCaosWrite);
203
204 fCaosAnalyse = new MGPopupMenu(p);
205 fCaosAnalyse->AddEntry("S&tart Analyse", IDM_kCaosAnalStart);
206 fCaosAnalyse->AddEntry("St&opp Analyse", IDM_kCaosAnalStop);
207 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
208 // fCaosAnalyse->AddEntry("&Reset Histograms", IDM_kResetHistograms);
209 // fCaosAnalyse->AddEntry("Reset &Graph", IDM_kResetGraph);
210 fCaosAnalyse->Associate(this);
211 fList->Add(fCaosAnalyse);
212
213 fMenu = new MGMenuBar(this, 0, 0, kHorizontalFrame);
214 fMenu->AddPopup("&Display", fDisplay, NULL);
215 fMenu->AddPopup("&WritePics", fWritePictures, NULL);
216 fMenu->AddPopup("&Setup", fSetup, NULL);
217 fMenu->Resize(fMenu->GetDefaultSize());
218 fMenu->BindKeys(this);
219 AddFrame(fMenu);
220 fList->Add(fMenu);
221
222 fCaOs = new MGPopupMenu(p);
223 fCaOs->AddPopup("&Write", fCaosWrite);
224 fCaOs->AddPopup("&Print", fCaosPrint);
225 fCaOs->AddPopup("&Analyse", fCaosAnalyse);
226 fCaOs->Associate(this);
227 fCaOs->BindKeys(fMenu, this);
228 fList->Add(fCaOs);
229
230 fCRaDec = new MGCoordinates(this, kETypeRaDec);
231 fCRaDec->Move(1, fMenu->GetDefaultHeight()+584);
232 AddFrame(fCRaDec);
233 fList->Add(fCRaDec);
234
235 fCZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
236 fCZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+584);
237 AddFrame(fCZdAz);
238 fList->Add(fCZdAz);
239
240 fPZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
241 fPZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+630);
242 AddFrame(fPZdAz);
243 fList->Add(fPZdAz);
244
245 fFps = new TGLabel(this, "---fps");
246 fFps->SetTextJustify(kTextRight);
247 fFps->Move(633, fMenu->GetDefaultHeight()+578);
248 AddFrame(fFps);
249 fList->Add(fFps);
250
251 fPosZoom = new TGLabel(this, "----.--d/----.--d (----, ----)");
252 fPosZoom->SetTextJustify(kTextRight);
253 fPosZoom->Move(620, fMenu->GetDefaultHeight()+678);
254 AddFrame(fPosZoom);
255 fList->Add(fPosZoom);
256
257 TGLabel *l = new TGLabel(this, "Arb.-Sky Pos");
258 l->SetTextJustify(kTextLeft);
259 l->Move(480+32, fMenu->GetDefaultHeight()+590);
260 AddFrame(l);
261 fList->Add(l);
262
263 l = new TGLabel(this, "arcsec/pix");
264 l->SetTextJustify(kTextLeft);
265 l->Move(605, fMenu->GetDefaultHeight()+619+13);
266 AddFrame(l);
267 fList->Add(l);
268
269 l = new TGLabel(this, "deg");
270 l->SetTextJustify(kTextLeft);
271 l->Move(605, fMenu->GetDefaultHeight()+619-10);
272 AddFrame(l);
273 fList->Add(l);
274
275 l = new TGLabel(this, "Pointing Pos");
276 l->SetTextJustify(kTextLeft);
277 l->Move(480+32, fMenu->GetDefaultHeight()+655);
278 AddFrame(l);
279 fList->Add(l);
280
281 const Double_t pixsize = 23.4;
282
283 fSao->SetPixSize(pixsize/3600);
284 fSao->SetRotationAngle(0);
285
286 TString txt;
287 txt += pixsize;
288
289 fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
290 fPixSize->SetAlignment(kTextCenterX);
291 fPixSize->Move(547, fMenu->GetDefaultHeight()+617+13);
292 AddFrame(fPixSize);
293 fList->Add(fPixSize);
294
295 fAngle = new TGTextEntry(this, " 0", IDM_kAngle);
296 fAngle->SetAlignment(kTextCenterX);
297 fAngle->Move(547, fMenu->GetDefaultHeight()+617-10);
298 AddFrame(fAngle);
299 fList->Add(fAngle);
300
301 // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
302 // AddFrame(fLineSep, new TGLayoutHints (kLHintsNormal | kLHintsExpandX));
303 // fList->Add(fLineSep);
304
305 //
306 // Create Image Display
307 //
308 fZoomImage = new MGImage(this, kZOOM, kZOOM);
309 fZoomImage->Move(768-kZOOM-2, 700-kZOOM-2);
310 AddFrame(fZoomImage);
311 fList->Add(fZoomImage);
312
313 fImage = new MGImage(this, 768, 576);
314 fImage->Move(0, fMenu->GetDefaultHeight());
315 AddFrame(fImage);
316 fList->Add(fImage);
317
318 //
319 // Make everything visible
320 //
321 SetWindowName("MStarguider Main Window");
322 SetIconName("MStarguider");
323
324 MapSubwindows();
325 MapWindow();
326
327 //------------------------------------------------------------
328 // XY xy(3.819444, 24.05333);
329 // fCRaDec->SetCoordinates(xy);
330 // fRaDec->Set(xy.X()*360/24, xy.Y());
331 //------------------------------------------------------------
332}
333
334MStarguider::MStarguider(MObservatory::LocationName_t obs)
335: TGMainFrame(gClient->GetRoot(), 768, 740), fCosy(NULL), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2)
336{
337 cout << " #### FIXME: Make MCaos Thread safe!" << endl;
338
339 fSao = new StarCatalog(obs);
340 fRaDec = new RaDec(180, 40);
341
342 fCaos = new MCaos;
343 fCaos->ReadResources();
344
345 InitGui();
346
347 gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kTRUE);
348
349 fTimer=new TTimer(this, 1000/25); // 100ms
350 fTimer->TurnOn();
351
352 fTime.Now();
353}
354
355MStarguider::~MStarguider()
356{
357 gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kFALSE);
358
359 fTimer->TurnOff();
360 delete fTimer;
361
362 delete fList;
363
364 delete fCaos;
365 delete fSao;
366 delete fRaDec;
367
368 cout << "Camera Display destroyed." << endl;
369}
370
371void MStarguider::Layout()
372{
373 // Resize(GetDefaultSize());
374}
375
376void MStarguider::CloseWindow()
377{
378 cout << "EventDisplay::CloseWindow: Exit Application Loop." << endl;
379
380 //fClient.ExitLoop();
381 // cout << "FIXME: ExitLoop not called!!!!!!" << endl;
382 gSystem->ExitLoop();
383}
384
385void MStarguider::Toggle(MGPopupMenu *p, UInt_t id)
386{
387 if (p->IsEntryChecked(id))
388 p->UnCheckEntry(id);
389 else
390 p->CheckEntry(id);
391}
392
393Bool_t MStarguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
394{
395 switch (GET_MSG(msg))
396 {
397 case kC_TEXTENTRY:
398 if (GET_SUBMSG(msg)==kTE_ENTER)
399 switch (mp1)
400 {
401 case IDM_kPixSize:
402 {
403 const Float_t pixsize = atof(fPixSize->GetText());
404 cout << "Pixel Size changed to " << pixsize << "\"/pix" << endl;
405 fSao->SetPixSize(pixsize/3600);
406 return kTRUE;
407 }
408 case IDM_kAngle:
409 {
410 const Float_t angle = atof(fAngle->GetText());
411 cout << "Rotation Angle changed to " << angle << "deg" << endl;
412 fSao->SetRotationAngle(angle);
413 return kTRUE;
414 }
415 }
416 return kTRUE;
417
418 case kC_COMMAND:
419 switch (GET_SUBMSG(msg))
420 {
421 case kCM_MENU:
422 switch (mp1)
423 {
424 case IDM_kCatalog:
425 Toggle(fDisplay, IDM_kCatalog);
426 if (fDisplay->IsEntryChecked(IDM_kCatalog))
427 fDisplay->EnableEntry(IDM_kStarguider);
428 else
429 {
430 fDisplay->UnCheckEntry(IDM_kStarguider);
431 fDisplay->DisableEntry(IDM_kStarguider);
432 }
433 return kTRUE;
434
435 case IDM_kStarguider:
436 Toggle(fDisplay, IDM_kStarguider);
437 gSystem->Unlink("tracking_error.txt");
438 return kTRUE;
439
440 case IDM_kFilter:
441 Toggle(fDisplay, IDM_kFilter);
442 if (fDisplay->IsEntryChecked(IDM_kFilter))
443 fDisplay->EnableEntry(IDM_kStarguider);
444 else
445 {
446 fDisplay->UnCheckEntry(IDM_kStarguider);
447 fDisplay->DisableEntry(IDM_kStarguider);
448 }
449 return kTRUE;
450
451 case IDM_kStretch:
452 Toggle(fDisplay, IDM_kStretch);
453 return kTRUE;
454
455 case IDM_kCaosFilter:
456 //if (!fDisplay->IsEntryChecked(IDM_kCaosFilter))
457 // fCaos->OpenFile();
458 Toggle(fDisplay, IDM_kCaosFilter);
459 if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
460 fMenu->AddPopup("&CaOs", fCaOs, NULL);
461 else
462 {
463 if(fCaosWrite->IsEntryChecked(IDM_kCaosPrintLeds))
464 fCaosWrite->UnCheckEntry(IDM_kCaosPrintLeds);
465 if(fCaosWrite->IsEntryChecked(IDM_kCaosPrintRings))
466 fCaosWrite->UnCheckEntry(IDM_kCaosPrintRings);
467 if(fCaosAnalyse->IsEntryEnabled(IDM_kCaosAnalStop))
468 {
469 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
470 fCaosAnalyse->EnableEntry(IDM_kCaosAnalStart);
471 fCaos->DeleteHistograms();
472 }
473 if(fCaosWrite->IsEntryEnabled(IDM_kCaosWriteStop))
474 {
475 fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
476 fCaosWrite->EnableEntry(IDM_kCaosWriteStart);
477 fCaos->CloseFile();
478 }
479 fMenu->RemovePopup("CaOs");
480 }
481 fMenu->Resize(fMenu->GetDefaultSize());
482 MapSubwindows();
483 MapWindow();
484 return kTRUE;
485
486 case IDM_kCaosPrintLeds:
487 case IDM_kCaosPrintRings:
488 Toggle(fCaosPrint, mp1);
489 return kTRUE;
490
491 case IDM_kCaosAnalStart:
492 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStart);
493 fCaosAnalyse->EnableEntry(IDM_kCaosAnalStop);
494 fCaos->InitHistograms();
495 return kTRUE;
496
497 case IDM_kCaosAnalStop:
498 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
499 fCaosAnalyse->EnableEntry(IDM_kCaosAnalStart);
500 fCaos->ShowHistograms();
501 fCaos->DeleteHistograms();
502 return kTRUE;
503
504 case IDM_kCaosWriteStart:
505 fCaosWrite->DisableEntry(IDM_kCaosWriteStart);
506 fCaosWrite->EnableEntry(IDM_kCaosWriteStop);
507 fCaos->OpenFile();
508 return kTRUE;
509
510 case IDM_kCaosWriteStop:
511 fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
512 fCaosWrite->EnableEntry(IDM_kCaosWriteStart);
513 fCaos->CloseFile();
514 return kTRUE;
515
516 case IDM_kStart:
517 fWritePictures->DisableEntry(IDM_kStart);
518 fWritePictures->EnableEntry(IDM_kStop);
519 return kTRUE;
520
521 case IDM_kStop:
522 fWritePictures->DisableEntry(IDM_kStop);
523 fWritePictures->EnableEntry(IDM_kStart);
524 return kTRUE;
525
526 case IDM_kPNG:
527 fFileType->CheckEntry(IDM_kPNG);
528 fFileType->UnCheckEntry(IDM_kPPM);
529 return kTRUE;
530
531 case IDM_kPPM:
532 fFileType->CheckEntry(IDM_kPPM);
533 fFileType->UnCheckEntry(IDM_kPNG);
534 return kTRUE;
535
536 case IDM_kOnce:
537 fWriteType->CheckEntry(IDM_kOnce);
538 fWriteType->UnCheckEntry(IDM_kContinous);
539 return kTRUE;
540
541 case IDM_kContinous:
542 fWriteType->CheckEntry(IDM_kContinous);
543 fWriteType->UnCheckEntry(IDM_kOnce);
544 return kTRUE;
545
546 case IDM_kRate25ps:
547 case IDM_kRate5ps:
548 case IDM_kRate1s:
549 case IDM_kRate5s:
550 case IDM_kRate30s:
551 case IDM_kRate1m:
552 case IDM_kRate5m:
553 for (int i=IDM_kRate25ps; i<=IDM_kRate5m; i++)
554 if (mp1==i)
555 fWriteRate->CheckEntry(i);
556 else
557 fWriteRate->UnCheckEntry(i);
558 switch (mp1)
559 {
560 case IDM_kRate25ps:
561 fWrtRate = 1;
562 return kTRUE;
563 case IDM_kRate5ps:
564 fWrtRate = 5;
565 return kTRUE;
566 case IDM_kRate1s:
567 fWrtRate = 25;
568 return kTRUE;
569 case IDM_kRate5s:
570 fWrtRate = 5*25;
571 return kTRUE;
572 case IDM_kRate30s:
573 fWrtRate = 30*25;
574 return kTRUE;
575 case IDM_kRate1m:
576 fWrtRate = 60*25;
577 return kTRUE;
578 case IDM_kRate5m:
579 fWrtRate = 5*60*25;
580 return kTRUE;
581 }
582 return kTRUE;
583
584 case IDM_kInterpol125:
585 case IDM_kInterpol50:
586 case IDM_kInterpol25:
587 case IDM_kInterpol10:
588 case IDM_kInterpol5:
589 case IDM_kInterpol2:
590 case IDM_kInterpol1:
591 for (int i=IDM_kInterpol125; i<=IDM_kInterpol1; i++)
592 if (mp1==i)
593 fInterpol->CheckEntry(i);
594 else
595 fInterpol->UnCheckEntry(i);
596 switch (mp1)
597 {
598 case IDM_kInterpol1:
599 fIntRate = 1;
600 return kTRUE;
601 case IDM_kInterpol2:
602 fIntRate = 2;
603 return kTRUE;
604 case IDM_kInterpol5:
605 fIntRate = 5;
606 return kTRUE;
607 case IDM_kInterpol10:
608 fIntRate = 10;
609 return kTRUE;
610 case IDM_kInterpol25:
611 fIntRate = 25;
612 return kTRUE;
613 case IDM_kInterpol50:
614 fIntRate = 50;
615 return kTRUE;
616 case IDM_kInterpol125:
617 fIntRate = 125;
618 return kTRUE;
619 }
620 return kTRUE;
621
622 case IDM_kLimMag3:
623 case IDM_kLimMag4:
624 case IDM_kLimMag5:
625 case IDM_kLimMag6:
626 case IDM_kLimMag7:
627 case IDM_kLimMag8:
628 case IDM_kLimMag9:
629 for (int i=IDM_kLimMag3; i<=IDM_kLimMag9; i++)
630 if (mp1==i)
631 fLimMag->CheckEntry(i);
632 else
633 fLimMag->UnCheckEntry(i);
634
635 fSao->SetLimitMag(mp1-IDM_kLimMag3+3);
636 return kTRUE;
637 }
638 break;
639 }
640 break;
641 }
642
643 return kTRUE;
644}
645
646void MStarguider::SetPointingPosition(RaDec rd)
647{
648 rd.Ra(rd.Ra()*24/360);
649 fCRaDec->SetCoordinates(rd);
650}
651
652ZdAz MStarguider::TrackingError(TArrayF &x, TArrayF &y, TArrayF &mag) const
653{
654 //
655 // Viewable area (FIXME: AZ)
656 //
657 TH2F h("Hist", "dX/dY", 77, -768/2-.5, 768/2+.5, 58, -576/2-.5, 576/2+.5); // 3
658
659 /*
660 TH1F hmag("HistMag", "Mag", 19, 0, 100);
661 for (int i=0; i<mag.GetSize(); i++)
662 hmag.Fill(mag[i]);
663 */
664
665 //
666 // Search for matching Magnitudes
667 //
668 for (int i=0; i<mag.GetSize(); i++)
669 {
670 if (mag[i]>48-15 && mag[i]<48+15)
671 h.Fill(x[i], y[i]);
672 }
673
674 //
675 // Serach for an excess in the histogram
676 //
677 Int_t mx, my, dummy;
678 h.GetMaximumBin(mx, my, dummy);
679
680 const double xmax = h.GetXaxis()->GetBinCenter(mx);
681 const double dx = h.GetXaxis()->GetBinWidth(mx);
682
683 const double ymax = h.GetYaxis()->GetBinCenter(my);
684 const double dy = h.GetYaxis()->GetBinWidth(my);
685
686 cout << setprecision(3);
687 cout << "Cut-XY: " << xmax << " +- " << dx << " / " << ymax << " +- " << dy << endl;
688
689 TGraph g;
690 for (int i=0; i<mag.GetSize(); i++)
691 {
692 if (!(x[i]>xmax-dx && x[i]<xmax+dx &&
693 y[i]>ymax-dy && y[i]<ymax+dy &&
694 mag[i]>48-15 && mag[i]<48+15))
695 continue;
696
697 g.SetPoint(g.GetN(), x[i], y[i]);
698 }
699
700 cout << "Offset-XY: " << g.GetMean(1) << " +- " << g.GetRMS(1) << " / ";
701 cout << g.GetMean(2) << " +- " << g.GetRMS(2) << endl;
702
703 AltAz pos0 = fSao->CalcAltAzFromPix(768/2, 576/2)*kRad2Deg;
704 AltAz pos1 = fSao->CalcAltAzFromPix(768/2+g.GetMean(1), 576/2+g.GetMean(2))*kRad2Deg;
705
706 ofstream fout1("pointingpos.txt");
707 fout1 << setprecision(10) << fSao->GetMjd()-52000 << " ";
708 if (fCosy)
709 fout1 << fCosy->GetPointingPos() << " ";
710 fout1 << -pos1.Alt() << " " << pos1.Az() << endl;
711
712 pos1 -= pos0;
713
714 ofstream fout2("tracking_error.txt", ios::app);
715 fout2 << setprecision(10) << fSao->GetMjd()-52000 << " ";
716 if (fCosy)
717 fout2 << fCosy->GetPointingPos() << " ";
718 fout2 << -pos1.Alt() << " " << pos1.Az() << endl;
719
720 return ZdAz(-pos1.Alt(), pos1.Az());
721}
722
723void MStarguider::CalcTrackingError(Leds &leds, MStarList &stars)
724{
725 const Int_t max = leds.GetEntries();
726
727 if (stars.GetRealEntries() < 3)
728 {
729 cout << "Sorry, less than 3 stars in FOV!" << endl;
730 return;
731 }
732
733 if (max < 1)
734 {
735 cout << "Sorry, less than 1 detected spot in FOV!" << endl;
736 return;
737 }
738
739 stars.Sort(); // Sort by magnitude
740
741 TString str = "data/tracking_";
742 str += fSao->GetMjd()-52000;
743 str += ".txt";
744
745 ofstream fout(str);
746
747 TArrayF x, y, mag;
748
749 Int_t num = 0;
750
751 // FIXME: Is predifined value 3 a good idea?
752
753 MStar *star;
754 MStarListIter NextStar(&stars);
755 while ((star=NextStar()) && num++<max+3)
756 {
757 TIter NextSp(&leds);
758 Led *spot=NULL;
759 while ((spot=(Led*)NextSp()))
760 {
761 const XY dpos(spot->GetX()-star->GetX(), spot->GetY()-star->GetY());
762
763 const Int_t idx = x.GetSize();
764
765 x.Set(idx+1);
766 y.Set(idx+1);
767 mag.Set(idx+1);
768
769 x.AddAt(dpos.X(), idx);
770 y.AddAt(dpos.Y(), idx);
771 mag.AddAt(spot->GetMag()/star->GetMag(), idx);
772
773 if (fout)
774 fout << x[idx] << " " << y[idx] << " " << mag[idx] << endl;
775 }
776 }
777
778 ZdAz d = TrackingError(x, y, mag);
779
780 //
781 // Calculated offsets
782 //
783
784 // round= floor(x+.5)
785 cout << "Offset-ZdAz: " << d.Zd()*60 << "' / " << d.Az()*60 << "'" << endl;
786 cout << "Offset-ZdAz: " << d.Zd()/360*16384 << " / " << d.Az()/360*16384 << " (SE) " << endl;
787
788 //
789 // Current Pointing position
790 //
791 ZdAz cpos = fSao->GetZdAz()-d;
792 fPZdAz->SetCoordinates(cpos);
793}
794
795bool MStarguider::Interpolate(const unsigned long n, byte *img) const
796{
797 if (fIntRate<=1)
798 return true;
799
800 static unsigned short myimg[768*576];
801
802 unsigned short *f = myimg;
803 byte *i = img;
804 byte *e = img+768*576;
805
806 while (i<e)
807 *f++ += *i++;
808
809 if (n%fIntRate)
810 return false;
811
812 f = myimg;
813 i = img;
814 e = img+768*576;
815
816 while (i<e)
817 *i++ = (byte)(*f++/fIntRate);
818
819 memset(myimg, 0, sizeof(myimg));
820
821 return true;
822}
823
824void MStarguider::ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
825{
826 static unsigned long n0 = n;
827
828 Timer t(tm);
829
830 const Double_t d = t-fTime;
831 if (d>1)
832 {
833 fFps->SetText(Form("%dfps", (int)((n-n0)/d+.5)));
834 fTime = t;
835 n0 = n;
836 }
837
838 if (!Interpolate(n, img))
839 return;
840
841 FilterLed f(img, 768, 576, 2.5); // 2.5
842
843 if (fDisplay->IsEntryChecked(IDM_kStretch))
844 f.Stretch();
845
846 if (!fWritePictures->IsEntryEnabled(IDM_kStart) &&
847 (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
848 {
849 if (fFileType->IsEntryChecked(IDM_kPNG))
850 Writer::Png("pix/file", img, tm);
851
852 if (fFileType->IsEntryChecked(IDM_kPPM))
853 Writer::Ppm("pix/file", img, tm);
854
855 if (fWriteType->IsEntryChecked(IDM_kOnce))
856 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
857 }
858
859 MStarList spots;
860 if (fDisplay->IsEntryChecked(IDM_kFilter))
861 f.Execute();
862
863 if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
864 {
865 const bool printl = fCaosPrint->IsEntryChecked(IDM_kCaosPrintLeds);
866 const bool printr = fCaosPrint->IsEntryChecked(IDM_kCaosPrintRings);
867 ZdAz pos;
868 if (fCosy)
869 pos = fCosy->GetPointingPos();
870
871 fCaos->Run(img, printl, printr, pos, t);
872 }
873
874 byte zimg[kZOOM*kZOOM];
875 for (int y=0; y<kZOOM; y++)
876 for (int x=0; x<kZOOM; x++)
877 zimg[x+y*kZOOM] = img[(fDx+(x-kZOOM/2)/2)+(fDy+(y-kZOOM/2)/2)*768];
878
879 fZoomImage->DrawImg(zimg);
880
881 if (fDisplay->IsEntryChecked(IDM_kCatalog))
882 {
883 Timer time(tm);
884
885 XY xy = fCRaDec->GetCoordinates();
886 fRaDec->Set(xy.X()*360/24, xy.Y());
887
888 UpdatePosZoom();
889
890 MStarList stars;
891 fSao->GetStars(stars, time.GetMjd(), *fRaDec);
892
893 if (fDisplay->IsEntryChecked(IDM_kStarguider))
894 {
895 Leds leds;
896 f.Execute(leds);
897
898 cout << "Found: " << leds.GetEntries() << " stars." << endl;
899
900 CalcTrackingError(leds, stars);
901 }
902
903 byte cimg[768*576];
904 fSao->GetImg(img, cimg, stars);
905
906 const float r = 60*60/fSao->GetPixSize()+1;
907 f.DrawCircle(0.5*r);
908 f.DrawCircle(1.0*r);
909 f.DrawCircle(1.5*r);
910
911 fCZdAz->SetCoordinates(fSao->GetZdAz());
912
913 fImage->DrawColImg(img, cimg);
914 }
915 else
916 fImage->DrawImg(img);
917}
918
919void MStarguider::UpdatePosZoom()
920{
921 if (fDisplay->IsEntryChecked(IDM_kCatalog))
922 {
923 fSao->Now();
924 AltAz aa = fSao->CalcAltAzFromPix(fDx, fDy)*kRad2Deg;
925
926 if (aa.Az()<0)
927 aa.Az(aa.Az()+360);
928 fPosZoom->SetText(Form("%.1fd/%.1fd (%d, %d)",
929 -aa.Alt(), aa.Az()-180, fDx, fDy));
930 }
931 else
932 fPosZoom->SetText(Form("(%d, %d)", fDx, fDy));
933}
934
935Bool_t MStarguider::HandleDoubleClick(Event_t *event)
936{
937 const Int_t w = fImage->GetWidth();
938 const Int_t h = fImage->GetHeight();
939 const Int_t x = fImage->GetX();
940 const Int_t y = fImage->GetY();
941
942 if (!(event->fX>x && event->fX<x+w && event->fY>y && event->fY<y+h))
943 return kTRUE;
944
945 Int_t dx = event->fX-x;
946 Int_t dy = event->fY-y;
947
948 if (dx<kZOOM/4) dx=kZOOM/4;
949 if (dy<kZOOM/4) dy=kZOOM/4;
950 if (dx>766-kZOOM/4) dx=766-kZOOM/4;
951 if (dy>574-kZOOM/4) dy=574-kZOOM/4;
952
953 fDx = dx;
954 fDy = dy;
955
956 UpdatePosZoom();
957 return kTRUE;
958}
Note: See TracBrowser for help on using the repository browser.