-
Notifications
You must be signed in to change notification settings - Fork 3
/
LogViewFrame.cpp
2274 lines (1994 loc) · 68 KB
/
LogViewFrame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of phdlogview
*
* Copyright (C) 2016-2020 Andy Galasso
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, visit the http://fsf.org website.
*/
#include "LogViewFrame.h"
#include "LogViewApp.h"
#include "AnalysisWin.h"
#include "logparser.h"
#include <wx/aboutdlg.h>
#include <wx/busyinfo.h>
#include <wx/clipbrd.h>
#include <wx/colordlg.h>
#include <wx/dcbuffer.h>
#include <wx/dnd.h>
#include <wx/filedlg.h>
#include <wx/graphics.h>
#include <wx/grid.h>
#include <wx/log.h>
#include <wx/splitter.h>
#include <wx/stopwatch.h>
#include <wx/textentry.h>
#include <wx/valnum.h>
#include <wx/wfstream.h>
#include <wx/wupdlock.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <sstream>
#define MAX_HSCALE_GUIDE 100.0
#define MIN_HSCALE_GUIDE 0.1
#define MAX_HSCALE_CAL 400.0
#define MIN_HSCALE_CAL 5.0
#define DECEL 0.03
#define MIN_SHOW 25
#define APP_NAME "PHD2 Log Viewer"
#define APP_VERSION_STR "0.6.4"
PointArray s_tmp;
Settings s_settings;
static GuideLog s_log;
enum DragMode
{
DRAG_PAN,
DRAG_EXCLUDE,
DRAG_INCLUDE,
};
struct DragInfo
{
bool m_dragging;
DragMode m_dragMode;
// for DRAG_EXCLUDE / DRAG_INCLUDE
wxPoint m_anchorPoint;
wxPoint m_endPoint;
bool dragMoved;
DragDirection drag_direction;
wxPoint m_lastMousePos;
wxPoint m_mousePos[2];
wxLongLong_t m_mouseTime[2];
double decel;
wxRealPoint m_rate; // pixels per millisecond
};
static DragInfo s_drag;
static int s_analyze_pos;
struct ScatterPlot
{
wxBitmap *bitmap;
bool valid;
ScatterPlot() : bitmap(0), valid(false) { }
~ScatterPlot() { delete bitmap; }
void Invalidate() { valid = false; }
};
static ScatterPlot s_scatter;
struct FileDropTarget : public wxFileDropTarget
{
LogViewFrame *m_lvf;
FileDropTarget(LogViewFrame *lvf) : m_lvf(lvf) { }
bool OnDropFiles(wxCoord x, wxCoord y, const wxArrayString& filenames) {
if (filenames.size() == 1)
{
m_lvf->OpenLog(filenames[0]);
return true;
}
return false;
}
};
enum
{
ID_TIMER = 10001,
ID_INCLUDE_ALL,
ID_INCLUDE_NONE,
ID_EXCLUDE_SETTLE,
ID_ANALYZE_GA,
ID_ANALYZE_ALL,
ID_ANALYZE_ALL_NORA,
};
wxBEGIN_EVENT_TABLE(LogViewFrame, LogViewFrameBase)
EVT_MENU(wxID_OPEN, LogViewFrame::OnFileOpen)
EVT_MENU(wxID_SETTINGS, LogViewFrame::OnFileSettings)
EVT_MENU(wxID_EXIT, LogViewFrame::OnFileExit)
EVT_MENU(wxID_HELP, LogViewFrame::OnHelp)
EVT_MENU(wxID_ABOUT, LogViewFrame::OnHelpAbout)
EVT_MENU_RANGE(ID_INCLUDE_ALL, ID_EXCLUDE_SETTLE, LogViewFrame::OnMenuInclude)
EVT_MENU(ID_ANALYZE_GA, LogViewFrame::OnMenuAnalyzeGA)
EVT_MENU_RANGE(ID_ANALYZE_ALL, ID_ANALYZE_ALL_NORA, LogViewFrame::OnMenuAnalyzeAll)
EVT_MOUSEWHEEL(LogViewFrame::OnMouseWheel)
EVT_TIMER(ID_TIMER, LogViewFrame::OnTimer)
wxEND_EVENT_TABLE()
inline static bool vscale_locked()
{
return s_settings.vscale != 0.0;
}
static void _update_vscale_setting(double vscale)
{
s_settings.vscale = vscale;
Config->Write("/vscale", vscale);
}
inline static void update_vscale_setting(double vscale, double pixelScale)
{
_update_vscale_setting(vscale / pixelScale);
}
inline static double get_vscale_setting(double pixelScale)
{
return s_settings.vscale * pixelScale;
}
static void InitLegends(bool radec, wxTextCtrl *ra, wxTextCtrl *dec)
{
if (radec)
{
ra->ChangeValue(wxT("\u2015RA"));
dec->ChangeValue(wxT("\u2015Dec"));
}
else
{
ra->ChangeValue(wxT("\u2015dx"));
dec->ChangeValue(wxT("\u2015dy"));
}
}
void SaveGeometry(const wxFrame *win, const wxString& key)
{
Config->Write(key, wxString::Format("%d;%d;%d;%d;%d",
win->IsMaximized() ? 1 : 0,
win->GetSize().x, win->GetSize().y,
win->GetPosition().x, win->GetPosition().y));
}
void LoadGeometry(wxFrame *win, const wxString& key)
{
wxString geometry = Config->Read(key, wxEmptyString);
if (!geometry.IsEmpty())
{
wxArrayString f = wxSplit(geometry, ';');
if (f[0] == "1")
win->Maximize();
long w, h, x, y;
f[1].ToLong(&w);
f[2].ToLong(&h);
f[3].ToLong(&x);
f[4].ToLong(&y);
win->SetSize(w, h);
win->SetPosition(wxPoint(x, y));
}
}
LogViewFrame::LogViewFrame()
:
LogViewFrameBase(0),
m_sessionIdx(-1),
m_session(nullptr),
m_calibration(nullptr),
m_timer(this, ID_TIMER),
m_analysisWin(nullptr)
{
SetTitle(APP_NAME);
m_graph->SetBackgroundStyle(wxBG_STYLE_PAINT);
m_graph->Connect(wxEVT_MOUSE_CAPTURE_LOST, wxMouseCaptureLostEventHandler(LogViewFrame::OnCaptureLost), NULL, this);
Bind(wxEVT_CHAR_HOOK, &LogViewFrame::OnKeyDown, this);
SetDropTarget(new FileDropTarget(this));
LoadGeometry(this, "/geometry");
// load the two splitter positions
{
long val;
wxString s;
s = Config->Read("/geometry.splitter1", wxEmptyString);
if (!s.ToLong(&val))
val = 215;
m_splitter1->SetSashPosition(val);
s = Config->Read("/geometry.splitter2", wxEmptyString);
if (!s.ToLong(&val))
val = 330;
m_splitter2->SetSashPosition(val);
}
s_settings.excludeByServer = Config->ReadBool("/settle/excludeByServer", true);
s_settings.excludeParametric = Config->ReadBool("/settle/excludeParametric", false);
s_settings.settle.pixels = Config->ReadDouble("/settle/pixels", 1.0);
s_settings.settle.seconds = Config->ReadDouble("/settle/seconds", 10.0);
s_settings.raColor = wxColor(Config->Read("/color/ra", wxColor(100, 100, 255).GetAsString(wxC2S_HTML_SYNTAX)));
s_settings.decColor = wxColor(Config->Read("/color/dec", wxRED->GetAsString(wxC2S_HTML_SYNTAX)));
s_settings.vscale = Config->ReadDouble("/vscale", 0.0);
m_raLegend->SetForegroundColour(s_settings.raColor);
m_decLegend->SetForegroundColour(s_settings.decColor);
InitLegends(true, m_raLegend, m_decLegend);
m_vlock->SetValue(vscale_locked());
}
LogViewFrame::~LogViewFrame()
{
if (m_analysisWin)
m_analysisWin->Destroy();
}
static wxString durStr(double dur)
{
wxString s;
double hrs = floor(dur / 3600.0);
if (hrs > 0.0)
{
s += wxString::Format("%.fh", hrs);
dur -= hrs * 3600.0;
}
double mins = floor(dur / 60.0);
if (mins > 0.0)
{
s += wxString::Format("%.fm", mins);
dur -= mins * 60.0;
}
s += wxString::Format("%.fs", dur);
return s;
}
inline static void IncludeRange(GuideSession::EntryVec& entries, bool include, unsigned int i = 0, unsigned int i1 = (unsigned int)-1)
{
for (auto it = entries.begin() + i; i < i1 && it != entries.end(); ++it, ++i)
it->included = include;
}
inline static void IncludeAll(GuideSession::EntryVec& entries)
{
IncludeRange(entries, true);
}
inline static void IncludeNone(GuideSession::EntryVec& entries)
{
IncludeRange(entries, false);
}
static void ExcludeSettlingByAPI(GuideSession *session)
{
bool settling = false;
int start_idx = 0;
auto& infos = session->infos;
auto& entries = session->entries;
for (auto it = infos.begin(); it != infos.end(); ++it)
{
if (settling)
{
if (it->info.find("Settling complete") != wxString::npos || it->info.find("Settling fail") != wxString::npos)
{
settling = false;
IncludeRange(entries, false, start_idx, it->idx);
}
}
else
{
if (it->info.find("Settling start") != wxString::npos)
{
settling = true;
start_idx = it->idx;
}
}
}
if (settling)
IncludeRange(entries, false, start_idx);
}
static void ExcludeSettlingByDistance(GuideSession *session, const SettleParams& params)
{
auto& infos = session->infos;
auto& entries = session->entries;
double lim2 = params.pixels * params.pixels;
for (auto it = infos.begin(); it != infos.end(); ++it)
{
if (it->info.find("DITHER") != wxString::npos)
{
if (it->idx >= (int)entries.size())
break;
int start_idx = it->idx;
auto eit = entries.begin() + it->idx;
double start_time = eit->dt;
bool close = false;
bool settled = false;
int end_idx = start_idx;
for (; eit != entries.end(); ++eit, ++end_idx)
{
double dx = eit->dx;
double dy = eit->dy;
double d2 = dx * dx + dy * dy;
double t = eit->dt;
if (d2 < lim2)
{
if (!close)
{
close = true;
start_time = t;
}
else
{
double elapsed = t - start_time;
if (elapsed > params.seconds)
{
settled = true;
break;
}
}
}
else
{
close = false;
}
}
if (settled)
IncludeRange(entries, false, start_idx, end_idx);
}
}
}
static void ExcludeSettling(GuideSession *session)
{
if (s_settings.excludeByServer)
ExcludeSettlingByAPI(session);
if (s_settings.excludeParametric)
ExcludeSettlingByDistance(session, s_settings.settle);
}
void LogViewFrame::OpenLog(const wxString& filename)
{
m_filename.clear();
std::ifstream ifs(filename.fn_str());
if (!ifs.good())
{
wxLogError("Cannot open file '%s'.", filename);
return;
}
m_sessions->Hide();
m_filename = filename;
wxFileName fn(filename);
SetTitle(wxString::Format(APP_NAME " - %s", fn.GetFullName()));
// must do this before calling parser since parse will Yield and call OnPaint
m_sessionIdx = -1;
m_session = 0;
m_sessionInfo->Clear();
m_stats->ClearGrid();
m_stats2->SetPage(wxEmptyString);
m_sessions->BeginBatch();
if (m_sessions->GetNumberRows() > 8)
m_sessions->DeleteRows(8, m_sessions->GetNumberRows() - 8);
m_sessions->ClearGrid();
m_sessions->EndBatch();
m_rowInfo->Clear();
wxGetApp().Yield();
{
wxWindowDisabler disableAll;
wxBusyInfo wait("Please wait, working...");
LogParser().Parse(ifs, s_log);
}
if (!s_log.phd_version.empty())
SetTitle(wxString::Format(APP_NAME " - %s - PHD2 %s", fn.GetFullName(), s_log.phd_version.c_str()));
// load the grid
m_sessions->BeginBatch();
int row = 0;
for (auto it = s_log.sections.begin(); it != s_log.sections.end(); ++it, ++row)
{
if (row >= m_sessions->GetNumberRows())
m_sessions->AppendRows(1, false);
m_sessions->SetCellValue(row, 0, wxString::Format("%d", row + 1));
LogSection *s;
if (it->type == CALIBRATION_SECTION)
{
s = &s_log.calibrations[it->idx];
m_sessions->SetCellValue(row, 2, "Calibration");
m_sessions->SetCellValue(row, 3, wxEmptyString);
}
else
{
s = &s_log.sessions[it->idx];
GuideSession *session = static_cast<GuideSession*>(s);
IncludeAll(session->entries);
ExcludeSettling(session);
session->CalcStats();
m_sessions->SetCellValue(row, 2, "Guiding");
m_sessions->SetCellValue(row, 3, durStr(session->duration));
}
m_sessions->SetCellValue(row, 1, s->date);
}
m_sessions->GoToCell(0, 0);
m_sessions->AutoSize();
m_sessions->EndBatch();
if (s_log.sections.empty())
{
m_sessionInfo->SetValue("(empty log file)");
}
else
{
// FIXME
// Hack to cause graph scrollbars be displayed when grid's
// rows do not fit in the panel
{
int x = m_splitter1->GetSashPosition();
m_splitter1->SetSashPosition(x + 1);
m_splitter1->SetSashPosition(x);
} // end hack
m_sessions->Show();
}
s_scatter.Invalidate();
m_graph->Refresh();
}
void LogViewFrame::OnFileExit(wxCommandEvent& event)
{
Close();
}
void LogViewFrame::OnFileOpen(wxCommandEvent& event)
{
wxFileDialog openFileDialog(this, _("Open PHD2 Guide Log"),
Config->Read("/FileOpenDir", wxEmptyString),
wxEmptyString,
"PHD2 Guide Logs (*PHD2_GuideLog*.txt)|*PHD2_GuideLog*.txt|"
"Text files (*.txt)|*.txt", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
return;
// save the location
Config->Write("/FileOpenDir", wxFileName(openFileDialog.GetPath()).GetPath());
OpenLog(openFileDialog.GetPath());
}
static void OnColor(wxWindow *parent, wxButton *btn, wxColor *color)
{
wxColourDialog dlg(parent);
dlg.GetColourData().SetColour(*color);
if (dlg.ShowModal() == wxID_OK)
{
*color = dlg.GetColourData().GetColour();
btn->SetForegroundColour(*color);
}
}
void SettingsDialog::OnRAColor(wxCommandEvent& event)
{
OnColor(this, m_raColorBtn, &m_raColor);
}
void SettingsDialog::OnDecColor(wxCommandEvent& event)
{
OnColor(this, m_decColorBtn, &m_decColor);
}
void LogViewFrame::OnFileSettings(wxCommandEvent& event)
{
SettingsDialog dlg(this);
dlg.m_excludeApi->SetValue(s_settings.excludeByServer);
dlg.m_excludeByParam->SetValue(s_settings.excludeParametric);
double pixels = s_settings.settle.pixels;
dlg.m_settlePixels->SetValidator(wxFloatingPointValidator<double>(2, &pixels, 0));
double seconds = s_settings.settle.seconds;
dlg.m_settleSeconds->SetValidator(wxFloatingPointValidator<double>(1, &seconds, 0));
dlg.m_raColor = s_settings.raColor;
dlg.m_decColor = s_settings.decColor;
dlg.m_raColorBtn->SetForegroundColour(dlg.m_raColor);
dlg.m_decColorBtn->SetForegroundColour(dlg.m_decColor);
if (dlg.ShowModal() == wxID_CANCEL)
return;
s_settings.excludeByServer = dlg.m_excludeApi->GetValue();
s_settings.excludeParametric = dlg.m_excludeByParam->GetValue();
s_settings.settle.pixels = pixels;
s_settings.settle.seconds = seconds;
s_settings.raColor = dlg.m_raColor;
s_settings.decColor = dlg.m_decColor;
// in case color changed
m_graph->Refresh();
m_raLegend->SetForegroundColour(s_settings.raColor);
m_decLegend->SetForegroundColour(s_settings.decColor);
Config->Write("/settle/excludeByServer", s_settings.excludeByServer);
Config->Write("/settle/excludeParametric", s_settings.excludeParametric);
Config->Write("/settle/pixels", s_settings.settle.pixels);
Config->Write("/settle/seconds", s_settings.settle.seconds);
Config->Write("/color/ra", s_settings.raColor.GetAsString(wxC2S_HTML_SYNTAX));
Config->Write("/color/dec", s_settings.decColor.GetAsString(wxC2S_HTML_SYNTAX));
}
inline static int IdxFromScreen(const GraphInfo& ginfo, wxCoord x)
{
return (int)(ginfo.i0 + 0.5 + x / ginfo.hscale);
}
void LogViewFrame::OnRightUp(wxMouseEvent& event)
{
if (!m_session)
{
event.Skip();
return;
}
wxMenu *menu = new wxMenu();
menu->Append(ID_INCLUDE_ALL, _("Include all frames"));
menu->Append(ID_INCLUDE_NONE, _("Exclude all frames"));
menu->Append(ID_EXCLUDE_SETTLE, _("Exclude frames settling"));
menu->AppendSeparator();
wxMenuItem *mi1 = menu->Append(ID_ANALYZE_ALL, _("Analyze selected frames"));
wxMenuItem *mi2 = menu->Append(ID_ANALYZE_ALL_NORA, _("Analyze selected, raw RA"));
if (!AnalysisWin::CanAnalyzeAll(*m_session))
{
mi1->Enable(false);
mi2->Enable(false);
}
wxMenuItem *mi = menu->Append(ID_ANALYZE_GA, _("Analyze unguided section"));
{
GraphInfo& ginfo = m_session->m_ginfo;
int i = IdxFromScreen(ginfo, event.GetPosition().x);
if (i >= 0 && AnalysisWin::CanAnalyzeGA(*m_session, i))
{
s_analyze_pos = i;
}
else
{
mi->Enable(false);
}
}
wxWindow *w = wxDynamicCast(event.GetEventObject(), wxWindow);
if (w)
PopupMenu(menu, ScreenToClient(w->ClientToScreen(event.GetPosition())));
delete menu;
}
std::string FormatNum(double n)
{
char buf[20];
#ifdef _MSC_VER
# define snprintf _snprintf
#endif
snprintf(buf, sizeof(buf), "%+.2f", n);
return buf;
}
static void InitStats(wxGrid *stats, wxHtmlWindow *stats2, const GuideSession *session)
{
stats->BeginBatch();
stats->SetCellValue(0, 0, wxString::Format("% .2f\" (%.2f px)", session->pixelScale * session->rms_ra, session->rms_ra));
stats->SetCellValue(1, 0, wxString::Format("% .2f\" (%.2f px)", session->pixelScale * session->rms_dec, session->rms_dec));
double tot = sqrt(session->rms_ra * session->rms_ra + session->rms_dec * session->rms_dec);
stats->SetCellValue(2, 0, wxString::Format("% .2f\" (%.2f px)", session->pixelScale * tot, tot));
stats->SetCellValue(0, 1, wxString::Format("% .2f\" (% .2f px)", session->pixelScale * session->peak_ra, session->peak_ra));
stats->SetCellValue(1, 1, wxString::Format("% .2f\" (% .2f px)", session->pixelScale * session->peak_dec, session->peak_dec));
stats->SetCellValue(2, 1, wxString::Format(" Elong.: %.f%%", session->elongation * 100.));
stats->AutoSize();
stats->EndBatch();
std::ostringstream os;
os << "<span style='font-family:monospace;font-size:8;'><table cellspacing=1 cellpadding=1 border=0>"
"<tr><td>RA Drift</td><td>" << FormatNum(session->drift_ra * session->pixelScale) << "\"/min, " << FormatNum(session->drift_ra) << " px/min</td></tr>"
<< "<tr><td>Dec Drift</td><td>" << FormatNum(session->drift_dec * session->pixelScale) << "\"/min, " << FormatNum(session->drift_dec) << " px/min</td></tr>"
<< "<tr><td>Polar Alignment Error </td><td>" << std::fixed << std::setprecision(1) << session->paerr << "'</td></tr>"
<< "</table></span>";
stats2->SetPage(os.str());
}
static void UpdateStats(wxGrid *stats, wxHtmlWindow *stats2, GuideSession *session)
{
session->CalcStats();
InitStats(stats, stats2, session);
}
void LogViewFrame::OnMenuInclude(wxCommandEvent& event)
{
if (!m_session)
return;
if (event.GetId() == ID_INCLUDE_ALL)
{
IncludeAll(m_session->entries);
UpdateStats(m_stats, m_stats2, m_session);
s_scatter.Invalidate();
m_graph->Refresh();
}
else if (event.GetId() == ID_INCLUDE_NONE)
{
IncludeNone(m_session->entries);
UpdateStats(m_stats, m_stats2, m_session);
s_scatter.Invalidate();
m_graph->Refresh();
}
else if (event.GetId() == ID_EXCLUDE_SETTLE)
{
ExcludeSettling(m_session);
UpdateStats(m_stats, m_stats2, m_session);
s_scatter.Invalidate();
m_graph->Refresh();
}
}
void LogViewFrame::OnMenuAnalyzeGA(wxCommandEvent& event)
{
if (!m_analysisWin)
m_analysisWin = new AnalysisWin(this);
m_analysisWin->AnalyzeGA(*m_session, s_analyze_pos);
if (m_analysisWin->IsShown())
{
m_analysisWin->m_graph->Refresh();
m_analysisWin->Raise();
}
else
m_analysisWin->Show();
}
void LogViewFrame::OnMenuAnalyzeAll(wxCommandEvent& event)
{
if (!m_analysisWin)
m_analysisWin = new AnalysisWin(this);
bool undo_ra_corrections = event.GetId() == ID_ANALYZE_ALL_NORA;
m_analysisWin->AnalyzeAll(*m_session, undo_ra_corrections);
if (m_analysisWin->IsShown())
{
m_analysisWin->m_graph->Refresh();
m_analysisWin->Raise();
}
else
m_analysisWin->Show();
}
void LogViewFrame::OnHelpAbout(wxCommandEvent& event)
{
wxAboutDialogInfo aboutInfo;
aboutInfo.SetName(APP_NAME);
aboutInfo.SetVersion(APP_VERSION_STR);
aboutInfo.SetDescription(_("A tool for visualizing PHD2 guide log data"));
aboutInfo.SetCopyright("(C) 2018-2020 Andy Galasso <[email protected]>");
aboutInfo.SetWebSite("http://adgsoftware.com/phd2utils");
aboutInfo.AddDeveloper("Andy Galasso");
wxAboutBox(aboutInfo);
}
class HelpDialog : public HelpDialogBase
{
public:
HelpDialog(wxWindow *parent);
~HelpDialog();
};
static HelpDialog *s_help;
HelpDialog::HelpDialog(wxWindow *parent) : HelpDialogBase(parent)
{
static const wxString BR = "<br>";
wxString s;
s << "<html>"
<< "<h3>Opening a guide log</h3>"
<< "There are three ways to open a PHD2 Log File:" << BR
<< "1. Menu: File => Open" << BR
<< "2. Drag and drop from Windows explorer" << BR
<< "3. As an argument on the command-line" << BR
<< "<h3>Viewing calibration</h3>"
<< "Drag with the mouse to pan" << BR
<< "Use the mouse wheel or the lower +/- buttons to zoom in and out" << BR
<< "<h3>Viewing guiding data</h3>"
<< "Drag left or right with the mouse to pan, or use the horizontal scrollbar" << BR
<< "The mouse wheel or the lower +/- buttons will zoom the horizontal scale" << BR
<< "Drag up or down with the mouse or use the +/- buttons on the right to change the vertical scale" << BR
<< "<h3>Statistics</h3>"
<< "To exclude a range of points from the statistics, hold down CTRL and drag the mouse," << BR
<< "To include a range of points from the statistics, hold down Shift and drag the mouse," << BR
<< "CTRL-Click on an excluded range to un-exclude the range of points." << BR
<< "Right-click on the graph for some more options." << BR
<< "</html>";
m_html->SetPage(s);
s_help = this;
}
HelpDialog::~HelpDialog()
{
s_help = 0;
}
void LogViewFrame::OnHelp(wxCommandEvent& event)
{
if (!s_help)
s_help = new HelpDialog(this);
s_help->Show();
}
inline static void UpdateRange(GraphInfo *ginfo)
{
ginfo->i0 = (double) ginfo->xofs / ginfo->hscale;
ginfo->i1 = (double) (ginfo->xofs + ginfo->width) / ginfo->hscale;
}
void LogViewFrame::InitGraph()
{
GraphInfo& ginfo = m_session->m_ginfo;
// find max ra or dec
double mxr = 0.0;
double mxy = 0.0;
int mxmass = 0;
double mxsnr = 0.0;
//for (const auto& e : m_session->entries)
for (auto it = m_session->entries.begin(); it != m_session->entries.end(); ++it)
{
const auto& e = *it;
double val = fabs(e.raraw);
if (val > mxr)
mxr = val;
val = fabs(e.decraw);
if (val > mxr)
mxr = val;
val = fabs(e.dx);
if (val > mxy)
mxy = val;
val = fabs(e.dy);
if (val > mxy)
mxy = val;
if (e.mass > mxmass)
mxmass = e.mass;
if (e.snr > mxsnr)
mxsnr = e.snr;
}
ginfo.max_ofs = mxr;
ginfo.max_mass = mxmass;
ginfo.max_snr = mxsnr;
ginfo.yofs = 0;
}
void LogViewFrame::InitCalDisplay()
{
CalDisplay& disp = m_calibration->display;
disp.xofs = disp.yofs = 0;
disp.firstWest = -1;
disp.firstNorth = -1;
double maxv = 0.0;
int i = 0;
for (auto it = m_calibration->entries.begin(); it != m_calibration->entries.end(); ++it)
{
const auto& p = *it;
if (p.direction == WEST)
{
if (disp.firstWest == -1) disp.firstWest = i;
disp.lastWest = i;
}
else if (p.direction == NORTH)
{
if (disp.firstNorth == -1) disp.firstNorth = i;
disp.lastNorth = i;
}
double d = fabs(p.dx);
if (d > maxv) maxv = d;
d = fabs(p.dy);
if (d > maxv) maxv = d;
++i;
}
if (maxv == 0.0)
maxv = 25.0;
int size = wxMin(m_graph->GetSize().GetWidth(), m_graph->GetSize().GetHeight());
size = std::max(size, 40); // prevent -ive scale
disp.scale = (double)(size - 30) / (2.0 * maxv);
disp.min_scale = std::min(disp.scale, MIN_HSCALE_CAL);
disp.valid = true;
}
void LogViewFrame::OnCellSelected(wxGridEvent& event)
{
int row = event.GetRow();
m_sessions->SelectRow(row);
if (row == m_sessionIdx)
goto out;
m_sessionIdx = row;
if (row < (int) s_log.sections.size())
{
LogSection *section;
const LogSectionLoc &loc = s_log.sections[m_sessionIdx];
bool enableMount = false;
bool enableAO = false;
if (loc.type == CALIBRATION_SECTION)
{
m_session = 0;
section = m_calibration = &s_log.calibrations[loc.idx];
if (m_calibration->device == MOUNT)
enableMount = true;
else
enableAO = true;
}
else
{
m_calibration = 0;
section = m_session = &s_log.sessions[loc.idx];
if (m_session->mount.isValid)
enableMount = true;
if (m_session->ao.isValid)
enableAO = true;
}
m_device->Enable(0, enableMount);
m_device->Enable(1, enableAO);
if (enableMount && !enableAO)
m_device->SetSelection(0);
else if (enableAO && !enableMount)
m_device->SetSelection(1);
{
wxWindowUpdateLocker lck(m_sessionInfo);
m_sessionInfo->Clear();
for (auto it = section->hdr.begin(); it != section->hdr.end(); ++it)
*m_sessionInfo << *it << "\n";
m_sessionInfo->SetInsertionPoint(0);
}
if (m_session)
{
bool first = !m_session->m_ginfo.IsValid();
if (first)
InitGraph();
if (!m_mainSizer->IsShown(m_guideControlsSizer))
{
bool enable = true;
m_vplus->Enable(enable);
m_vminus->Enable(enable);
m_vreset->Enable(enable);
m_vpan->Enable(enable);
m_vlock->Enable(enable);
m_scrollbar->Enable(enable);
m_mainSizer->Show(m_guideControlsSizer);
m_mainSizer->Layout();
}
InitStats(m_stats, m_stats2, m_session);
if (first)
{
wxCommandEvent dummy;
OnHReset(dummy);
if (!vscale_locked())
OnVReset(dummy);
}
else
UpdateScrollbar();
}
else
{
// do this first so InitCalDisplay has the right window sizes
if (m_mainSizer->IsShown(m_guideControlsSizer))
{
bool enable = false;
m_vplus->Enable(enable);
m_vminus->Enable(enable);
m_vreset->Enable(enable);
m_vpan->Enable(enable);
m_vlock->Enable(enable);
m_scrollbar->Enable(enable);
m_mainSizer->Hide(m_guideControlsSizer);
m_mainSizer->Layout();
}
m_stats->ClearGrid();
if (!m_calibration->display.valid)
InitCalDisplay();
}
}
else
{
m_session = 0;
m_calibration = 0;
m_sessionInfo->Clear();
m_stats->ClearGrid();
}
m_rowInfo->Clear();
s_scatter.Invalidate();
m_graph->Refresh();
out:
m_graph->SetFocus();
}
inline static wxLongLong_t now()
{
return ::wxGetUTCTimeMillis().GetValue();
}
void LogViewFrame::OnLeftDown(wxMouseEvent& event)
{
if (!m_session && !m_calibration)
{
event.Skip();
return;
}
s_drag.m_dragging = true;
if (m_session && event.ControlDown())
{
s_drag.m_dragMode = DRAG_EXCLUDE;
s_drag.m_anchorPoint = s_drag.m_endPoint = event.GetPosition();
s_drag.dragMoved = false;
}