-
Notifications
You must be signed in to change notification settings - Fork 7
/
CCalibrateReferencesDialog.cpp
398 lines (335 loc) · 13.2 KB
/
CCalibrateReferencesDialog.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
// CCalibrateReferenes.cpp : implementation file
//
#include "StdAfx.h"
#include "CCalibrateReferencesDialog.h"
#include "afxdialogex.h"
#include "resource.h"
#include <fstream>
#include "Common.h"
#include <SpectralEvaluation/DialogControllers/ReferenceCreationController.h>
#include "OpenInstrumentCalibrationDialog.h"
#include <SpectralEvaluation/Evaluation/CrossSectionData.h>
#include <SpectralEvaluation/File/File.h>
#include <SpectralEvaluation/File/XmlUtil.h>
#include <SpectralEvaluation/Calibration/StandardCrossSectionSetup.h>
#include "CCreateStandardReferencesDialog.h"
#include <sstream>
// CCalibrateReferenes dialog
IMPLEMENT_DYNAMIC(CCalibrateReferencesDialog, CPropertyPage)
CCalibrateReferencesDialog::CCalibrateReferencesDialog(CWnd* pParent /*=nullptr*/)
: CPropertyPage(IDD_CALIBRATE_REFERENCES)
, m_highPassFilterReference(TRUE)
, m_calibrationFile(_T(""))
, m_inputInVacuum(TRUE)
, m_standardCrossSections(nullptr)
{
m_controller = new ReferenceCreationController();
}
CCalibrateReferencesDialog::~CCalibrateReferencesDialog()
{
delete m_controller;
delete m_standardCrossSections;
}
BOOL CCalibrateReferencesDialog::OnInitDialog() {
CPropertyPage::OnInitDialog();
CRect rect;
int margin = 2;
m_graphHolder.GetWindowRect(&rect);
rect.bottom -= rect.top + margin;
rect.right -= rect.left + margin;
rect.top = margin + 7;
rect.left = margin;
m_graph.Create(WS_VISIBLE | WS_CHILD, rect, &m_graphHolder);
m_graph.SetRange(0, 500, 1, -100.0, 100.0, 1);
m_graph.SetYUnits("");
m_graph.SetXUnits("Wavelength");
m_graph.SetBackgroundColor(RGB(0, 0, 0));
m_graph.SetGridColor(RGB(255, 255, 255));
m_graph.SetPlotColor(RGB(255, 0, 0));
m_graph.CleanPlot();
m_unitCombo.SetCurSel(0);
// First load the default setup.
LoadDefaultSetup();
// Then load any references the user may have added.
LoadLastSetup();
return TRUE; // return TRUE unless you set the focus to a control
}
void CCalibrateReferencesDialog::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO_HIGH_RES_CROSSSECTION, m_crossSectionsCombo);
DDX_Check(pDX, IDC_CHECK_HIGH_PASS_FILTER, m_highPassFilterReference);
DDX_Text(pDX, IDC_EDIT_CALIBRATION, m_calibrationFile);
DDX_Control(pDX, IDC_STATIC_GRAPH_HOLDER2, m_graphHolder);
DDX_Check(pDX, IDC_CHECK_INPUT_IN_VACUUM, m_inputInVacuum);
DDX_Control(pDX, IDC_BUTTON_SAVE, m_saveButton);
DDX_Control(pDX, IDC_BUTTON_CREATE_STANDARD_REFERENCES, m_createStandardReferencesButton);
DDX_Control(pDX, IDC_COMBO_REFERENCE_UNIT, m_unitCombo);
}
BEGIN_MESSAGE_MAP(CCalibrateReferencesDialog, CPropertyPage)
ON_BN_CLICKED(IDC_BUTTON_BROWSE_SOLAR_SPECTRUM, &CCalibrateReferencesDialog::OnClickedBrowseCrossSection)
ON_CBN_SELCHANGE(IDC_COMBO_HIGH_RES_CROSSSECTION, &CCalibrateReferencesDialog::OnConvolutionOptionChanged)
ON_CBN_SELCHANGE(IDC_COMBO_REFERENCE_UNIT, &CCalibrateReferencesDialog::OnConvolutionOptionChanged)
ON_BN_CLICKED(IDC_CHECK_HIGH_PASS_FILTER, &CCalibrateReferencesDialog::OnConvolutionOptionChanged)
ON_BN_CLICKED(IDC_CHECK_INPUT_IN_VACUUM, &CCalibrateReferencesDialog::OnConvolutionOptionChanged)
ON_BN_CLICKED(IDC_BUTTON_RUN_CREATE_REFERENCE, &CCalibrateReferencesDialog::OnClickedButtonRunCreateReference)
ON_BN_CLICKED(IDC_BUTTON_SAVE, &CCalibrateReferencesDialog::OnClickedButtonSave)
ON_BN_CLICKED(IDC_BUTTON_SELECT_CALIBRATION, &CCalibrateReferencesDialog::OnClickedButtonSelectCalibration)
ON_BN_CLICKED(IDC_BUTTON_CREATE_STANDARD_REFERENCES, &CCalibrateReferencesDialog::OnClickedButtonCreateStandardReferences)
END_MESSAGE_MAP()
std::string CCalibrateReferencesDialog::SetupFilePath()
{
Common common;
common.GetExePath();
CString path;
path.Format("%sCalibrateReferencesDlg.config", common.m_exePath);
return std::string(path);
}
void CCalibrateReferencesDialog::SaveSetup()
{
try
{
std::ofstream dst(SetupFilePath(), std::ios::out);
dst << "<CalibrateReferencesDlg>" << std::endl;
for (int ii = 0; ii < m_crossSectionsCombo.GetCount(); ++ii)
{
CString filePath;
m_crossSectionsCombo.GetLBText(ii, filePath);
if (filePath.Find(" [Standard]") < 0)
{
dst << "\t<CrossSection>" << filePath << "</CrossSection>" << std::endl;
}
}
dst << "</CalibrateReferencesDlg>" << std::endl;
}
catch (std::exception&)
{
}
}
void CCalibrateReferencesDialog::LoadLastSetup()
{
try
{
// Super basic xml parsing
std::ifstream file(SetupFilePath(), std::ios::in);
std::string line;
while (std::getline(file, line))
{
if (line.find("CrossSection") != std::string::npos)
{
auto str = novac::ParseXmlString("CrossSection", line);
CString crossSectionFile(str.c_str());
if (IsExistingFile(crossSectionFile))
{
m_crossSectionsCombo.AddString(crossSectionFile);
}
}
}
if (m_crossSectionsCombo.GetCount() > 0)
{
m_crossSectionsCombo.SetCurSel(0);
}
}
catch (std::exception&)
{
}
}
void CCalibrateReferencesDialog::LoadDefaultSetup()
{
Common common;
common.GetExePath();
m_crossSectionsCombo.Clear();
std::string exePath = common.m_exePath;
m_standardCrossSections = new novac::StandardCrossSectionSetup{ exePath };
auto allCrossSections = m_standardCrossSections->ListReferences();
for (const std::string& crossSectionFile : allCrossSections)
{
CString fileName{ crossSectionFile.c_str() };
fileName.Append(" [Standard]");
m_crossSectionsCombo.AddString(fileName);
}
if (m_crossSectionsCombo.GetCount() > 0)
{
m_crossSectionsCombo.SetCurSel(0);
}
}
// CCalibrateReferenes message handlers
void CCalibrateReferencesDialog::OnClickedBrowseCrossSection()
{
CString crossSectionFile;
if (!Common::BrowseForFile("Spectrum Files\0*.txt;*.xs\0", crossSectionFile))
{
return;
}
int elementIdx = m_crossSectionsCombo.AddString(crossSectionFile);
m_crossSectionsCombo.SetCurSel(elementIdx);
UpdateData(FALSE); // Update the UI
UpdateReference();
}
void CCalibrateReferencesDialog::OnConvolutionOptionChanged()
{
UpdateReference();
}
void CCalibrateReferencesDialog::OnClickedButtonRunCreateReference()
{
UpdateReference();
}
void CCalibrateReferencesDialog::OnClickedButtonSave()
{
try
{
CString destinationFileName = L"";
if (Common::BrowseForFile_SaveAs("Reference Files\0*.txt\0", destinationFileName))
{
std::string dstFileName = novac::EnsureFilenameHasSuffix(std::string(destinationFileName), "txt");
novac::SaveCrossSectionFile(dstFileName, *(m_controller->m_resultingCrossSection));
}
}
catch (std::exception& e)
{
MessageBox(e.what(), "Failed to save cross section file", MB_OK);
}
}
void CCalibrateReferencesDialog::UpdateReference()
{
try
{
UpdateData(TRUE); // get the selections from the user interface
if (m_calibrationFile.IsEmpty() ||
m_crossSectionsCombo.GetCurSel() < 0)
{
return;
}
if (!IsExistingFile(m_calibrationFile))
{
MessageBox("Please select an existing wavelength calibration file", "Missing input", MB_OK);
return;
}
int selectedReferenceIdx = m_crossSectionsCombo.GetCurSel();
CString crossSectionFilePath;
bool isPseudoAbsorber = false;
if (m_standardCrossSections != nullptr && selectedReferenceIdx < static_cast<int>(m_standardCrossSections->NumberOfReferences()))
{
const std::string path = m_standardCrossSections->ReferenceFileName(selectedReferenceIdx);
crossSectionFilePath.Format("%s", path.c_str());
isPseudoAbsorber = m_standardCrossSections->IsAdditionalAbsorber(selectedReferenceIdx);
}
else
{
m_crossSectionsCombo.GetLBText(selectedReferenceIdx, crossSectionFilePath);
}
// Make sure that we can't change the unit of a pseudo absorber
m_unitCombo.EnableWindow(isPseudoAbsorber ? 0 : 1);
if (!IsExistingFile(crossSectionFilePath))
{
MessageBox("Please select an existing high resolved cross section file", "Missing input", MB_OK);
return;
}
const auto calibration = m_controller->ReadCalibration((LPCSTR)m_calibrationFile, (LPCSTR)m_instrumentLineshapeFile);
m_controller->m_highPassFilter = m_highPassFilterReference;
m_controller->m_convertToAir = m_inputInVacuum;
m_controller->m_unitSelection = m_unitCombo.GetCurSel();
m_controller->m_isPseudoAbsorber = isPseudoAbsorber;
m_controller->m_highResolutionCrossSection = crossSectionFilePath;
m_controller->ConvolveReference(calibration);
UpdateGraph();
SaveSetup();
m_saveButton.EnableWindow(TRUE);
}
catch (std::exception& e)
{
MessageBox(e.what(), "Failed to convolve reference.", MB_OK);
UpdateGraph();
m_saveButton.EnableWindow(FALSE);
m_createStandardReferencesButton.EnableWindow(FALSE);
}
}
void CCalibrateReferencesDialog::UpdateGraph()
{
m_graph.CleanPlot();
// the reference
if (m_controller->m_resultingCrossSection != nullptr && m_controller->m_resultingCrossSection->GetSize() > 0)
{
m_graph.SetPlotColor(RGB(255, 0, 0));
m_graph.XYPlot(
m_controller->m_resultingCrossSection->m_waveLength.data(),
m_controller->m_resultingCrossSection->m_crossSection.data(),
static_cast<int>(m_controller->m_resultingCrossSection->GetSize()),
Graph::CGraphCtrl::PLOT_CONNECTED);
}
}
void CCalibrateReferencesDialog::OnClickedButtonSelectCalibration()
{
OpenInstrumentCalibrationDialog dlg;
dlg.m_state.initialCalibrationFile = m_calibrationFile;
dlg.m_state.instrumentLineshapeFile = m_instrumentLineshapeFile;
if (IDOK == dlg.DoModal())
{
m_calibrationFile = dlg.m_state.initialCalibrationFile;
m_instrumentLineshapeFile = dlg.m_state.instrumentLineshapeFile;
UpdateData(FALSE);
UpdateReference();
if (m_standardCrossSections != nullptr &&
m_standardCrossSections->NumberOfReferences() > 0)
{
m_createStandardReferencesButton.EnableWindow(TRUE);
}
}
}
void CCalibrateReferencesDialog::OnClickedButtonCreateStandardReferences()
{
if (m_standardCrossSections == nullptr || m_standardCrossSections->NumberOfReferences() == 0)
{
MessageBox("Failed to load standard references", "Missing data", MB_OK);
m_createStandardReferencesButton.EnableWindow(FALSE);
return;
}
CCreateStandardReferencesDialog userInputDialog;
userInputDialog.m_standardCrossSections = m_standardCrossSections;
userInputDialog.m_fileNameFilteringInfix = (m_highPassFilterReference) ? "_HP500" : "";
if (m_unitCombo.GetCurSel() == 0)
{
userInputDialog.m_fileNameFilteringInfix = userInputDialog.m_fileNameFilteringInfix + "_PPMM";
}
if (IDOK != userInputDialog.DoModal())
{
return;
}
try
{
const auto calibration = m_controller->ReadCalibration((LPCSTR)m_calibrationFile, (LPCSTR)m_instrumentLineshapeFile);
m_controller->m_highPassFilter = m_highPassFilterReference;
m_controller->m_unitSelection = m_unitCombo.GetCurSel();
// First the ordinary references
for (size_t ii = 0; ii < m_standardCrossSections->NumberOfReferences(); ++ii)
{
// Do the convolution
m_controller->m_convertToAir = m_standardCrossSections->IsReferenceInVacuum(ii);
m_controller->m_highResolutionCrossSection = m_standardCrossSections->ReferenceFileName(ii);
m_controller->m_isPseudoAbsorber = m_standardCrossSections->IsAdditionalAbsorber(ii);
m_controller->ConvolveReference(calibration);
// Save the result
std::string dstFileName = userInputDialog.ReferenceName(ii, !m_standardCrossSections->IsAdditionalAbsorber(ii));
novac::SaveCrossSectionFile(dstFileName, *(m_controller->m_resultingCrossSection));
}
// Save the Fraunhofer reference as well
{
// Do the convolution
m_controller->m_highPassFilter = false;
m_controller->m_convertToAir = false;
m_controller->m_highResolutionCrossSection = m_standardCrossSections->FraunhoferReferenceFileName();
m_controller->m_isPseudoAbsorber = true;
m_controller->ConvolveReference(calibration);
// Save the result
std::string dstFileName = userInputDialog.FraunhoferReferenceName();
novac::SaveCrossSectionFile(dstFileName, *(m_controller->m_resultingCrossSection));
}
}
catch (std::exception& e)
{
MessageBox(e.what(), "Failed to convolve reference.", MB_OK);
UpdateGraph();
m_saveButton.EnableWindow(FALSE);
m_createStandardReferencesButton.EnableWindow(FALSE);
}
}