-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogFile.cs
295 lines (244 loc) · 9.4 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
using System.Globalization;
using System.Text;
namespace ImportWC
{
static partial class LogFile
{
private static readonly SortedList<DateTime, LogFileRec> records = [];
public static int RecordsCount { get => records.Count; }
public static double LastRainCounter { get => records.Last().Value.RainfallCounter; }
private static double startOfDayRainCounter = -999;
public static DateTime LastTimeStamp { get; set; }
internal static void Initialise()
{
records.Clear();
LastTimeStamp = DateTime.MinValue;
}
internal static void AddRecord(WeatherCatRecord rec)
{
if (!rec.RainYear.HasValue && LastTimeStamp.Date != rec.Timestamp.Date)
{
startOfDayRainCounter = rec.RainDay ?? 0;
}
LastTimeStamp = rec.Timestamp;
if (!records.TryGetValue(rec.Timestamp, out var value))
{
value = new LogFileRec() { LogTime = rec.Timestamp };
records.Add(rec.Timestamp, value);
}
value.Temperature = rec.OutsideTemp ?? 0;
value.Humidity = rec.OutsideHumidity ?? 0;
value.Dewpoint = rec.Dewpoint ?? 0;
value.WindSpeed = rec.WindSpeed ?? 0;
value.WindGust = rec.WindGust ?? 0;
value.WindBearing = rec.WindDir;
if (rec.RainYear.HasValue)
{
value.RainfallToday = rec.RainDay ?? 0;
value.RainfallCounter = rec.RainYear ?? 0;
}
else
{
value.RainfallToday = (rec.RainDay ?? 0) - startOfDayRainCounter;
value.RainfallCounter = rec.RainDay ?? 0;
}
value.RainfallRate = rec.RainRate ?? 0;
value.RainSinceMidnight = value.RainfallToday;
value.Baro = rec.Baro ?? 0;
value.InsideTemp = rec.InsideTemp ?? 0;
value.InsideHum = rec.InsideHumidity ?? 0;
value.CurrentGust = rec.WindGust ?? 0;
value.WindChill = rec.WindChill ?? 0;
value.UVI = rec.UV ?? 0;
value.SolarRad = rec.Solar ?? 0;
value.ET = rec.ET ?? 0;
value.ETyear = rec.ETYear ?? 0;
value.CurrentBearing = rec.WindDir;
if (rec.OutsideTemp.HasValue && rec.OutsideHumidity.HasValue)
{
var 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 (!rec.Dewpoint.HasValue)
{
val = MeteoLib.DewPoint(ConvertUnits.UserTempToC(value.Temperature), value.Humidity);
value.Dewpoint = ConvertUnits.TempCToUser(val);
}
if (rec.WindSpeed.HasValue)
{
val = MeteoLib.ApparentTemperature(ConvertUnits.UserTempToC(value.Temperature), ConvertUnits.UserWindToMS(value.WindSpeed), value.Humidity);
value.ApparentTemp = ConvertUnits.TempCToUser(val);
val = MeteoLib.FeelsLike(ConvertUnits.UserTempToC(value.Temperature), ConvertUnits.UserWindToKPH(value.WindSpeed), value.Humidity);
value.FeelsLike = ConvertUnits.TempCToUser(val);
}
}
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); // 0
sb.Append(rec.LogTime.ToString("HH:mm", inv) + sep); // 1
sb.Append(rec.Temperature.ToString(Program.Cumulus.TempFormat, inv) + sep); // 2
sb.Append(rec.Humidity.ToString() + sep); // 3
sb.Append(rec.Dewpoint.ToString(Program.Cumulus.TempFormat, inv) + sep); // 4
sb.Append(rec.WindSpeed.ToString(Program.Cumulus.WindAvgFormat, inv) + sep); // 5
sb.Append(rec.WindGust.ToString(Program.Cumulus.WindFormat, inv) + sep); // 6
sb.Append(rec.WindBearing.ToString() + sep); // 7
sb.Append(rec.RainfallRate.ToString(Program.Cumulus.RainFormat, inv) + sep); // 8
sb.Append(rec.RainfallToday.ToString(Program.Cumulus.RainFormat, inv) + sep); // 9
sb.Append(rec.Baro.ToString(Program.Cumulus.PressFormat, inv) + sep); // 10
sb.Append(rec.RainfallCounter.ToString(Program.Cumulus.RainFormat, inv) + sep); // 11
sb.Append(rec.InsideTemp.ToString(Program.Cumulus.TempFormat, inv) + sep); // 12
sb.Append(rec.InsideHum.ToString() + sep); // 13
sb.Append(rec.CurrentGust.ToString(Program.Cumulus.WindFormat, inv) + sep); // 14
sb.Append(rec.WindChill.ToString(Program.Cumulus.TempFormat, inv) + sep); // 15
sb.Append(rec.HeatIndex.ToString(Program.Cumulus.TempFormat, inv) + sep); // 16
sb.Append(rec.UVI.ToString(Program.Cumulus.UVFormat, inv) + sep); // 17
sb.Append(rec.SolarRad.ToString() + sep); // 18
sb.Append(rec.ET.ToString(Program.Cumulus.ETFormat, inv) + sep); // 19
sb.Append(rec.ETyear.ToString(Program.Cumulus.ETFormat, inv) + sep); // annual ET
sb.Append(rec.ApparentTemp.ToString(Program.Cumulus.TempFormat, inv) + sep); // 21
sb.Append(rec.SolarMax.ToString() + sep); // 22
sb.Append(rec.SunshineHours.ToString(Program.Cumulus.SunFormat, inv) + sep); // 23
sb.Append(rec.WindBearing.ToString() + sep); // 24
sb.Append(rec.RG11Rain.ToString(Program.Cumulus.RainFormat, inv) + sep); // 25
sb.Append(rec.RainSinceMidnight.ToString(Program.Cumulus.RainFormat, inv) + sep); // 26
sb.Append(rec.FeelsLike.ToString(Program.Cumulus.TempFormat, inv) + sep); // 27
sb.Append(rec.Humidex.ToString(Program.Cumulus.TempFormat, inv)); // 28
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 ETyear;
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;
}
}