forked from NOVACProject/MobileDOAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostFluxDlg.cpp
1565 lines (1307 loc) · 45.7 KB
/
PostFluxDlg.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
// CPostFluxDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DMSpec.h"
#include "PostFluxDlg.h"
#include "Common.h"
#include "RouteDlg.h"
#include "SourceSelectionDlg.h"
#include "WindFieldDlg.h"
#include "ExportEvLogDlg.h"
#include "Flux/Traverse.h"
#include "Common/KMLFileHandler.h"
#include "Dialogs/SelectionDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace ReEvaluation;
using namespace Graph;
extern CString g_exePath; // <-- This is the path to the executable. This is a global variable and should only be changed in DMSpecView.cpp
#define DLGLEFT 93
#define DLGTOP 14
#define DLGRIGHT 673
#define DLGBOTTOM 556
#define ORANGE RGB(255,128,0)
#define YELLOW RGB(255,255,0)
#define GREEN RGB(0,255,0)
/////////////////////////////////////////////////////////////////////////////
// CPostFluxDlg dialog
CPostFluxDlg::CPostFluxDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPostFluxDlg::IDD, pParent)
{
m_srcLat = 0.0;
m_srcLon = 0.0;
m_WindDirect = 0.0;
m_WindSpeed = 10.0;
m_lowIndex = 0;
m_highIndex = 499;
m_flux = new CFlux();
fMovePlot = FALSE;
m_windDirectionAverage = 0;
m_windDirectionMax = 0;
m_GraphToolTip = nullptr;
m_recordNumber = 0;
m_XAxisUnit = 1;
/* by default - show the intensity in the column graph */
m_showIntensity = true;
/* by default - don't show the column errors */
m_showColumnError = false;
m_additionalLogCheck = false;
}
CPostFluxDlg::~CPostFluxDlg(){
if(m_flux != nullptr)
delete(m_flux);
}
void CPostFluxDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPostFluxDlg)
// Selecting the offset
DDX_Control(pDX, IDC_SLIDEROFFSET, m_sliderOffset);
DDX_Text(pDX, IDC_FLUXDOFFSET, m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset);
// Selecting the range
DDX_Control(pDX, IDC_SLIDERTO, m_sliderTo);
DDX_Control(pDX, IDC_SLIDERFROM, m_sliderFrom);
DDX_Text(pDX, IDC_FLUXDFROM, m_lowIndex);
DDV_MinMaxLong(pDX, m_lowIndex, 0, 65535);
DDX_Text(pDX, IDC_FLUXDTO, m_highIndex);
DDV_MinMaxLong(pDX, m_highIndex, 0, 65536);
// The source
DDX_Text(pDX, IDC_FLUXDLAT, m_srcLat);
DDV_MinMaxDouble(pDX, m_srcLat, -90., 90.);
DDX_Text(pDX, IDC_FLUXDLON, m_srcLon);
DDV_MinMaxDouble(pDX, m_srcLon, -180., 180.);
// The wind speed and wind direction
DDX_Text(pDX, IDC_FLUXDWD, m_WindDirect);
DDX_Text(pDX, IDC_FLUXDWS, m_WindSpeed);
DDV_MinMaxDouble(pDX, m_WindSpeed, 0., 1000000.);
// Selecting points based on intensity
DDX_Control(pDX, IDC_SLIDERINTENSITY_LOW, m_intensitySliderLow);
DDX_Control(pDX, IDC_SLIDERINTENSITY_HIGH, m_intensitySliderHigh);
DDX_Control(pDX, IDC_PATH_LIST, m_pathList);
DDX_Control(pDX, IDC_UNIT_COMBO, m_unitSelection);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPostFluxDlg, CDialog)
//{{AFX_MSG_MAP(CPostFluxDlg)
// Opening a evaluation-log
ON_BN_CLICKED(IDC_BTNCHOOSEFILE, OnBtnOpenLogFile)
ON_COMMAND(ID_FILE_OPENLOGFILE, OnBtnOpenLogFile)
ON_COMMAND(ID_RELOAD_LOGFILE, OnReloadLogfile)
ON_BN_CLICKED(IDC_CALC, OnCalculateFlux)
ON_BN_CLICKED(IDC_BTNFOR, OnBtnfor)
ON_BN_CLICKED(IDC_BTNBACK, OnBtnback)
ON_COMMAND(ID_FILE_CREATEADDITIONALLOGFILE, OnChangeCreateAdditionalLogFile)
// Showing the route-graph
ON_BN_CLICKED(IDC_BTNROUTE, OnBtnShowRoute)
// Deleting points
ON_BN_CLICKED(IDC_BTN_DEL_INTENSITY, OnBnClickedBtnDelIntensity)
ON_BN_CLICKED(IDC_BTNDEL, OnBtnDeleteSelected)
// Showing the sources-list
ON_BN_CLICKED(IDC_BTN_SOURCE_LAT, OnBnClickedBtnSourceLat)
ON_BN_CLICKED(IDC_BTN_SOURCE_LONG, OnBnClickedBtnSourceLong)
// Sliders
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDERFROM, OnReleasedcaptureSliderfrom)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDERTO, OnReleasedcaptureSliderto)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDEROFFSET, OnReleasedcaptureSlideroffset)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDERINTENSITY_LOW, OnNMReleasedcaptureSliderintensityLow)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDERINTENSITY_HIGH, OnNMReleasedcaptureSliderintensityHigh)
// Changing the source latitude/longitude
ON_EN_KILLFOCUS(IDC_FLUXDLON, OnChangeSource)
ON_EN_KILLFOCUS(IDC_FLUXDLAT, OnChangeSource)
// Tool-tips
ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)
// Selecting one of the calculated wind-directions
ON_STN_CLICKED(IDC_MAXWD, OnStnClickedMaxwd)
ON_STN_CLICKED(IDC_AVWD, OnStnClickedAvwd)
// Changing the graph to show
ON_LBN_SELCHANGE(IDC_PATH_LIST, OnLbnSelchangePathList)
// Changing what to show
ON_COMMAND(ID_VIEW_SHOWINTENSITY, OnMenuViewShowIntensity)
ON_COMMAND(ID_VIEW_SHOWCOLUMNERROR, OnMenuViewShowColumnError)
ON_COMMAND(ID_VIEW_SHOWVSTIME, OnViewShowVsTime)
ON_COMMAND(ID_VIEW_SHOWVSDISTANCETRAVELLED, OnViewShowVsDistance)
ON_COMMAND(ID_VIEW_SHOWVSMEASUREMENTNUMBER, OnViewShowVsNumber)
ON_COMMAND(ID_COLUMNGRAPH_INCREASELINEWIDTH, OnColumnGraphIncreaseLineWidth)
ON_COMMAND(ID_COLUMNGRAPH_DECREASELINEWIDTH, OnColumnGraphDecreaseLineWidth)
// Re-Evaluating
ON_COMMAND(155, OnReEvaluateThisTraverse)
ON_COMMAND(ID_FILE_CLOSEALLLOGFILES, OnFileCloseAllLogFiles)
ON_BN_CLICKED(IDC_BTN_IMPORT_WINDFIELD, OnImportWindField)
ON_WM_CLOSE()
// Updating the interface
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWINTENSITY, OnUpdateViewShowintensity)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWCOLUMNERROR, OnUpdateViewShowColumnError)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWVSTIME, OnUpdateViewShowVsTime)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWVSDISTANCETRAVELLED, OnUpdateViewShowVsDistance)
ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWVSMEASUREMENTNUMBER, OnUpdateViewShowVsNumber)
ON_UPDATE_COMMAND_UI(ID_RELOAD_LOGFILE, OnUpdateItemReloadLogFile)
ON_UPDATE_COMMAND_UI(IDC_CALC, OnUpdateItemCalculateFlux)
ON_UPDATE_COMMAND_UI(IDC_BTNROUTE, OnUpdateItemShowRouteDlg)
ON_UPDATE_COMMAND_UI(155, OnUpdateItemReEvaluateTraverse)
ON_UPDATE_COMMAND_UI(ID_FILE_CLOSEALLLOGFILES, OnUpdateItemCloseAllLogFiles)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVECOLUMNGRAPH, OnUpdateItemSaveColumnGraph)
ON_UPDATE_COMMAND_UI(ID_FILE_EXPORTTHISLOGFILE, OnUpdateItemExportLogFile)
ON_UPDATE_COMMAND_UI(IDC_BTN_DEL_LOW_INTENSITY, OnUpdateItemDeleteLowIntensity)
ON_UPDATE_COMMAND_UI(IDC_BTNDEL, OnUpdateItemDeleteSelected)
ON_UPDATE_COMMAND_UI(IDC_BTN_IMPORT_WINDFIELD, OnUpdateItemImportWindField)
ON_UPDATE_COMMAND_UI(ID_FILE_CREATEADDITIONALLOGFILE, OnUpdateMenuItemCreateAdditionalLogFile)
ON_UPDATE_COMMAND_UI(ID_FILE_EXPORT_TRAVERSE_TO_KML, OnUpdateItemExportLogFile)
// Saving the graph
ON_COMMAND(ID_FILE_SAVECOLUMNGRAPH, OnFileSaveColumnGraph)
ON_COMMAND(ID_FILE_EXPORTTHISLOGFILE, OnFileExportLogfile)
ON_COMMAND(ID_FILE_EXPORT_TRAVERSE_TO_KML, OnFileExportToKML)
// Updating the interface, notice that this has to be here due to a bug in Microsoft MFC
// http://support.microsoft.com/kb/242577
ON_WM_INITMENUPOPUP()
ON_CBN_SELCHANGE(IDC_UNIT_COMBO, &CPostFluxDlg::ChangeFluxUnit)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPostFluxDlg message handlers
void CPostFluxDlg::OnBtnOpenLogFile(){
static char szFile[16384];
szFile[0] = 0;
OPENFILENAME ofn; // common dialog box structure
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = this->m_hWnd;
ofn.hInstance = AfxGetInstanceHandle();
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "Evaluation Logs\0*.txt";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = nullptr;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = nullptr;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT;
if(GetOpenFileName(&ofn) == TRUE){
CString fileName, filePath;
char *pt = szFile;
if(strlen(pt) == 0){
return;
}
filePath.Format("%s", pt);
pt += strlen(filePath) + 1;
/* only one file chosen */
if(strlen(pt) == 0){
pt = strrchr(filePath.GetBuffer(), '\\');
fileName.Format("%s", pt + 1);
pt[0] = 0;
OpenLogFile(fileName, filePath);
return;
}
/* several files chosen */
while(strlen(pt) != 0){
fileName.Format("%s", pt);
if(OpenLogFile(fileName, filePath))
break;
pt += strlen(pt) + 1;
if(pt[1] == 0)
break;
}
}
}
int CPostFluxDlg::OpenLogFile(CString &filename, CString &filePath){
int fileType, nChannels;
double fileVersion;
memset((void*)columnBuffer,0,sizeof(double)*MAX_TRAVERSELENGTH);
memset((void*)colErrBuffer,0,sizeof(double)*MAX_TRAVERSELENGTH);
memset((void*)intensityBuffer,0,sizeof(double)*MAX_TRAVERSELENGTH);
memset((void*)altitudeBuffer,0,sizeof(double)*MAX_TRAVERSELENGTH);
memset((void*)timeBuffer,0,sizeof(double)*MAX_TRAVERSELENGTH);
memset((void*)latBuffer,0,sizeof(double)*MAX_TRAVERSELENGTH);
memset((void*)lonBuffer,0,sizeof(double)*MAX_TRAVERSELENGTH);
if(UpdateData(TRUE)){
// Read the header of the log file and see if it is an ok file
fileType = m_flux->ReadSettingFile(filename, nChannels, fileVersion);
if(fileType!=1){
MessageBox(TEXT("The file is not evaluation log file with right format.\nPlease choose a right file"),NULL,MB_OK);
return 1;
}
// allocate space for the new channels
for(int k = 1; k <= nChannels; ++k){
CTraverse *tr = new CTraverse();
m_flux->m_traverse.SetAtGrow(m_flux->m_traverseNum + k, tr);
}
// Read the data from the file
int oldNumberOfFilesOpened = m_flux->m_traverseNum;
if(0 == m_flux->ReadLogFile(filePath, filename, nChannels, fileVersion)){
MessageBox(TEXT("That file is empty"));
return 1;
}
// Update the list of opened log files
CString tmpStr;
for(int k = 0; k < nChannels; ++k){
tmpStr.Format(m_flux->m_traverse[oldNumberOfFilesOpened + k]->m_fileName);
m_pathList.AddString(tmpStr);
}
m_pathList.UpdateWidth();
this->m_pathList.SetCurSel(m_flux->m_curTraverse);
}
OnChangeSelectedFile();
return 0;
}
/* when the user has selected the menu item 'Reload This Log File' */
void CPostFluxDlg::OnReloadLogfile()
{
//CString filePath, fileName;
//m_flux->GetCurrentFileName(filePath); // get the full path and filename of the file
//int fileType = m_flux->ReadSettingFile(filePath, m_flux->m_curTraverse);
//if(fileType != 1){
// MessageBox(TEXT("The file is not evaluation log file with right format.\nPlease choose a right file"),NULL,MB_OK);
// return;
//}
//int stringIndex = filePath.ReverseFind('\\');
//if(stringIndex != -1)
// fileName.Format(filePath.Right(strlen(filePath) - stringIndex - 1));
//filePath.Format(filePath.Left(stringIndex));
//if(0 == m_flux->ReadLogFile(filePath, fileName, m_flux->m_curTraverse)){
// MessageBox(TEXT("That file is empty"));
// return;
//}
//OnChangeSelectedFile();
}
/* When the button 'Flux=' has been pressed, calculate the flux */
void CPostFluxDlg::OnCalculateFlux()
{
CString fluxMessage, fluxRangeMessage, strMessage,str1,str2,str3, fileName;
Common* m_common = new Common();
char timetxt[25];
if(!UpdateData(TRUE)){
delete m_common;
return;
}
// 1. Make sure that the range of data points is valid
if(m_lowIndex >= m_highIndex){
MessageBox(TEXT("It should be from one value to a bigger value.\nPleas input again"),
TEXT("Input Error"),MB_OK);
this->SetDlgItemText(IDC_FLUXDFROM,TEXT("0"));
this->SetDlgItemText(IDC_FLUXDTO,TEXT("1"));
m_sliderFrom.SetPos(0);
m_sliderTo.SetPos(1);
delete m_common;
return;
}
m_flux->SetParams(m_WindSpeed,m_WindDirect,m_srcLat,m_srcLon,
m_lowIndex,m_highIndex, m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset);
m_flux->fCreateAdditionalLog = m_additionalLogCheck;
ShowColumn();
m_common->GetDateTimeText(timetxt);
m_flux->SetParams(m_WindSpeed,m_WindDirect,m_srcLat,m_srcLon,
m_lowIndex,m_highIndex, m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset);
// Calculate the flux
double totalFlux = m_flux->GetTotalFlux();
// Calculate the relative error in flux
double fluxError_Low = (m_flux->m_totalFlux_Low - m_flux->m_totalFlux) / m_flux->m_totalFlux;
double fluxError_High = (m_flux->m_totalFlux_High - m_flux->m_totalFlux) / m_flux->m_totalFlux;
// Convert the unit...
CString dispFmt, dispUnit;
double dispFlux, dispLow, dispHigh, dispErrorLow, dispErrorHigh;
if(m_unitSelection.GetCurSel() == UNIT_KGS){
dispFmt = "(%.2f - %.2f) [kg/s] <-> \n (%.1lf - %.1lf) [%%]";
dispUnit = " [kg/s]\n";
dispFlux = abs(totalFlux);
dispLow = abs(m_flux->m_totalFlux_Low);
dispHigh = abs(m_flux->m_totalFlux_High);
dispErrorLow = 100 * fluxError_Low;
dispErrorHigh = 100 * fluxError_High;
}else{
dispFmt = "(%.2f - %.2f) [ton/day] <-> \n (%.1lf - %.1lf) [%%]";
dispUnit = " [ton/day]\n";
dispFlux = abs(totalFlux) * 3.6 * 24;
dispLow = abs(m_flux->m_totalFlux_Low) * 3.6 * 24;
dispHigh = abs(m_flux->m_totalFlux_High) * 3.6 * 24;
dispErrorLow = 100 * fluxError_Low;
dispErrorHigh = 100 * fluxError_High;
}
fluxMessage.Format(_T("%f"), dispFlux);
fluxRangeMessage.Format(dispFmt, dispLow, dispHigh, dispErrorLow, dispErrorHigh);
this->SetDlgItemText(IDC_FLUX_EDIT, fluxMessage);
this->SetDlgItemText(IDC_LABEL_FLUXRANGE, fluxRangeMessage);
fluxMessage.AppendFormat(dispUnit);
// Output the results to the user
strMessage.Format("PlumeWidth=%ld [m]\r\nTraverseLength=%ld [m]",
(long)abs(m_flux->plumeWidth), (long)m_flux->traverseLength);
this->SetDlgItemText(IDC_STATISTICS, strMessage);
// Output the results to file
m_flux->GetCurrentFileName(fileName);
str1.Format("Processed file: \t%s\n", fileName);
str2.Format("Processed time:\t%s\n",timetxt);
if(m_flux->m_traverse[m_flux->m_curTraverse]->m_useWindField)
str3.Format("Wind Speed from imported wind field\nWind Direction from imported wind field\n");
else
str3.Format("Wind Speed=%f\nWind Direction=%f\n", m_WindSpeed,m_WindDirect);
str3.AppendFormat("Source latitude=%f\nSource longitude=%f\nColumn offset=%f\n",
m_srcLat,m_srcLon, m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset);
m_common->WriteLogFile(TEXT("fluxCalculation.txt"),fluxMessage+str1+str2+str3+strMessage+"\n");
delete m_common;
UpdateMobileLog();
}
BOOL CPostFluxDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CRect columnRect;
m_routeDlg = new CRouteDlg();
columnRect =CRect(DLGLEFT,DLGTOP,DLGRIGHT,DLGBOTTOM);
m_ColumnPlot.Create(WS_VISIBLE | WS_CHILD, columnRect, this) ;
m_PlotColor = RGB(255,0, 0);
m_ColumnPlot.SetSecondYUnit(TEXT("Intensity"));
m_ColumnPlot.SetYUnits("Column") ;
m_ColumnPlot.SetXUnits("Number") ;
m_ColumnPlot.SetBackgroundColor(RGB(0, 0, 0)) ;
m_ColumnPlot.SetGridColor(RGB(255,255,255));//(192, 192, 255)) ;
m_ColumnPlot.SetPlotColor(m_PlotColor) ;
m_ColumnPlot.SetRange(0,500,0,-100.0,100.0,1);
m_ColumnPlot.SetSecondRangeY(0, 100, 0);
m_ColumnPlot.m_parentWindow = this;
m_sliderFrom.SetRange(0,499);
m_sliderTo.SetRange(1,500);
m_sliderOffset.SetRange(-100,100);
m_sliderOffset.SetTicFreq(1);
m_sliderFrom.SetPos(0);
m_sliderTo.SetPos(499);
m_sliderOffset.SetPos(100);
m_intensitySliderLow.SetRange(0, 100);// the intensity slider is in percent
m_intensitySliderLow.SetPos(95);/* The intensity slider is upside down */
m_intensitySliderLow.SetTicFreq(25);
m_intensitySliderHigh.SetRange(0, 100);// the intensity slider is in percent
m_intensitySliderHigh.SetPos(5);/* The intensity slider is upside down */
m_intensitySliderHigh.SetTicFreq(25);
this->SetDlgItemText(IDC_INTENSITY_EDIT_LOW, TEXT("400"));
this->SetDlgItemText(IDC_INTENSITY_EDIT_HIGH, TEXT("-400"));
this->SetDlgItemText(IDC_STATISTICS, TEXT(""));
/* make sure that this window handles the messages from the popup-menu in the path list */
m_pathList.m_parentWindow = this;
/* Enable tool tips */
this->EnableToolTips();
m_GraphToolTip = new CToolTipCtrl();
if(!m_GraphToolTip->Create(this)){
TRACE("Unable To create ToolTip\n");
return TRUE;
}
m_GraphToolTip->SetMaxTipWidth(400);
m_GraphToolTip->AddTool(&m_ColumnPlot,"The Column plot. \r\n Red - The column values (scale to the left) \r\n White - the intensities (in % of max) \r\n Green Line - Peak of plume \r\n Yellow Line - Center of mass of plume ");
m_GraphToolTip->Activate(TRUE);
/* set the unit picker */
this->m_unitSelection.SetCurSel(UNIT_TONDAY);
/* Read the mobile log to remember what settings was used the last time */
ReadMobileLog();
UpdateData(FALSE);
// Also move the window to the centre of the screen
CRect rect;
this->GetWindowRect(rect);
int windowWidth = rect.Width();
int windowHeight = rect.Height();
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
rect.left = (screenWidth - windowWidth) / 2;
rect.right = rect.left + windowWidth;
rect.top = (screenHeight - windowHeight) / 2;
rect.bottom= rect.top + windowHeight;
this->MoveWindow(rect);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CPostFluxDlg::OnClose()
{
if(m_flux != nullptr){
delete(m_flux);
m_flux = nullptr;
}
if(m_routeDlg != nullptr){
delete(m_routeDlg);
m_routeDlg = nullptr;
}
if(m_GraphToolTip != nullptr){
delete(m_GraphToolTip);
m_GraphToolTip = nullptr;
}
CDialog::OnClose();
}
void CPostFluxDlg::ShowColumn()
{
Common common;
static double *number = (double *)calloc(MAX_TRAVERSELENGTH, sizeof(double));
static double *distance = (double *)calloc(MAX_TRAVERSELENGTH, sizeof(double));
if(number[1] == 0){
for(int i = 1; i < MAX_TRAVERSELENGTH; ++i)
number[i] = i;
}
if(m_left >= m_right)
return;
// Set the buffer to use for the x-axis data
double *xBuffer = nullptr;
if(m_XAxisUnit == 2){
m_ColumnPlot.SetXAxisNumberFormat(Graph::FORMAT_GENERAL);
m_ColumnPlot.SetXUnits("Distance travelled [km]");
xBuffer = distance;
// fill in the distance buffer
distance[0] = 0.0;
int k;
for(k = 1; k < m_right-m_left+1; ++k){
distance[k] = distance[k-1] + 1e-3 * GPSDistance(latBuffer[k], lonBuffer[k], latBuffer[k-1], lonBuffer[k-1]);
}
if(distance[k-1] < 1.0){
MessageBox("No GPS data found in traverse. Will show measurement against spectrum number");
m_XAxisUnit = 0;
}
}
if(m_XAxisUnit == 1){
if(timeBuffer[1] < 1.0){
MessageBox("No Time data found in traverse. Will show measurement against spectrum number");
m_XAxisUnit = 0;
}else{
m_ColumnPlot.SetXAxisNumberFormat(Graph::FORMAT_TIME);
xBuffer = this->timeBuffer;
m_ColumnPlot.SetXUnits("Time");
}
}
if(m_XAxisUnit == 0){
m_ColumnPlot.SetXAxisNumberFormat(Graph::FORMAT_GENERAL);
m_ColumnPlot.SetXUnits("Number");
xBuffer = number;
}
/* Draw the column */
if(m_showColumnError){
m_ColumnPlot.XYPlot(xBuffer, columnBuffer, NULL, NULL, colErrBuffer,(int)(m_right-m_left+1));
}else{
m_ColumnPlot.XYPlot(xBuffer, columnBuffer, (int)(m_right-m_left+1));
}
/* Draw the plume-center lines */
if(maxBuf[4] < (m_right-m_left+1)){
m_ColumnPlot.DrawLine(VERTICAL, xBuffer[(int)maxBuf[4]], GREEN, STYLE_DASHED);
}
if(avBuf[4] < (m_right-m_left+1)){
m_ColumnPlot.DrawLine(VERTICAL, xBuffer[(int)avBuf[4]], YELLOW, STYLE_DASHED);
}
/* draw the offset line */
m_ColumnPlot.DrawLine(HORIZONTAL, m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset,ORANGE, STYLE_DASHED);
/* draw the intensity */
if(m_showIntensity){
/* the intensity low limit */
int selectedIntensityLow = 100 - m_intensitySliderLow.GetPos();
m_ColumnPlot.DrawLine(HORIZONTAL, selectedIntensityLow, YELLOW, STYLE_DASHED, CGraphCtrl::PLOT_SECOND_AXIS);
/* the intensity high limit */
int selectedIntensityHigh = 100 - m_intensitySliderHigh.GetPos();
m_ColumnPlot.DrawLine(HORIZONTAL, selectedIntensityHigh, YELLOW, STYLE_DASHED, CGraphCtrl::PLOT_SECOND_AXIS);
/* the circles showing the intensity */
m_ColumnPlot.DrawCircles(xBuffer, intensityBuffer, (int)(m_right-m_left+1), CGraphCtrl::PLOT_SECOND_AXIS);
}
/* draw the lower and higher limit */
int from = m_sliderFrom.GetPos();
int to = m_sliderTo.GetPos();
m_ColumnPlot.DrawLine(VERTICAL, xBuffer[from], ORANGE, STYLE_DASHED);
m_ColumnPlot.DrawLine(VERTICAL, xBuffer[to], ORANGE, STYLE_DASHED);
/* shade the area outside of the lower and higher limit */
double shade = 0.7;
if(from != 0){
m_ColumnPlot.ShadeFilledSquare(xBuffer[1], xBuffer[from], m_ColumnPlot.GetYMin()+1, m_ColumnPlot.GetYMax()-1, shade);
}
if(to != (int)(m_right-m_left)){
m_ColumnPlot.ShadeFilledSquare(xBuffer[to], xBuffer[(int)(m_right-m_left)], m_ColumnPlot.GetYMin()+1, m_ColumnPlot.GetYMax()-1, shade);
}
}
/* The user has pressed the '>>' button, show the next part of the traverse */
void CPostFluxDlg::OnBtnfor()
{
if(fMovePlot && (m_right <= m_recordNumber))
{
m_left = m_left+100;
m_right = m_right +100;
ShowColumn();
m_sliderFrom.SetRange(m_left,m_right);
m_sliderTo.SetRange(m_left,m_right);
}
}
/* The user has pressed the '<<' button, show the previous part of the traverse */
void CPostFluxDlg::OnBtnback()
{
if(fMovePlot&&(m_left>=100))//0))
{
m_left = m_left-100;
m_right = m_right-100;
if(m_left >= 0)
ShowColumn();
m_sliderFrom.SetRange(m_left,m_right);
m_sliderTo.SetRange(m_left,m_right);
}
}
void CPostFluxDlg::ShowPlumeCenter()
{
CString strMaxwd,strAvwd;
m_flux->GetPlumeCenter(m_srcLat, m_srcLon, maxBuf, avBuf);
strMaxwd.Format("Max Center\t%.2f", maxBuf[3]);
strAvwd.Format("Average Center\t%.2f", avBuf[3]);
//if(UpdateData(TRUE))
{
this->SetDlgItemText(IDC_MAXWD, strMaxwd);
this->SetDlgItemText(IDC_AVWD, strAvwd);
}
m_windDirectionAverage = avBuf[3];
m_windDirectionMax = maxBuf[3];
}
void CPostFluxDlg::OnReleasedcaptureSliderfrom(NMHDR* pNMHDR, LRESULT* pResult)
{
CString str;
m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset = m_ColumnPlot.GetYMax() - m_sliderOffset.GetPos() + m_ColumnPlot.GetYMin();
ShowColumn();
str.Format("%d",m_sliderFrom.GetPos());
this->SetDlgItemText(IDC_FLUXDFROM,str);
*pResult = 0;
}
void CPostFluxDlg::OnReleasedcaptureSliderto(NMHDR* pNMHDR, LRESULT* pResult)
{
CString str;
m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset = m_ColumnPlot.GetYMax() - m_sliderOffset.GetPos() + m_ColumnPlot.GetYMin();
ShowColumn();
m_lowIndex = m_sliderFrom.GetPos();
m_highIndex = m_sliderTo.GetPos();
UpdateData(FALSE);
*pResult = 0;
}
void CPostFluxDlg::OnReleasedcaptureSlideroffset(NMHDR* pNMHDR, LRESULT* pResult)
{
if(m_flux->m_traverseNum == 0)
return;
m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset = m_ColumnPlot.GetYMax() - m_sliderOffset.GetPos() + m_ColumnPlot.GetYMin();
ShowColumn(); /* draw the graph */
UpdateData(FALSE);
*pResult = 0;
}
/**Delete the bad points which are not needed in evaluation
*
*/
void CPostFluxDlg::OnBtnDeleteSelected()
{
if(UpdateData(TRUE))
{
/* if the user has marked more than 50 points for deletion, make an question of
this really is what the user wants to do */
if(m_highIndex - m_lowIndex > 50){
CString message;
message.Format("This will delete %ld points from the traverse. Continue?", m_highIndex - m_lowIndex);
int answer = MessageBox(message, "Delete", MB_YESNO);
if(IDNO == answer)
return;
}
// Delete the points
m_flux->m_traverse[m_flux->m_curTraverse]->DeletePoints(m_lowIndex, m_highIndex);
m_recordNumber = m_flux->GetColumn(columnBuffer);
m_flux->GetColumnError(colErrBuffer);
m_flux->GetIntensity(intensityBuffer);
m_flux->GetAltitude(altitudeBuffer);
m_flux->GetTime(timeBuffer);
m_flux->GetLat(latBuffer);
m_flux->GetLon(lonBuffer);
// Convert the intensities to saturation-ratios
double dynRange_inv = 100.0 / (double)m_flux->GetDynamicRange();
for(int k = 0; k < m_recordNumber; ++k){
intensityBuffer[k] = intensityBuffer[k] * dynRange_inv;
}
fMovePlot = TRUE;
if(m_recordNumber < MAX_TRAVERSE_SHOWN){
m_left = 0;
m_right = m_recordNumber-1;
}else{
m_left = 0;
m_right = MAX_TRAVERSE_SHOWN-1;
}
m_sliderFrom.SetRange(0,m_right-1);
m_sliderTo.SetRange(0,m_right);
m_sliderFrom.SetPos(0);
m_sliderTo.SetPos(m_right);
ShowPlumeCenter();
ShowColumn();
m_sliderOffset.SetRange((int)m_ColumnPlot.GetYMin(), (int)m_ColumnPlot.GetYMax());
m_sliderOffset.SetPos((int)(m_ColumnPlot.GetYMax() + m_ColumnPlot.GetYMin() - m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset));
m_lowIndex = 0;
m_highIndex = m_right;
m_flux->m_traverse[m_flux->m_curTraverse]->CalculateOffset();
UpdateData(FALSE);
}
}
void CPostFluxDlg::OnBnClickedBtnDelIntensity()
{
long nDeleted;
if(UpdateData(TRUE)){
// delete the points
long dynRange = m_flux->GetDynamicRange();
long intensityLimitLow = (long)(dynRange * (100.0 - m_intensitySliderLow.GetPos()) / 100.0);
nDeleted = m_flux->m_traverse[m_flux->m_curTraverse]->DeleteLowIntensityPoints(intensityLimitLow);
long intensityLimitHigh = (long)(dynRange * (100.0 - m_intensitySliderHigh.GetPos()) / 100.0);
nDeleted += m_flux->m_traverse[m_flux->m_curTraverse]->DeleteHighIntensityPoints(intensityLimitHigh);
m_recordNumber = m_flux->GetColumn(columnBuffer);
m_flux->GetColumnError(colErrBuffer);
m_flux->GetIntensity(intensityBuffer);
m_flux->GetAltitude(altitudeBuffer);
m_flux->GetTime(timeBuffer);
m_flux->GetLat(latBuffer);
m_flux->GetLon(lonBuffer);
// Convert the intensities to saturation-ratios
double dynRange_inv = 100.0 / (double)m_flux->GetDynamicRange();
for(int k = 0; k < m_recordNumber; ++k){
intensityBuffer[k] = intensityBuffer[k] * dynRange_inv;
}
fMovePlot = TRUE;
if(m_recordNumber < MAX_TRAVERSE_SHOWN){
m_left = 0;
m_right = m_recordNumber-1;
}else{
m_left = 0;
m_right = MAX_TRAVERSE_SHOWN-1;
}
m_sliderFrom.SetRange(0,m_right-1);
m_sliderTo.SetRange(0,m_right);
m_sliderFrom.SetPos(0);
m_sliderTo.SetPos(m_right);
ShowPlumeCenter();
ShowColumn();
m_sliderOffset.SetRange((int)m_ColumnPlot.GetYMin(), (int)m_ColumnPlot.GetYMax());
m_sliderOffset.SetPos((int)(m_ColumnPlot.GetYMax() + m_ColumnPlot.GetYMin() - m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset));
m_lowIndex = 0;
m_highIndex = m_right;
m_flux->m_traverse[m_flux->m_curTraverse]->m_Offset = 0;
UpdateData(FALSE);
CString tmpStr;
tmpStr.Format("Deleted %ld points", nDeleted);
MessageBox(tmpStr);
}
}
void CPostFluxDlg::OnBtnShowRoute(){
UpdateData(TRUE);
m_routeDlg->m_flux = this->m_flux;
m_routeDlg->m_srcLat = m_srcLat;
m_routeDlg->m_srcLon = m_srcLon;
m_routeDlg->m_initalSelection = m_pathList.GetCurSel();
m_routeDlg->DoModal();
// The dialog has closed. Check if the user has changed the lat and long
// of the source, if so then change also in this window
if(m_srcLat != m_routeDlg->m_srcLat || m_srcLon != m_routeDlg->m_srcLon){
m_srcLat = m_routeDlg->m_srcLat;
m_srcLon = m_routeDlg->m_srcLon;
UpdateData(FALSE);
OnChangeSource();
}
}
// if the user clicked the 'View'->'Show Intensity' menu item
void CPostFluxDlg::OnMenuViewShowIntensity()
{
m_showIntensity = !m_showIntensity;
ShowColumn();
}
// if the user clicked the 'View'->'Show ColumnError' menu item
void CPostFluxDlg::OnMenuViewShowColumnError()
{
m_showColumnError = !m_showColumnError;
ShowColumn();
}
void CPostFluxDlg::OnNMReleasedcaptureSliderintensityLow(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
CString tmpStr;
tmpStr.Format("%ld", 100 - m_intensitySliderLow.GetPos());
this->SetDlgItemText(IDC_INTENSITY_EDIT_LOW, tmpStr);
ShowColumn();
}
void CPostFluxDlg::OnNMReleasedcaptureSliderintensityHigh(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
CString tmpStr;
tmpStr.Format("%ld", 100 - m_intensitySliderHigh.GetPos());
this->SetDlgItemText(IDC_INTENSITY_EDIT_HIGH, tmpStr);
ShowColumn();
}
/* Read the mobile log - the 'memory' of the program */
void CPostFluxDlg::ReadMobileLog(){
double lat, lon;
char txt[256];
char *pt = 0;
/* Find the last flux center used */
FILE *f = fopen(g_exePath + "MobileLog.txt", "r");
if(0 != f){
while(fgets(txt, sizeof(txt)-1, f)){
if(pt = strstr(txt,"FLUXCENTER=")){
pt = strstr(txt,"=");
sscanf(&pt[1],"%lf\t%lf",&lat, &lon);
if(-180 <= lat && 180 >= lat && -180 <= lon && 180 >= lon){
m_srcLat = lat;
m_srcLon = lon;
}
break;
}
}
}else{
return;
}
fclose(f);
return;
}
void CPostFluxDlg::UpdateMobileLog(){
fpos_t filePos;
char txt[256];
char *pt = 0;
/* Open the mobile log file */
FILE *f = fopen(g_exePath + "MobileLog.txt", "r+");
if(0 == f){
/* File might not exist */
f = fopen(g_exePath + "MobileLog.txt", "w");
if(0 == f){
/* File cannot be opened */
return;
}else{
fprintf(f, "FLUXCENTER=%lf\t%lf\n", m_srcLat, m_srcLon);
fclose(f);
return;
}
}else{
if(0 != fgetpos(f, &filePos)){
fclose(f);
return;
}
while(fgets(txt, sizeof(txt)-1, f)){
if(pt = strstr(txt,"FLUXCENTER=")){
/* Update */
fflush(f);
fsetpos(f, &filePos);
fprintf(f, "FLUXCENTER=%lf\t%lf\n", m_srcLat, m_srcLon);
fclose(f);
return;
}else{
/* Save the current position before we continue */
if(0 != fgetpos(f, &filePos)){
fclose(f);
return;
}
}
}
/* there was no note about the flux center in the mobile log */
fprintf(f, "FLUXCENTER=%lf\t%lf\n", m_srcLat, m_srcLon);
fclose(f);
return;
}
}
void CPostFluxDlg::OnStnClickedMaxwd()
{
m_WindDirect = m_windDirectionMax;
UpdateData(FALSE);
}
void CPostFluxDlg::OnStnClickedAvwd()
{
m_WindDirect = m_windDirectionAverage;
UpdateData(FALSE);
}
BOOL CPostFluxDlg::OnToolTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult ){
static char string[512];
TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
UINT_PTR nID = pNMHDR->idFrom;
nID = ::GetDlgCtrlID((HWND)nID);
int ok = 0;
if(nID == IDC_BTN_DEL_INTENSITY){
ok = sprintf(string, "Deletes all spectra with intensity below or above the values indicated by the Intensity sliders above.");
}
if(nID == IDC_BTNDEL){
ok = sprintf(string, "Deletes all spectra in the range indicated by the 'From' and 'To' sliders above.");
}
if(nID == IDC_CALC){
ok = sprintf(string, "Calculates the flux using the spectrum in the selected range and the values of wind speed and wind direction given above");
}
if(nID == IDC_SLIDERINTENSITY_LOW){
ok = sprintf(string, "Intensity slider low: exact value of this slider is shown below. ");
}
if (nID == IDC_SLIDERINTENSITY_HIGH) {
ok = sprintf(string, "Intensity slider high: exact value of this slider is shown below. ");
}
if(nID == IDC_INTENSITY_EDIT_LOW){
ok = sprintf(string, "Value of the Intensity (low) slider.");
}
if (nID == IDC_INTENSITY_EDIT_HIGH) {
ok = sprintf(string, "Value of the Intensity (high) slider.");
}
if(nID == IDC_SLIDEROFFSET){
ok = sprintf(string, "Offset Slider. Should be set so that the column values outside of the plume varies around zero. Exact value shown below.");
}
if(nID == IDC_INTENSITY_CHECK){