Merge branch 'master' of git.ucc.asn.au:/matches/honours
[matches/honours.git] / research / transmission_spectroscopy / TOF / TofDialog.cpp
1 ///////////////////////////////////////\r
2 // TofDialog.cpp\r
3 \r
4 #include "stdafx.h"\r
5 #include "TofDialog.h"\r
6 #include "resource.h"\r
7 \r
8 #include "utils.h"\r
9 #include "analyse.h"\r
10 #include <sstream>\r
11 #include <iostream>\r
12 #include <fstream>\r
13 #include <vector>\r
14 \r
15 using namespace std;\r
16 \r
17 // Definitions for the CTofDialog class\r
18 CTofDialog::CTofDialog(UINT nResID, CWnd* pParent)\r
19         : CDialog(nResID, pParent)\r
20 {\r
21         m_hInstRichEdit = ::LoadLibrary(_T("RICHED32.DLL"));\r
22     if (!m_hInstRichEdit)\r
23                 ::MessageBox(NULL, _T("CTofDialog::CRichView  Failed to load RICHED32.DLL"), _T(""), MB_ICONWARNING);\r
24 }\r
25 \r
26 CTofDialog::~CTofDialog()\r
27 {\r
28         ::FreeLibrary(m_hInstRichEdit);\r
29 }\r
30 \r
31 INT_PTR CTofDialog::DialogProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
32 {\r
33 //      switch (uMsg)\r
34 //      {\r
35                 //Additional messages to be handled go here\r
36 //      }\r
37 \r
38         // Pass unhandled messages on to parent DialogProc\r
39         return DialogProcDefault(uMsg, wParam, lParam);\r
40 }\r
41 \r
42 BOOL CTofDialog::OnCommand(WPARAM wParam, LPARAM lParam)\r
43 {\r
44         UNREFERENCED_PARAMETER(lParam);\r
45 \r
46         switch (LOWORD(wParam))\r
47         {\r
48                 case IDC_BTN_EXIT:\r
49                         OnExit();\r
50                         break;\r
51                 case IDC_BTN_OK:\r
52                         OnOK();\r
53                         break;\r
54                 case IDC_BTN_OUTPUT:\r
55                         OnBrowseOutput();\r
56                         break;\r
57                 case IDC_BTN_INPUT:\r
58                         OnBrowseInput();\r
59                         break;\r
60                 case IDC_BTN_HELP:\r
61                         OnHelp();\r
62                         break;\r
63 \r
64     } //switch (LOWORD(wParam))\r
65 \r
66         return FALSE;\r
67 }\r
68 \r
69 BOOL CTofDialog::OnInitDialog()\r
70 {\r
71         // Set the Icon\r
72         SetIconLarge(IDW_MAIN);\r
73         SetIconSmall(IDW_MAIN);\r
74 \r
75         // Put some text in the edit boxes\r
76         //SetDlgItemText(IDC_EDIT1, _T("Edit Control"));\r
77         //SetDlgItemText(IDC_RICHEDIT1, _T("Rich Edit Window"));\r
78 \r
79         // Put some text in the list box\r
80         //for (int i = 0 ; i < 8 ; i++)\r
81         //      SendDlgItemMessage(IDC_LIST1, LB_ADDSTRING, 0, (LPARAM) _T("List Box"));\r
82 \r
83         return true;\r
84 }\r
85 \r
86 void CTofDialog::OnOK()\r
87 {\r
88         int maxBufferSize = 1024;\r
89         char inputFilename[maxBufferSize]; \r
90         char outputFilename[maxBufferSize];\r
91         strcpy(inputFilename, LPCTSTR(GetDlgItemText(IDC_EDT_INPUT)));\r
92         strcpy(outputFilename, LPCTSTR(GetDlgItemText(IDC_EDT_OUTPUT)));\r
93 \r
94 \r
95         fstream input; fstream output;\r
96 \r
97 \r
98         float peakValue = atof(GetDlgItemText(IDC_EDT_PEAK).GetBuffer(maxBufferSize));\r
99         float binWidth = atof(GetDlgItemText(IDC_EDT_RES).GetBuffer(maxBufferSize));\r
100         int numberSpectra = atoi(GetDlgItemText(IDC_EDT_SPECTRA).GetBuffer(maxBufferSize));\r
101         float mass = atof(GetDlgItemText(IDC_EDT_MASS).GetBuffer(maxBufferSize));\r
102         float length = atof(GetDlgItemText(IDC_EDT_LENGTH).GetBuffer(maxBufferSize));\r
103         float peakCutOff = atof(GetDlgItemText(IDC_EDT_PEAK_CUTOFF).GetBuffer(maxBufferSize));\r
104         \r
105 \r
106         if (peakValue <= 0.0f)\r
107         {\r
108                 MessageBox("Elastic peak value must be larger than zero", _T("Error"), MB_OK);\r
109                 return;\r
110         }\r
111 \r
112         if (binWidth <= 0.0f)\r
113         {\r
114                 MessageBox("Bin width must be larger than zero", _T("Error"), MB_OK);\r
115                 return;\r
116         }\r
117         if (mass <= 0.0f)\r
118         {\r
119                 MessageBox("Mass must be larger than zero", _T("Error"), MB_OK);\r
120                 return;\r
121         }\r
122         if (length <= 0.0f)\r
123         {\r
124                 MessageBox("Path length must be larger than zero", _T("Error"), MB_OK);\r
125                 return;\r
126         }\r
127 \r
128         if (numberSpectra <= 0)\r
129         {\r
130                 MessageBox("There should be at least one spectra in the data!", "Error", MB_OK);\r
131                 return;\r
132         }\r
133 \r
134         input.open(inputFilename);\r
135 \r
136         if (!input.is_open())\r
137         {\r
138                 MessageBox("Input file \"" + CString(inputFilename) + "\" Not Found", _T("Error"), MB_OK);\r
139                 return;\r
140         }\r
141 \r
142         output.open(outputFilename);\r
143         if (output.is_open())\r
144         {\r
145                 output.close();\r
146                 if (MessageBox(_T("Output file already exists! Overwrite?"), _T("Warning"),MB_YESNO) == IDNO)\r
147                 {\r
148                         MessageBox(_T("No action taken."), _T("Message"), MB_OK);\r
149                         input.close();\r
150                         return; \r
151                 }\r
152         }\r
153         output.open(outputFilename, ios::out);\r
154         if (!output.is_open())\r
155         {\r
156                 MessageBox("Output file \"" + CString(outputFilename) + "\" Couldn't be opened", _T("Error"), MB_OK);\r
157                 input.close();\r
158                 return;\r
159         }\r
160 \r
161         string line = "";\r
162         \r
163         /*\r
164         //Ignore headers\r
165         while (line != "]" && input.good())\r
166         {\r
167                 line = "";\r
168                 input >> line;\r
169         }\r
170         */\r
171         vector<int> data;\r
172         //We are now at the data\r
173         while (input.good())\r
174         {\r
175                 line = "";\r
176                 input >> line;\r
177                 //MessageBox("Line reads " + CString(line.c_str()), _T("DEBUG"), MB_OK);\r
178                 int dataPoint = atoi(line.c_str());\r
179                 if (dataPoint == 0 && line != "0" && line != "")\r
180                 {\r
181                         //MessageBox("Suspected corrupt data \"" + CString(line.c_str()) + "\"", _T("Error"), MB_OK);\r
182                 }\r
183                 else\r
184                 {\r
185                         data.push_back(dataPoint);\r
186                 }\r
187         }\r
188         \r
189         //Have aquired all data; perform operations\r
190         map<float, vector<int> > outputData; //Data as energy,counts columns\r
191 \r
192 \r
193 \r
194         Analysis::AnalyseData(data, outputData, peakValue, binWidth, mass, length, peakCutOff, numberSpectra);\r
195         \r
196         output.precision(32);\r
197 \r
198         for (map<float, vector<int> >::iterator i = outputData.begin(); i != outputData.end(); ++i)\r
199         {\r
200                 if ((*i).second.size() == numberSpectra)\r
201                 {\r
202                         output << (*i).first << " ";\r
203                         for (int ii=0; ii < (*i).second.size(); ++ii)\r
204                         {\r
205                                 output << (*i).second[ii];\r
206                                 if (ii < (*i).second.size() - 1)\r
207                                         output << " ";\r
208                         }\r
209                         output << "\n";\r
210                 }\r
211         }       \r
212 \r
213         MessageBox("Conversion complete!", _T("Message"), MB_OK);\r
214         input.close();\r
215         output.close();\r
216 \r
217 \r
218 }\r
219 \r
220 void CTofDialog::OnBrowseInput()\r
221 {\r
222         string s = openfilename();\r
223         SetDlgItemText(IDC_EDT_INPUT, _T(s.c_str()));\r
224 }\r
225 \r
226 void CTofDialog::OnBrowseOutput()\r
227 {\r
228         string s = openfilename();\r
229         SetDlgItemText(IDC_EDT_OUTPUT, _T(s.c_str()));\r
230 }\r
231 \r
232 void CTofDialog::OnExit()\r
233 {\r
234      CDialog::OnOK();\r
235 }\r
236 \r
237 void CTofDialog::Unimplemented()\r
238 {\r
239      MessageBox(_T("This feature doesn't work yet. Sorry."), _T("Button"), MB_OK); \r
240 }\r
241 \r
242 void CTofDialog::OnHelp()\r
243 {\r
244         CString helpString = "";\r
245         helpString += "This program converts electron Time of Flight (ToF) data to an energy spectrum.\n\n";\r
246 \r
247         helpString += "The formula is:\n E = m/2 (L/(L*sqrt(m/2Ep) + dt))^2\n";\r
248         helpString += " where Ep is the peak energy, m is mass, L is path length, dt is time difference relative to peak, E is energy\n\n";\r
249 \r
250         helpString += "Input File:\n";\r
251         helpString += "The expected input file should contain a single column of counts detected in successive channels.\n\n";\r
252 \r
253         helpString += "Output File:\n";\r
254         helpString += "The output file will contain multiple columns, corresponding to computed energy values (eV) and the count values for each spectrum.\n\n";\r
255 \r
256         helpString += "Elastic Peak:\n";\r
257         helpString += "The energy associated with each time channel is calculated after assuming the energy value corresponding to the time with the peak number of counts.";\r
258         helpString += "This peak normally corresponds to elastic scattering processes.\n\n";\r
259         \r
260         helpString += "Bin Width:\n";\r
261         helpString += "Each line in the input file corresponds to a time channel. The seperation in time between adjacent channels is used to calculate the absolute time difference between channels, dt.\n\n";\r
262 \r
263         helpString += "#no of spectra:\n";\r
264         helpString += "One input file might contain multiple seperate TOF spectra, spread accross seperate channels. The program detects where one spectrum ends and the next begins after a series of zero counts in the channels.\n\n";\r
265 \r
266         helpString += "Path Length:\n"; \r
267         helpString += "Assuming the acceleration distance is negligable, the path length is how far the particles travel under constant velocity.\n\n";\r
268 \r
269         helpString += "Mass:\n";\r
270         helpString += "The mass of the particles determines their energy.\n\n";\r
271 \r
272         helpString += "Peak Cut Off (%):\n";\r
273         helpString += "The program locates the elastic peak by first determining the maximum count value, and then finding the median point of all points in the TOF spectrum with count levels above this percentage of the maximum value.";\r
274         helpString += "This is useful for reducing the effects of noise on the elastic peak value.";\r
275         helpString += "To just use the time bin with maximum counts for the elastic peak, set this to 100.\n\n";\r
276 \r
277         helpString += "About:\n";\r
278         helpString += "Written by Sam Moore, 02/2012";\r
279                 \r
280         MessageBox(helpString, "Help", MB_OK);\r
281 }\r
282 \r
283 /*\r
284 void CTofDialog::OnButton()\r
285 {\r
286         SetDlgItemText(IDC_STATIC3, _T("Button Pressed"));\r
287         TRACE(_T("Button Pressed\n"));\r
288 }\r
289 */\r
290 \r

UCC git Repository :: git.ucc.asn.au