-
Notifications
You must be signed in to change notification settings - Fork 7
/
DMSpecView.cpp
1697 lines (1438 loc) · 52.3 KB
/
DMSpecView.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
// DMSpecView.cpp : implementation of the CDMSpecView class
#undef min
#undef max
#include "stdafx.h"
#include <afxdlgs.h>
#include <memory>
#include "DMSpec.h"
#include "DMSpecDoc.h"
#include "DMSpecView.h"
#include "MainFrm.h"
#include <MobileDoasLib/DateTime.h>
#include "PostFluxDlg.h"
#include <MobileDoasLib/Flux/Flux1.h>
#include "DualBeam/PostWindDlg.h"
#include "DualBeam/PostPlumeHeightDlg.h"
#include "CSpectrometerCalibrationDlg.h"
#include "CommentDlg.h"
#include "InformationDialog.h"
#include "Dialogs/SpectrumInspectionDlg.h"
#include "ReEvaluation\ReEvaluationDlg.h"
#include "Configuration/ConfigurationDialog.h"
#include "Configuration/Configure_Evaluation.h"
#include "Configuration/Configure_GPS.h"
#include "Configuration/Configure_Spectrometer.h"
#include "Configuration/Configure_Directory.h"
#include "Configuration/Configure_Calibration.h"
#include "MeasurementSetup.h"
#include <algorithm>
#include <Mmsystem.h> // used for PlaySound
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace ReEvaluation;
#define LEFT 50
#define TOP 14
#define RIGHT 970
#define BOTTOM 520//560
CString g_exePath; // <-- This is the path to the executable. This is a global variable and should only be changed in DMSpecView.cpp
CFormView* pView; // <-- The main window
/////////////////////////////////////////////////////////////////////////////
// CDMSpecView
IMPLEMENT_DYNCREATE(CDMSpecView, CFormView)
BEGIN_MESSAGE_MAP(CDMSpecView, CFormView)
// Menu commands
// Starting and stopping the program
ON_BN_CLICKED(IDC_BTNSTART, OnControlStart) // <-- the toolbar button
ON_BN_CLICKED(ID_CONTROL_STARTWINDMEASUREMENT, OnControlStartWindMeasurement)
ON_COMMAND(ID_CONTROL_STOP, OnControlStop)
// Just view the spectra from the spectrometer without evaluations
ON_COMMAND(ID_CONTROL_VIEWSPECTRAFROMSPECTROMETER, OnControlViewSpectra) // <-- view the output from the spectrometer
ON_COMMAND(ID_CONTROL_VIEWSPECTRAFROMDIRECTORY, OnControlProcessSpectraFromDirectory) // <-- view latest spectra file in directory
ON_COMMAND(ID_ANALYSIS_POSTFLUX, OnMenuShowPostFluxDialog)
ON_COMMAND(ID_ANALYSIS_VIEWMEASUREDSPECTRA, OnMenuShowSpectrumInspectionDialog)
ON_COMMAND(ID_CONTROL_COUNTFLUX, OnControlCountflux)
// Changing the plot
ON_COMMAND(ID_CONFIGURATION_PLOT_CHANGEBACKGROUND, OnConfigurationPlotChangebackground)
ON_COMMAND(ID_CONFIGURATION_PLOT_CHANGEPLOTCOLOR, OnConfigurationPlotChangeplotcolor)
ON_COMMAND(ID_CONFIGURATION_PLOT_CHANGEPLOTCOLOR_SLAVE, OnConfigurationPlotChangeplotcolor_Slave)
// dual-beam
ON_COMMAND(ID_ANALYSIS_PLUMEHEIGHTMEASUREMENT, OnMenuAnalysisPlumeheightmeasurement)
ON_COMMAND(ID_ANALYSIS_WINDSPEEDMEASUREMENT, OnMenuAnalysisWindSpeedMeasurement)
ON_COMMAND(ID_CONFIGURATION_OPERATION, OnConfigurationOperation)
ON_MESSAGE(WM_DRAWCOLUMN, OnDrawColumn)
ON_MESSAGE(WM_STATUSMSG, OnShowStatus)
ON_MESSAGE(WM_READGPS, OnReadGPS)
ON_MESSAGE(WM_SHOWINTTIME, OnShowIntTime)
ON_MESSAGE(WM_CHANGEDSPEC, OnChangeSpectrometer)
ON_MESSAGE(WM_DRAWSPECTRUM, OnDrawSpectrum)
ON_MESSAGE(WM_CHANGEDSPECSCALE, OnChangedSpectrumScale)
ON_MESSAGE(WM_SHOWDIALOG, OnShowInformationDialog)
ON_COMMAND(ID_VIEW_REALTIMEROUTE, OnViewRealtimeroute)
ON_COMMAND(ID_VIEW_SPECTRUMFIT, OnViewSpectrumFit)
ON_UPDATE_COMMAND_UI(ID_VIEW_REALTIMEROUTE, OnUpdateViewRealtimeroute)
ON_UPDATE_COMMAND_UI(ID_VIEW_SPECTRUMFIT, OnUpdateViewSpectrumFit)
ON_COMMAND(ID_CONTROL_ADDCOMMENT, OnControlAddComment)
ON_COMMAND(ID_CONTROL_REEVALUATE, OnControlReevaluate)
ON_UPDATE_COMMAND_UI(ID_CONTROL_REEVALUATE, OnUpdateControlReevaluate)
ON_COMMAND(ID_CONFIGURATION_CHANGEEXPOSURETIME, OnConfigurationChangeexposuretime)
ON_WM_HELPINFO()
ON_COMMAND(ID_CONTROL_TESTTHEGPS, OnMenuControlTestTheGPS)
ON_COMMAND(ID_CONTROL_STARTTHEGPS, OnMenuControlRunTheGPS)
ON_COMMAND(ID_ANALYSIS_CALIBRATESPECTROMETER, OnAnalysisCalibratespectrometer)
ON_COMMAND(ID_VIEW_COLUMNERROR, OnViewColumnError)
ON_UPDATE_COMMAND_UI(ID_VIEW_COLUMNERROR, OnUpdateViewColumnError)
ON_UPDATE_COMMAND_UI(ID_CONTROL_TESTTHEGPS, OnUpdate_DisableOnRun)
ON_UPDATE_COMMAND_UI(IDC_BTNSTART, OnUpdate_DisableOnRun)
ON_UPDATE_COMMAND_UI(ID_CONTROL_STOP, OnUpdate_EnableOnRun)
ON_UPDATE_COMMAND_UI(ID_CONTROL_VIEWSPECTRAFROMSPECTROMETER, OnUpdate_DisableOnRun)
ON_UPDATE_COMMAND_UI(ID_CONTROL_STARTWINDMEASUREMENT, OnUpdateWindMeasurement)
ON_UPDATE_COMMAND_UI(ID_CONTROL_STARTTHEGPS, OnUpdate_StartTheGps)
ON_UPDATE_COMMAND_UI(ID_CONTROL_ADDCOMMENT, OnUpdate_EnableOnRun)
ON_UPDATE_COMMAND_UI(ID_CONFIGURATION_CHANGEEXPOSURETIME, OnUpdate_EnableOnRun)
ON_WM_CLOSE()
ON_WM_DESTROY()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDMSpecView construction/destruction
// boolean flag to help us determine if the spectrometer thread is running. s_ stands for statics...
static bool s_spectrometerAcquisitionThreadIsRunning = false;
// label colors
COLORREF warning = RGB(255, 75, 75);
COLORREF normal = RGB(236, 233, 216);
CDMSpecView::CDMSpecView()
: CFormView(CDMSpecView::IDD)
{
m_WindDirection = 0.0;
m_WindSpeed = 8.0;
pView = this;
s_spectrometerAcquisitionThreadIsRunning = false;
m_columnChartXAxisValues.resize(200);
for (int i = 0; i < 200; i++)
{
m_columnChartXAxisValues[i] = i;
}
m_spectrumChartXAxisValues.resize(MAX_SPECTRUM_LENGTH);
for (int i = 0; i < MAX_SPECTRUM_LENGTH; i++)
{
m_spectrumChartXAxisValues[i] = (200.0 * i) / MAX_SPECTRUM_LENGTH;
}
m_Spectrometer = nullptr;
m_showErrorBar = FALSE;
}
CDMSpecView::~CDMSpecView()
{
if (m_Spectrometer != nullptr)
{
delete(m_Spectrometer);
m_Spectrometer = nullptr;
}
}
void CDMSpecView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BASEEDIT, m_BaseEdit);
DDX_Text(pDX, IDC_WINDDIRECTION, m_WindDirection);
DDV_MinMaxDouble(pDX, m_WindDirection, 0., 360.);
DDX_Text(pDX, IDC_WINDSPEED, m_WindSpeed);
DDV_MinMaxDouble(pDX, m_WindSpeed, 0., 1000000.);
DDX_Control(pDX, IDC_SIGNALLIMIT_SLIDER, m_intensitySliderLow);
// The GPS-Labels
DDX_Control(pDX, IDC_LAT, m_gpsLatLabel);
DDX_Control(pDX, IDC_LON, m_gpsLonLabel);
DDX_Control(pDX, IDC_GPSTIME, m_gpsTimeLabel);
DDX_Control(pDX, IDC_NGPSSAT, m_gpsNSatLabel);
// Spectrometer Info labels
DDX_Control(pDX, IDC_INTTIME, m_expLabel);
DDX_Control(pDX, IDC_SCANNO, m_scanNoLabel);
DDX_Control(pDX, IDC_CONCENTRATION, m_colLabel);
DDX_Control(pDX, IDC_SPECNO, m_noSpecLabel);
DDX_Control(pDX, IDC_SH, m_shiftLabel);
DDX_Control(pDX, IDC_SQ, m_squeezeLabel);
DDX_Control(pDX, IDC_TEMPERATURE, m_tempLabel);
// The legend
DDX_Control(pDX, IDC_LABEL_COLOR_SPECTRUM, m_colorLabelSpectrum1);
DDX_Control(pDX, IDC_LABEL_COLOR_SPECTRUM2, m_colorLabelSpectrum2);
DDX_Control(pDX, IDC_LABEL_COLOR_SERIES1, m_colorLabelSeries1);
DDX_Control(pDX, IDC_LABEL_COLOR_SERIES2, m_colorLabelSeries2);
DDX_Control(pDX, IDC_LABEL_SPECTRUM, m_legendSpectrum1);
DDX_Control(pDX, IDC_LABEL_SPECTRUM2, m_legendSpectrum2);
DDX_Control(pDX, IDC_LABEL_SERIES1, m_legendSeries1);
DDX_Control(pDX, IDC_LABEL_SERIES2, m_legendSeries2);
}
BOOL CDMSpecView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CDMSpecView::OnInitialUpdate()
{
CRect rect;
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
/* get the path to the program */
Common common;
common.GetExePath();
g_exePath.Format(common.m_exePath);
// The size of the graph
int left = 50;
int top = 14;
int right = 970;
int bottom = 520;
// Get the resolution of the screen
int cx = GetSystemMetrics(SM_CXSCREEN);
// rescale the graph to fit the width of the window
right = cx * right / 1024;
// Also move the slider to the right of the window
m_intensitySliderLow.GetWindowRect(rect);
int diff = rect.right - rect.left;
rect = CRect(right, TOP, right + diff + 1, BOTTOM);
m_intensitySliderLow.MoveWindow(rect);
rect = CRect(LEFT, TOP, right, BOTTOM);
m_ColumnPlot.Create(WS_VISIBLE | WS_CHILD, rect, this);
// customize the control
m_columnLimit = 100.0;
m_PlotColor[0] = RGB(255, 0, 0);
m_PlotColor[1] = RGB(0, 0, 255);
m_ColumnPlot.SetSecondYUnit(TEXT("Intensity [%]"));
m_ColumnPlot.SetYUnits("Column [ppmm]");
m_ColumnPlot.SetXUnits("Number");
m_ColumnPlot.EnableGridLinesX(false);
m_ColumnPlot.SetBackgroundColor(RGB(0, 0, 0));
m_ColumnPlot.SetGridColor(RGB(255, 255, 255));
m_ColumnPlot.SetPlotColor(m_PlotColor[0]);
m_ColumnPlot.SetRange(0, 200, 1, 0.0, 100.0, 1);
m_ColumnPlot.SetMinimumRangeX(200.0f);
m_ColumnPlot.SetSecondRange(0.0, 200, 0, 0.0, 100.0, 0);
m_BaseEdit.LimitText(99);
ReadMobileLog();
/* the intensity slider */
m_intensitySliderLow.SetRange(0, 100); /** The scale of the intensity slider is in percent */
m_intensitySliderLow.SetPos(100 - 25); /* The slider is upside down - i.e. the real value is "100 - m_intensitySlider.GetPos()"*/
m_intensitySliderLow.SetTicFreq(25);
/* The colors for the spectrum plots */
m_Spectrum0Color = RGB(0, 255, 0);
m_Spectrum0FitColor = RGB(0, 150, 0);
m_Spectrum1Color = RGB(255, 0, 255);
m_Spectrum1FitColor = RGB(150, 0, 150);
m_SpectrumLineWidth = 1;
// Fix the legend
UpdateLegend();
// set background color for spectrometer info labels
m_expLabel.SetBackgroundColor(normal);
m_scanNoLabel.SetBackgroundColor(normal);
m_colLabel.SetBackgroundColor(normal);
m_noSpecLabel.SetBackgroundColor(normal);
m_shiftLabel.SetBackgroundColor(normal);
m_squeezeLabel.SetBackgroundColor(normal);
m_tempLabel.SetBackgroundColor(normal);
}
/////////////////////////////////////////////////////////////////////////////
// CDMSpecView diagnostics
#ifdef _DEBUG
void CDMSpecView::AssertValid() const
{
CFormView::AssertValid();
}
void CDMSpecView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CDMSpecDoc* CDMSpecView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDMSpecDoc)));
return (CDMSpecDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDMSpecView message handlers
LRESULT CDMSpecView::OnDrawColumn(WPARAM wParam, LPARAM lParam)
{
CString cCon; // the concentration str
CString cShift; //shift str
CString cSqueeze; //squeeze str
CString cScanNo; //scanned spectra number after sky and dark spectra
CString cTemp; // detector temperature
// if the program is no longer running, then don't try to draw anything more...
if (!s_spectrometerAcquisitionThreadIsRunning)
{
return 0;
}
const long dynRange = m_Spectrometer->m_spectrometerDynRange;
const int intensityLimit = (100 - m_intensitySliderLow.GetPos());
// Get the last value and the total number of values
mobiledoas::ReferenceFitResult lastEvaluationResult;
m_Spectrometer->GetLastColumn(lastEvaluationResult);
const long scanNo = m_Spectrometer->GetNumberOfSpectraAcquired() - 2;
// Get the number of channels used and the number of fit-regions used
const int nChannels = m_Spectrometer->m_NChannels;
const int fitRegionNum = m_Spectrometer->GetFitRegionNum();
// --- Update the column, shift and squeeze ---
cCon.Format("%.2lf \u00B1 %.2lf", lastEvaluationResult.m_column, lastEvaluationResult.m_columnError);
cShift.Format("%.1f", lastEvaluationResult.m_shift);
cSqueeze.Format("%.1f", lastEvaluationResult.m_squeeze);
cScanNo.Format("%d", scanNo);
this->SetDlgItemText(IDC_CONCENTRATION, cCon);
this->SetDlgItemText(IDC_SH, cShift);
this->SetDlgItemText(IDC_SQ, cSqueeze);
this->SetDlgItemText(IDC_SCANNO, cScanNo);
// update the temperature
double temp = m_Spectrometer->detectorTemperature;
if (!std::isnan(temp))
{
cTemp.Format("%.1f", temp);
if (m_Spectrometer->detectorTemperatureIsSetPointTemp)
{
m_tempLabel.SetBackgroundColor(normal);
}
else
{
m_tempLabel.SetBackgroundColor(warning);
}
}
else
{
cTemp = "N/A";
m_tempLabel.SetBackgroundColor(normal);
}
this->SetDlgItemText(IDC_TEMPERATURE, cTemp);
// --- Get the data, but plot mo more than 199 values (to keep the graph readable) ---
const long size = std::min(long(199), m_Spectrometer->GetColumnNumber());
std::vector<double> intensity(size);
m_Spectrometer->GetIntensity(intensity, size);
std::vector<double> masterChannelColumns(size);
std::vector<double> masterChannelColumnErrors(size);
m_Spectrometer->GetColumns(masterChannelColumns, size, 0);
m_Spectrometer->GetColumnErrors(masterChannelColumnErrors, size, 0);
std::vector<double> slaveChannelColumns;
std::vector<double> slaveChannelColumnErrors;
if (fitRegionNum > 1)
{
slaveChannelColumns.resize(size);
slaveChannelColumnErrors.resize(size);
m_Spectrometer->GetColumns(slaveChannelColumns, size, 1);
m_Spectrometer->GetColumnErrors(slaveChannelColumnErrors, size, 1);
}
// -- Convert the intensity to saturation ratio
for (int k = 0; k < size; ++k)
{
intensity[k] = intensity[k] * 100.0 / dynRange;
}
double maxColumn = 0.0;
double minColumn = 0.0;
// -- Get the limits for the data ---
for (int i = 0; i < size; i++)
{
if (intensity[i] > intensityLimit)
{
maxColumn = std::max(maxColumn, std::abs(masterChannelColumns[i]));
minColumn = std::min(minColumn, masterChannelColumns[i]);
if (fitRegionNum > 1)
{
maxColumn = std::max(maxColumn, std::abs(slaveChannelColumns[i]));
minColumn = std::min(minColumn, slaveChannelColumns[i]);
}
}
}
const double lowLimit = (-1.25) * std::abs(minColumn);
m_columnLimit = 1.25 * maxColumn;
if (m_columnLimit == 0)
{
m_columnLimit = 0.1;
}
// Set the range for the plot
m_ColumnPlot.SetRange(0.0, 199.0, 0, lowLimit, m_columnLimit, 1);
m_ColumnPlot.SetSecondRange(0.0, 200, 0, m_minSaturationRatio, m_maxSaturationRatio, 0);
// Draw the columns (don't change the scale again here...)
if (m_spectrometerMode == MODE_TRAVERSE || m_spectrometerMode == MODE_DIRECTORY)
{
if (fitRegionNum == 1)
{
m_ColumnPlot.SetPlotColor(m_PlotColor[0]);
if (m_showErrorBar)
{
m_ColumnPlot.BarChart(m_columnChartXAxisValues.data(), masterChannelColumns.data(), masterChannelColumnErrors.data(), size, Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}
else
{
m_ColumnPlot.BarChart(m_columnChartXAxisValues.data(), masterChannelColumns.data(), size, Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}
}
else
{
m_ColumnPlot.SetPlotColor(m_PlotColor[0]);
if (m_showErrorBar)
{
m_ColumnPlot.BarChart2(m_columnChartXAxisValues.data(), masterChannelColumns.data(), slaveChannelColumns.data(), masterChannelColumnErrors.data(), slaveChannelColumnErrors.data(), m_PlotColor[1], size, Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}
else
{
m_ColumnPlot.BarChart2(m_columnChartXAxisValues.data(), masterChannelColumns.data(), slaveChannelColumns.data(), m_PlotColor[1], size, Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}
}
}
else if (m_spectrometerMode == MODE_WIND)
{
if (m_showErrorBar)
{
m_ColumnPlot.SetPlotColor(m_PlotColor[0]);
m_ColumnPlot.XYPlot(m_columnChartXAxisValues.data(), masterChannelColumns.data(), NULL, NULL, masterChannelColumnErrors.data(), size, Graph::CGraphCtrl::PLOT_FIXED_AXIS | Graph::CGraphCtrl::PLOT_CONNECTED);
m_ColumnPlot.SetPlotColor(m_PlotColor[1]);
m_ColumnPlot.XYPlot(m_columnChartXAxisValues.data(), slaveChannelColumns.data(), NULL, NULL, slaveChannelColumnErrors.data(), size, Graph::CGraphCtrl::PLOT_FIXED_AXIS | Graph::CGraphCtrl::PLOT_CONNECTED);
}
else
{
m_ColumnPlot.SetPlotColor(m_PlotColor[0]);
m_ColumnPlot.XYPlot(m_columnChartXAxisValues.data(), masterChannelColumns.data(), size, Graph::CGraphCtrl::PLOT_FIXED_AXIS | Graph::CGraphCtrl::PLOT_CONNECTED);
m_ColumnPlot.SetPlotColor(m_PlotColor[1]);
m_ColumnPlot.XYPlot(m_columnChartXAxisValues.data(), slaveChannelColumns.data(), size, Graph::CGraphCtrl::PLOT_FIXED_AXIS | Graph::CGraphCtrl::PLOT_CONNECTED);
}
}
// Draw the intensities
m_ColumnPlot.DrawCircles(m_columnChartXAxisValues.data(), intensity.data(), size, Graph::CGraphCtrl::PLOT_SECOND_AXIS);
// Draw the spectrum
DrawSpectrum();
if (m_realTimeRouteGraph.fVisible)
{
m_realTimeRouteGraph.m_intensityLimit = dynRange * (100 - m_intensitySliderLow.GetPos());
m_realTimeRouteGraph.DrawRouteGraph();
}
if (m_showFitDlg.m_isVisible)
{
m_showFitDlg.DrawFit();
}
return 0;
}
LRESULT CDMSpecView::OnDrawSpectrum(WPARAM wParam, LPARAM lParam)
{
// to not overload the computer, make sure that we don't draw too often...
static double secondsBetweenDraw = 0.05;
static clock_t cLastCall = 0;
clock_t now = clock();
double secondsSinceLastDraw = (double)(now - cLastCall) / (double)CLOCKS_PER_SEC;
if (secondsSinceLastDraw < secondsBetweenDraw)
{
return 0;
}
cLastCall = now;
m_ColumnPlot.CleanPlot();
// set the ranges for the plot
if (m_spectrometerMode == MODE_VIEW || m_spectrometerMode == MODE_DIRECTORY)
{
m_ColumnPlot.SetRange(0.0, 2048, 0, m_minSaturationRatio, m_maxSaturationRatio, 0);
}
else
{
m_ColumnPlot.SetSecondRange(0.0, 200, 0, m_minSaturationRatio, m_maxSaturationRatio, 0);
}
DrawSpectrum();
// also update the integration time
OnShowIntTime(wParam, lParam);
return 0;
}
/** Changes the saturation-ratio scale of the spectrum-view */
LRESULT CDMSpecView::OnChangedSpectrumScale(WPARAM wParam, LPARAM lParam)
{
this->m_minSaturationRatio = (int)wParam;
this->m_maxSaturationRatio = (int)lParam;
return OnDrawSpectrum(wParam, lParam);
}
LRESULT CDMSpecView::OnShowIntTime(WPARAM wParam, LPARAM lParam)
{
CString expTime, nAverage;
int averageInSpectrometer = 0;
int averageInComputer = 0;
// if the program is no longer running, then don't try to draw anything more...
if (!s_spectrometerAcquisitionThreadIsRunning)
{
return 0;
}
expTime.Format("%d ms", m_Spectrometer->GetCurrentIntegrationTime());
this->SetDlgItemText(IDC_INTTIME, expTime);
if (m_spectrometerMode == MODE_DIRECTORY)
{
nAverage.Format("%d", m_Spectrometer->NumberOfSpectraToAverage());
}
else
{
m_Spectrometer->GetNSpecAverage(averageInSpectrometer, averageInComputer);
nAverage.Format("%dx%d", averageInSpectrometer, averageInComputer);
}
this->SetDlgItemText(IDC_SPECNO, nAverage);
// Update the legend
UpdateLegend();
// forward the message to the spectrum-settings dialog(if any);
if (this->m_specSettingsDlg.m_hWnd != nullptr)
{
m_specSettingsDlg.PostMessage(WM_SHOWINTTIME);
}
return 0;
}
LRESULT CDMSpecView::OnChangeSpectrometer(WPARAM wParam, LPARAM lParam)
{
// forward the message to the spectrum-settings dialog(if any);
if (this->m_specSettingsDlg.m_hWnd != nullptr)
{
m_specSettingsDlg.PostMessage(WM_CHANGEDSPEC);
}
return 0;
}
void CDMSpecView::OnMenuShowPostFluxDialog()
{
CPostFluxDlg fluxDlg;
fluxDlg.DoModal();
}
void CDMSpecView::OnMenuShowSpectrumInspectionDialog()
{
Dialogs::CSpectrumInspectionDlg dlg;
dlg.DoModal();
}
/** This function is the thread function to start running spectrometer
**
*/
UINT CollectSpectra(LPVOID pParam)
{
CSpectrometer* spec = (CSpectrometer*)pParam;
spec->Run();
s_spectrometerAcquisitionThreadIsRunning = false;
return 0;
}
LRESULT CDMSpecView::OnShowStatus(WPARAM wParam, LPARAM lParam)
{
if (s_spectrometerAcquisitionThreadIsRunning)
{
CString str;
str = m_Spectrometer->m_statusMsg;
ShowStatusMsg(str);
}
return 0;
}
LRESULT CDMSpecView::OnReadGPS(WPARAM wParam, LPARAM lParam)
{
mobiledoas::GpsData data;
static int latNSat = 10;
// if the program is no longer running, then don't try to draw anything more...
if (!s_spectrometerAcquisitionThreadIsRunning)
{
return 0;
}
m_Spectrometer->GetGpsPos(data);
CString lat, lon, tim, strHr, strMin, strSec, nSat;
int hr, min, sec;
ExtractTime(data, hr, min, sec);
if (data.latitude >= 0.0)
{
lat.Format("%f degree N", data.latitude);
}
else
{
lat.Format("%f degree S", -1.0 * data.latitude);
}
if (data.longitude >= 0.0)
{
lon.Format("%f degree E", data.longitude);
}
else
{
lon.Format("%f degree W", -1.0 * data.longitude);
}
if (hr < 10)
{
strHr.Format("0%d:", hr);
}
else
{
strHr.Format("%d:", hr);
}
if (min < 10)
{
strMin.Format("0%d:", min);
}
else
{
strMin.Format("%d:", min);
}
if (sec < 10)
{
strSec.Format("0%d", sec);
}
else
{
strSec.Format("%d", sec);
}
nSat.Format("%d", (long)data.nSatellitesTracked);
tim = strHr + strMin + strSec;
this->SetDlgItemText(IDC_GPSTIME, tim);
this->SetDlgItemText(IDC_LAT, lat);
this->SetDlgItemText(IDC_LON, lon);
this->SetDlgItemText(IDC_NGPSSAT, nSat);
if (!(m_spectrometerMode == MODE_DIRECTORY) && !m_Spectrometer->GpsGotContact())
{
// If the communication with the GPS is broken (e.g. device unplugged)
COLORREF warning = RGB(255, 75, 75);
// Set the background color to red
m_gpsLatLabel.SetBackgroundColor(warning);
m_gpsLonLabel.SetBackgroundColor(warning);
m_gpsTimeLabel.SetBackgroundColor(warning);
m_gpsNSatLabel.SetBackgroundColor(warning);
SoundAlarm();
}
else if (latNSat != 0 && data.nSatellitesTracked == 0)
{
COLORREF warning = RGB(255, 75, 75);
// Set the background color to red
m_gpsLatLabel.SetBackgroundColor(warning);
m_gpsLonLabel.SetBackgroundColor(warning);
m_gpsTimeLabel.SetBackgroundColor(warning);
m_gpsNSatLabel.SetBackgroundColor(warning);
}
else
{
// Set the background color to normal
m_gpsLatLabel.SetBackgroundColor(normal);
m_gpsLonLabel.SetBackgroundColor(normal);
m_gpsTimeLabel.SetBackgroundColor(normal);
m_gpsNSatLabel.SetBackgroundColor(normal);
}
// Remember the number of satelites
latNSat = (int)data.nSatellitesTracked;
return 0;
}
void CDMSpecView::ShowStatusMsg(CString& str)
{
CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus = &pFrame->m_wndStatusBar;
if (pStatus)
{
pStatus->SetPaneText(0, str);
}
}
void CDMSpecView::OnControlCountflux()
{
if (s_spectrometerAcquisitionThreadIsRunning)
{
double flux = m_Spectrometer->GetFlux();
// m_Spectrometer->WriteFluxLog();
CString str;
str.Format("By now the flux is %f", flux);
MessageBox(str, "Flux", MB_OK);
}
else
{
MessageBox(TEXT("The spectrometer hasn't been started.\nStart it first,\nthen you can use this function"), "Notice", MB_OK);
}
}
void CDMSpecView::OnConfigurationPlotChangebackground()
{
CColorDialog dlg;
if (dlg.DoModal() == IDOK)
{
m_bkColor = dlg.m_cc.rgbResult;
m_ColumnPlot.SetBackgroundColor(m_bkColor);
}
}
void CDMSpecView::OnConfigurationPlotChangeplotcolor()
{
CColorDialog dlg;
if (dlg.DoModal() == IDOK)
{
m_PlotColor[0] = dlg.m_cc.rgbResult;
m_ColumnPlot.SetPlotColor(m_PlotColor[0]);
}
// Update the legend
UpdateLegend();
}
void CDMSpecView::OnConfigurationPlotChangeplotcolor_Slave()
{
CColorDialog dlg;
if (dlg.DoModal() == IDOK)
{
m_PlotColor[1] = dlg.m_cc.rgbResult;
}
// Update the legend
UpdateLegend();
}
std::unique_ptr<Configuration::CMobileConfiguration> ReadConfiguration()
{
CString cfgFile = g_exePath + TEXT("cfg.xml");
std::unique_ptr<Configuration::CMobileConfiguration> conf;
conf.reset(new Configuration::CMobileConfiguration(cfgFile));
return conf;
}
void CDMSpecView::OnControlStart()
{
if (!s_spectrometerAcquisitionThreadIsRunning)
{
/* Check that the base name does not contain any illegal characters */
CString tmpStr;
this->GetDlgItemText(IDC_BASEEDIT, tmpStr);
if (-1 != tmpStr.FindOneOf("\\/:*?\"<>|"))
{
tmpStr.Format("The base name is not allowed to contain any of the following characters: \\ / : * ? \" < > | Please choose another basename and try again.");
MessageBox(tmpStr, "Error", MB_OK);
return;
}
auto conf = ReadConfiguration();
if (conf->m_spectrometerConnection == conf->CONNECTION_DIRECTORY)
{
OnControlProcessSpectraFromDirectory();
return;
}
// Initialize a new CSpectromber object, this is the one which actually does everything...
m_Spectrometer = CreateSpectrometer(MODE_TRAVERSE, *this, std::move(conf));
// Copy the settings that the user typed in the dialog
char text[100];
memset(text, 0, (size_t)100);
if (UpdateData(TRUE))
{
m_BaseEdit.GetWindowText(text, 255);
m_Spectrometer->SetUserParameters(m_WindSpeed, m_WindDirection, text);
}
// Start the measurement thread
pSpecThread = AfxBeginThread(CollectSpectra, (LPVOID)(m_Spectrometer), THREAD_PRIORITY_NORMAL, 0, 0, NULL);
s_spectrometerAcquisitionThreadIsRunning = true;
m_spectrometerMode = MODE_TRAVERSE;
// If the user wants to see the real-time route then initialize it also
if (m_realTimeRouteGraph.fVisible)
{
m_realTimeRouteGraph.m_spectrometer = m_Spectrometer;
m_realTimeRouteGraph.DrawRouteGraph();
}
if (m_showFitDlg.m_isVisible)
{
m_showFitDlg.m_spectrometer = m_Spectrometer;
m_showFitDlg.DrawFit();
}
}
else
{
MessageBox(TEXT("Spectra are collecting"), "Notice", MB_OK);
}
}
/** Starts the viewing of spectra from the spectrometer,
without saving or evaluating them. */
void CDMSpecView::OnControlViewSpectra()
{
char text[100];
CString tmpStr;
CRect rect;
if (!s_spectrometerAcquisitionThreadIsRunning)
{
CDMSpecDoc* pDoc = GetDocument();
auto conf = ReadConfiguration();
m_Spectrometer = CreateSpectrometer(MODE_VIEW, *this, std::move(conf));
memset(text, 0, (size_t)100);
pSpecThread = AfxBeginThread(CollectSpectra, (LPVOID)(m_Spectrometer), THREAD_PRIORITY_LOWEST, 0, 0, NULL);
s_spectrometerAcquisitionThreadIsRunning = true;
m_spectrometerMode = MODE_VIEW;
// Show the window that makes it possible to change the exposure time
m_specSettingsDlg.m_Spectrometer = m_Spectrometer;
if (!IsWindow(m_specSettingsDlg))
{
m_specSettingsDlg.Create(IDD_SPECTRUM_SETTINGS_DLG, this);
}
m_specSettingsDlg.ShowWindow(SW_SHOW);
// Show the window that makes it possible to change the spectrum-scale
if (!IsWindow(m_specScaleDlg))
{
m_specScaleDlg.Create(IDD_SPECTRUM_SCALE_DLG, this);
}
m_specScaleDlg.SetMainForm(this);
m_specScaleDlg.ShowWindow(SW_SHOW);
m_specScaleDlg.GetWindowRect(rect);
int width = rect.Width();
int cx = GetSystemMetrics(SM_CXSCREEN); // the width of the screen
rect.right = cx - 10;
rect.left = rect.right - width;
m_specScaleDlg.MoveWindow(rect);
if (m_realTimeRouteGraph.fVisible)
{
m_realTimeRouteGraph.m_spectrometer = m_Spectrometer;
m_realTimeRouteGraph.DrawRouteGraph();
}
if (m_showFitDlg.m_isVisible)
{
m_showFitDlg.m_spectrometer = m_Spectrometer;
m_showFitDlg.DrawFit();
}
// Also set the column-plot to only show the measured spectrum
m_ColumnPlot.SetSecondYUnit("");
m_ColumnPlot.SetYUnits("Intensity [%]");
m_ColumnPlot.SetXUnits("Pixel");
m_ColumnPlot.EnableGridLinesX(false);
m_ColumnPlot.SetRange(0.0, 2048, 0, 0.0, 100.0, 0);
}
else
{
MessageBox(TEXT("Spectra are collecting"), "Notice", MB_OK);
}
}
/** Starts the viewing of latest spectra in a directory specified by config file. */
void CDMSpecView::OnControlProcessSpectraFromDirectory()
{
auto conf = ReadConfiguration();
m_Spectrometer = CreateSpectrometer(MODE_DIRECTORY, *this, std::move(conf));
pSpecThread = AfxBeginThread(CollectSpectra, (LPVOID)(m_Spectrometer), THREAD_PRIORITY_LOWEST, 0, 0, NULL);
s_spectrometerAcquisitionThreadIsRunning = true;
m_spectrometerMode = MODE_DIRECTORY;
m_ColumnPlot.SetYUnits("Column [ppmm]");
m_ColumnPlot.SetSecondYUnit("Intensity [%]");
m_ColumnPlot.SetXUnits("Number");
m_ColumnPlot.EnableGridLinesX(false);
m_ColumnPlot.SetBackgroundColor(RGB(0, 0, 0));
m_ColumnPlot.SetGridColor(RGB(255, 255, 255));
m_ColumnPlot.SetPlotColor(m_PlotColor[0]);
m_ColumnPlot.SetRange(0, 200, 1, 0.0, 100.0, 1);
m_ColumnPlot.SetMinimumRangeX(200.0f);
m_ColumnPlot.SetSecondRange(0.0, 200, 0, 0.0, 100.0, 0);
}
void CDMSpecView::OnControlStartWindMeasurement()
{
char text[100];
CString tmpStr;
if (!s_spectrometerAcquisitionThreadIsRunning)
{
/* Check that the base name does not contain any illegal characters */
this->GetDlgItemText(IDC_BASEEDIT, tmpStr);
if (-1 != tmpStr.FindOneOf("\\/:*?\"<>|"))
{
tmpStr.Format("The base name is not allowed to contain any of the following characters: \\ / : * ? \" < > | Please choose another basename and try again.");
MessageBox(tmpStr, "Error", MB_OK);
return;
}
auto conf = ReadConfiguration();
// Initialize a new CSpectrometer object, this is the one which actually does everything...
m_Spectrometer = CreateSpectrometer(MODE_WIND, *this, std::move(conf));
// Copy the settings that the user typed in the dialog
memset(text, 0, (size_t)100);
if (UpdateData(TRUE))
{
m_BaseEdit.GetWindowText(text, 255);
m_Spectrometer->SetUserParameters(m_WindSpeed, m_WindDirection, text);
}
// Start the measurement thread
pSpecThread = AfxBeginThread(CollectSpectra, (LPVOID)(m_Spectrometer), THREAD_PRIORITY_NORMAL, 0, 0, NULL);
s_spectrometerAcquisitionThreadIsRunning = true;
m_spectrometerMode = MODE_WIND;
// If the user wants to see the real-time route then initialize it also
if (m_realTimeRouteGraph.fVisible)
{
m_realTimeRouteGraph.m_spectrometer = m_Spectrometer;
m_realTimeRouteGraph.DrawRouteGraph();
}
if (m_showFitDlg.m_isVisible)
{