-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWeatherCatRecord.cs
More file actions
453 lines (409 loc) · 10.5 KB
/
Copy pathWeatherCatRecord.cs
File metadata and controls
453 lines (409 loc) · 10.5 KB
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
using System.Globalization;
namespace ImportWC
{
internal class WeatherCatRecord
{
private readonly int _year;
private readonly int _month;
public DateTime Timestamp { get; private set; }
public double? OutsideTemp { get; private set; }
public double? InsideTemp { get; private set; }
public double? Dewpoint { get; private set; }
public double? Baro { get; private set; }
public double? WindSpeed { get; private set; }
public int WindDir { get; private set; } = 0;
public double? WindChill { get; private set; }
public double? WindGust { get; private set; }
public double? RainHour { get; private set; }
public double? RainDay { get; private set; }
public double? RainMonth { get; private set; }
public double? RainYear { get; set; }
public double? RainRate { get; private set; }
public int? OutsideHumidity { get; private set; }
public int? InsideHumidity { get; private set; }
public int? Solar { get; private set; }
public double? UV { get; private set; }
//public double? Sunshine { get; private set; }
//public double? WindRun { get; private set; }
//public double? WindAvg { get; private set; }
//public double? TempTrend { get; private set; }
//public double? WindTrend { get; private set; }
public double? ET { get; private set; }
public double? ETMonth { get; private set; }
public double? ETYear { get; private set; }
public double?[] ExtraTemp { get; private set; } = new double?[8];
public int?[] ExtraHum { get; private set; } = new int?[8];
public int?[] SoilMoist { get; private set; } = new int?[4];
public double?[] SoilTemp { get; private set; } = new double?[4];
public int?[] LeafWet { get; private set; } = new int?[4];
public int?[] CO2 { get; private set; } = new int?[4];
public double?[] Synth { get; private set; } = new double?[10];
public bool HasExtraData { get; private set; }
public bool HasSynthData { get; private set; }
public WeatherCatRecord(int year, int month, string entry)
{
_year = year;
_month = month;
// replace conditions string
if (entry.Contains("C:"))
{
var start = entry.IndexOf("C:") + 3;
var end = entry.IndexOf("\" ", start);
entry = string.Concat(entry.AsSpan(0, start), "x", entry.AsSpan(end));
}
var arr = entry.Split(' ', StringSplitOptions.RemoveEmptyEntries);
// ignore the first entry = record number
for (var i = 1; i < arr.Length; i++)
{
try
{
ProcessPair(arr[i].Split(':'));
}
catch(Exception ex)
{
Program.LogConsole($"Error at entry={arr[0]} field={i} year={year} month={month}: {ex.Message}", ConsoleColor.Red);
Program.LogMessage($"Error at entry={arr[0]} field={i} year={year} month={month}: {ex.Message}");
}
}
}
/*
* t and V are not optional, all other fields are.
*
* t is the day, hour and minute (2 digits each)
* T is outside temperature
* Ti is internal temperature
* D is dew point
* Pr is barometric pressure
* W is wind speed
* Wd is wind direction
* Wc is wind chill
* Wg is wind gust
* Ph is hourly precipitation
* P is total precipitation
* Pm is monthly precipitation
* Py is annual precipitation
* H is outside humidity
* Hi is internal humidity
* S is solar
* U is UV
* T1 to T8 is auxiliary temperatures
* H1 to H8 are auxiliary humidity sensors
* Sm1 to Sm4 is soil moisture
* St1 to St4 is soil temperature
* Lw1 to Lw4 is leaf wetness
* Lt1 to Lt4 is leaf temperature
* Sy1 to Sy? is ????
* CO21 to CO24 is CO2
* Ed is daily ET
* Em is monthly ET
* Ey is yearly ET
* C is current conditions (delimited by double quotes)
* V is validation.
*/
private void ProcessPair(string[] entry)
{
if (entry.Length != 2)
{
Program.LogConsole("Invalid entry = " + entry[0] + " " + entry[1], ConsoleColor.Red);
Program.LogMessage("Invalid entry = " + entry[0] + " " + entry[1]);
return;
}
var key = entry[0];
var val = entry[1];
switch (key)
{
case "t":
// t is the day, hour and minute (2 digits each)
if (val.Length != 6)
{
Program.LogMessage("Invalid 't' value = " + val);
break;
}
Timestamp = new DateTime(_year, _month, int.Parse(val[0..2]), int.Parse(val[2..4]), int.Parse(val[4..6]), 0, DateTimeKind.Local);
break;
case "T":
// T is outside temperature
if (double.TryParse(val, CultureInfo.InvariantCulture, out double temp))
{
OutsideTemp = DoTemp(temp);
}
break;
case "Ti":
// Ti is internal temperature
if (double.TryParse(val, CultureInfo.InvariantCulture, out double intemp))
{
InsideTemp = DoTemp(intemp);
}
break;
case "D":
// D is dew point
if (double.TryParse(val, CultureInfo.InvariantCulture, out double dp))
{
Dewpoint = DoDewPt(dp);
}
break;
case "Pr":
// Pr is barometric pressure
if (double.TryParse(val, CultureInfo.InvariantCulture, out double pr))
{
Baro = DoPress(pr);
}
break;
case "W":
// W is wind speed
if (double.TryParse(val, CultureInfo.InvariantCulture, out double ws))
{
WindSpeed = DoWind(ws);
}
break;
case "Wd":
// Wd is wind direction
if (int.TryParse(val, out int wd))
{
WindDir = wd;
}
break;
case "Wc":
// Wc is wind chill
if (double.TryParse(val, CultureInfo.InvariantCulture, out double wc))
{
WindChill = DoTemp(wc);
}
break;
case "Wg":
// Wg is wind gust
if (double.TryParse(val, CultureInfo.InvariantCulture, out double wg))
{
WindGust = DoWind(wg);
}
break;
case "Ph":
// Ph is hourly precipitation
if (double.TryParse(val, CultureInfo.InvariantCulture, out double ph))
{
RainHour = DoRain(ph);
// There is no rainfall rate in the WC files, so use the hourly value
RainRate = RainHour;
}
break;
case "P":
// P is total precipitation
if (double.TryParse(val, CultureInfo.InvariantCulture, out double p))
{
RainDay = DoRain(p);
}
break;
case "Pm":
// Pm is monthly precipitation
if (double.TryParse(val, CultureInfo.InvariantCulture, out double pm))
{
RainMonth = DoRain(pm);
}
break;
case "Py":
// Py is annual precipitation
if (double.TryParse(val, CultureInfo.InvariantCulture, out double py))
{
RainYear = DoRain(py);
}
break;
case "H":
// H is outside humidity
if (int.TryParse(val, out int hum))
{
OutsideHumidity = hum;
}
break;
case "Hi":
// Hi is internal humidity
if (int.TryParse(val, out int hi))
{
InsideHumidity = hi;
}
break;
case "S":
// S is solar
if (int.TryParse(val, out int sol))
{
Solar = sol;
}
break;
case "U":
// U is uv
if (double.TryParse(val, CultureInfo.InvariantCulture, out double uv))
{
UV = uv;
}
break;
case "Ed":
// Ed is daily evapotranspiration
if (double.TryParse(val, CultureInfo.InvariantCulture, out double ed))
{
ET = ed;
}
break;
case "Em":
// Em is monthly evapotranspiration
if (double.TryParse(val, CultureInfo.InvariantCulture, out double em))
{
ETMonth = em;
}
break;
case "Ey":
// Ey is annual evapotranspiration
if (double.TryParse(val, CultureInfo.InvariantCulture, out double ey))
{
ETYear = ey;
}
break;
case "T1":
case "T2":
case "T3":
case "T4":
case "T5":
case "T6":
case "T7":
case "T8":
// Tn is temperature
if (double.TryParse(val, CultureInfo.InvariantCulture, out double tempn))
{
var ind = int.Parse(key[1..]);
ExtraTemp[ind - 1] = DoTemp(tempn);
HasExtraData = true;
}
break;
case "H1":
case "H2":
case "H3":
case "H4":
case "H5":
case "H6":
case "H7":
case "H8":
// Hn is humidity
if (int.TryParse(val, out int humn))
{
var ind = int.Parse(key[1..]);
ExtraHum[ind - 1] = humn;
HasExtraData = true;
}
break;
case "Sm1":
case "Sm2":
case "Sm3":
case "Sm4":
// Smn is Soil moisture
if (int.TryParse(val, out int sm))
{
var ind = int.Parse(key[2..]);
SoilMoist[ind - 1] = sm;
HasExtraData = true;
}
break;
case "St1":
case "St2":
case "St3":
case "St4":
// Stn is soil temperature
if (double.TryParse(val, CultureInfo.InvariantCulture, out double st))
{
var ind = int.Parse(key[2..]);
SoilTemp[ind - 1] = DoTemp(st);
HasExtraData = true;
}
break;
case "Lw1":
case "Lw2":
case "Lw3":
case "Lw4":
// Lwn is leaf wetness
if (int.TryParse(val, out int lw))
{
var ind = int.Parse(key[2..]);
LeafWet[ind - 1] = lw;
HasExtraData = true;
}
break;
case "CO21":
case "CO22":
case "CO23":
case "CO24":
// CO2n is CO2
if (int.TryParse(val, out int co2))
{
var ind = int.Parse(key[3..]);
CO2[ind - 1] = co2;
HasExtraData = true;
}
break;
case "Sy1":
case "Sy2":
case "Sy3":
case "Sy4":
case "Sy5":
case "Sy6":
case "Sy7":
case "Sy8":
case "Sy9":
case "Sy10":
if (double.TryParse(val, out double sy))
{
var ind = int.Parse(key[2..]);
Synth[ind - 1] = sy;
HasSynthData = true;
}
break;
}
}
private static double DoTemp(double inp)
{
if (Program.WcConfigTemp == "c")
{
return ConvertUnits.TempCToUser(inp);
}
else
{
return ConvertUnits.TempFToUser(inp);
}
}
private static double DoDewPt(double inp)
{
if (Program.WcConfigDew == "c")
{
return ConvertUnits.TempCToUser(inp);
}
else
{
return ConvertUnits.TempFToUser(inp);
}
}
private static double DoPress(double inp)
{
return Program.WcConfigPress switch
{
"inhg" => ConvertUnits.PressINHGToUser(inp),
"hpa" or "mb" => ConvertUnits.PressMBToUser(inp),
"kpa" => ConvertUnits.PressKPAToUser(inp),
_ => inp,
};
}
private static double DoWind(double inp)
{
return Program.WcConfigWind switch
{
"mph" => ConvertUnits.WindMPHToUser(inp),
"kts" => ConvertUnits.WindKnotsToUser(inp),
_ => inp,
};
}
private static double DoRain(double inp)
{
return Program.WcConfigRain switch
{
"mm" => ConvertUnits.RainMMToUser(inp),
"in" => ConvertUnits.RainINToUser(inp),
_ => inp,
};
}
}
}