-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathNOAAReports.cs
299 lines (265 loc) · 8.15 KB
/
NOAAReports.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace CumulusMX
{
internal class NOAAReports
{
private readonly Cumulus cumulus;
private readonly WeatherStation station;
private string noaafile;
public NOAAReports(Cumulus cumulus, WeatherStation station)
{
this.cumulus = cumulus;
this.station = station;
}
public string GenerateNoaaYearReport(int year)
{
NOAA noaa = new NOAA(cumulus, station);
DateTime noaats = new DateTime(year, 1, 1);
cumulus.LogMessage("Creating NOAA yearly report");
var report = noaa.CreateYearlyReport(noaats);
try
{
// If not using UTF, then we have to convert the character set
var utf8WithoutBom = new UTF8Encoding(false);
var encoding = cumulus.NOAAconf.UseUtf8 ? utf8WithoutBom : Encoding.GetEncoding("iso-8859-1");
var reportName = noaats.ToString(cumulus.NOAAconf.YearFile);
noaafile = cumulus.ReportPath + reportName;
cumulus.LogMessage("Saving yearly NOAA report as " + noaafile);
File.WriteAllText(noaafile, report, encoding);
}
catch (Exception e)
{
cumulus.LogErrorMessage($"Error creating NOAA yearly report: {e.Message}");
throw;
}
return report;
}
public string GenerateNoaaMonthReport(int year, int month)
{
NOAA noaa = new NOAA(cumulus, station);
DateTime noaats = new DateTime(year, month, 1);
cumulus.LogMessage("Creating NOAA monthly report");
var report = noaa.CreateMonthlyReport(noaats);
var reportName = String.Empty;
try
{
// If not using UTF, then we have to convert the character set
var utf8WithoutBom = new UTF8Encoding(false);
var encoding = cumulus.NOAAconf.UseUtf8 ? utf8WithoutBom : Encoding.GetEncoding("iso-8859-1");
reportName = noaats.ToString(cumulus.NOAAconf.MonthFile);
noaafile = cumulus.ReportPath + reportName;
cumulus.LogMessage("Saving monthly NOAA report as " + noaafile);
File.WriteAllText(noaafile, report, encoding);
}
catch (Exception e)
{
cumulus.LogErrorMessage($"Error creating NOAA yearly report '{reportName}': {e.Message}");
throw;
}
return report;
}
public string GenerateMissing()
{
var missingMonths = new List<DateTime>();
var missingYears = new List<DateTime>();
var checkDate = cumulus.RecordsBeganDateTime.Date;
string reportName;
var now = DateTime.Now;
var lastRptDate = GetLastReportDate();
var lastYear = 0;
// iterate all years and months since records began date
var doMore = true;
while (doMore)
{
// first check the yearly report
if (lastYear != checkDate.Year)
{
reportName = checkDate.ToString(cumulus.NOAAconf.YearFile);
if (!File.Exists(cumulus.ReportPath + reportName))
{
missingYears.Add(checkDate);
}
lastYear = checkDate.Year;
}
// then check the monthly report
reportName = checkDate.ToString(cumulus.NOAAconf.MonthFile);
if (!File.Exists(cumulus.ReportPath + reportName))
{
missingMonths.Add(checkDate);
}
// increment the month
// note this may reset the day
checkDate = checkDate.AddMonths(1);
if (checkDate.Year == lastRptDate.Year && checkDate.Month == lastRptDate.Month)
{
doMore = false;
}
}
if (missingMonths.Count > 0 || missingYears.Count > 0)
{
// spawn a task to recreate the reports, but don't wait for it to complete
Task.Run(() =>
{
// first do the months
foreach (var month in missingMonths)
{
GenerateNoaaMonthReport(month.Year, month.Month);
}
// then the years
foreach (var year in missingYears)
{
GenerateNoaaYearReport(year.Year);
}
});
// report back how many reports are being created
var sb = new StringBuilder("Recreating the following reports...\n");
if (missingMonths.Count > 0)
{
sb.AppendLine("Monthly:");
foreach (var rpt in missingMonths)
{
sb.AppendLine("\t" + rpt.ToString("MMM yyyy"));
}
}
if (missingYears.Count > 0)
{
sb.AppendLine("\nYearly:");
foreach (var rpt in missingYears)
{
sb.AppendLine("\t" + rpt.ToString("yyyy"));
}
}
sb.Append("\nThis may take a little while, you can check the progress in the MX diags log");
return sb.ToString();
}
else
{
return "There are no missing reports to recreate. If you want to recreate some existing reports you must first delete them from your Reports folder";
}
}
public string GetNoaaYearReport(int year)
{
DateTime noaats = new DateTime(year, 1, 1);
var reportName = string.Empty;
var report = string.Empty;
try
{
reportName = noaats.ToString(cumulus.NOAAconf.YearFile);
noaafile = cumulus.ReportPath + reportName;
var encoding = cumulus.NOAAconf.UseUtf8 ? Encoding.GetEncoding("utf-8") : Encoding.GetEncoding("iso-8859-1");
report = File.Exists(noaafile) ? File.ReadAllText(noaafile, encoding) : "That report does not exist";
}
catch (Exception e)
{
cumulus.LogErrorMessage($"Error getting NOAA yearly report '{reportName}': {e.Message}");
report = "Something went wrong!";
}
return report;
}
public string GetNoaaMonthReport(int year, int month)
{
DateTime noaats = new DateTime(year, month, 1);
var reportName = string.Empty;
var report = string.Empty;
try
{
reportName = noaats.ToString(cumulus.NOAAconf.MonthFile);
noaafile = cumulus.ReportPath + reportName;
var encoding = cumulus.NOAAconf.UseUtf8 ? Encoding.GetEncoding("utf-8") : Encoding.GetEncoding("iso-8859-1");
report = File.Exists(noaafile) ? File.ReadAllText(noaafile, encoding) : "That report does not exist";
}
catch (Exception e)
{
cumulus.LogErrorMessage($"Error getting NOAA monthly report '{reportName}': {e.Message}");
report = "Something went wrong!";
}
return report;
}
public string GetLastNoaaYearReportFilename(DateTime dat, bool fullPath)
{
// First determine the date for the log file.
// If we're using 9am roll-over, the date should be 9 hours (10 in summer)
// before 'Now'
// This assumes that the caller has already subtracted a day if required
DateTime logfiledate;
if (cumulus.RolloverHour == 0)
{
logfiledate = dat.AddDays(-1);
}
else
{
if (cumulus.Use10amInSummer && TimeZoneInfo.Local.IsDaylightSavingTime(dat))
{
// Locale is currently on Daylight (summer) time
logfiledate = dat.AddHours(-10);
}
else
{
// Locale is currently on Standard time or unknown
logfiledate = dat.AddHours(-9);
}
}
if (fullPath)
return cumulus.ReportPath + logfiledate.ToString(cumulus.NOAAconf.YearFile);
else
return logfiledate.ToString(cumulus.NOAAconf.YearFile);
}
public string GetLastNoaaMonthReportFilename(DateTime dat, bool fullPath)
{
// First determine the date for the log file.
// If we're using 9am roll-over, the date should be 9 hours (10 in summer)
// before 'Now'
// This assumes that the caller has already subtracted a day if required
DateTime logfiledate;
if (cumulus.RolloverHour == 0)
{
logfiledate = dat.AddDays(-1);
}
else
{
if (cumulus.Use10amInSummer && TimeZoneInfo.Local.IsDaylightSavingTime(dat))
{
// Locale is currently on Daylight (summer) time
logfiledate = dat.AddHours(-10);
}
else
{
// Locale is currently on Standard time or unknown
logfiledate = dat.AddHours(-9);
}
}
if (fullPath)
return cumulus.ReportPath + logfiledate.AddHours(-1).ToString(cumulus.NOAAconf.MonthFile);
else
return logfiledate.AddHours(-1).ToString(cumulus.NOAAconf.MonthFile);
}
private DateTime GetLastReportDate()
{
// returns the datetime of the latest possible report
var now = DateTime.Now;
DateTime reportDate;
if (cumulus.RolloverHour == 0)
{
reportDate = now.AddDays(-1);
}
else
{
if (cumulus.Use10amInSummer && TimeZoneInfo.Local.IsDaylightSavingTime(now))
{
// Locale is currently on Daylight (summer) time
reportDate = now.AddHours(-10);
}
else
{
// Locale is currently on Standard time or unknown
reportDate = now.AddHours(-9);
}
}
return reportDate.Date;
}
}
}