1
|
//
|
2
|
// Author: A. Konig
|
3
|
//
|
4
|
|
5
|
using log4net;
|
6
|
using ServerApp.Parser.InputData;
|
7
|
using ServerApp.Parser.OutputInfo;
|
8
|
using System;
|
9
|
using System.Collections.Generic;
|
10
|
using System.IO;
|
11
|
|
12
|
namespace ServerApp.Parser.Parsers
|
13
|
{
|
14
|
/// <summary>
|
15
|
/// Class parsing weather files into instances of WeatherInfo divided by given time interval
|
16
|
/// Data parsed from 7am (included) to 18pm (included)
|
17
|
/// </summary>
|
18
|
/// <author>A. Konig</author>
|
19
|
public class WeatherParser
|
20
|
{
|
21
|
/// <summary> Logger </summary>
|
22
|
private static readonly ILog _log = LogManager.GetLogger(typeof(WeatherParser));
|
23
|
|
24
|
/// <summary> Datafile loader </summary>
|
25
|
IDataLoader loader;
|
26
|
|
27
|
/// <summary>
|
28
|
/// Constructor
|
29
|
/// </summary>
|
30
|
/// <param name="loader">Datafile loader</param>
|
31
|
public WeatherParser(IDataLoader loader)
|
32
|
{
|
33
|
this.loader = loader;
|
34
|
}
|
35
|
|
36
|
/// <summary>
|
37
|
/// Parses weather data to WeatherInfo instances
|
38
|
/// Data parsed from 7am (included) to 18pm (included)
|
39
|
/// </summary>
|
40
|
/// <param name="weatherFiles">Paths to files with weather data files</param>
|
41
|
/// <param name="endTime">End time of related data</param>
|
42
|
/// <param name="startTime">Start time of related data</param>
|
43
|
/// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
|
44
|
/// <param name="interval">Time interval to divide days by, minimum is 1h</param>
|
45
|
/// <returns></returns>
|
46
|
public List<WeatherInfo> ParseWeatherData(List<string> weatherFiles, DateTime startTime, DateTime endTime, bool wholeDay = true, int interval = 1)
|
47
|
{
|
48
|
List<WeatherInfo> list = new List<WeatherInfo>();
|
49
|
|
50
|
if (weatherFiles == null || interval <= 0)
|
51
|
return list;
|
52
|
|
53
|
string current = "";
|
54
|
try
|
55
|
{
|
56
|
// get all files in folder
|
57
|
foreach (string fileName in weatherFiles)
|
58
|
{
|
59
|
current = fileName;
|
60
|
|
61
|
if (!File.Exists(fileName))
|
62
|
continue;
|
63
|
|
64
|
// parse as one instance per day
|
65
|
List<WeatherInfo> loadedData = null;
|
66
|
if (wholeDay)
|
67
|
loadedData = ProcessOneWeatherFileAsDays(fileName, startTime, endTime);
|
68
|
// parse according to interval
|
69
|
else
|
70
|
{
|
71
|
loadedData = ProcessOneWeatherFileAsIntervals(fileName, interval, startTime, endTime);
|
72
|
}
|
73
|
|
74
|
list.AddRange(loadedData);
|
75
|
}
|
76
|
} catch
|
77
|
{
|
78
|
_log.Error("Incorrect Weather input file " + current);
|
79
|
}
|
80
|
|
81
|
return list;
|
82
|
}
|
83
|
|
84
|
/// <summary>
|
85
|
/// Parses data from one data file as one instance per day
|
86
|
/// </summary>
|
87
|
/// <param name="path">Path ti file</param>
|
88
|
/// <param name="endTime">End time of related data</param>
|
89
|
/// <param name="startTime">Start time of related data</param>
|
90
|
/// <returns>List with WeatherInfo instances</returns>
|
91
|
private List<WeatherInfo> ProcessOneWeatherFileAsDays(string path, DateTime startTime, DateTime endTime)
|
92
|
{
|
93
|
List<WeatherInfo> weatherInfo = new List<WeatherInfo>();
|
94
|
|
95
|
List<WeatherInstance> list = loader.LoadWeatherFile(path);
|
96
|
if (list == null || list.Count == 0)
|
97
|
return weatherInfo;
|
98
|
|
99
|
// array with data [temp, rain, wind, lum]
|
100
|
double[] recordedAmount = new double[4];
|
101
|
// min/max hour taken into account
|
102
|
int[] minmaxHour = new int[] { 7, 18 };
|
103
|
// interval length
|
104
|
int range = minmaxHour[1] - minmaxHour[0];
|
105
|
|
106
|
// first day
|
107
|
DateTime lastStartDay = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
|
108
|
// number of values in day
|
109
|
int values = 0, weatherValues = 0;
|
110
|
for (int i = 0; i < list.Count; i++)
|
111
|
{
|
112
|
int currHour = list[i].dateTime.Hour;
|
113
|
if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
|
114
|
continue;
|
115
|
|
116
|
// start of new day -> make a new instance
|
117
|
DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, minmaxHour[0], 0, 0);
|
118
|
if (!date.Equals(lastStartDay))
|
119
|
{
|
120
|
WeatherInfo dayInfo = new WeatherInfo(lastStartDay, recordedAmount[0] / values, (int)(recordedAmount[1] / values * 100), recordedAmount[2] / values, recordedAmount[3] / weatherValues, range);
|
121
|
weatherInfo.Add(dayInfo);
|
122
|
|
123
|
recordedAmount = new double[4];
|
124
|
lastStartDay = date;
|
125
|
values = 0;
|
126
|
weatherValues = 0;
|
127
|
}
|
128
|
|
129
|
// if not in allowed time window -> discard
|
130
|
if (list[i].dateTime < startTime || list[i].dateTime > endTime)
|
131
|
{
|
132
|
continue;
|
133
|
}
|
134
|
|
135
|
// aggregate data
|
136
|
recordedAmount[0] += list[i].temp;
|
137
|
recordedAmount[1] += list[i].rain;
|
138
|
recordedAmount[2] += list[i].wind;
|
139
|
|
140
|
if (ValueToConditions.TransferLuxToConditions(list[i].lum * 1000) != WeatherConditions.Dark)
|
141
|
{
|
142
|
recordedAmount[3] += list[i].lum * 1000; weatherValues++;
|
143
|
}
|
144
|
|
145
|
values++;
|
146
|
}
|
147
|
|
148
|
// data from last day
|
149
|
if (values != 0)
|
150
|
{
|
151
|
WeatherInfo dayInfo2 = new WeatherInfo(lastStartDay, recordedAmount[0] / values, (int)(recordedAmount[1] / values * 100), recordedAmount[2] / values, recordedAmount[3] / weatherValues, range);
|
152
|
weatherInfo.Add(dayInfo2);
|
153
|
}
|
154
|
|
155
|
return weatherInfo;
|
156
|
}
|
157
|
|
158
|
/// <summary>
|
159
|
/// Parses data from one data file as one instance per interval length
|
160
|
/// </summary>
|
161
|
/// <param name="path">Path to file</param>
|
162
|
/// <param name="interval">Interval length</param>
|
163
|
/// <param name="endTime">End time of related data</param>
|
164
|
/// <param name="startTime">Start time of related data</param>
|
165
|
/// <returns>List with ActivityInfo instances</returns>
|
166
|
private List<WeatherInfo> ProcessOneWeatherFileAsIntervals(string path, int interval, DateTime startTime, DateTime endTime)
|
167
|
{
|
168
|
List<WeatherInfo> weatherInfo = new List<WeatherInfo>();
|
169
|
|
170
|
List<WeatherInstance> list = loader.LoadWeatherFile(path);
|
171
|
if (list == null || list.Count == 0)
|
172
|
return weatherInfo;
|
173
|
|
174
|
// min/max hour taken into account
|
175
|
int[] minmaxHour = new int[] { 7, 18 };
|
176
|
int range = minmaxHour[1] - minmaxHour[0];
|
177
|
|
178
|
if (interval > range)
|
179
|
return null;
|
180
|
|
181
|
int indices = (int)Math.Ceiling(range / (double)interval);
|
182
|
int[] to = new int[indices];
|
183
|
double[][] data = new double[indices][];
|
184
|
int[] count = new int[indices];
|
185
|
for (int i = 0; i < to.Length; i++)
|
186
|
{
|
187
|
to[i] = minmaxHour[0] + interval * (i + 1);
|
188
|
data[i] = new double[4];
|
189
|
count[i] = 0;
|
190
|
}
|
191
|
|
192
|
// first day
|
193
|
DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
|
194
|
int index;
|
195
|
for (int i = 0; i < list.Count; i++)
|
196
|
{
|
197
|
int currHour = list[i].dateTime.Hour;
|
198
|
if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
|
199
|
continue;
|
200
|
|
201
|
// start of the day -> make an instance
|
202
|
DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, list[i].dateTime.Hour, 0, 0);
|
203
|
|
204
|
// end of the day
|
205
|
if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
|
206
|
{
|
207
|
// note down all aggregated data
|
208
|
for (int k = 0; k < to.Length; k++)
|
209
|
{
|
210
|
int raincount = 0;
|
211
|
double rainval = 0;;
|
212
|
for (int l = 0; l <= k; l++)
|
213
|
{
|
214
|
raincount += count[l];
|
215
|
rainval += data[l][1];
|
216
|
}
|
217
|
|
218
|
DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
|
219
|
|
220
|
WeatherInfo intervalInfo = new WeatherInfo(stTime, data[k][0] / count[k], (int)((rainval / raincount) * 100), data[k][2] / count[k], data[k][3] / count[k], interval);
|
221
|
if (count[k] != 0)
|
222
|
weatherInfo.Add(intervalInfo);
|
223
|
|
224
|
}
|
225
|
|
226
|
lastStartTime = date;
|
227
|
count = new int[indices];
|
228
|
for (int l = 0; l < data.Length; l++)
|
229
|
{
|
230
|
data[l] = new double[4];
|
231
|
count[l] = 0;
|
232
|
}
|
233
|
}
|
234
|
|
235
|
// if not in allowed time window -> discard
|
236
|
if (list[i].dateTime < startTime || list[i].dateTime > endTime)
|
237
|
continue;
|
238
|
|
239
|
// find index for current instance
|
240
|
index = 0;
|
241
|
for (int k = 1; k < to.Length; k++)
|
242
|
{
|
243
|
if (to[k] > list[i].dateTime.Hour && to[k - 1] <= list[i].dateTime.Hour)
|
244
|
{
|
245
|
index = k;
|
246
|
break;
|
247
|
}
|
248
|
}
|
249
|
|
250
|
// aggregate data
|
251
|
data[index][0] += list[i].temp;
|
252
|
data[index][1] += list[i].rain;
|
253
|
data[index][2] += list[i].wind;
|
254
|
data[index][3] += list[i].lum * 1000;
|
255
|
count[index]++;
|
256
|
|
257
|
}
|
258
|
|
259
|
for (int k = 0; k < to.Length; k++)
|
260
|
{
|
261
|
int raincount = 0;
|
262
|
double rainval = 0; ;
|
263
|
for (int l = 0; l <= k; l++)
|
264
|
{
|
265
|
raincount += count[l];
|
266
|
rainval += data[l][1];
|
267
|
}
|
268
|
|
269
|
DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
|
270
|
|
271
|
WeatherInfo intervalInfo = new WeatherInfo(stTime, data[k][0] / count[k], (int)((rainval / raincount) * 100), data[k][2] / count[k], data[k][3] / count[k], interval);
|
272
|
if (count[k] != 0)
|
273
|
weatherInfo.Add(intervalInfo);
|
274
|
}
|
275
|
|
276
|
return weatherInfo;
|
277
|
}
|
278
|
}
|
279
|
|
280
|
}
|