-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectrum.cs
316 lines (251 loc) · 11.1 KB
/
Spectrum.cs
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
/*
Gamma Analyzer - Controlling application for Burn
Copyright (C) 2016 Norwegian Radiation Protection Authority
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Authors: Dag robole,
using System;
using System.Collections.Generic;
using System.Globalization;
using NLua;
using Newtonsoft.Json;
namespace crash
{
// Class used to store a spectrum
public class Spectrum
{
// List of channels
private List<float> mChannels;
// Name of parent session
public string SessionName { get; set; }
// This spectrums session index
public int SessionIndex { get; set; }
// Label to use for this spectrum
public string Label { get; set; }
// Property to access channels
public List<float> Channels { get { return mChannels; } }
// Number of channels used for this spectrum
public float NumChannels { get; set; }
// Max count found in this spectrum
public float MaxCount { get; set; }
// Min count found in this spectrum
public float MinCount { get; set; }
// Total counts stored in this spectrum
public float TotalCount { get; set; }
// Latitude when this spectrum was started
public double Latitude { get; set; }
// Latitude error when this spectrum was started
public double LatitudeError { get; set; }
// Longitude when this spectrum was started
public double Longitude { get; set; }
// Longitude error when this spectrum was started
public double LongitudeError { get; set; }
// Altitude when this spectrum was started
public double Altitude { get; set; }
// Altitude error when this spectrum was started
public double AltitudeError { get; set; }
// Date and time when this spectrum was started
public DateTime GpsTime { get; set; }
public float GpsTrack { get; set; }
public float GpsTrackError { get; set; }
// Speed when this spectrum was started
public float GpsSpeed { get; set; }
// Speed error when this spectrum was started
public float GpsSpeedError { get; set; }
// Climb when this spectrum was started
public float GpsClimb { get; set; }
// Climb error when this spectrum was started
public float GpsClimbError { get; set; }
// Realtime for this spectrum
public int Realtime { get; set; }
// Livetime for this spectrum
public int Livetime { get; set; }
// Doserate for this spectrum in nanosievert per hour
public double Doserate { get; set; }
public Spectrum()
{
mChannels = new List<float>();
}
public Spectrum(burn.ProtocolMessage msg)
{
SessionName = msg.Params["session_name"].ToString();
SessionIndex = Convert.ToInt32(msg.Params["index"]);
Label = "Spectrum " + SessionIndex;
NumChannels = Convert.ToInt32(msg.Params["num_channels"]);
Latitude = Convert.ToDouble(msg.Params["latitude"], CultureInfo.InvariantCulture);
LatitudeError = Convert.ToDouble(msg.Params["latitude_error"], CultureInfo.InvariantCulture);
Longitude = Convert.ToDouble(msg.Params["longitude"], CultureInfo.InvariantCulture);
LongitudeError = Convert.ToDouble(msg.Params["longitude_error"], CultureInfo.InvariantCulture);
Altitude = Convert.ToDouble(msg.Params["altitude"], CultureInfo.InvariantCulture);
AltitudeError = Convert.ToDouble(msg.Params["altitude_error"], CultureInfo.InvariantCulture);
if(msg.Params["time"] == null || String.IsNullOrEmpty(msg.Params["time"].ToString().Trim()))
GpsTime = new DateTime(1900, 1, 1, 1, 1, 1);
else
GpsTime = Convert.ToDateTime(msg.Params["time"]);
GpsTrack = Convert.ToSingle(msg.Params["track"], CultureInfo.InvariantCulture);
GpsTrackError = Convert.ToSingle(msg.Params["track_error"], CultureInfo.InvariantCulture);
GpsSpeed = Convert.ToSingle(msg.Params["speed"], CultureInfo.InvariantCulture);
GpsSpeedError = Convert.ToSingle(msg.Params["speed_error"], CultureInfo.InvariantCulture);
GpsClimb = Convert.ToSingle(msg.Params["climb"], CultureInfo.InvariantCulture);
GpsClimbError = Convert.ToSingle(msg.Params["climb_error"], CultureInfo.InvariantCulture);
Realtime = Convert.ToInt32(msg.Params["realtime"]);
Livetime = Convert.ToInt32(msg.Params["livetime"]);
TotalCount = 0f;
Doserate = 0.0;
mChannels = new List<float>();
// Split channel string and store each count in channel array
LoadSpectrumString(msg.Params["channels"].ToString());
}
public override string ToString()
{
return SessionName + " - " + SessionIndex.ToString();
}
public void LoadSpectrumString(string spectrums)
{
mChannels.Clear();
string[] items = spectrums.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in items)
{
float ch = Convert.ToSingle(item, CultureInfo.InvariantCulture);
mChannels.Add(ch);
if (ch > MaxCount)
MaxCount = ch;
if (ch < MinCount)
MinCount = ch;
TotalCount += ch;
}
}
public float GetCountInROI(int start, int end)
{
// Get count for a given ROI
if (start < 0 || start >= mChannels.Count || end < 0 || end >= mChannels.Count)
return 0f;
float max = 0f;
for (int i = start; i < end; i++)
max += mChannels[i];
return max;
}
public double CalculateDoserate(Detector det, LuaFunction GEFactorFunc)
{
// Calculate doserate for this spectrum
if (det == null)
throw new Exception("Unable to calculate doserate, detector is null");
if (GEFactorFunc == null)
throw new Exception("Unable to calculate doserate, GEFactorFunc is null");
Doserate = 0.0;
// Trim off discriminators
int startChan = (int)((double)det.NumChannels * (det.LLD / 100.0));
int endChan = (int)((double)det.NumChannels * (det.ULD / 100.0));
if(endChan > det.NumChannels) // FIXME: Can not exceed 100% atm
endChan = det.NumChannels;
// Accumulate doserates of each channel
for (int i = startChan; i < endChan; i++)
{
float sec = (float)Livetime / 1000000f;
float cps = Channels[i] / sec;
double E = det.GetEnergy(i) / 1000.0;
if (E < 0.05) // Energies below 0.05 are invalid
continue;
double GE = (double)GEFactorFunc.Call(E).GetValue(0);
double chanDose = GE * (cps * 60.0);
Doserate += chanDose;
}
return Doserate;
}
public Spectrum Clone()
{
// Create a clone of this spectrum
Spectrum res = new Spectrum();
res.mChannels.AddRange(mChannels.ToArray());
res.SessionName = SessionName;
res.SessionIndex = SessionIndex;
res.Label = Label;
res.NumChannels = NumChannels;
res.MaxCount = MaxCount;
res.MinCount = MinCount;
res.TotalCount = TotalCount;
res.Latitude = Latitude;
res.LatitudeError = LatitudeError;
res.Longitude = Longitude;
res.LongitudeError = LongitudeError;
res.Altitude = Altitude;
res.AltitudeError = AltitudeError;
res.GpsTime = GpsTime;
res.GpsTrack = GpsTrack;
res.GpsTrackError = GpsTrackError;
res.GpsSpeed = GpsSpeed;
res.GpsSpeedError = GpsSpeedError;
res.GpsClimb = GpsClimb;
res.GpsClimbError = GpsClimbError;
res.Realtime = Realtime;
res.Livetime = Livetime;
res.Doserate = Doserate;
return res;
}
public void Merge(Spectrum s)
{
// Merge a given spectrum
Livetime += s.Livetime;
Realtime += s.Realtime;
for(int i=0; i<mChannels.Count; i++)
mChannels[i] += s.mChannels[i];
}
}
public class APISpectrum
{
public APISpectrum(Spectrum spec)
{
SessionName = spec.SessionName;
SessionIndex = spec.SessionIndex;
StartTime = spec.GpsTime.ToString("yyyy-MM-ddThh:mm:ss.fffZ");
Latitude = spec.Latitude;
Longitude = spec.Longitude;
Altitude = spec.Altitude;
Track = spec.GpsTrack;
Speed = spec.GpsSpeed;
Climb = spec.GpsClimb;
Livetime = spec.Livetime;
Realtime = spec.Realtime;
NumChannels = (int)spec.NumChannels;
Channels = string.Join(" ", spec.Channels);
Doserate = spec.Doserate;
}
[JsonProperty("session_name")]
public string SessionName { get; set; }
[JsonProperty("session_index")]
public int SessionIndex { get; set; }
[JsonProperty("start_time")]
public string StartTime { get; set; }
[JsonProperty("latitude")]
public double Latitude { get; set; }
[JsonProperty("longitude")]
public double Longitude { get; set; }
[JsonProperty("altitude")]
public double Altitude { get; set; }
[JsonProperty("track")]
public double Track { get; set; }
[JsonProperty("speed")]
public double Speed { get; set; }
[JsonProperty("climb")]
public double Climb { get; set; }
[JsonProperty("livetime")]
public double Livetime { get; set; }
[JsonProperty("realtime")]
public double Realtime { get; set; }
[JsonProperty("num_channels")]
public int NumChannels { get; set; }
[JsonProperty("channels")]
public string Channels { get; set; }
[JsonProperty("doserate")]
public double Doserate { get; set; }
}
}