source: trunk/Cosy/main/MStarguider.cc@ 18581

Last change on this file since 18581 was 16764, checked in by tbretz, 11 years ago
Updated Interpolate so that for the sum image there is only one loop over the whole data
File size: 62.6 KB
Line 
1#undef EXPERT
2#define FACT
3
4#include "MStarguider.h"
5
6#include <fstream> // ifstream
7
8#include <TH2F.h>
9#include <TGraph.h>
10#include <TTimer.h>
11#include <TEnv.h>
12#include <TSystem.h>
13#include <TFile.h> // temp writeout of histos
14//#include <TSocket.h>
15
16#include <TGMenu.h>
17#include <TGLabel.h>
18#include <TGButton.h>
19#include <TGSplitter.h> // TGHorizontal3DLine
20#include <TGTextEntry.h>
21#include <TGLayout.h>
22#include "MString.h"
23
24#include "MLog.h"
25#include "MLogManip.h"
26
27#include "MAstro.h"
28#include "MString.h"
29
30
31#include "MCosy.h"
32#include "MCaos.h"
33// #include "MStargLeds.h"
34#include "MStargHistograms.h"
35#include "MDriveCom.h"
36
37#include "MGStarg.h"
38#include "MGNumStars.h"
39
40#include "MGImage.h"
41#include "MGCoordinates.h"
42
43#include "Camera.h"
44#include "PngReader.h"
45
46#include "Led.h"
47#include "Writer.h"
48#include "FilterLed.h"
49#include "MStarList.h"
50#include "CaosFilter.h"
51#include "StarCatalog.h"
52#include "MGeomCamMagic.h"
53#include "MAstroCamera.h"
54
55#include "MGCosy.h"
56
57ClassImp(MStarguider);
58
59using namespace std;
60
61enum {
62 IDM_kFilter,
63 IDM_kFindStar,
64 IDM_kCaosFilter,
65 IDM_kCatalog,
66 IDM_kStarguider,
67 IDM_kStart,
68 IDM_kStop,
69 IDM_kFileType,
70 IDM_kPPM,
71 IDM_kPNG,
72 IDM_kOnce,
73 IDM_kStretch,
74 IDM_kInput,
75 IDM_kChannel1,
76 IDM_kChannel2,
77 IDM_kChannel3,
78 IDM_kContinous,
79 IDM_kRate25ps,
80 IDM_kRate5ps,
81 IDM_kRate1s,
82 IDM_kRate5s,
83 IDM_kRate30s,
84 IDM_kRate1m,
85 IDM_kRate5m,
86 IDM_kSetup,
87 IDM_kLimMag3,
88 IDM_kLimMag4,
89 IDM_kLimMag5,
90 IDM_kLimMag6,
91 IDM_kLimMag7,
92 IDM_kLimMag8,
93 IDM_kLimMag9,
94 IDM_kPixSize,
95 IDM_kAngle,
96 IDM_kCut,
97 IDM_kInterpol250,
98 IDM_kInterpol125,
99 IDM_kInterpol50,
100 IDM_kInterpol25,
101 IDM_kInterpol10,
102 IDM_kInterpol5,
103 IDM_kInterpol2,
104 IDM_kInterpol1,
105 IDM_kCaosPrintRings,
106 IDM_kCaosPrintLeds,
107 IDM_kCaosAnalStart,
108 IDM_kCaosAnalStop,
109 IDM_kCaosWriteStart,
110 IDM_kCaosWriteStop,
111 IDM_kResetHistograms,
112 IDM_kStargAnalysis,
113 IDM_kStargCaosFilter,
114 IDM_kStarguiderMode,
115 IDM_kTpointMode
116
117};
118
119Bool_t MStarguider::HandleTimer(TTimer *t)
120{
121 if (IsMapped())
122 {
123 fImage->DoRedraw();
124 fZoomImage->DoRedraw();
125 }
126
127 if (fCosy && fCosy->GetWin()->IsMapped())
128 fCosy->GetWin()->GetImage()->DoRedraw();
129
130 fGStarg->Update(fPos, fD);
131 if (fDisplay->IsEntryChecked(IDM_kStarguider))
132 fGNumStars->Update(fNumStarsDetected, fNumStarsCorrelated);
133
134 return kTRUE;
135}
136
137#define kZOOM 96
138
139XY MStarguider::GetCoordinates() const
140{
141 return fPZdAz->GetCoordinates();
142}
143
144void MStarguider::InitGui(Int_t channel)
145{
146 fList = new MGList;
147 fList->SetOwner();
148
149 const TGWindow *p=gClient->GetRoot();
150
151 fChannel = new TGPopupMenu(p);
152 fChannel->AddEntry("Starfield Camera", IDM_kChannel1);
153 fChannel->AddEntry("TPoint Camera", IDM_kChannel2);
154 fChannel->AddEntry("Read from File", IDM_kChannel3);
155 if (channel<0)
156 fChannel->CheckEntry(IDM_kChannel3);
157 else
158 fChannel->CheckEntry(channel==0?IDM_kChannel1:IDM_kChannel2);
159 fChannel->Associate(this);
160 fList->Add(fChannel);
161
162 fFileType = new TGPopupMenu(p);
163 fFileType->AddEntry("PP&M", IDM_kPPM);
164 fFileType->AddEntry("&PNG", IDM_kPNG);
165 fFileType->CheckEntry(IDM_kPNG);
166 fFileType->Associate(this);
167 fList->Add(fFileType);
168
169 fWriteType = new TGPopupMenu(p);
170 fWriteType->AddEntry("&Once", IDM_kOnce);
171 fWriteType->AddEntry("&Continous", IDM_kContinous);
172 fWriteType->CheckEntry(IDM_kOnce);
173 fWriteType->Associate(this);
174 fList->Add(fWriteType);
175
176 fWriteRate = new TGPopupMenu(p);
177 fWriteRate->AddEntry("25/s", IDM_kRate25ps);
178 fWriteRate->AddEntry("5/s", IDM_kRate5ps);
179 fWriteRate->AddEntry("1s", IDM_kRate1s);
180 fWriteRate->AddEntry("5s", IDM_kRate5s);
181 fWriteRate->AddEntry("30s", IDM_kRate30s);
182 fWriteRate->AddEntry("1min", IDM_kRate1m);
183 fWriteRate->AddEntry("5min", IDM_kRate5m);
184 fWriteRate->CheckEntry(IDM_kRate1m);
185 fWriteRate->Associate(this);
186 fList->Add(fWriteRate);
187
188 fWrtRate = 25*60;
189
190 fLimMag = new TGPopupMenu(p);
191 fLimMag->AddEntry("3", IDM_kLimMag3);
192 fLimMag->AddEntry("4", IDM_kLimMag4);
193 fLimMag->AddEntry("5", IDM_kLimMag5);
194 fLimMag->AddEntry("6", IDM_kLimMag6);
195 fLimMag->AddEntry("7", IDM_kLimMag7);
196 fLimMag->AddEntry("8", IDM_kLimMag8);
197 fLimMag->AddEntry("9", IDM_kLimMag9);
198 fLimMag->CheckEntry(IDM_kLimMag9);
199 fLimMag->Associate(this);
200 fList->Add(fLimMag);
201
202 fSao->SetLimitMag(9.0);
203
204 fInterpol = new TGPopupMenu(p);
205 fInterpol->AddEntry("250", IDM_kInterpol250);
206 fInterpol->AddEntry("125", IDM_kInterpol125);
207 fInterpol->AddEntry("50", IDM_kInterpol50);
208 fInterpol->AddEntry("25", IDM_kInterpol25);
209 fInterpol->AddEntry("10", IDM_kInterpol10);
210 fInterpol->AddEntry("5", IDM_kInterpol5);
211 fInterpol->AddEntry("2", IDM_kInterpol2);
212 fInterpol->AddEntry("Off", IDM_kInterpol1);
213 fInterpol->Associate(this);
214 fList->Add(fInterpol);
215
216 TString disp=gVirtualX->DisplayName();
217 gLog << all << "Display: " << disp << endl;
218 if (disp.First(':')>=0)
219 disp=disp(0, disp.First(':'));
220
221 if (disp.IsNull() || disp==(TString)"localhost")
222 {
223 fInterpol->CheckEntry(IDM_kInterpol5);
224 fIntRate = 50;
225 }
226 else
227 {
228 fInterpol->CheckEntry(IDM_kInterpol125);
229 fIntRate = 125;
230 }
231
232 fCaosPrint = new TGPopupMenu(p);
233 fCaosPrint->AddEntry("&Leds", IDM_kCaosPrintLeds);
234 fCaosPrint->AddEntry("&Rings", IDM_kCaosPrintRings);
235 fCaosPrint->Associate(this);
236 fList->Add(fCaosPrint);
237
238 fCaosWrite = new TGPopupMenu(p);
239 fCaosWrite->AddEntry("&Start", IDM_kCaosWriteStart);
240 fCaosWrite->AddEntry("Sto&p", IDM_kCaosWriteStop);
241 fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
242 fCaosWrite->Associate(this);
243 fList->Add(fCaosWrite);
244
245 fCaosAnalyse = new TGPopupMenu(p);
246 fCaosAnalyse->AddEntry("S&tart Analysis", IDM_kCaosAnalStart);
247 fCaosAnalyse->AddEntry("St&op Analysis", IDM_kCaosAnalStop);
248 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
249 fCaosAnalyse->Associate(this);
250 fList->Add(fCaosAnalyse);
251
252 fMenu = new TGMenuBar(this, 0, 0, kHorizontalFrame);
253 fDisplay = fMenu->AddPopup("&Display");
254 fMode = fMenu->AddPopup("&Mode");
255 fWritePictures = fMenu->AddPopup("&WritePics");
256 fSetup = fMenu->AddPopup("&Setup");
257 fOperations = fMenu->AddPopup("&Operations");
258 fMenu->Resize(fMenu->GetDefaultSize());
259 AddFrame(fMenu);
260
261 //
262 // Create Menu for MStarguider Display
263 //
264 //fDisplay = new MMGPopupMenu(p);
265 fDisplay->AddEntry("&Filter", IDM_kFilter);
266 fDisplay->AddEntry("Stretch", IDM_kStretch);
267 fDisplay->AddSeparator();
268 fDisplay->AddEntry("Find &Star", IDM_kFindStar);
269 fDisplay->AddEntry("C&aos Filter", IDM_kCaosFilter);
270 fDisplay->AddSeparator();
271 fDisplay->AddEntry("SAO &Catalog", IDM_kCatalog);
272 fDisplay->AddEntry("Starguider", IDM_kStarguider);
273 fDisplay->AddEntry("Starguider LED Filter", IDM_kStargCaosFilter);
274 fDisplay->AddSeparator();
275 if (channel>=0)
276 fDisplay->AddPopup("&Input", fChannel);
277 fDisplay->CheckEntry(IDM_kStretch);
278 fDisplay->Associate(this);
279
280 fMode->AddEntry("Starguider", IDM_kStarguiderMode);
281 fMode->AddEntry("Tpoint", IDM_kTpointMode);
282 fMode->Associate(this);
283
284 fWritePictures->AddEntry("&Start", IDM_kStart);
285 fWritePictures->AddEntry("Sto&p", IDM_kStop);
286 fWritePictures->AddSeparator();
287 fWritePictures->AddPopup("File &Type", fFileType);
288 fWritePictures->AddPopup("&Write Type", fWriteType);
289 fWritePictures->AddPopup("Write &Rate", fWriteRate);
290 fWritePictures->DisableEntry(IDM_kStop);
291 fWritePictures->Associate(this);
292
293 fSetup->AddPopup("Lim. &Magnitude", fLimMag);
294 fSetup->AddPopup("Disp. &Interpolation", fInterpol);
295 fSetup->Associate(this);
296
297 fOperations->AddEntry("Starguider Analysis", IDM_kStargAnalysis);
298 fOperations->DisableEntry(IDM_kStargAnalysis);
299 fOperations->Associate(this);
300
301 fCaOs = new TGPopupMenu(p);
302 fCaOs->AddPopup("&Write", fCaosWrite);
303 fCaOs->AddPopup("&Print", fCaosPrint);
304 fCaOs->AddPopup("&Analyse", fCaosAnalyse);
305 fCaOs->Associate(this);
306 fList->Add(fCaOs);
307
308 fGStarg = new MGStarg(this, 235);
309 fGStarg->Move(530,596+5);
310 fList->Add(fGStarg);
311
312 //RA,Dec for catalog
313 fCRaDec = new MGCoordinates(this, kETypeRaDec);
314 fCRaDec->Move(4, fMenu->GetDefaultHeight()+584);
315 AddFrame(fCRaDec);
316
317 //telescope position
318 fCZdAz = new MGCoordinates(this, kETypeZdAz, 2);
319 fCZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+597);
320 AddFrame(fCZdAz);
321
322 //starguider position
323 fPZdAz = new MGCoordinates(this, kETypeZdAz, 2);
324 fPZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+640);
325 AddFrame(fPZdAz);
326
327 //mispointing
328 fDZdAz = new MGCoordinates(this, kETypeZdAz, 2);
329 fDZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+683);
330 AddFrame(fDZdAz);
331
332 fSZdAz = new MGCoordinates(this, kETypeZdAz, 2);
333 fSZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+795);
334 AddFrame(fSZdAz);
335
336 fGNumStars = new MGNumStars(this, 235);
337 fGNumStars->DrawText("Number of stars");
338 fGNumStars->Move(278, fMenu->GetDefaultHeight()+713);
339 fList->Add(fGNumStars);
340
341 fTPoint = new TGTextButton(this, "TPoint");
342 //fTPoint->Move(4, fMenu->GetDefaultHeight()+785);
343 fTPoint->Move(170, fMenu->GetDefaultHeight()+785);
344 fTPoint->AllowStayDown(kTRUE);
345 AddFrame(fTPoint);
346 /*
347 fStargTPoint = new TGTextButton(this, "StargTPoint");
348 fStargTPoint->Move(170, fMenu->GetDefaultHeight()+785);
349 fStargTPoint->AllowStayDown(kTRUE);
350 AddFrame(fStargTPoint);
351 */
352
353 fFps = new TGLabel(this, "---fps");
354 fFps->SetTextJustify(kTextRight);
355 fFps->Move(650-495, fMenu->GetDefaultHeight()+714+23);
356 AddFrame(fFps);
357
358 fPosZoom = new TGLabel(this, "(----, ----) ----.--d/----.--d");
359 fPosZoom->SetTextJustify(kTextLeft);
360 fPosZoom->Move(4, fMenu->GetDefaultHeight()+765);
361 AddFrame(fPosZoom);
362
363 fSkyBright = new TGLabel(this, "Sky Brightness: --- ");
364 fSkyBright->SetTextJustify(kTextLeft);
365 fSkyBright->Move(4, fMenu->GetDefaultHeight()+785);
366 AddFrame(fSkyBright);
367
368 TGLabel *l = new TGLabel(this, "deg");
369 l->SetTextJustify(kTextLeft);
370 l->Move(606-412, fMenu->GetDefaultHeight()+669);
371 AddFrame(l);
372
373 l = new TGLabel(this, "arcsec/pix");
374 l->SetTextJustify(kTextLeft);
375 l->Move(606-412, fMenu->GetDefaultHeight()+692);
376 AddFrame(l);
377
378 l = new TGLabel(this, "sigma");
379 l->SetTextJustify(kTextLeft);
380 l->Move(606-412, fMenu->GetDefaultHeight()+715);
381 AddFrame(l);
382
383 fCZdAzText = new TGLabel(this, "Zd/Az telescope pointing at");
384 fCZdAzText->SetTextJustify(kTextLeft);
385 fCZdAzText->Move(240+12+20+7, fMenu->GetDefaultHeight()+584-5);
386 AddFrame(fCZdAzText);
387
388 fPZdAzText = new TGLabel(this, "Zd/Az starguider pointing at");
389 fPZdAzText->SetTextJustify(kTextLeft);
390 fPZdAzText->Move(240+12+20+7, fMenu->GetDefaultHeight()+630+20-5-23);
391 AddFrame(fPZdAzText);
392
393 fDZdAzText = new TGLabel(this, "Zd/Az mispointing");
394 fDZdAzText->SetTextJustify(kTextLeft);
395 fDZdAzText->Move(240+12+20+7, fMenu->GetDefaultHeight()+676+2*20-5-46);
396 AddFrame(fDZdAzText);
397
398 // Set input box for rotation angle
399 fAngle = new TGTextEntry(this, " ", IDM_kAngle);
400 fAngle->SetAlignment(kTextCenterX);
401 fAngle->Move(547-410, fMenu->GetDefaultHeight()+667);
402 AddFrame(fAngle);
403
404 SetRotationAngle(-0.2);
405
406 // Set input box for pixel size
407 fPixSize = new TGTextEntry(this, " ", IDM_kPixSize);
408 fPixSize->SetAlignment(kTextCenterX);
409 fPixSize->Move(547-410, fMenu->GetDefaultHeight()+690);
410 AddFrame(fPixSize);
411
412 SetPixSize(48.9);
413
414 // Set input box for cleaning cut
415 fCut = new TGTextEntry(this, " ", IDM_kCut);
416 fCut->SetAlignment(kTextCenterX);
417 fCut->Move(547-410, fMenu->GetDefaultHeight()+713);
418 AddFrame(fCut);
419
420 SetCut(3.0);
421
422 // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
423 // AddFrame(fLineSep, new TGLayoutHints (kLHintsNormal | kLHintsExpandX));
424 // fList->Add(fLineSep);
425
426 //
427 // Create Image Display
428 //
429 fZoomImage = new MGImage(this, kZOOM, kZOOM);
430 // fZoomImage->Move(768-kZOOM-2, 700-kZOOM-2);
431 fZoomImage->Move(4, 700-kZOOM-2+85);
432 AddFrame(fZoomImage);
433
434 fImage = new MGImage(this, 768, 576);
435 fImage->Move(0, fMenu->GetDefaultHeight());
436 AddFrame(fImage);
437
438 const Int_t w = 768;
439 const Int_t h = 840;
440 SetWMSizeHints(w, h, w, h, 1, 1); // set the smallest and biggest size of the Main frame
441
442 //
443 // Make everything visible
444 //
445 SetWindowName("MStarguider Main Window");
446 SetIconName("MStarguider");
447
448 MapSubwindows();
449 fTPoint->UnmapWindow();
450 //fStargTPoint->UnmapWindow();
451 fGStarg->UnmapWindow();
452 fGNumStars->UnmapWindow();
453 fCRaDec->UnmapWindow();
454 fCZdAz->UnmapWindow();
455 fCZdAzText->UnmapWindow();
456 fPZdAz->UnmapWindow();
457 fPZdAzText->UnmapWindow();
458 fDZdAz->UnmapWindow();
459 fDZdAzText->UnmapWindow();
460 fSZdAz->UnmapWindow();
461 fSkyBright->UnmapWindow();
462 MapWindow();
463
464
465 //IconifyWindow();
466
467 //------------------------------------------------------------
468 // XY xy(3.819444, 24.05333);
469 // fCRaDec->SetCoordinates(xy);
470 // fRaDec->Set(xy.X()*360/24, xy.Y());
471 //------------------------------------------------------------
472}
473
474MStarguider::MStarguider(MObservatory::LocationName_t obs, Int_t channel)
475 : TGMainFrame(gClient->GetRoot(), 768, 840),
476 fNumStarsDetected(0),
477 fNumStarsCorrelated(0),
478 fCosy(NULL),
479 fOutTp(0),
480 fOutStargTp(0),
481// fOutRq(0),
482 fDx((768-kZOOM)/2),
483 fDy((512-kZOOM)/2),
484 fStatus(MDriveCom::kStandby),
485 fRadius(200),
486 fTPointFromCC(-1),
487 fFindStarCut(3.0), fFindStarBox(70)
488{
489 gLog << warn << " #### FIXME: Make MCaos Thread safe!" << endl;
490
491 // This means that all objects added with AddFrame are deleted
492 // automatically, including all LayoutHints.
493 SetCleanup();
494
495 fSao = new StarCatalog(obs);
496 fRaDec = new RaDec(180, 40);
497
498 fCaos = new MCaos;
499 fStargCaos = new MCaos;
500
501 fStargHistograms = new MStargHistograms();
502
503 InitGui(channel);
504
505 fTimer=new TTimer(this, 1000/25); // 40ms
506 fTimer->TurnOn();
507
508 fTime.Now();
509
510 fTimeFromTp.Clear();
511 fAltAzOffsetFromTp = AltAz(-1000,-1000);
512
513 gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kTRUE);
514
515 if (channel<0)
516 fGetter = new PngReader(*this);
517 else
518 fGetter = new Camera(*this, channel);
519}
520
521void MStarguider::SetRotationAngle(Double_t angle)
522{
523 fSao->SetRotationAngle(angle);
524
525 TString txt;
526 txt += angle;
527
528 fAngle->SetText(txt);
529}
530
531void MStarguider::SetPixSize(Double_t size)
532{
533 fSao->SetPixSize(size);
534
535 TString txt;
536 txt += size;
537
538 fPixSize->SetText(txt);
539}
540
541void MStarguider::SetCut(Double_t cut)
542{
543 TString txt;
544 txt += cut;
545
546 fCut->SetText(txt);
547}
548
549void MStarguider::SetupEnv(TEnv &env)
550{
551 fCaos->ReadEnv(env, "TPointLeds", kTRUE);
552 fStargCaos->ReadEnv(env, "StarguiderLeds", kTRUE);
553
554 SetRotationAngle(env.GetValue("Starguider.RotationAngle", fSao->GetRotationAngle()));
555 SetCut(env.GetValue("Starguider.CleaningLevel", atof(fCut->GetText())));
556
557 SetPixSize(env.GetValue("StarguiderLeds.ArcsecPerPixel", fSao->GetPixSize()));
558
559 fRadius = env.GetValue("Leds.Radius", fRadius);
560
561 fStarguiderW = env.GetValue("Starguider.Width", fStarguiderW);
562 fStarguiderH = env.GetValue("Starguider.Height", fStarguiderH);
563 fStarguiderX = env.GetValue("Starguider.X", fStarguiderX);
564 fStarguiderY = env.GetValue("Starguider.Y", fStarguiderY);
565
566 fSkyOffsetX = env.GetValue("Starguider.SkyOffsetX", fSkyOffsetX);
567 fSkyOffsetY = env.GetValue("Starguider.SkyOffsetY", fSkyOffsetY);
568
569 fFindStarBox = env.GetValue("FindStar.SizeBox", fFindStarBox);
570 fFindStarCut = env.GetValue("FindStar.CleaningLevel", fFindStarCut);
571}
572
573MStarguider::~MStarguider()
574{
575 fGetter->ExitLoop();
576 delete fGetter;
577
578 gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kFALSE);
579
580 fTimer->TurnOff();
581 delete fTimer;
582
583 delete fList;
584
585 delete fCaos;
586 delete fStargCaos;
587 delete fStargHistograms;
588 delete fSao;
589 delete fRaDec;
590
591 if (fOutTp)
592 delete fOutTp;
593
594 if (fOutStargTp)
595 delete fOutStargTp;
596
597// if (fOutRq)
598// delete fOutRq;
599
600 gLog << inf2 << "Camera Display destroyed." << endl;
601}
602
603void MStarguider::Layout()
604{
605 // Resize(GetDefaultSize());
606}
607
608void MStarguider::CloseWindow()
609{
610 gLog << inf2 << "EventDisplay::CloseWindow: Exit Application Loop." << endl;
611
612 //fClient.ExitLoop();
613 // cout << "FIXME: ExitLoop not called!!!!!!" << endl;
614 fGetter->ExitLoop();
615 gSystem->ExitLoop();
616}
617
618void MStarguider::SwitchOff(TGPopupMenu *p, UInt_t id)
619{
620 p->UnCheckEntry(id);
621 p->DisableEntry(id);
622}
623
624void MStarguider::SetChannel()
625{
626 if (fChannel->IsEntryChecked(IDM_kChannel3))
627 {
628 if (dynamic_cast<PngReader*>(fGetter)==0)
629 {
630 delete fGetter;
631 fGetter=new PngReader(*this);
632 }
633 }
634 else
635 {
636 const Int_t ch = fChannel->IsEntryChecked(IDM_kChannel1) ? 0 : 1;
637 if (dynamic_cast<Camera*>(fGetter)==0)
638 {
639 delete fGetter;
640 fGetter = new Camera(*this, ch);
641 }
642 else
643 fGetter->SetChannel(ch);
644 }
645}
646
647void MStarguider::Toggle(TGPopupMenu *p, UInt_t id)
648{
649 if (p->IsEntryChecked(id))
650 p->UnCheckEntry(id);
651 else
652 p->CheckEntry(id);
653}
654
655void MStarguider::ToggleStargAnalysis()
656{
657 if (fOperations->IsEntryChecked(IDM_kStargAnalysis))
658 fStargHistograms->OpenFile();
659 else
660 fStargHistograms->CloseFile();
661}
662
663/*
664void MStarguider::ToggleFindStar()
665{
666 if (fDisplay->IsEntryChecked(IDM_kFindStar) && fCosy)
667 fTPoint->MapWindow();
668 else
669 {
670 fTPoint->UnmapWindow();
671 fTPoint->SetDown(kFALSE);
672 }
673}
674*/
675void MStarguider::ToggleStarguider()
676{
677 if (fDisplay->IsEntryChecked(IDM_kStarguider))
678 {
679 fLastBright = 0xff;
680
681 fChannel->EnableEntry(IDM_kChannel1);
682 fDisplay->DisableEntry(IDM_kFindStar);
683 fDisplay->DisableEntry(IDM_kCaosFilter);
684 fOperations->EnableEntry(IDM_kStargAnalysis);
685
686 //fStargTPoint->MapWindow();
687
688 fPZdAz->MapWindow();
689 fDZdAz->MapWindow();
690 fSkyBright->MapWindow();
691
692 for (int i=IDM_kLimMag3; i<=IDM_kLimMag9; i++)
693 fLimMag->UnCheckEntry(i);
694 fLimMag->CheckEntry(IDM_kLimMag9);
695 fSao->SetLimitMag(9.0);
696
697 for (int i=IDM_kInterpol250; i<=IDM_kInterpol1; i++)
698 fInterpol->UnCheckEntry(i);
699 fInterpol->CheckEntry(IDM_kInterpol125);
700 fIntRate = 125;
701
702 //catalog
703 fDisplay->CheckEntry(IDM_kCatalog);
704 fCRaDec->MapWindow();
705 fCZdAz->MapWindow();
706 fCZdAzText->MapWindow();
707
708 fPZdAz->MapWindow();
709 fPZdAzText->MapWindow();
710 fDZdAz->MapWindow();
711 fDZdAzText->MapWindow();
712
713 fGStarg->MapWindow();
714 fGNumStars->MapWindow();
715
716 SwitchOff(fChannel, IDM_kChannel2);
717 fChannel->CheckEntry(IDM_kChannel1);
718 }
719 else
720 {
721 fStatus = MDriveCom::kStandby;
722
723 //fStargTPoint->UnmapWindow();
724 //fStargTPoint->SetDown(kFALSE);
725
726 fPZdAz->UnmapWindow();
727 fPZdAzText->UnmapWindow();
728 fDZdAz->UnmapWindow();
729 fDZdAzText->UnmapWindow();
730 fSkyBright->UnmapWindow();
731 //catalog
732 fCRaDec->UnmapWindow();
733 fCZdAz->UnmapWindow();
734 fCZdAzText->UnmapWindow();
735 fDisplay->UnCheckEntry(IDM_kCatalog);
736
737 fGStarg->UnmapWindow();
738 fGNumStars->UnmapWindow();
739
740 fChannel->EnableEntry(IDM_kChannel2);
741 fDisplay->EnableEntry(IDM_kFindStar);
742 fDisplay->EnableEntry(IDM_kCaosFilter);
743 fOperations->DisableEntry(IDM_kStargAnalysis);
744 }
745
746 SetChannel();
747 //gSystem->Unlink("tracking_error.txt");
748}
749
750void MStarguider::ToggleCaosFilter()
751{
752 if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
753 {
754 if (!fMode->IsEntryChecked(IDM_kStarguiderMode)
755 && !fMode->IsEntryChecked(IDM_kTpointMode))
756 fMenu->AddPopup("&CaOs", fCaOs, NULL);
757
758 SwitchOff(fDisplay, IDM_kStarguider);
759 SwitchOff(fDisplay, IDM_kStargCaosFilter);
760 }
761 else
762 {
763 fCaosWrite->UnCheckEntry(IDM_kCaosPrintLeds);
764 fCaosWrite->UnCheckEntry(IDM_kCaosPrintRings);
765
766 if (fCaosAnalyse->IsEntryEnabled(IDM_kCaosAnalStop))
767 {
768 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
769 fCaosAnalyse->EnableEntry(IDM_kCaosAnalStart);
770 fCaos->DeleteHistograms();
771 }
772 if (fCaosWrite->IsEntryEnabled(IDM_kCaosWriteStop))
773 {
774 fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
775 fCaosWrite->EnableEntry(IDM_kCaosWriteStart);
776 fCaos->CloseFile();
777 }
778 fMenu->RemovePopup("CaOs");
779
780 fDisplay->EnableEntry(IDM_kStargCaosFilter);
781 fDisplay->EnableEntry(IDM_kStarguider);
782 }
783 fMenu->Resize(fMenu->GetDefaultSize());
784 MapSubwindows(); // maps everything, but we don't want that
785 fTPoint->UnmapWindow();
786 //fStargTPoint->UnmapWindow();
787 fGStarg->UnmapWindow();
788 fGNumStars->UnmapWindow();
789 fCRaDec->UnmapWindow();
790 fCZdAz->UnmapWindow();
791 fCZdAzText->UnmapWindow();
792 fPZdAz->UnmapWindow();
793 fPZdAzText->UnmapWindow();
794 fDZdAz->UnmapWindow();
795 fDZdAzText->UnmapWindow();
796 fSZdAz->UnmapWindow();
797 fSkyBright->UnmapWindow();
798 MapWindow();
799}
800
801Bool_t MStarguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
802{
803 switch (GET_MSG(msg))
804 {
805 case kC_TEXTENTRY:
806 if (GET_SUBMSG(msg)==kTE_ENTER)
807 switch (mp1)
808 {
809 case IDM_kPixSize:
810 {
811 const Float_t pixsize = atof(fPixSize->GetText());
812 gLog << all << "Pixel Size changed to " << pixsize << "\"/pix" << endl;
813 fSao->SetPixSize(pixsize);
814 return kTRUE;
815 }
816 case IDM_kAngle:
817 {
818 const Float_t angle = atof(fAngle->GetText());
819 gLog << all << "Rotation Angle changed to " << angle << "deg" << endl;
820 fSao->SetRotationAngle(angle);
821 return kTRUE;
822 }
823 case IDM_kCut:
824 {
825 const Float_t cut = atof(fCut->GetText());
826 gLog << all << "Starguider cleaning level changed to " << cut << " sigma." << endl;
827 return kTRUE;
828 }
829 }
830 return kTRUE;
831
832 case kC_COMMAND:
833 switch (GET_SUBMSG(msg))
834 {
835 case kCM_MENU:
836 switch (mp1)
837 {
838 case IDM_kCatalog:
839 Toggle(fDisplay, IDM_kCatalog);
840 if (fDisplay->IsEntryChecked(IDM_kCatalog))
841 {
842 fCRaDec->MapWindow();
843 fCZdAz->MapWindow();
844 fCZdAzText->MapWindow();
845 }
846 else
847 {
848 fCRaDec->UnmapWindow();
849 fCZdAz->UnmapWindow();
850 fCZdAzText->UnmapWindow();
851 }
852 return kTRUE;
853
854 case IDM_kStarguider:
855 Toggle(fDisplay, IDM_kStarguider);
856 ToggleStarguider();
857 return kTRUE;
858
859 case IDM_kStargAnalysis:
860 Toggle(fOperations, IDM_kStargAnalysis);
861 ToggleStargAnalysis();
862 return kTRUE;
863
864 case IDM_kStarguiderMode:
865 Toggle(fMode, IDM_kStarguiderMode);
866
867 if (fMode->IsEntryChecked(IDM_kStarguiderMode))
868 {
869 //uncheck not needed items
870 //general
871 SwitchOff(fDisplay, IDM_kFilter);
872 SwitchOff(fChannel, IDM_kChannel3);
873 SwitchOff(fOperations, IDM_kStargAnalysis);
874
875 //tpoint
876 SwitchOff(fDisplay, IDM_kFindStar);
877 SwitchOff(fDisplay, IDM_kCaosFilter);
878 ToggleCaosFilter();
879 fMode->UnCheckEntry(IDM_kTpointMode);
880
881 //enable starguider items
882 fDisplay->EnableEntry(IDM_kStargCaosFilter);
883 fDisplay->EnableEntry(IDM_kStarguider);
884 fDisplay->EnableEntry(IDM_kCatalog);
885
886 //check needed items
887 fDisplay->UnCheckEntry(IDM_kStretch);
888 fDisplay->CheckEntry(IDM_kStargCaosFilter);
889 fDisplay->CheckEntry(IDM_kStarguider);
890 ToggleStarguider();
891
892 fTPoint->MapWindow();
893 }
894 else
895 {
896 //uncheck
897 //starguider items
898 fDisplay->UnCheckEntry(IDM_kStargCaosFilter);
899 fDisplay->UnCheckEntry(IDM_kStarguider);
900 fTPoint->UnmapWindow();
901 ToggleStarguider();
902
903 //general
904 fDisplay->UnCheckEntry(IDM_kCatalog);
905 fDisplay->EnableEntry(IDM_kFilter);
906 fDisplay->EnableEntry(IDM_kCaosFilter);
907 fDisplay->EnableEntry(IDM_kFindStar);
908 fChannel->EnableEntry(IDM_kChannel3);
909 }
910 return kTRUE;
911
912 case IDM_kTpointMode:
913 Toggle(fMode, IDM_kTpointMode);
914
915 if (fMode->IsEntryChecked(IDM_kTpointMode))
916 {
917 //unchecking not needed items
918 //general
919 SwitchOff(fDisplay, IDM_kFilter);
920 SwitchOff(fChannel, IDM_kChannel3);
921
922 //from starguider
923 SwitchOff(fDisplay, IDM_kStargCaosFilter);
924 SwitchOff(fDisplay, IDM_kCatalog);
925 SwitchOff(fDisplay, IDM_kStarguider);
926 ToggleStarguider();
927 fMode->UnCheckEntry(IDM_kStarguiderMode);
928 SwitchOff(fOperations, IDM_kStargAnalysis);
929 ToggleStargAnalysis();
930
931 //switch camera
932 SwitchOff(fChannel, IDM_kChannel1);
933 fChannel->CheckEntry(IDM_kChannel2);
934
935 SetChannel();
936
937 //checking needed items
938 fDisplay->UnCheckEntry(IDM_kStretch);
939 fDisplay->CheckEntry(IDM_kCaosFilter);
940 ToggleCaosFilter();
941 fDisplay->CheckEntry(IDM_kFindStar);
942 fTPoint->MapWindow();
943 }
944 else
945 {
946 //enable
947 //starguider items
948 fDisplay->EnableEntry(IDM_kStargCaosFilter);
949 fDisplay->EnableEntry(IDM_kCatalog);
950 fDisplay->EnableEntry(IDM_kStarguider);
951 fOperations->EnableEntry(IDM_kStargAnalysis);
952
953 //general
954 fDisplay->EnableEntry(IDM_kFilter);
955 fChannel->EnableEntry(IDM_kChannel1);
956 fChannel->EnableEntry(IDM_kChannel3);
957
958 //tpoint
959 fDisplay->UnCheckEntry(IDM_kCaosFilter);
960 ToggleCaosFilter();
961 fDisplay->UnCheckEntry(IDM_kFindStar);
962 fTPoint->UnmapWindow();
963 }
964 return kTRUE;
965
966 case IDM_kFilter:
967 Toggle(fDisplay, IDM_kFilter);
968 return kTRUE;
969
970 case IDM_kFindStar:
971 Toggle(fDisplay, IDM_kFindStar);
972 //ToggleFindStar();
973 return kTRUE;
974
975 case IDM_kStretch:
976 Toggle(fDisplay, IDM_kStretch);
977 return kTRUE;
978
979 case IDM_kCaosFilter:
980 Toggle(fDisplay, IDM_kCaosFilter);
981 ToggleCaosFilter();
982 return kTRUE;
983
984 case IDM_kStargCaosFilter:
985 Toggle(fDisplay, IDM_kStargCaosFilter);
986 if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter))
987 {
988 SwitchOff(fDisplay, IDM_kCaosFilter);
989 SwitchOff(fDisplay, IDM_kFindStar);
990 SwitchOff(fDisplay, IDM_kFilter);
991 }
992 else
993 {
994 fDisplay->EnableEntry(IDM_kFilter);
995 fDisplay->EnableEntry(IDM_kFindStar);
996 fDisplay->EnableEntry(IDM_kCaosFilter);
997 }
998 return kTRUE;
999
1000 case IDM_kCaosPrintLeds:
1001 case IDM_kCaosPrintRings:
1002 Toggle(fCaosPrint, mp1);
1003 return kTRUE;
1004
1005 case IDM_kCaosAnalStart:
1006 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStart);
1007 fCaosAnalyse->EnableEntry(IDM_kCaosAnalStop);
1008 fCaos->InitHistograms();
1009 return kTRUE;
1010
1011 case IDM_kCaosAnalStop:
1012 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
1013 fCaosAnalyse->EnableEntry(IDM_kCaosAnalStart);
1014 fCaos->ShowHistograms();
1015 fCaos->DeleteHistograms();
1016 return kTRUE;
1017
1018 case IDM_kCaosWriteStart:
1019 fCaosWrite->DisableEntry(IDM_kCaosWriteStart);
1020 fCaosWrite->EnableEntry(IDM_kCaosWriteStop);
1021 fCaos->OpenFile();
1022 return kTRUE;
1023
1024 case IDM_kCaosWriteStop:
1025 fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
1026 fCaosWrite->EnableEntry(IDM_kCaosWriteStart);
1027 fCaos->CloseFile();
1028 return kTRUE;
1029
1030 case IDM_kStart:
1031 fWritePictures->DisableEntry(IDM_kStart);
1032 fWritePictures->EnableEntry(IDM_kStop);
1033 return kTRUE;
1034
1035 case IDM_kStop:
1036 fWritePictures->DisableEntry(IDM_kStop);
1037 fWritePictures->EnableEntry(IDM_kStart);
1038 return kTRUE;
1039
1040 case IDM_kPNG:
1041 fFileType->CheckEntry(IDM_kPNG);
1042 fFileType->UnCheckEntry(IDM_kPPM);
1043 return kTRUE;
1044
1045 case IDM_kPPM:
1046 fFileType->CheckEntry(IDM_kPPM);
1047 fFileType->UnCheckEntry(IDM_kPNG);
1048 return kTRUE;
1049
1050 case IDM_kOnce:
1051 fWriteType->CheckEntry(IDM_kOnce);
1052 fWriteType->UnCheckEntry(IDM_kContinous);
1053 return kTRUE;
1054
1055 case IDM_kContinous:
1056 fWriteType->CheckEntry(IDM_kContinous);
1057 fWriteType->UnCheckEntry(IDM_kOnce);
1058 return kTRUE;
1059
1060 case IDM_kRate25ps:
1061 case IDM_kRate5ps:
1062 case IDM_kRate1s:
1063 case IDM_kRate5s:
1064 case IDM_kRate30s:
1065 case IDM_kRate1m:
1066 case IDM_kRate5m:
1067 for (int i=IDM_kRate25ps; i<=IDM_kRate5m; i++)
1068 if (mp1==i)
1069 fWriteRate->CheckEntry(i);
1070 else
1071 fWriteRate->UnCheckEntry(i);
1072 switch (mp1)
1073 {
1074 case IDM_kRate25ps:
1075 fWrtRate = 1;
1076 return kTRUE;
1077 case IDM_kRate5ps:
1078 fWrtRate = 5;
1079 return kTRUE;
1080 case IDM_kRate1s:
1081 fWrtRate = 25;
1082 return kTRUE;
1083 case IDM_kRate5s:
1084 fWrtRate = 5*25;
1085 return kTRUE;
1086 case IDM_kRate30s:
1087 fWrtRate = 30*25;
1088 return kTRUE;
1089 case IDM_kRate1m:
1090 fWrtRate = 60*25;
1091 return kTRUE;
1092 case IDM_kRate5m:
1093 fWrtRate = 5*60*25;
1094 return kTRUE;
1095 }
1096 return kTRUE;
1097
1098 case IDM_kChannel1:
1099 case IDM_kChannel2:
1100 {
1101 const Int_t ch0 = fChannel->IsEntryChecked(IDM_kChannel1) ? 0 : 1;
1102 const Int_t ch1 = mp1==IDM_kChannel1 ? 0 : 1;
1103
1104 if (ch0==ch1)
1105 return kTRUE;
1106
1107 fChannel->CheckEntry (ch1==0?IDM_kChannel1:IDM_kChannel2);
1108 fChannel->UnCheckEntry(ch1==1?IDM_kChannel1:IDM_kChannel2);
1109
1110 SetChannel();
1111 }
1112 return kTRUE;
1113
1114 case IDM_kInterpol250:
1115 case IDM_kInterpol125:
1116 case IDM_kInterpol50:
1117 case IDM_kInterpol25:
1118 case IDM_kInterpol10:
1119 case IDM_kInterpol5:
1120 case IDM_kInterpol2:
1121 case IDM_kInterpol1:
1122 for (int i=IDM_kInterpol250; i<=IDM_kInterpol1; i++)
1123 if (mp1==i)
1124 fInterpol->CheckEntry(i);
1125 else
1126 fInterpol->UnCheckEntry(i);
1127 switch (mp1)
1128 {
1129 case IDM_kInterpol1:
1130 fIntRate = 1;
1131 return kTRUE;
1132 case IDM_kInterpol2:
1133 fIntRate = 2;
1134 return kTRUE;
1135 case IDM_kInterpol5:
1136 fIntRate = 5;
1137 return kTRUE;
1138 case IDM_kInterpol10:
1139 fIntRate = 10;
1140 return kTRUE;
1141 case IDM_kInterpol25:
1142 fIntRate = 25;
1143 return kTRUE;
1144 case IDM_kInterpol50:
1145 fIntRate = 50;
1146 return kTRUE;
1147 case IDM_kInterpol125:
1148 fIntRate = 125;
1149 return kTRUE;
1150 case IDM_kInterpol250:
1151 fIntRate = 250;
1152 return kTRUE;
1153 }
1154 return kTRUE;
1155
1156 case IDM_kLimMag3:
1157 case IDM_kLimMag4:
1158 case IDM_kLimMag5:
1159 case IDM_kLimMag6:
1160 case IDM_kLimMag7:
1161 case IDM_kLimMag8:
1162 case IDM_kLimMag9:
1163 for (int i=IDM_kLimMag3; i<=IDM_kLimMag9; i++)
1164 if (mp1==i)
1165 fLimMag->CheckEntry(i);
1166 else
1167 fLimMag->UnCheckEntry(i);
1168
1169 fSao->SetLimitMag(mp1-IDM_kLimMag3+3);
1170 return kTRUE;
1171 }
1172 break;
1173 }
1174 break;
1175 }
1176
1177 return kTRUE;
1178}
1179
1180void MStarguider::SetPointingPosition(RaDec rd)
1181{
1182 rd.Ra(rd.Ra()*24/360);
1183 fCRaDec->SetCoordinates(rd);
1184}
1185
1186ZdAz MStarguider::TrackingError(TArrayF &x, TArrayF &y, TArrayF &mag,
1187 Int_t &num) const
1188{
1189 num = -1;
1190
1191 // Width of the final 2D gaussian
1192 const Double_t width = 0.8;
1193
1194 // The binning is made 1.7 sigma (which, in the 1D case, gives the
1195 // highest significance of a gaussian signal) (1bin equiv 3x3 sigma)
1196 const Double_t max = 50;
1197 const Int_t n = TMath::Nint(2*max/(1.77*1.7*width));
1198 //1.77=sqrt(pi) comes from pir=bin
1199
1200 // Fill a histogram with the dx/dy values from the file
1201 TH2F hist("Hist", "dX/dY", n, -max, max, n, -max, max);
1202// hist.SetDirectory(0);
1203
1204 //
1205 // Search for matching Magnitudes
1206 //
1207 //for (int i=0; i<mag.GetSize(); i++)
1208 //{
1209 // if (mag[i]>48-15 && mag[i]<48+15)
1210 // h.Fill(x[i], y[i]);
1211 //}
1212
1213 // fill dx and dy into histogram
1214 for (int i=0; i<mag.GetSize(); i++)
1215 hist.Fill(x[i], y[i]);
1216
1217 // Remove all bins which have content of only a single event
1218 // including under- and overflow bins
1219 for (int i=0; i<hist.GetSize(); i++)
1220 if (hist.GetBinContent(i)<1.5)
1221 hist.SetBinContent(i, 0);
1222
1223 // Find the bin containing the maximum
1224 Int_t bx, by, bz;
1225 hist.GetMaximumBin(bx, by, bz);
1226
1227 // In the worst case the events are spread through the
1228 // bins which are the neighbors of the maximum
1229 // Normally neighbors which do not belong to the signal are empty!
1230 hist.GetXaxis()->SetRange(bx-1, bx+1); // 3x3bins ~8x8pix ~9x9sigma
1231 hist.GetYaxis()->SetRange(by-1, by+1); // 3x3bins ~8x8pix ~9x9sigma
1232
1233 // Check whether this region contains events
1234 num = TMath::Nint(hist.Integral());
1235 if (num<1)
1236 return ZdAz();
1237
1238 // Get Mean for the given axis range
1239 Double_t mx = hist.GetMean(1);
1240 Double_t my = hist.GetMean(2);
1241
1242 const Int_t max1 = TMath::Nint(hist.GetBinContent(bx, by));
1243
1244 // Check if the maximum is unique
1245 Int_t bx2, by2, bz2;
1246 hist.GetXaxis()->SetRange(-1, 9999);
1247 hist.GetYaxis()->SetRange(-1, 9999);
1248 hist.SetBinContent(bx, by, 0);
1249 hist.GetMaximumBin(bx2, by2, bz2);
1250
1251 const Int_t max2 = TMath::Nint(hist.GetBinContent(bx2, by2));
1252 if (max1==max2 && TMath::Abs(bx2-bx)+TMath::Abs(by2-by)>1)
1253 return ZdAz();
1254
1255 // loop again over the file and fill the histogram again.
1256 // to get closer to the correct value
1257 Double_t sumx = 0;
1258 Double_t sumy = 0;
1259 num = 0;
1260 for (int i=0; i<mag.GetSize(); i++)
1261 {
1262 // only fill values into the histogram which
1263 // are inside 2*1.7=3.4 sigma (99.7%)
1264 if (TMath::Hypot(x[i]-mx, y[i]-my)>width*3.4)
1265 continue;
1266
1267 sumx += x[i];
1268 sumy += y[i];
1269 num++;
1270 }
1271
1272 if (num<1)
1273 return ZdAz();
1274
1275 // calc MeanX and MeanY
1276 mx = sumx/num;
1277 my = sumy/num;
1278
1279 // loop again to fill the mispointing corrected histograms
1280 // and fill another histogram to check the quality of the calculated
1281 // mispointing.
1282 sumx = 0;
1283 sumy = 0;
1284 num = 0;
1285 for (int i=0; i<mag.GetSize(); i++)
1286 {
1287 // only fill values into the histogram which
1288 // are inside 1.7=3.4 sigma (92%)
1289 // Cut at 1.7 sigma which gives the best background supression
1290 if (TMath::Hypot(x[i]-mx, y[i]-my)>width*1.7)
1291 continue;
1292
1293 sumx += x[i];
1294 sumy += y[i];
1295 num++;
1296 }
1297
1298 if (num<1)
1299 return ZdAz(); // FIXME!!!!!!!!!!!!
1300
1301 // Mispointing
1302 mx = sumx/num;
1303 my = sumy/num;
1304
1305#ifdef EXPERT
1306 cout << "Offset-XY: " << mx << " +- " << my << endl;
1307#endif
1308
1309 return fSao->CalcDeltaZdAzFromPix(mx, my)*TMath::RadToDeg();
1310/*
1311 AltAz pos0 = fSao->CalcAltAzFromPix(768/2, 576/2)*kRad2Deg;
1312 AltAz pos1 = fSao->CalcAltAzFromPix(768/2+mx, 576/2+my)*kRad2Deg;
1313
1314 pos1 -= pos0;
1315
1316 ZdAz res2 = ZdAz(-pos1.Alt(), pos1.Az());
1317
1318 return res1;
1319*/
1320}
1321
1322Int_t MStarguider::CalcTrackingError(const Leds &leds, MStarList &stars,
1323 ZdAz &d, const MTime &t, const double &bright,
1324 Int_t &num, const Ring &center, Int_t numleds, Int_t numrings)
1325{
1326 // Get tracking coordinates
1327 const XY xy = fCRaDec->GetCoordinates();
1328
1329 num = leds.GetEntries();
1330 if (num < 3) //was 1
1331 {
1332 gLog << warn << "Sorry, less than 3 detected spot in FOV!" << endl;
1333 if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
1334 fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num); // Report
1335 fTPoint->SetDown(kFALSE);
1336 return 0;
1337 }
1338
1339 if (stars.GetRealEntries() < 3)
1340 {
1341 gLog << warn << "Sorry, less than 3 stars in FOV!" << endl;
1342 if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
1343 fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num); // Report
1344 fTPoint->SetDown(kFALSE);
1345 return 0;
1346 }
1347
1348 stars.Sort(); // Sort by magnitude
1349
1350#ifdef EXPERT
1351 TString str = "data/tracking_";
1352 str += fSao->GetMjd();
1353 str += ".txt";
1354
1355 ofstream fout(str);
1356#endif
1357
1358 TArrayF x, y, mag;
1359
1360 // FIXME: Is predefined value 3 a good idea?
1361 Int_t cnt=0;
1362
1363 MStar *star=0;
1364 MStarListIter NextStar(&stars);
1365 while ((star=NextStar()) && cnt<num+5)
1366 {
1367 TIter NextSp(&leds);
1368 Led *spot=NULL;
1369 while ((spot=(Led*)NextSp()))
1370 {
1371 const XY dpos(spot->GetX()-(768-star->GetX()),
1372 spot->GetY()-star->GetY());
1373
1374 const Int_t idx = x.GetSize();
1375
1376 x.Set(idx+1);
1377 y.Set(idx+1);
1378 mag.Set(idx+1);
1379
1380 x.AddAt(dpos.X(), idx);
1381 y.AddAt(dpos.Y(), idx);
1382 mag.AddAt(1, idx);
1383// mag.AddAt(log(spot->GetMag())/star->GetMag(), idx);
1384/*
1385#ifdef EXPERT
1386 if (fout)
1387 {
1388 fout << spot->GetX() << " "
1389 << spot->GetY() << " "
1390 << -2.5*log(spot->GetMag()) << " "
1391 << star->GetX() << " "
1392 << star->GetY() << " "
1393 << star->GetMag() << " ";
1394 fout << x[idx] << " " << y[idx] << " " << mag[idx] << endl;
1395 }
1396#endif
1397*/
1398 }
1399 cnt++;
1400 }
1401
1402 Int_t numcor = 0;
1403 d = TrackingError(x, y, mag, numcor);
1404 if (numcor<1)
1405 {
1406 fTPoint->SetDown(kFALSE);
1407 if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
1408 fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num, numcor); // Report
1409 return 0;
1410 }
1411
1412 fDZdAz->SetCoordinates(d);
1413
1414 //
1415 // Current Pointing position
1416 //
1417 ZdAz cpos = fSao->GetZdAz()-d;
1418 fPZdAz->SetCoordinates(cpos);
1419
1420 // Check StargTPoint data set request
1421 if (!fMode->IsEntryChecked(IDM_kStarguiderMode) || !fTPoint->IsDown())
1422 return numcor;
1423 fTPoint->SetDown(kFALSE);
1424
1425 // If no file open: open new file
1426 if (!fOutStargTp)
1427 {
1428 // open tpoint file
1429 const TString name = MCosy::GetFileName("tpoint", "starg", "txt");
1430 gLog << all << "TPoint-Starg File ********* " << name << " ********** " << endl;
1431
1432 fOutStargTp = new ofstream(name);
1433 *fOutStargTp << "Magic Model STARGTPOINT data file" << endl;
1434 *fOutStargTp << ": ALTAZ" << endl;
1435 *fOutStargTp << "49 48 0 ";
1436 *fOutStargTp << t << endl;
1437 }
1438
1439 const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
1440
1441 // From the star position in the camera we calculate the Alt/Az
1442 // position we are currently tracking (real pointing position)
1443 fSao->SetMjd(t.GetMjd());
1444 AltAz za0 = fSao->CalcAltAz(rd)*kRad2Deg;
1445
1446 //correction with offset from last tpoint measurement
1447 za0 -= fAltAzOffsetFromTp;
1448
1449 //if the difference between the tpoint and the starguider tpoint
1450 //is too big, the starguider tpoint is not stored
1451 if ((t.GetMjd()-fTimeFromTp.GetMjd())>0.001) //1min20sec
1452 {
1453 cout << " time difference between tpoint and starguider-tpoint > 1m20s" << endl;
1454 cout << " => starguider tpoint hasn't been stored. " << endl;
1455 cout << " Please repeat whole procedure. " << endl;
1456 return numcor;
1457 }
1458
1459
1460 // Write real pointing position
1461 *fOutStargTp << setprecision(7) << za0.Az() << " " << za0.Alt() << " ";
1462
1463 // Write system pointing position
1464 *fOutStargTp << fmod(cpos.Az()+360, 360) << " " << 90-cpos.Zd();
1465
1466 *fOutStargTp << " " << xy.X() << " " << xy.Y();
1467 *fOutStargTp << " " << d.Zd() << " " << d.Az();
1468 *fOutStargTp << " " << setprecision(11) << t.GetMjd();
1469 *fOutStargTp << " " << setprecision(4) << center.GetMag();
1470 *fOutStargTp << " 0";// << star->GetMag();
1471 *fOutStargTp << " " << center.GetX() << " " << center.GetY();
1472 *fOutStargTp << " 0 0";
1473 *fOutStargTp << " " << numleds << " " << numrings;
1474 *fOutStargTp << " " << num << " " << numcor << " " << bright;
1475 *fOutStargTp << " " << fTPointStarMag << " " << fTPointStarName;
1476 *fOutStargTp << endl;
1477
1478 gLog << all << "Starguider TPoint successfully taken." << endl;
1479
1480 fTimeFromTp.Clear();
1481
1482 if (!fCosy)
1483 return numcor;
1484
1485 MDriveCom *com = fCosy->GetDriveCom();
1486 if (!com)
1487 return numcor;
1488
1489 com->SendTPoint(true, 'S', fTPointStarMag, fTPointStarName, za0, cpos, xy, d.Zd(), d.Az(), t, center, Led(), numleds, numrings, num, numcor); // Report
1490
1491 return numcor;
1492}
1493
1494void MStarguider::FindStar(const FilterLed &f, const FilterLed &f2, const Ring &center, const MTime &t, Int_t numleds, Int_t numrings)
1495{
1496 // Get tracking coordinates
1497 const XY xy = fCRaDec->GetCoordinates(); // [h, deg]
1498
1499 if (center.GetX()<=0 && center.GetY()<=0)
1500 {
1501 gLog << warn << "Couldn't determine center of the camera." << endl;
1502 if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
1503 fCosy->GetDriveCom()->SendTPoint(false, 'T', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings); // Report
1504 return;
1505 }
1506
1507 // Try to find the star
1508 Leds leds;
1509 f.FindStar(leds, (Int_t)center.GetX(), (Int_t)center.GetY(), true);
1510
1511 // Check whether star found
1512 Led *star = (Led*)leds.At(0);
1513 if (!star)
1514 {
1515 gLog << warn << "No star found." << endl;
1516 if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
1517 fCosy->GetDriveCom()->SendTPoint(false, 'T', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings); // Report
1518 return;
1519 }
1520
1521 cout << "Found star @ " << flush;
1522 star->Print();
1523 f2.MarkPoint(star->GetX(), star->GetY(), 2<<2);
1524
1525 const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
1526
1527 // Initialize Star Catalog on the camera plane
1528 MGeomCamMagic geom;
1529 MAstroCamera ac;
1530 ac.SetGeom(geom);
1531 ac.SetRadiusFOV(3);
1532 ac.SetObservatory(*fSao);
1533 ac.SetTime(t);
1534 ac.SetRaDec(rd.Ra(), rd.Dec());
1535
1536 // Convert from Pixel to millimeter (1pix=2.6mm) [deg/pix / deg/mm = mm/pix]
1537 // Correct for abberation.
1538 const Double_t conv = fCaos->GetArcsecPerPixel()/3600/geom.GetConvMm2Deg()/ 1.0713;
1539
1540 // Adapt coordinate system (GUIs and humans are counting Y in different directions)
1541 const Double_t dx = (star->GetX()-center.GetX())*conv;
1542 const Double_t dy = (center.GetY()-star->GetY())*conv;
1543
1544 // Convert offset from camera plane into local ccordinates
1545 Double_t dzd, daz;
1546 ac.GetDiffZdAz(dx, dy, dzd, daz);
1547
1548 ZdAz zdaz(dzd,daz);
1549
1550 // Check TPoint data set request
1551 if (!fMode->IsEntryChecked(IDM_kTpointMode) || !fTPoint->IsDown())
1552 return;
1553
1554 // If no file open: open new file
1555 if (!fOutTp)
1556 {
1557 //
1558 // open tpoint file
1559 //
1560 const TString name = MCosy::GetFileName("tpoint", "tpoint", "txt");
1561 cout << "TPoint File ********* " << name << " ********** " << endl;
1562
1563 fOutTp = new ofstream(name);
1564 *fOutTp << "Magic Model TPOINT data file" << endl;
1565 *fOutTp << ": ALTAZ" << endl;
1566 *fOutTp << "49 48 0 ";
1567 *fOutTp << t << endl;
1568 // temp(°C) pressure(mB) height(m) humidity(1) wavelength(microm) troplapserate(K/m)
1569 }
1570
1571 // Output Ra/Dec the drive system thinks that it is currently tracking
1572 //cout << "TPoint Star: " << xy.X() << "h " << xy.Y() << "°" << endl;
1573
1574 // From the star position in the camera we calculate the Alt/Az
1575 // position we are currently tracking (real pointing position)
1576 fSao->SetMjd(t.GetMjd());
1577 AltAz za0 = fSao->CalcAltAz(rd)*kRad2Deg;
1578
1579 //ZdAz za0 = fSao->GetZdAz();
1580 za0 -= AltAz(-dzd, daz);
1581 fAltAzOffsetFromTp = AltAz(-dzd, daz);
1582 fTimeFromTp=t;
1583
1584 // From the Shaftencoders we get the current 'pointing' position
1585 // as it is seen by the drive system (system pointing position)
1586 const ZdAz za1 = fCosy->GetSePos()*360; // [deg]
1587
1588
1589 // Write real pointing position
1590 //cout << " Alt/Az: " << za0.Alt() << "° " << za0.Az() << "°" << endl;
1591 *fOutTp << setprecision(7) << za0.Az() << " " << za0.Alt() << " ";
1592
1593 // Write system pointing position
1594 //cout << " SE-Pos: " << 90-za1.Zd() << "° " << za1.Az() << "°" << endl;
1595 *fOutTp << fmod(za1.Az()+360, 360) << " " << 90-za1.Zd();
1596
1597 *fOutTp << " " << xy.X() << " " << xy.Y();
1598 *fOutTp << " " << -dzd << " " << -daz;
1599 *fOutTp << " " << setprecision(11) << t.GetMjd();
1600 *fOutTp << " " << setprecision(4) << center.GetMag();
1601 *fOutTp << " " << star->GetMag();
1602 *fOutTp << " " << center.GetX() << " " << center.GetY();
1603 *fOutTp << " " << star->GetX() << " " << star->GetY();
1604 *fOutTp << " " << numleds << " " << numrings;
1605 *fOutTp << " 0 0 0";
1606 *fOutTp << " " << fTPointStarMag << " " << fTPointStarName;
1607 *fOutTp << endl;
1608
1609 gLog << all << "TPoint successfully taken." << endl;
1610/*
1611 MLog &outrep = *fCosy->GetOutRep();
1612 if (outrep.Lock("MStarguider::FindStar"))
1613 {
1614 outrep << "FINDSTAR-REPORT 00 " << MTime(-1) << " " << setprecision(7);
1615 outrep << 90-za0.Alt() << " " << za0.Az() << " ";
1616 outrep << za1.Zd() << " " << za1.Az() << " ";
1617 outrep << xy.X() << " " << xy.Y() << " ";
1618 outrep << -dzd << " " << -daz << " ";
1619 outrep << star->GetX() << " " << star->GetY() << " ";
1620 outrep << center.GetX() << " " << center.GetY() << " ";
1621 outrep << dx/conv << " " << dy/conv << " " << star->GetMag();
1622 outrep << setprecision(11) << t.GetMjd() << endl;
1623 outrep.UnLock("MStarguider::FindStar");
1624 }*/
1625
1626// return zdaz;
1627
1628 if (!fCosy)
1629 return;
1630
1631 MDriveCom *com = fCosy->GetDriveCom();
1632 if (!com)
1633 return;
1634
1635 com->SendTPoint(true, 'T', fTPointStarMag, fTPointStarName, za0, za1, xy, -dzd, -daz, t, center, *star, numleds, numrings); // Report
1636}
1637
1638bool MStarguider::Interpolate(const unsigned long n, byte *img) const
1639{
1640 if (fIntRate<=1)
1641 return true;
1642
1643 static unsigned short myimg[768*576];
1644
1645 unsigned short *f = myimg;
1646
1647 const byte *end = img+768*576;
1648
1649 if (n%fIntRate)
1650 {
1651 while (img<end)
1652 *f++ += *img++;
1653 return false;
1654 }
1655 else
1656 {
1657 while (img<end)
1658 {
1659 *img = (*img + *f)/fIntRate;
1660 ++img;
1661 *f++ = 0;
1662 }
1663
1664 return true;
1665 }
1666}
1667
1668void MStarguider::DrawZoomImage(const byte *img)
1669{
1670 byte zimg[kZOOM*kZOOM];
1671 for (int y=0; y<kZOOM; y++)
1672 for (int x=0; x<kZOOM; x++)
1673 zimg[x+y*kZOOM] = img[(fDx+(x-kZOOM/2)/2)+(fDy+(y-kZOOM/2)/2)*768];
1674
1675 fZoomImage->DrawImg(zimg);
1676}
1677
1678void MStarguider::DrawCosyImage(const byte *img)
1679{
1680 if (!fCosy)
1681 return;
1682
1683 byte simg[(768/2-1)*(576/2-1)];
1684 for (int y=0; y<576/2-1; y++)
1685 for (int x=0; x<768/2-1; x++)
1686 simg[x+y*(768/2-1)] = ((unsigned int)img[2*x+2*y*768]+img[2*x+2*y*768+1]+img[2*x+2*(y+1)*768]+img[2*x+2*(y+1)*768+1])/4;
1687
1688 fCosy->GetWin()->GetImage()->DrawImg(simg);
1689}
1690
1691void MStarguider::StartTPoint(char *cmd)
1692{
1693 fTPointStarName = "Dummy";
1694 fTPointStarMag = 0;
1695
1696 TString str(cmd);
1697
1698 TObjArray *arr = str.Tokenize(' ');
1699
1700 if (arr->GetEntries()==2)
1701 {
1702 fTPointStarName = (*arr)[0]->GetName();
1703 fTPointStarMag = atof((*arr)[1]->GetName());
1704 }
1705
1706 delete arr;
1707
1708 if (fTPointFromCC<0)
1709 fTPointFromCC=0;
1710}
1711
1712void MStarguider::StartStarguider(bool on)
1713{
1714 // Switch to starguider mode
1715 if (on)
1716 {
1717 cout << " * Switching Starguider mode on" << endl;
1718 fMode->UnCheckEntry(IDM_kStarguiderMode);
1719 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
1720 }
1721 else
1722 {
1723 cout << " * Switching Starguider mode off" << endl;
1724 fMode->CheckEntry(IDM_kStarguiderMode);
1725 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
1726 }
1727}
1728
1729Int_t MStarguider::GetStarguiderMode() const
1730{
1731 return fMode->IsEntryChecked(IDM_kStarguiderMode) ? 1 : 2;
1732
1733}
1734
1735Bool_t MStarguider::DoTPoint()
1736{
1737 if (fTPoint->IsDown() && fTPointFromCC<0)
1738 {
1739 fTPointFromCC = 0;
1740 fTPoint->SetDown(kFALSE);
1741 }
1742
1743 if (fTPointFromCC<0)
1744 return kTRUE;
1745
1746 switch (++fTPointFromCC)
1747 {
1748 case 1:
1749 fTimeFromTp.Clear();
1750 fNumStarsDetected = 0;
1751
1752 if (!fMode->IsEntryChecked(IDM_kTpointMode))
1753 {
1754 cout << " * Switching to TPoint mode" << endl;
1755 // Switch to tpoint mode
1756 fMode->UnCheckEntry(IDM_kTpointMode);
1757 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kTpointMode, 0);
1758 return kFALSE;
1759 }
1760 fTPointFromCC++;
1761
1762 case 2:
1763 case 3:
1764 cout << " * Waiting one frame" << endl;
1765 // Wait one frame
1766 return kFALSE;
1767
1768 case 4:
1769 cout << " * Taking TPoint" << endl;
1770 fTPoint->SetDown(); // kTRUE
1771 return kTRUE;
1772
1773 case 5:
1774 if (!fTimeFromTp) // TPoint failed
1775 break;
1776#ifdef FACT
1777 fTPointFromCC = -1;
1778 return kFALSE;
1779#else
1780 // Switch to starguider mode
1781 cout << " * Switching to Starguider mode" << endl;
1782 fMode->UnCheckEntry(IDM_kStarguiderMode);
1783 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
1784 return kFALSE;
1785#endif
1786 case 6:
1787 case 7:
1788 cout << " * Waiting one frame" << endl;
1789 // Wait one frame
1790 return kFALSE;
1791
1792 case 8:
1793 cout << " * Taking Starguider TPoint" << endl;
1794 fTPoint->SetDown(); // kTRUE
1795 return kTRUE;
1796
1797// case 9:
1798// cout << " * Send Report" << endl;
1799// return kTRUE;
1800
1801 case 9:
1802 cout << " * Switching to TPoint mode" << endl;
1803 // Switch to tpoint mode
1804 fMode->UnCheckEntry(IDM_kTpointMode);
1805 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kTpointMode, 0);
1806 fTPointFromCC = -1;
1807 return kFALSE;
1808 }
1809
1810 // Send report
1811/*
1812 if (!fCosy)
1813 return kTRUE;
1814
1815 MDriveCom *com = fCosy->GetDriveCom();
1816 if (!com)
1817 return kTRUE;
1818
1819 // nominalaz, nominalel, realaz, realel, nomra, nomdec,
1820 // diffaz, diffel, mjd, numleds, artmag
1821
1822 //fTimeFromTp.Clear();
1823 //fStatus==MDriveCom::kMonitoring
1824 //fNumStarsDetected = numstars;
1825 //fNumStarsCorrelated = rc;
1826 com->SendTPoint(fNumStarsCorrelated>0);
1827*/
1828 return kTRUE;
1829}
1830
1831void MStarguider::ProcessFrame(const unsigned long n, byte *img,
1832 struct timeval *tm)
1833{
1834 static unsigned long n0 = n;
1835
1836 MTime t(*tm);
1837
1838 const Double_t d = t-fTime;
1839 if (d>1)
1840 {
1841 const TString txt = MString::Format("%dfps", (int)((n-n0)/d+.5));
1842 fFps->SetText(txt);
1843 fTime = t;
1844 n0 = n;
1845 }
1846
1847 if (!Interpolate(n, img))
1848 return;
1849
1850 byte cimg[768*576];
1851 memset(cimg, 0, 768*576);
1852
1853 FilterLed f(img, 768, 576, 2.5); // 2.5
1854 FilterLed f2(cimg, 768, 576); // former color 0xb0
1855
1856 if (fDisplay->IsEntryChecked(IDM_kStretch))
1857 f.Stretch();
1858
1859 if (!fWritePictures->IsEntryEnabled(IDM_kStart) &&
1860 (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
1861 {
1862
1863 if (fFileType->IsEntryChecked(IDM_kPNG))
1864 Writer::Png("pix/file", img, tm, fCRaDec->GetCoordinates());
1865
1866 if (fFileType->IsEntryChecked(IDM_kPPM))
1867 Writer::Ppm("pix/file", img, tm, fCRaDec->GetCoordinates());
1868
1869 if (fWriteType->IsEntryChecked(IDM_kOnce))
1870 ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
1871 }
1872
1873 // Visual Filter, whole FOV
1874 if (fDisplay->IsEntryChecked(IDM_kFilter))
1875 f.Execute();
1876
1877 if (!DoTPoint())
1878 return;
1879
1880 Int_t numleds = 0;
1881 Int_t numrings = 0;
1882
1883 // Find Center of Camera for Caos and Tpoints
1884// Ring center;//(-1, -1);
1885 Ring center(5, 5);//(-1, -1);
1886 if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
1887 {
1888 const bool printl = fCaosPrint->IsEntryChecked(IDM_kCaosPrintLeds);
1889 const bool printr = fCaosPrint->IsEntryChecked(IDM_kCaosPrintRings);
1890 ZdAz pos;
1891 if (fCosy)
1892 pos = fCosy->GetPointingPos();
1893
1894 center = fCaos->Run(img, printl, printr, pos, t);
1895
1896 numleds = fCaos->GetNumDetectedLEDs();
1897 numrings = fCaos->GetNumDetectedRings();
1898 }
1899
1900 // Find Star at Center---for Tpoint Procedure
1901 if (fDisplay->IsEntryChecked(IDM_kFindStar))
1902 {
1903 // Set search Paremeters (FIXME: Get them from user input!)
1904 f.SetCut(fFindStarCut);
1905 f.SetBox(fFindStarBox);
1906
1907 FindStar(f, f2, center, t, numleds, numrings);
1908 }
1909
1910 DrawZoomImage(img);
1911 DrawCosyImage(img);
1912
1913 // Position corresponding to the camera center (53.2, 293.6)
1914 Ring sgcenter;//(-1, -1); // Center of camera in SG picture [px]
1915
1916 // Find Center of Camera in Starfield Camera picture
1917 if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter))
1918 {
1919 ZdAz pos;
1920 if (fCosy)
1921 pos = fCosy->GetPointingPos();
1922
1923 sgcenter = fStargCaos->Run(img, kFALSE, kFALSE, pos, t); // [px]
1924
1925 numleds = fStargCaos->GetNumDetectedLEDs();
1926 numrings = fStargCaos->GetNumDetectedRings();
1927 }
1928
1929 // we obtain a list of stars in the FOV from the SAO catalog
1930 if (fDisplay->IsEntryChecked(IDM_kCatalog))
1931 {
1932 MTime time(*tm);
1933
1934 XY xy = fCRaDec->GetCoordinates(); //[h, deg]
1935 fRaDec->Set(xy.X()*15, xy.Y()); //[deg,deg]
1936
1937 UpdatePosZoom();
1938
1939 // Always call SetMjd first!
1940 fSao->SetPointing(time.GetMjd(), *fRaDec);
1941 fCZdAz->SetCoordinates(fSao->GetZdAz());
1942
1943 fSao->SetBox(fStarguiderW, fStarguiderH); // 280 Region of interest around center
1944
1945 // If center of camera cannot be determined sgcenter is (0,0)
1946 const Bool_t centerisvalid = sgcenter.GetX()>0 && sgcenter.GetY()>0;
1947
1948 // We determine the ideal starfield using camera sagging info
1949 // from the LEDs
1950 //Please never change this offsets!!!
1951 // 53.2 and 293.6 are the "preliminary" camera center
1952 // -9 and 28.5 are the offsets of the pointing position in the sky
1953 //const XY off(sgcenter.GetX()- 53.2-9,
1954 // sgcenter.GetY()-293.6+28.5);
1955
1956 // Skycenter
1957 const XY off(sgcenter.GetX() + fSkyOffsetX,
1958 sgcenter.GetY() + fSkyOffsetY);
1959
1960 MStarList stars;
1961 if (centerisvalid)
1962 {
1963 // we obtain stars in the effective star FOV and draw them.
1964 // coordinates are video frame coords.
1965 fSao->CalcStars(stars, fStarguiderX, fStarguiderY, off.X(), off.Y());
1966 fSao->DrawStars(stars, cimg);
1967 }
1968
1969 // There are two corrections to the mispointing
1970 // - Sagging of the camera as measured with the LEDs
1971 // - Star not ideally centered on MAGIC Camera
1972
1973 // Next we evaluate the offset given by the LEDs. This we obtain
1974 // in Zd/Az and add it to the tracking error.
1975 if (fDisplay->IsEntryChecked(IDM_kStarguider))
1976 {
1977 const Float_t cut = atof(fCut->GetText());
1978
1979 Leds spots;
1980 f.SetBox(fStarguiderW, fStarguiderH); // 280
1981 f.SetCut(cut);
1982
1983 double bright;
1984 f.ExecuteAndMark(spots, fStarguiderX, fStarguiderY, bright);
1985
1986 ULong_t color;
1987 gClient->GetColorByName("Green", color);
1988 if (bright> 60)
1989 gClient->GetColorByName("Yellow", color);
1990 if (bright> 85)
1991 gClient->GetColorByName("Orange", color);
1992 if (bright> 95)
1993 gClient->GetColorByName("Red", color);
1994 fSkyBright->SetBackgroundColor(color);
1995
1996 const MString txt = MString::Format("Sky Brightness: %.1f", bright);
1997 fSkyBright->SetText(txt);
1998
1999 const Bool_t brightnessisvalid = bright<1.75*fLastBright &&
2000 bright>30 && bright<110;
2001
2002 fLastBright = bright;
2003
2004 Int_t numstars = 0;
2005 const Int_t rc = CalcTrackingError(spots, stars, fD, t, bright, numstars, sgcenter, numleds, numrings);
2006
2007 const Bool_t monitoring = brightnessisvalid && centerisvalid && fNumStarsCorrelated>2;
2008
2009 fStatus = monitoring ? MDriveCom::kMonitoring : MDriveCom::kError;
2010
2011 if (fCosy)
2012 fPos = fCosy->GetPointingPos();
2013
2014 if (fOperations->IsEntryChecked(IDM_kStargAnalysis))
2015 fStargHistograms->Fill(spots, stars, fD, fSao->GetZdAz(),
2016 sgcenter, bright, fPos, t);
2017
2018 fNumStarsDetected = numstars;
2019 fNumStarsCorrelated = rc;
2020
2021 if (fCosy)
2022 {
2023 MDriveCom *com = fCosy->GetDriveCom();
2024 if (com)
2025 com->SendStargReport(fStatus, fD, fSao->GetZdAz(),
2026 sgcenter, numstars, rc, bright,
2027 time.GetMjd(), numleds, numrings); // Report
2028 }
2029
2030 } //kStarguider
2031
2032 if (centerisvalid && fNumStarsCorrelated>2)
2033 {
2034 // Position around which the circles are drawn.
2035 const Ring skycenter(off.X(), off.Y());
2036
2037 f2.DrawCircle(skycenter, 2.0, 0x0a);
2038
2039 const Double_t ap = fSao->GetPixSize()/3600; //[deg/pix]
2040
2041 f2.DrawCircle(skycenter, 0.10/ap, 0x0a); //0.1deg
2042 f2.DrawHexagon(skycenter, 2.06/ap, 0x0a);
2043 f2.DrawHexagon(skycenter, 3.50/ap, 0x0a);
2044 }
2045
2046 } //CalcStars
2047
2048 if (fTPoint->IsDown())
2049 fTPoint->SetDown(kFALSE);
2050
2051 // Draw Circles around center of Camera
2052 if (fDisplay->IsEntryChecked(IDM_kCaosFilter) &&
2053 center.GetX()>0 && center.GetY()>0)
2054 {
2055 f2.DrawCircle(center, 0x0a);
2056 f2.DrawCircle(center, 7.0,
2057 fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
2058 f2.DrawCircle(center, 115.0, 0x0a);
2059 f2.DrawCircle(center, 230.0, 0x0a);
2060 f2.DrawCircle(center, 245.0, 0x0a);
2061 }
2062
2063 if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter) &&
2064 sgcenter.GetX()>0 && sgcenter.GetY()>0)
2065 {
2066 f2.DrawCircle(sgcenter, 0x0a);
2067 f2.DrawCircle(sgcenter, 5.0,
2068 fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
2069 }
2070
2071 if (fDisplay->IsEntryChecked(IDM_kCaosFilter) ||
2072 fDisplay->IsEntryChecked(IDM_kCatalog) ||
2073 fDisplay->IsEntryChecked(IDM_kFindStar))
2074 fImage->DrawColImg(img, cimg);
2075 else
2076 fImage->DrawImg(img);
2077}
2078
2079void MStarguider::UpdatePosZoom()
2080{
2081 MString txt;/*
2082 if (fDisplay->IsEntryChecked(IDM_kCatalog))
2083 {
2084 // FIXME: Necessary?
2085 fSao->Now();
2086 AltAz aa = fSao->CalcAltAzFromPix(fDx, fDy)*kRad2Deg;
2087 if (aa.Az()<0)
2088 aa.Az(aa.Az()+360);
2089 txt.Form("(%d, %d) %.1fd/%.1fd", fDx, fDy, -aa.Alt(), aa.Az()-180);
2090 }
2091 else*/
2092 txt.Form("(%d, %d)", fDx, fDy);
2093 fPosZoom->SetText(txt);
2094}
2095
2096Bool_t MStarguider::HandleDoubleClick(Event_t *event)
2097{
2098 const Int_t w = fImage->GetWidth();
2099 const Int_t h = fImage->GetHeight();
2100 const Int_t x = fImage->GetX();
2101 const Int_t y = fImage->GetY();
2102
2103 if (!(event->fX>x && event->fX<x+w && event->fY>y && event->fY<y+h))
2104 return kTRUE;
2105
2106 Int_t dx = event->fX-x;
2107 Int_t dy = event->fY-y;
2108
2109 if (dx<kZOOM/4) dx=kZOOM/4;
2110 if (dy<kZOOM/4) dy=kZOOM/4;
2111 if (dx>766-kZOOM/4) dx=766-kZOOM/4;
2112 if (dy>574-kZOOM/4) dy=574-kZOOM/4;
2113
2114 fDx = dx;
2115 fDy = dy;
2116
2117 UpdatePosZoom();
2118 return kTRUE;
2119}
2120
2121void MStarguider::Print(TString &str, Double_t deg) const
2122{
2123 Char_t sgn;
2124 UShort_t d, m, s;
2125
2126 MAstro::Deg2Dms(deg, sgn, d, m, s);
2127
2128 str += MString::Format("%c %03d %02d %03d ", sgn, d, m, s);
2129}
Note: See TracBrowser for help on using the repository browser.