Projekt

Obecné

Profil

Stáhnout (4.5 KB) Statistiky
| Větev: | Tag: | Revize:
1
using ServerApp.Parser.InputData;
2
using ServerApp.Parser.OutputInfo;
3
using System;
4
using System.Collections.Generic;
5
using System.IO;
6

    
7
namespace ServerApp.Parser.Parsers
8
{
9
    /// <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
    class WeatherParser
14
    {
15
        /// <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
        public List<WeatherInfo> ParseWeatherData(string folder, bool wholeDay = true, int interval = 1)
24
        {
25
            if (!Directory.Exists(folder))
26
                return null;
27

    
28
            List<WeatherInfo> list = new List<WeatherInfo>();
29

    
30
            // get all files in folder
31
            string[] fileEntries = Directory.GetFiles(folder);
32
            foreach (string fileName in fileEntries)
33
            {
34
                List<WeatherInfo> loadedData = null;
35
                
36
                // parse as one instance per day
37
                if (wholeDay)
38
                    loadedData = ProcessOneWeatherFileAsDays(fileName);
39
                // parse according to interval
40
                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
        /// <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
        private static List<WeatherInfo> ProcessOneWeatherFileAsDays(string path)
58
        {
59
            if (!File.Exists(path))
60
                return null;
61

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

    
65
            // array with data [temp, rain, wind, lum]
66
            double[] recordedAmount = new double[4];
67
            // min/max hour taken into account
68
            int[] minmaxHour = new int[] { 7, 18 };
69
            // interval length
70
            int range = minmaxHour[1] - minmaxHour[0];
71

    
72
            // 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
            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
                // 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
                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
                // aggregate data
95
                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

    
100
                values++;
101
            }
102

    
103
            // data from last day
104
            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
        private static List<ActivityInfo> ProcessOneWeatherFileAsIntervals(string path, int interval)
111
        {
112
            throw new NotImplementedException();
113
        }
114
    }
115
}
(5-5/5)