Projekt

Obecné

Profil

Stáhnout (4.5 KB) Statistiky
| Větev: | Tag: | Revize:
1 734533a8 A-Konig
using ServerApp.Parser.InputData;
2
using ServerApp.Parser.OutputInfo;
3 0da0ac88 A-Konig
using System;
4 5d9a5bd9 A-Konig
using System.Collections.Generic;
5 0da0ac88 A-Konig
using System.IO;
6 5d9a5bd9 A-Konig
7 734533a8 A-Konig
namespace ServerApp.Parser.Parsers
8 5d9a5bd9 A-Konig
{
9 734533a8 A-Konig
    /// <summary>
10
    /// Class parsing weather files into instances of WeatherInfo divided by given time interval
11
    /// Data parsed from 7am (included) to 18pm (included)
12
    /// </summary>
13 5d9a5bd9 A-Konig
    class WeatherParser
14
    {
15 734533a8 A-Konig
        /// <summary>
16
        /// Parses weather data to WeatherInfo instances
17
        /// Data parsed from 7am (included) to 18pm (included)
18
        /// </summary>
19
        /// <param name="folder">Folder with weather data files</param>
20
        /// <param name="wholeDay">Should data be parsed as one instance per day (if true parameter interval will be ignored)</param>
21
        /// <param name="interval">Time interval to divide days by, minimum is 1h</param>
22
        /// <returns></returns>
23 0da0ac88 A-Konig
        public List<WeatherInfo> ParseWeatherData(string folder, bool wholeDay = true, int interval = 1)
24
        {
25
            if (!Directory.Exists(folder))
26
                return null;
27
28 734533a8 A-Konig
            List<WeatherInfo> list = new List<WeatherInfo>();
29
30
            // get all files in folder
31 0da0ac88 A-Konig
            string[] fileEntries = Directory.GetFiles(folder);
32
            foreach (string fileName in fileEntries)
33
            {
34
                List<WeatherInfo> loadedData = null;
35 734533a8 A-Konig
                
36
                // parse as one instance per day
37 0da0ac88 A-Konig
                if (wholeDay)
38
                    loadedData = ProcessOneWeatherFileAsDays(fileName);
39 734533a8 A-Konig
                // parse according to interval
40 0da0ac88 A-Konig
                else
41
                {
42
                    // pokud: konec dne nebo konec aktuálního intervalu -> vemu to co sem nasčítal
43
                    throw new NotImplementedException();
44
                }
45
46
                list.AddRange(loadedData);
47
            }
48
49
            return list;
50
        }
51
52 734533a8 A-Konig
        /// <summary>
53
        /// Parses data from one data file as one instance per day
54
        /// </summary>
55
        /// <param name="path">Path ti file</param>
56
        /// <returns>List with WeatherInfo instances</returns>
57 0da0ac88 A-Konig
        private static List<WeatherInfo> ProcessOneWeatherFileAsDays(string path)
58
        {
59 734533a8 A-Konig
            if (!File.Exists(path))
60
                return null;
61 0da0ac88 A-Konig
62 734533a8 A-Konig
            List<WeatherInfo> weatherInfo = new List<WeatherInfo>();
63 0da0ac88 A-Konig
            List<WeatherInstance> list = CsvDataLoader.LoadWeatherFile(path);
64
65 734533a8 A-Konig
            // array with data [temp, rain, wind, lum]
66 0da0ac88 A-Konig
            double[] recordedAmount = new double[4];
67 734533a8 A-Konig
            // min/max hour taken into account
68 0da0ac88 A-Konig
            int[] minmaxHour = new int[] { 7, 18 };
69 734533a8 A-Konig
            // interval length
70 0da0ac88 A-Konig
            int range = minmaxHour[1] - minmaxHour[0];
71
72 734533a8 A-Konig
            // first day
73
            DateTime lastStartDay = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day, minmaxHour[0], 0, 0);
74
            // number of values in day
75 0da0ac88 A-Konig
            int values = 0;
76
            for (int i = 0; i < list.Count; i++)
77
            {
78
                int currHour = list[i].dateTime.Hour;
79
                if (currHour < minmaxHour[0] || currHour > minmaxHour[1])
80
                    continue;
81
82 734533a8 A-Konig
                // start of new day -> make a new instance
83
                DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day, minmaxHour[0], 0, 0);
84 0da0ac88 A-Konig
                if (!date.Equals(lastStartDay))
85
                {
86
                    WeatherInfo dayInfo = new WeatherInfo(lastStartDay, recordedAmount[0] / values, (int)(recordedAmount[1] / values * 100), recordedAmount[2] / values, recordedAmount[3] / values, range); 
87
                    weatherInfo.Add(dayInfo);
88
89
                    recordedAmount = new double[4];
90
                    lastStartDay = date;
91
                    values = 0;
92
                }
93
94 734533a8 A-Konig
                // aggregate data
95 0da0ac88 A-Konig
                recordedAmount[0] += list[i].temp;
96
                recordedAmount[1] += list[i].rain;
97
                recordedAmount[2] += list[i].wind;
98
                recordedAmount[3] += list[i].lum * 1000;
99 734533a8 A-Konig
100 0da0ac88 A-Konig
                values++;
101
            }
102
103 734533a8 A-Konig
            // data from last day
104 0da0ac88 A-Konig
            WeatherInfo dayInfo2 = new WeatherInfo(lastStartDay, recordedAmount[0] / values, (int)(recordedAmount[1] / values * 100), recordedAmount[2] / values, recordedAmount[3] / values, range); 
105
            weatherInfo.Add(dayInfo2);
106
107
            return weatherInfo;
108
        }
109
110 734533a8 A-Konig
        private static List<ActivityInfo> ProcessOneWeatherFileAsIntervals(string path, int interval)
111 0da0ac88 A-Konig
        {
112
            throw new NotImplementedException();
113
        }
114 5d9a5bd9 A-Konig
    }
115
}