-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogFile.cs
413 lines (349 loc) · 12.3 KB
/
LogFile.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
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
using System.Globalization;
using System.Text;
namespace ImportWLK
{
static partial class LogFile
{
private static readonly SortedList<DateTime, LogFileRec> records = [];
public static double HumidexHigh { get; set; }
public static DateTime HumidexHighTime { get; set; }
public static double ApparentHigh { get; set; }
public static double ApparentLow { get; set; }
public static DateTime ApparentHighTime { get; set; }
public static DateTime ApparentLowTime { get; set; }
public static double FeelsLikeHigh { get; set; }
public static double FeelsLikeLow { get; set; }
public static DateTime FeelsLikeHighTime { get; set; }
public static DateTime FeelsLikeLowTime { get; set; }
public static double WindAvgHigh { get; set; }
public static DateTime WindAvgHighTime { get; set; }
public static int ClicksToday { get; set; }
public static int ClicksThisYear { get; set; }
public static DateTime LastTimeStamp { get; set; }
private static readonly Dictionary<int, (double bucketSize, string bucketUnit)> BucketTypes = new()
{
{ 0x0000, (0.1, "in") },
{ 0x1000, (0.01, "in") },
{ 0x2000, (0.2, "mm") },
{ 0x3000, (1.0, "mm") },
{ 0x6000, (0.1, "mm") },
};
internal static void Initialise()
{
records.Clear();
LastTimeStamp = DateTime.MinValue;
}
internal static void AddRecord(WlkArchiveRecord rec)
{
LastTimeStamp = rec.Timestamp;
if (!records.TryGetValue(rec.Timestamp, out var value))
{
value = new LogFileRec() { LogTime = rec.Timestamp};
records.Add(rec.Timestamp, value);
}
// Only apply calibration to wind, pressure, rain, and solar values
// Temperature and humidity can already be calibrated in WeatherLinlk PC
var val = rec.OutsideTemp / 10.0;
var conv = ConvertUnits.TempFToUser(val);
value.Temperature = rec.OutsideTemp < -2000 ? 0 : conv;
// not used
// OutsideTempHi
// OutsideTempLo
val = rec.InsideTemp / 10.0;
conv = ConvertUnits.TempFToUser(val);
value.InsideTemp = rec.InsideTemp < -2000 ? 0 : conv;
val = rec.Baro / 1000.0;
conv = ConvertUnits.PressINHGToUser(val);
value.Baro = rec.Baro < 0 ? 0 : Program.Cumulus.Calib.Press.Calibrate(conv);
val = rec.OutsideHumidity / 10.0;
value.Humidity = rec.OutsideHumidity < 0 ? 0 : (int) val;
val = rec.InsideHumidity / 10.0;
value.InsideHum = rec.InsideHumidity < 0 ? 0 : (int) val;
double bucketSize;
string bucketUnit;
if (BucketTypes.TryGetValue(rec.RainCollectorType, out var bucketInfo))
{
(bucketSize, bucketUnit) = bucketInfo;
}
else
{
// Handle default case
bucketSize = 0.01;
bucketUnit = "in";
}
if (rec.RainClicks >= 0)
{
ClicksThisYear += rec.RainClicks;
ClicksToday += rec.RainClicks;
if (bucketUnit == "in")
{
value.RainfallCounter = ConvertUnits.RainINToUser(ClicksThisYear * bucketSize);
}
else
{
value.RainfallCounter = ConvertUnits.RainMMToUser(ClicksThisYear * bucketSize);
}
if (rec.Timestamp.Hour == 0 && rec.Timestamp.Minute == 0)
ClicksToday = 0;
var rainToday = ClicksToday * bucketSize;
if (bucketUnit == "in")
{
val = ConvertUnits.RainINToUser(rainToday);
}
else
{
val = ConvertUnits.RainMMToUser(rainToday);
}
value.RainfallToday = Program.Cumulus.Calib.Rain.Calibrate(val);
value.RainSinceMidnight = value.RainfallToday;
}
if (rec.RainRateHi >= 0)
{
val = rec.RainRateHi * bucketSize;
if (bucketUnit == "in")
{
conv = ConvertUnits.RainINToUser(val);
}
else
{
conv = ConvertUnits.RainMMToUser(val);
}
}
else
{
conv = 0;
}
value.RainfallRate = Program.Cumulus.Calib.Rain.Calibrate(conv);
val = rec.WindSpeed / 10.0;
conv = ConvertUnits.WindMPHToUser(val);
value.WindSpeed = rec.WindSpeed < 0 ? 0 : Program.Cumulus.Calib.WindSpeed.Calibrate(conv);
if (value.WindSpeed > WindAvgHigh)
{
WindAvgHigh = value.WindSpeed;
WindAvgHighTime = rec.Timestamp;
}
val = rec.WindDir == 255 ? 0 : (int) ((rec.WindDir + 1) * 22.5);
value.WindBearing = (int) val;
value.CurrentBearing = value.WindBearing;
val = rec.WindSpeedGust / 10.0;
conv = ConvertUnits.WindMPHToUser(val);
value.WindGust = rec.WindSpeedGust < 0 ? 0 : Program.Cumulus.Calib.WindGust.Calibrate(conv);
value.CurrentGust = value.WindGust;
value.SolarRad = rec.Solar < 0 ? 0 : (int) Program.Cumulus.Calib.Solar.Calibrate(rec.Solar);
value.UVI = rec.UV > 200 ? 0 : Program.Cumulus.Calib.UV.Calibrate(rec.UV / 10.0);
value.ET = ConvertUnits.RainINToUser(rec.ET > 200 ? 0 : rec.ET / 1000);
// TODO: Chill hours
if (rec.OutsideTemp > -2000 && rec.WindSpeed >= 0)
{
val = MeteoLib.WindChill(ConvertUnits.UserTempToC(value.Temperature), ConvertUnits.UserWindToKPH(value.WindSpeed));
value.WindChill = ConvertUnits.TempCToUser(val);
}
if (rec.OutsideTemp > -2000 && rec.OutsideHumidity >= 0 && rec.OutsideHumidity <= 1000)
{
val = MeteoLib.HeatIndex(ConvertUnits.UserTempToC(value.Temperature), value.Humidity);
value.HeatIndex = ConvertUnits.TempCToUser(val);
val = MeteoLib.Humidex(ConvertUnits.UserTempToC(value.Temperature), value.Humidity);
value.Humidex = ConvertUnits.TempCToUser(val);
if (value.Humidex > HumidexHigh)
{
HumidexHigh = val;
HumidexHighTime = rec.Timestamp;
}
val = MeteoLib.DewPoint(ConvertUnits.UserTempToC(value.Temperature), value.Humidity);
value.Dewpoint = ConvertUnits.TempCToUser(val);
}
if (rec.OutsideTemp > -2000 && rec.OutsideHumidity >= 0 && rec.OutsideHumidity <= 1000 && rec.WindSpeed >= 0)
{
val = MeteoLib.ApparentTemperature(ConvertUnits.UserTempToC(value.Temperature), ConvertUnits.UserWindToMS(value.WindSpeed), value.Humidity);
value.ApparentTemp = ConvertUnits.TempCToUser(val);
if (value.ApparentTemp > ApparentHigh)
{
ApparentHigh = val;
ApparentHighTime = rec.Timestamp;
}
if (value.ApparentTemp < ApparentLow)
{
ApparentLow = val;
ApparentLowTime = rec.Timestamp;
}
val = MeteoLib.FeelsLike(ConvertUnits.UserTempToC(value.Temperature), ConvertUnits.UserWindToKPH(value.WindSpeed), value.Humidity);
value.FeelsLike = ConvertUnits.TempCToUser(val);
if (value.FeelsLike > FeelsLikeHigh)
{
FeelsLikeHigh = val;
FeelsLikeHighTime = rec.Timestamp;
}
if (value.FeelsLike < FeelsLikeLow)
{
FeelsLikeLow = val;
FeelsLikeLowTime = rec.Timestamp;
}
}
value.SolarMax = AstroLib.SolarMax(
rec.Timestamp,
(double) Program.Cumulus.Longitude,
(double) Program.Cumulus.Latitude,
Utils.AltitudeM(Program.Cumulus.Altitude),
out _,
Program.Cumulus.SolarOptions
);
}
public static void WriteLogFile()
{
var logfilename = "data" + Path.DirectorySeparatorChar + GetLogFileName(records.First().Key);
if (records.Count == 0)
{
Program.LogMessage($"No records to write to {logfilename}!");
Program.LogConsole($" No records to write to {logfilename}!", ConsoleColor.Red);
return;
}
Program.LogMessage($"Writing {records.Count} to {logfilename}");
Program.LogConsole($" Writing to {logfilename}", ConsoleColor.Gray);
// backup old log file
if (File.Exists(logfilename))
{
if (!File.Exists(logfilename + ".sav"))
{
File.Move(logfilename, logfilename + ".sav");
}
else
{
var i = 1;
do
{
if (!File.Exists(logfilename + ".sav" + i))
{
File.Move(logfilename, logfilename + ".sav" + i);
break;
}
else
{
i++;
}
} while (true);
}
}
try
{
using FileStream fs = new FileStream(logfilename, FileMode.Append, FileAccess.Write, FileShare.Read);
using StreamWriter file = new StreamWriter(fs);
Program.LogMessage($"{logfilename} opened for writing {records.Count} records");
foreach (var rec in records)
{
var line = RecToCsv(rec);
if (null != line)
file.WriteLine(line);
}
file.Close();
Program.LogMessage($"{logfilename} write complete");
}
catch (Exception ex)
{
Program.LogMessage($"Error writing to {logfilename}: {ex.Message}");
}
}
public static string RecToCsv(KeyValuePair<DateTime, LogFileRec> keyval)
{
// Writes an entry to the n-minute log file. Fields are comma-separated:
// 0 Date in the form dd/mm/yy (the slash may be replaced by a dash in some cases)
// 1 Current time - hh:mm
// 2 Current temperature
// 3 Current humidity
// 4 Current dewpoint
// 5 Current wind speed
// 6 Recent (10-minute) high gust
// 7 Average wind bearing
// 8 Current rainfall rate
// 9 Total rainfall today so far
// 10 Current sea level pressure
// 11 Total rainfall counter as held by the station
// 12 Inside temperature
// 13 Inside humidity
// 14 Current gust (i.e. 'Latest')
// 15 Wind chill
// 16 Heat Index
// 17 UV Index
// 18 Solar Radiation
// 19 Evapotranspiration
// 20 Annual Evapotranspiration
// 21 Apparent temperature
// 22 Current theoretical max solar radiation
// 23 Hours of sunshine so far today
// 24 Current wind bearing
// 25 RG-11 rain total
// 26 Rain since midnight
// 27 Feels like
// 28 Humidex
var rec = keyval.Value;
// make sure solar max is calculated for those stations without a solar sensor
Program.LogDebugMessage("DoLogFile: Writing log entry for " + rec.LogTime);
var inv = CultureInfo.InvariantCulture;
var sep = ",";
var sb = new StringBuilder(256);
sb.Append(rec.LogTime.ToString("dd/MM/yy", inv) + sep);
sb.Append(rec.LogTime.ToString("HH:mm", inv) + sep);
sb.Append(rec.Temperature.ToString(Program.Cumulus.TempFormat, inv) + sep);
sb.Append(rec.Humidity.ToString() + sep);
sb.Append(rec.Dewpoint.ToString(Program.Cumulus.TempFormat, inv) + sep);
sb.Append(rec.WindSpeed.ToString(Program.Cumulus.WindAvgFormat, inv) + sep);
sb.Append(rec.WindGust.ToString(Program.Cumulus.WindFormat, inv) + sep);
sb.Append(rec.WindBearing.ToString() + sep);
sb.Append(rec.RainfallRate.ToString(Program.Cumulus.RainFormat, inv) + sep);
sb.Append(rec.RainfallToday.ToString(Program.Cumulus.RainFormat, inv) + sep);
sb.Append(rec.Baro.ToString(Program.Cumulus.PressFormat, inv) + sep);
sb.Append(rec.RainfallCounter.ToString(Program.Cumulus.RainFormat, inv) + sep);
sb.Append(rec.InsideTemp.ToString(Program.Cumulus.TempFormat, inv) + sep);
sb.Append(rec.InsideHum.ToString() + sep);
sb.Append(rec.CurrentGust.ToString(Program.Cumulus.WindFormat, inv) + sep);
sb.Append(rec.WindChill.ToString(Program.Cumulus.TempFormat, inv) + sep);
sb.Append(rec.HeatIndex.ToString(Program.Cumulus.TempFormat, inv) + sep);
sb.Append(rec.UVI.ToString(Program.Cumulus.UVFormat, inv) + sep);
sb.Append(rec.SolarRad.ToString() + sep);
sb.Append(rec.ET.ToString(Program.Cumulus.ETFormat, inv) + sep);
sb.Append(sep); // annual ET
sb.Append(rec.ApparentTemp.ToString(Program.Cumulus.TempFormat, inv) + sep);
sb.Append(rec.SolarMax.ToString() + sep);
sb.Append(rec.SunshineHours.ToString(Program.Cumulus.SunFormat, inv) + sep);
sb.Append(rec.WindBearing.ToString() + sep);
sb.Append(rec.RG11Rain.ToString(Program.Cumulus.RainFormat, inv) + sep);
sb.Append(rec.RainSinceMidnight.ToString(Program.Cumulus.RainFormat, inv) + sep);
sb.Append(rec.FeelsLike.ToString(Program.Cumulus.TempFormat, inv) + sep);
sb.Append(rec.Humidex.ToString(Program.Cumulus.TempFormat, inv));
return sb.ToString();
}
private static string GetLogFileName(DateTime thedate)
{
return thedate.ToString("yyyyMM") + "log.txt";
}
}
internal class LogFileRec
{
public DateTime LogTime;
public double Temperature;
public int Humidity;
public double Dewpoint;
public double WindSpeed;
public double WindGust;
public int WindBearing;
public double RainfallRate;
public double RainfallToday;
public double Baro;
public double RainfallCounter;
public double InsideTemp;
public double InsideHum;
public double CurrentGust;
public double WindChill;
public double HeatIndex;
public double UVI;
public int SolarRad;
public double ET;
public double ApparentTemp;
public int SolarMax;
public double SunshineHours = 0;
public int CurrentBearing;
public double RG11Rain = 0;
public double RainSinceMidnight;
public double FeelsLike;
public double Humidex;
}
}