Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 98b568bc

Přidáno uživatelem Alex Konig před téměř 4 roky(ů)

re #8612 Weather parser in hourly intervals

Zobrazit rozdíly:

Server/ServerApp/Parser/Parsers/WeatherParser.cs
10 10
    /// Class parsing weather files into instances of WeatherInfo divided by given time interval
11 11
    /// Data parsed from 7am (included) to 18pm (included)
12 12
    /// </summary>
13
    /// <author>Alex Konig</author>
13 14
    class WeatherParser
14 15
    {
16

  
17
        /// <summary> Datafile loader  </summary>
18
        DataLoader loader;
19

  
20
        /// <summary>
21
        /// Constructor
22
        /// </summary>
23
        /// <param name="loader">Datafile loader</param>
24
        public WeatherParser(DataLoader loader)
25
        {
26
            this.loader = loader;
27
        }
28

  
15 29
        /// <summary>
16 30
        /// Parses weather data to WeatherInfo instances
17 31
        /// Data parsed from 7am (included) to 18pm (included)
......
39 53
                // parse according to interval
40 54
                else
41 55
                {
42
                    // pokud: konec dne nebo konec aktuálního intervalu -> vemu to co sem nasčítal
43
                    throw new NotImplementedException();
56
                    loadedData = ProcessOneWeatherFileAsIntervals(fileName, interval);
44 57
                }
45 58

  
46 59
                list.AddRange(loadedData);
......
54 67
        /// </summary>
55 68
        /// <param name="path">Path ti file</param>
56 69
        /// <returns>List with WeatherInfo instances</returns>
57
        private static List<WeatherInfo> ProcessOneWeatherFileAsDays(string path)
70
        private List<WeatherInfo> ProcessOneWeatherFileAsDays(string path)
58 71
        {
59 72
            if (!File.Exists(path))
60 73
                return null;
61 74

  
62 75
            List<WeatherInfo> weatherInfo = new List<WeatherInfo>();
63
            List<WeatherInstance> list = CsvDataLoader.LoadWeatherFile(path);
76
            List<WeatherInstance> list = loader.LoadWeatherFile(path);
64 77

  
65 78
            // array with data [temp, rain, wind, lum]
66 79
            double[] recordedAmount = new double[4];
......
72 85
            // first day
73 86
            DateTime lastStartDay = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
74 87
            // number of values in day
75
            int values = 0;
88
            int values = 0, weatherValues = 0;
76 89
            for (int i = 0; i < list.Count; i++)
77 90
            {
78 91
                int currHour = list[i].dateTime.Hour;
......
83 96
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, minmaxHour[0], 0, 0);
84 97
                if (!date.Equals(lastStartDay))
85 98
                {
86
                    WeatherInfo dayInfo = new WeatherInfo(lastStartDay, recordedAmount[0] / values, (int)(recordedAmount[1] / values * 100), recordedAmount[2] / values, recordedAmount[3] / values, range); 
99
                    WeatherInfo dayInfo = new WeatherInfo(lastStartDay, recordedAmount[0] / values, (int)(recordedAmount[1] / values * 100), recordedAmount[2] / values, recordedAmount[3] / weatherValues, range); 
87 100
                    weatherInfo.Add(dayInfo);
88 101

  
89 102
                    recordedAmount = new double[4];
90 103
                    lastStartDay = date;
91 104
                    values = 0;
105
                    weatherValues = 0;
92 106
                }
93 107

  
94 108
                // aggregate data
95 109
                recordedAmount[0] += list[i].temp;
96 110
                recordedAmount[1] += list[i].rain;
97 111
                recordedAmount[2] += list[i].wind;
98
                recordedAmount[3] += list[i].lum * 1000;
112

  
113
                if (LuxToConditions.TransferLuxToConditions(list[i].lum * 1000) != WeatherConditions.Dark)
114
                    recordedAmount[3] += list[i].lum * 1000; weatherValues++;
99 115

  
100 116
                values++;
101 117
            }
......
107 123
            return weatherInfo;
108 124
        }
109 125

  
110
        private static List<ActivityInfo> ProcessOneWeatherFileAsIntervals(string path, int interval)
126
        /// <summary>
127
        /// Parses data from one data file as one instance per interval length
128
        /// </summary>
129
        /// <param name="path">Path to file</param>
130
        /// <param name="interval">Interval length</param>
131
        /// <returns>List with ActivityInfo instances</returns>
132
        private List<WeatherInfo> ProcessOneWeatherFileAsIntervals(string path, int interval)
111 133
        {
112
            throw new NotImplementedException();
134
            if (!File.Exists(path))
135
                return null;
136

  
137
            List<WeatherInfo> loignInfo = new List<WeatherInfo>();
138
            List<WeatherInstance> list = loader.LoadWeatherFile(path);
139

  
140
            // min/max hour taken into account
141
            int[] minmaxHour = new int[] { 7, 18 };
142
            int range = minmaxHour[1] - minmaxHour[0];
143

  
144
            if (interval > range)
145
                return null;
146

  
147
            int indices = (int)Math.Ceiling(range / (double)interval);
148
            int[] to = new int[indices];
149
            double[][] data = new double[indices][];
150
            int[] count = new int[indices];
151
            for (int i = 0; i < to.Length; i++)
152
            {
153
                to[i] = minmaxHour[0] + interval * (i + 1);
154
                data[i] = new double[4];
155
            }
156

  
157
            // first day
158
            DateTime lastStartTime = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
159
            int index;
160
            for (int i = 0; i < list.Count; i++)
161
            {
162
                int currHour = list[i].dateTime.Hour;
163
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
164
                    continue;
165

  
166
                // start of the day -> make an instance
167
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, list[i].dateTime.Hour, 0, 0);
168

  
169
                // end of the day
170
                if (!(date.Year == lastStartTime.Year && date.Month == lastStartTime.Month && date.Day == lastStartTime.Day))
171
                {
172
                    // note down all aggregated data
173
                    for (int k = 0; k < to.Length; k++)
174
                    {
175
                        int raincount = 0;
176
                        double rainval = 0;;
177
                        for (int l = 0; l <= k; l++)
178
                        {
179
                            raincount += count[l];
180
                            rainval += data[l][1];
181
                        }
182

  
183
                        DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
184

  
185
                        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);
186
                        if (count[k] != 0)
187
                            loignInfo.Add(intervalInfo);
188

  
189
                    }
190

  
191
                    lastStartTime = date;
192
                    count = new int[indices];
193
                    for (int l = 0; l < data.Length; l++)
194
                        data[l] = new double[4];
195
                }
196

  
197
                // find index for current instance
198
                index = 0;
199
                for (int k = 1; k < to.Length; k++)
200
                {
201
                    if (to[k] > list[i].dateTime.Hour && to[k - 1] <= list[i].dateTime.Hour)
202
                    {
203
                        index = k;
204
                        break;
205
                    }
206
                }
207

  
208
                // aggregate data
209
                data[index][0] += list[i].temp;
210
                data[index][1] += list[i].rain;
211
                data[index][2] += list[i].wind;
212
                data[index][3] += list[i].lum * 1000;
213
                count[index]++;
214

  
215
            }
216

  
217
            for (int k = 0; k < to.Length; k++)
218
            {
219
                int raincount = 0;
220
                double rainval = 0; ;
221
                for (int l = 0; l <= k; l++)
222
                {
223
                    raincount += count[l];
224
                    rainval += data[l][1];
225
                }
226

  
227
                DateTime stTime = new DateTime(lastStartTime.Year, lastStartTime.Month, lastStartTime.Day, to[k] - interval, 0, 0);
228

  
229
                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);
230
                if (count[k] != 0)
231
                    loignInfo.Add(intervalInfo);
232
            }
233

  
234
            return loignInfo;
113 235
        }
114 236
    }
237

  
115 238
}

Také k dispozici: Unified diff