-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCCalibrateInstrumentLineShape.cpp
501 lines (433 loc) · 18.7 KB
/
CCalibrateInstrumentLineShape.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
// CCalibrateInstrumentLineShape.cpp : implementation file
//
#include "stdafx.h"
#include "CCalibrateInstrumentLineShape.h"
#include "afxdialogex.h"
#include "resource.h"
#include "Common.h"
#include <SpectralEvaluation/DialogControllers/InstrumentLineshapeCalibrationController.h>
#include <SpectralEvaluation/File/File.h>
#include <SpectralEvaluation/Calibration/InstrumentCalibration.h>
#include <SpectralEvaluation/VectorUtils.h>
#include <algorithm>
#include <sstream>
#undef min
#undef max
// CCalibrateInstrumentLineShape dialog
IMPLEMENT_DYNAMIC(CCalibrateInstrumentLineShape, CPropertyPage)
CCalibrateInstrumentLineShape::CCalibrateInstrumentLineShape(CWnd* pParent /*=nullptr*/)
: CPropertyPage(IDD_CALIBRATE_LINESHAPE_DIALOG)
, m_inputSpectrum(_T(""))
, m_autoDetermineCalibration(FALSE)
{
m_controller = new InstrumentLineshapeCalibrationController();
}
CCalibrateInstrumentLineShape::~CCalibrateInstrumentLineShape()
{
delete m_controller;
}
BOOL CCalibrateInstrumentLineShape::OnInitDialog() {
CPropertyPage::OnInitDialog();
CRect mainGraphRect;
int margin = 2;
m_graphHolder.GetWindowRect(&mainGraphRect);
mainGraphRect.bottom -= mainGraphRect.top + margin;
mainGraphRect.right -= mainGraphRect.left + margin;
mainGraphRect.top = margin + 7;
mainGraphRect.left = margin;
m_spectrumPlot.Create(WS_VISIBLE | WS_CHILD, mainGraphRect, &m_graphHolder);
m_spectrumPlot.SetRange(0, 500, 1, -100.0, 100.0, 1);
m_spectrumPlot.SetYUnits("Intensity");
m_spectrumPlot.SetXUnits("Wavelength");
m_spectrumPlot.SetBackgroundColor(RGB(0, 0, 0));
m_spectrumPlot.SetGridColor(RGB(255, 255, 255));
m_spectrumPlot.SetPlotColor(RGB(255, 0, 0));
m_spectrumPlot.CleanPlot();
CRect minimapRect;
margin = 2;
m_minimapHolder.GetWindowRect(&minimapRect);
minimapRect.bottom -= minimapRect.top + margin;
minimapRect.right -= minimapRect.left + margin;
minimapRect.top = margin;
minimapRect.left = margin;
m_minimapPlot.HideXScale();
m_minimapPlot.HideYScale();
m_minimapPlot.Create(WS_VISIBLE | WS_CHILD, minimapRect, &m_minimapHolder);
m_minimapPlot.SetRange(0, 500, 1, -100.0, 100.0, 1);
m_minimapPlot.SetBackgroundColor(RGB(0, 0, 0));
m_minimapPlot.SetGridColor(RGB(255, 255, 255));
m_minimapPlot.SetPlotColor(RGB(255, 0, 0));
m_minimapPlot.EnableGridLinesX(false);
m_minimapPlot.EnableGridLinesY(false);
m_minimapPlot.CleanPlot();
m_labelSpectrumContainsNoWavelengthCalibration.ShowWindow(SW_HIDE);
m_saveButton.EnableWindow(FALSE); // disable the save button until the user has selected the emission line to save
UpdateExplanation();
UpdateFitResultLabel();
return TRUE; // return TRUE unless you set the focus to a control
}
void CCalibrateInstrumentLineShape::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_SPECTRUM, m_inputSpectrum);
DDX_Text(pDX, IDC_EDIT_SPECTRUM_DARK, m_darkSpectrum);
DDX_Control(pDX, IDC_LIST_FOUND_PEAKS, m_peaksList);
DDX_Control(pDX, IDC_STATIC_GRAPH_HOLDER, m_graphHolder);
DDX_Radio(pDX, IDC_RADIO_FIT_GAUSSIAN, m_fitFunctionOption);
DDX_Control(pDX, IDC_LABEL_MISSING_CALIBRATION, m_labelSpectrumContainsNoWavelengthCalibration);
DDX_Check(pDX, IDC_CALIBRATION_FROM_MERCURY_LINES, m_autoDetermineCalibration);
DDX_Control(pDX, IDC_STATIC_MINIMAP_HOLDER, m_minimapHolder);
DDX_Control(pDX, IDC_BUTTON_SAVE, m_saveButton);
DDX_Control(pDX, IDC_LABEL_SAVE_EXPLANATION, m_labelSaveExplanation);
DDX_Control(pDX, IDC_LABEL_FIT_RESULT, m_labelFitResult);
}
BEGIN_MESSAGE_MAP(CCalibrateInstrumentLineShape, CPropertyPage)
ON_BN_CLICKED(IDC_BUTTON_BROWSE_SPECTRUM, &CCalibrateInstrumentLineShape::OnBnClickedButtonBrowseSpectrum)
ON_BN_CLICKED(IDC_BUTTON_BROWSE_SPECTRUM_DARK, &CCalibrateInstrumentLineShape::OnBnClickedBrowseSpectrumDark)
ON_LBN_SELCHANGE(IDC_LIST_FOUND_PEAKS, &CCalibrateInstrumentLineShape::OnLbnSelchangeFoundPeak)
ON_BN_CLICKED(IDC_RADIO_FIT_GAUSSIAN, &CCalibrateInstrumentLineShape::OnBnClickedRadioFitGaussian)
ON_BN_CLICKED(IDC_RADIO_FIT_SUPER_GAUSSIAN, &CCalibrateInstrumentLineShape::OnBnClickedRadioFitGaussian)
ON_BN_CLICKED(IDC_RADIO_FIT_NOTHING, &CCalibrateInstrumentLineShape::OnBnClickedRadioFitGaussian)
ON_BN_CLICKED(IDC_BUTTON_SAVE, &CCalibrateInstrumentLineShape::OnBnClickedSave)
ON_BN_CLICKED(IDC_CALIBRATION_FROM_MERCURY_LINES, &CCalibrateInstrumentLineShape::OnBnClickedToggleCalibrationFromMercuryLines)
END_MESSAGE_MAP()
// CCalibrateInstrumentLineShape message handlers
void CCalibrateInstrumentLineShape::OnBnClickedButtonBrowseSpectrum()
{
if (!Common::BrowseForFile("Spectrum Files\0*.std;*.txt\0All Files\0*.*\0", m_inputSpectrum))
{
return;
}
m_controller->m_inputSpectrumPath = m_inputSpectrum;
UpdateLineShape();
}
void CCalibrateInstrumentLineShape::OnBnClickedBrowseSpectrumDark()
{
if (!Common::BrowseForFile("Spectrum Files\0*.std;*.txt\0All Files\0*.*\0", m_darkSpectrum))
{
return;
}
m_controller->m_darkSpectrumPath = m_darkSpectrum;
UpdateLineShape();
}
void CCalibrateInstrumentLineShape::UpdateLineShape()
{
try
{
m_controller->m_readWavelengthCalibrationFromFile = m_autoDetermineCalibration == 0;
m_controller->Update();
{
if (m_autoDetermineCalibration == FALSE && !m_controller->m_inputSpectrumContainsWavelength)
{
m_labelSpectrumContainsNoWavelengthCalibration.SetWindowTextA("No calibration in file, using pixels");
m_labelSpectrumContainsNoWavelengthCalibration.ShowWindow(SW_SHOW);
}
else if (!m_controller->m_wavelengthCalibrationSucceeded)
{
m_labelSpectrumContainsNoWavelengthCalibration.SetWindowTextA("Wavelength calibration failed, using default");
m_labelSpectrumContainsNoWavelengthCalibration.ShowWindow(SW_SHOW);
}
else
{
m_labelSpectrumContainsNoWavelengthCalibration.ShowWindow(SW_HIDE);
}
}
}
catch (std::invalid_argument& e)
{
MessageBox(e.what(), "Invalid input", MB_OK);
}
catch (std::exception& e)
{
MessageBox(e.what(), "Invalid input", MB_OK);
}
// Update the User interface
UpdateData(FALSE);
UpdateListOfPeaksFound();
UpdateGraph();
}
void CCalibrateInstrumentLineShape::UpdateListOfPeaksFound()
{
m_peaksList.ResetContent();
for each (auto peak in m_controller->m_peaksFound)
{
CString fmt;
if (peak.wavelength > 0 && m_controller->m_wavelengthCalibrationSucceeded)
{
fmt.AppendFormat("%.1lf nm", peak.wavelength);
}
else
{
fmt.AppendFormat("px: %.0lf", peak.pixel);
}
m_peaksList.AddString(fmt);
}
m_peaksList.AddString("--");
m_saveButton.EnableWindow(FALSE);
UpdateExplanation();
}
InstrumentLineshapeCalibrationController::LineShapeFunction OptionToLineShapeFunction(int radioButtonOption)
{
switch (radioButtonOption)
{
case 0: return InstrumentLineshapeCalibrationController::LineShapeFunction::Gaussian;
case 1: return InstrumentLineshapeCalibrationController::LineShapeFunction::SuperGauss;
default: return InstrumentLineshapeCalibrationController::LineShapeFunction::None;
}
}
void CCalibrateInstrumentLineShape::UpdateFittedLineShape()
{
try
{
UpdateData(TRUE); // get the selections from the user interface
const int selectedPeak = m_peaksList.GetCurSel();
const auto selectedFunctionToFit = OptionToLineShapeFunction(m_fitFunctionOption);
m_controller->FitFunctionToLineShape(selectedPeak, selectedFunctionToFit);
UpdateExplanation();
UpdateFitResultLabel();
}
catch (std::invalid_argument& e)
{
MessageBox(e.what(), "Invalid input", MB_OK);
}
catch (std::exception& e)
{
MessageBox(e.what(), "Invalid input", MB_OK);
}
}
void CCalibrateInstrumentLineShape::OnLbnSelchangeFoundPeak()
{
const int selectedElement = m_peaksList.GetCurSel();
UpdateFittedLineShape();
if (selectedElement >= 0 && selectedElement < static_cast<int>(m_controller->m_peaksFound.size()))
{
// zoom in on the selected peak
const novac::SpectrumDataPoint selectedPeak = m_controller->m_peaksFound[selectedElement];
const double leftWidth = selectedPeak.pixel - selectedPeak.leftPixel;
const double rightWidth = selectedPeak.rightPixel - selectedPeak.pixel;
const int firstPixel = std::max(static_cast<int>(selectedPeak.pixel - 3 * leftWidth), 0);
const int lastPixel = std::min(static_cast<int>(selectedPeak.pixel + 3 * rightWidth), static_cast<int>(m_controller->m_resultingCalibration->pixelToWavelengthMapping.size()) - 1);
const double lambdaMin = m_controller->m_resultingCalibration->pixelToWavelengthMapping[firstPixel];
const double lambdaMax = m_controller->m_resultingCalibration->pixelToWavelengthMapping[lastPixel];
m_spectrumPlot.SetRangeX(lambdaMin, lambdaMax, 1, false);
m_spectrumPlot.SetRangeY(
Min(begin(m_controller->m_inputSpectrum) + firstPixel, begin(m_controller->m_inputSpectrum) + lastPixel),
Max(begin(m_controller->m_inputSpectrum) + firstPixel, begin(m_controller->m_inputSpectrum) + lastPixel),
true);
m_saveButton.EnableWindow(TRUE);
}
else
{
// zoom out to show the entire graph
m_spectrumPlot.SetRangeX(
m_controller->m_resultingCalibration->pixelToWavelengthMapping.front(),
m_controller->m_resultingCalibration->pixelToWavelengthMapping.back(),
0,
false);
m_spectrumPlot.SetRangeY(
Min(m_controller->m_inputSpectrum),
Max(m_controller->m_inputSpectrum),
true);
m_saveButton.EnableWindow(FALSE);
}
UpdateExplanation();
UpdateFitResultLabel();
UpdateGraph(false);
}
void CCalibrateInstrumentLineShape::UpdateGraph(bool reset)
{
m_spectrumPlot.CleanPlot();
/* Draw the spectrum */
if (m_controller->m_inputSpectrum.size() > 0)
{
if (m_controller->m_wavelengthCalibrationSucceeded)
{
m_spectrumPlot.SetXUnits("Wavelength");
}
else
{
m_spectrumPlot.SetXUnits("Pixels");
}
int plotOption = (reset) ? Graph::CGraphCtrl::PLOT_CONNECTED : Graph::CGraphCtrl::PLOT_FIXED_AXIS | Graph::CGraphCtrl::PLOT_CONNECTED;
m_spectrumPlot.SetPlotColor(RGB(255, 0, 0));
m_spectrumPlot.XYPlot(
m_controller->m_resultingCalibration->pixelToWavelengthMapping.data(),
m_controller->m_inputSpectrum.data(),
static_cast<int>(m_controller->m_inputSpectrum.size()),
plotOption);
m_minimapPlot.SetPlotColor(RGB(255, 0, 0));
m_minimapPlot.XYPlot(
m_controller->m_resultingCalibration->pixelToWavelengthMapping.data(),
m_controller->m_inputSpectrum.data(),
static_cast<int>(m_controller->m_inputSpectrum.size()),
plotOption);
}
/* Draw the rejects */
if (m_controller->m_rejectedPeaks.size() > 0)
{
m_spectrumPlot.SetCircleColor(RGB(100, 100, 100));
m_minimapPlot.SetCircleColor(RGB(100, 100, 100));
std::vector<double> peakX;
std::vector<double> peakY;
for each (auto peak in m_controller->m_rejectedPeaks)
{
peakX.push_back(peak.wavelength);
peakY.push_back(peak.intensity);
}
m_spectrumPlot.DrawCircles(peakX.data(), peakY.data(), static_cast<int>(m_controller->m_rejectedPeaks.size()), Graph::CGraphCtrl::PLOT_FIXED_AXIS);
m_minimapPlot.DrawCircles(peakX.data(), peakY.data(), static_cast<int>(m_controller->m_rejectedPeaks.size()), Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}
/* Draw the peaks */
if (m_controller->m_peaksFound.size() > 0)
{
m_spectrumPlot.SetCircleColor(RGB(255, 255, 255));
m_minimapPlot.SetCircleColor(RGB(255, 255, 255));
std::vector<double> peakX;
std::vector<double> peakY;
for each (auto peak in m_controller->m_peaksFound)
{
peakX.push_back(peak.wavelength);
peakY.push_back(peak.intensity);
}
m_spectrumPlot.DrawCircles(peakX.data(), peakY.data(), static_cast<int>(m_controller->m_peaksFound.size()), Graph::CGraphCtrl::PLOT_FIXED_AXIS);
m_minimapPlot.DrawCircles(peakX.data(), peakY.data(), static_cast<int>(m_controller->m_peaksFound.size()), Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}
/* Draw the fitted line shape (if any) */
if (m_controller->m_resultingCalibration->instrumentLineShape.size() > 0)
{
m_spectrumPlot.SetPlotColor(RGB(0, 255, 0));
m_spectrumPlot.XYPlot(
m_controller->m_resultingCalibration->instrumentLineShapeGrid.data(),
m_controller->m_resultingCalibration->instrumentLineShape.data(),
static_cast<long>(m_controller->m_resultingCalibration->instrumentLineShape.size()),
Graph::CGraphCtrl::PLOT_CONNECTED | Graph::CGraphCtrl::PLOT_FIXED_AXIS);
}
// Display the zoomed region in the minimap
{
constexpr int ORANGE = RGB(255, 128, 0);
m_minimapPlot.DrawLine(Graph::VERTICAL, m_spectrumPlot.GetXMin(), ORANGE, Graph::STYLE_DASHED);
m_minimapPlot.DrawLine(Graph::VERTICAL, m_spectrumPlot.GetXMax(), ORANGE, Graph::STYLE_DASHED);
m_minimapPlot.ShadeFilledSquare(m_minimapPlot.GetXMin(), m_spectrumPlot.GetXMin(), m_minimapPlot.GetYMin(), m_minimapPlot.GetYMax(), 0.5);
m_minimapPlot.ShadeFilledSquare(m_spectrumPlot.GetXMax(), m_minimapPlot.GetXMax(), m_minimapPlot.GetYMin(), m_minimapPlot.GetYMax(), 0.5);
}
}
void CCalibrateInstrumentLineShape::OnBnClickedRadioFitGaussian()
{
UpdateFittedLineShape();
UpdateGraph(false);
}
void CCalibrateInstrumentLineShape::OnBnClickedSave()
{
try
{
// Extract the currently selected line shape.
// Notice that there must have been a peak selected and m_controller->FitFunctionToLineShape must have been called (should be done in the event handler)
const int selectedPeak = m_peaksList.GetCurSel();
if (selectedPeak < 0 || selectedPeak >= static_cast<int>(m_controller->m_peaksFound.size()))
{
MessageBox("Please select a peak to save in the list box to the left", "No peak selected", MB_OK);
return;
}
if ((m_autoDetermineCalibration == FALSE && !m_controller->m_inputSpectrumContainsWavelength) || !m_controller->m_wavelengthCalibrationSucceeded)
{
MessageBox("The provided spectrum does not contain a valid wavelength calibration and will not produce a valid instrument line shape file. Please make sure that there is a wavelength calibration and try again.", "No wavelength calibration", MB_OK);
return;
}
// Save the instrument line shape and the pixel-to-wavelength calibration to file
CString destinationFileName = L"";
int selectedType = 1;
if (Common::BrowseForFile_SaveAs("Extended Standard Files\0*.std\0QDOAS Calibrations\0*.clb;*.slf", destinationFileName, &selectedType))
{
if (selectedType == 2)
{
std::string dstFileName = novac::EnsureFilenameHasSuffix(std::string(destinationFileName), "clb");
m_controller->SaveResultAsClb(dstFileName);
dstFileName = novac::EnsureFilenameHasSuffix(std::string(destinationFileName), "slf");
m_controller->SaveResultAsSlf(dstFileName);
}
else
{
std::string dstFileName = novac::EnsureFilenameHasSuffix(std::string(destinationFileName), "std");
m_controller->SaveResultAsStd(dstFileName);
}
}
}
catch (std::exception& e)
{
MessageBox(e.what(), "Failed to save instrument line shape", MB_OK);
}
}
void CCalibrateInstrumentLineShape::UpdateWavelengthCalibrationOption()
{
try
{
UpdateData(TRUE); // get the selections from the user interface
if (m_controller->m_inputSpectrum.size() == 0)
{
return;
}
UpdateLineShape();
}
catch (std::invalid_argument& e)
{
MessageBox(e.what(), "Invalid input", MB_OK);
}
catch (std::exception& e)
{
MessageBox(e.what(), "Invalid input", MB_OK);
}
}
void CCalibrateInstrumentLineShape::OnBnClickedToggleCalibrationFromMercuryLines()
{
UpdateWavelengthCalibrationOption();
}
void CCalibrateInstrumentLineShape::UpdateExplanation()
{
const int selectedEmissionLine = m_peaksList.GetCurSel();
if (selectedEmissionLine >= 0 && selectedEmissionLine < static_cast<int>(m_controller->m_peaksFound.size()))
{
const auto selectedFunctionToFit = OptionToLineShapeFunction(m_fitFunctionOption);
if (selectedFunctionToFit == InstrumentLineshapeCalibrationController::LineShapeFunction::Gaussian)
{
m_labelSaveExplanation.SetWindowTextA("Click Save to save the selected Gaussian instrument line shape to file.");
}
else if (selectedFunctionToFit == InstrumentLineshapeCalibrationController::LineShapeFunction::SuperGauss)
{
m_labelSaveExplanation.SetWindowTextA("Click Save to save the selected Super Gaussian instrument line shape to file.");
}
else
{
m_labelSaveExplanation.SetWindowTextA("Click Save to save the selected measured instrument line shape to file.");
}
}
else
{
// default text, no emission line selected.
m_labelSaveExplanation.SetWindowTextA("Please select one emission line in the list to the left and (optionally) a function to model as Instrument Line Shape");
}
}
void CCalibrateInstrumentLineShape::UpdateFitResultLabel()
{
const int selectedEmissionLine = m_peaksList.GetCurSel();
const auto selectedFunctionToFit = OptionToLineShapeFunction(m_fitFunctionOption);
if (selectedEmissionLine >= 0 &&
selectedEmissionLine < static_cast<int>(m_controller->m_peaksFound.size()) &&
selectedFunctionToFit != InstrumentLineshapeCalibrationController::LineShapeFunction::None)
{
const auto description = m_controller->GetFittedFunctionDescription();
{
std::stringstream str;
for each (auto property in description)
{
str << property.first << ":\t" << property.second << "\n";
}
std::string text = str.str();
m_labelFitResult.SetWindowTextA(text.c_str());
}
m_labelFitResult.ShowWindow(SW_SHOW);
}
else
{
m_labelFitResult.ShowWindow(SW_HIDE);
}
}