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

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