Projekt

Obecné

Profil

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