-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
296 lines (242 loc) · 8.98 KB
/
Program.cs
File metadata and controls
296 lines (242 loc) · 8.98 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
using System.Diagnostics;
namespace ImportWC
{
static class Program
{
public static Cumulus Cumulus { get; set; } = new Cumulus();
public static string Location { get; set; } = string.Empty;
private static ConsoleColor defConsoleColour;
public static string WcDataPath { get; set; } = string.Empty;
public static string WcConfigTemp { get; set; } = string.Empty;
public static string WcConfigDew { get; set; } = string.Empty;
public static string WcConfigWind { get; set; } = string.Empty;
public static string WcConfigPress { get; set; } = string.Empty;
public static string WcConfigRain { get; set; } = string.Empty;
static int Main()
{
// Tell the user what is happening
TextWriterTraceListener myTextListener = new TextWriterTraceListener($"MXdiags{Path.DirectorySeparatorChar}ImportWC-{DateTime.Now:yyyyMMdd-HHmmss}.txt", "WClog");
Trace.Listeners.Add(myTextListener);
Trace.AutoFlush = true;
defConsoleColour = Console.ForegroundColor;
var fullVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version ?? new Version(0, 0, 0, 0);
var version = $"{fullVer.Major}.{fullVer.Minor}.{fullVer.Build}";
LogMessage("ImportWC v." + version);
Console.WriteLine("ImportWC v." + version);
// Read the config file
ReadWeatherCatConfig();
LogMessage("Processing started");
Console.WriteLine();
Console.WriteLine($"Processing started: {DateTime.Now:U}");
Console.WriteLine();
// get the location of the exe - we will assume this is in the Cumulus root folder
Location = AppDomain.CurrentDomain.BaseDirectory;
// Check meteo day
if (Cumulus.RolloverHour != 0)
{
LogMessage("Cumulus is not configured for a midnight rollover, so Import cannot create any day file entries");
LogConsole("Cumulus is not configured for a midnight rollover, so no day file entries will be created", ConsoleColor.DarkYellow);
LogConsole("You must run CreateMissing after this Import to create the day file entries", ConsoleColor.DarkYellow);
}
else
{
LogMessage("Cumulus is configured for a midnight rollover, Import will create day file entries");
LogConsole("Cumulus is configured for a midnight rollover, so day file entries will be created", ConsoleColor.Cyan);
LogConsole("You must still run CreateMissing after this Import to add missing details to those day file entries", ConsoleColor.Cyan);
}
Console.WriteLine();
// Find all the wlk files
// naming convention YYYY-MM.wlk, eg 2024-05.wlk
LogMessage("Searching for cat files");
Console.WriteLine("Searching for cat log files...");
if (!Directory.Exists(WcDataPath))
{
LogMessage($"The source directory '{WcDataPath}' does not exist, aborting");
LogConsole($"The source directory '{WcDataPath}' does not exist, aborting", ConsoleColor.Red);
Environment.Exit(1);
}
var dirInfo = new DirectoryInfo(WcDataPath);
var wcFiles = dirInfo.GetFiles("*_WeatherCatData.cat", SearchOption.AllDirectories);
LogMessage($"Found {wcFiles.Length} cat log files");
LogConsole($"Found {wcFiles.Length} cat log files", defConsoleColour);
// sort the file list
var wcList = wcFiles.OrderBy(f => f.FullName).ToList();
var year = 0;
var month = 0;
var rainCounter = 0.0;
foreach (var cat in wcList)
{
string[] lines;
try
{
lines = File.ReadAllLines(cat.FullName);
}
catch (Exception ex)
{
LogMessage($"Error opening file {cat.FullName} - {ex.Message}");
LogConsole($"Error opening file {cat.FullName} - {ex.Message}", ConsoleColor.Red);
LogConsole("Skipping to next file", defConsoleColour);
// abort this file
continue;
}
// get the year/month from the filename
// year is folder name containg the file
// month is the first 1 or 2 characters of the filename followed by an underscore
// eg /wc/2024/5_WeatherCatData.cat
var name = new DirectoryInfo(cat.FullName);
if (name.Parent == null || !int.TryParse(name.Parent.Name, out year))
{
LogMessage($"Error parsing year from {cat.FullName}");
LogConsole($"Error parsing year from {cat.FullName}", ConsoleColor.Red);
LogConsole("Skipping to next file", defConsoleColour);
// abort this file
continue;
}
if (!int.TryParse(cat.Name.Split('_')[0], out month))
{
LogMessage($"Error parsing month from {cat.FullName}");
LogConsole($"Error parsing month from {cat.FullName}", ConsoleColor.Red);
LogConsole("Skipping to next file", defConsoleColour);
// abort this file
continue;
}
LogConsole($"Processing {year} {cat.Name}...", ConsoleColor.Gray);
LogMessage($"Processing {cat.FullName}...");
LogMessage($" {year} {cat.Name} contains {lines.Length} lines");
var started = false;
foreach (var line in lines)
{
// we want to start reading data after the line containing "VERS:"
if (!started && !line.StartsWith("VERS:"))
{
continue;
}
else
{
if (!started)
{
started = true;
continue;
}
else if (line.Length == 0)
{
// skip blank lines afer VERS:
continue;
}
}
var rec = new WeatherCatRecord(year, month, line);
LogFile.AddRecord(rec);
if (rec.HasExtraData)
{
ExtraLogFile.AddRecord(rec);
}
if (rec.HasSynthData)
{
CustomLogFile.AddRecord(rec);
}
}
// Write out the log file
if (LogFile.RecordsCount > 0)
{
rainCounter += LogFile.LastRainCounter;
LogFile.WriteLogFile();
LogFile.Initialise();
}
if (ExtraLogFile.RecordsCount > 0)
{
ExtraLogFile.WriteLogFile();
ExtraLogFile.Initialise();
}
if (CustomLogFile.RecordsCount > 0)
{
CustomLogFile.WriteLogFile();
CustomLogFile.Initialise();
}
}
return 0;
}
public static void LogMessage(string message)
{
Trace.TraceInformation(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + message);
}
public static void LogDebugMessage(string message)
{
#if DEBUG
//Trace.TraceInformation(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + message)
#endif
}
public static void LogConsole(string msg, ConsoleColor colour, bool newLine = true)
{
Console.ForegroundColor = colour;
if (newLine)
{
Console.WriteLine(msg);
}
else
{
Console.Write(msg);
}
Console.ForegroundColor = defConsoleColour;
}
private static void ReadWeatherCatConfig()
{
if (!System.IO.File.Exists(Program.Location + "wc_config.ini"))
{
Program.LogMessage($"Failed to find wc_config.ini file at {Program.Location}wc_config.ini");
Console.WriteLine($"Failed to find wc_config.ini file at {Program.Location}wc_config.ini");
Environment.Exit(1);
}
Program.LogMessage("Reading wc_config.ini file");
IniFile ini = new IniFile("wc_config.ini");
WcDataPath = ini.GetValue("data", "path", "");
if (WcDataPath == "")
{
Program.LogMessage("Failed to find data path in wc_config.ini");
Console.WriteLine("Failed to find data path in wc_config.ini");
Environment.Exit(1);
}
// Check if the data path exists
if (!Directory.Exists(WcDataPath))
{
Program.LogMessage($"The data path specified in wc_config.ini does not exist - {WcDataPath}");
Console.WriteLine($"The data path specified in wc_config.ini does not exist - {WcDataPath}");
Environment.Exit(1);
}
WcConfigTemp = ini.GetValue("units", "temperature", "").ToLower();
if (WcConfigTemp == "" || (WcConfigTemp != "c" && WcConfigTemp != "f"))
{
Program.LogMessage("Failed to find temperature units in wc_config.ini");
Console.WriteLine("Failed to find temperature units in wc_config.ini");
Environment.Exit(1);
}
WcConfigDew = ini.GetValue("units", "dewpoint", "").ToLower();
if (WcConfigDew == "" || (WcConfigDew != "c" && WcConfigDew != "f"))
{
Program.LogMessage("Failed to find dewpoint units in wc_config.ini");
Console.WriteLine("Failed to find dewpoint units in wc_config.ini");
Environment.Exit(1);
}
WcConfigPress = ini.GetValue("units", "pressure", "").ToLower();
if (WcConfigPress == "" || (WcConfigPress != "inhg" && WcConfigPress != "mb" && WcConfigPress != "hpa"))
{
Program.LogMessage("Failed to find pressure units in wc_config.ini");
Console.WriteLine("Failed to find pressure units in wc_config.ini");
Environment.Exit(1);
}
WcConfigWind = ini.GetValue("units", "wind", "");
if (WcConfigWind == "" || (WcConfigWind != "kph" && WcConfigWind != "mps" && WcConfigWind != "mph" && WcConfigWind != "knots"))
{
Program.LogMessage("Failed to find wind units in wc_config.ini");
Console.WriteLine("Failed to find wind units in wc_config.ini");
Environment.Exit(1);
}
WcConfigRain = ini.GetValue("units", "rain", "");
if (WcConfigRain == "" || (WcConfigRain != "mm" && WcConfigRain != "in"))
{
Program.LogMessage("Failed to find rain units in wc_config.ini");
Console.WriteLine("Failed to find rain units in wc_config.ini");
Environment.Exit(1);
}
}
}
}