Revize 0da0ac88
Přidáno uživatelem Alex Konig před asi 4 roky(ů)
Server/Parser 1.0/Parsers/WeatherParser.cs | ||
---|---|---|
1 |
using System; |
|
1 |
using Parser_1._0.InputData; |
|
2 |
using Parser_1._0.OutputInfo; |
|
3 |
using System; |
|
2 | 4 |
using System.Collections.Generic; |
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
5 |
using System.IO; |
|
6 | 6 |
|
7 | 7 |
namespace Parser_1._0.Parsers |
8 | 8 |
{ |
9 | 9 |
class WeatherParser |
10 | 10 |
{ |
11 |
|
|
12 |
public List<WeatherInfo> ParseWeatherData(string folder, bool wholeDay = true, int interval = 1) |
|
13 |
{ |
|
14 |
List<WeatherInfo> list = new List<WeatherInfo>(); |
|
15 |
|
|
16 |
// najít složku, ve složce sou data co se budou parsovat |
|
17 |
|
|
18 |
if (!Directory.Exists(folder)) |
|
19 |
return null; |
|
20 |
|
|
21 |
// když v jednej složce budou všechny jis data co chci zpracovat |
|
22 |
// pro každej soubor budu spouštět parsování |
|
23 |
string[] fileEntries = Directory.GetFiles(folder); |
|
24 |
foreach (string fileName in fileEntries) |
|
25 |
{ |
|
26 |
List<WeatherInfo> loadedData = null; |
|
27 |
// pokud po jednom dni |
|
28 |
if (wholeDay) |
|
29 |
loadedData = ProcessOneWeatherFileAsDays(fileName); |
|
30 |
// pokud po hodinách |
|
31 |
else |
|
32 |
{ |
|
33 |
// pokud: konec dne nebo konec aktuálního intervalu -> vemu to co sem nasčítal |
|
34 |
throw new NotImplementedException(); |
|
35 |
} |
|
36 |
|
|
37 |
list.AddRange(loadedData); |
|
38 |
} |
|
39 |
|
|
40 |
return list; |
|
41 |
} |
|
42 |
|
|
43 |
private static List<WeatherInfo> ProcessOneWeatherFileAsDays(string path) |
|
44 |
{ |
|
45 |
List<WeatherInfo> weatherInfo = new List<WeatherInfo>(); |
|
46 |
|
|
47 |
// načíst data ze souboru |
|
48 |
List<WeatherInstance> list = CsvDataLoader.LoadWeatherFile(path); |
|
49 |
|
|
50 |
//temp, rain, wind, lum |
|
51 |
double[] recordedAmount = new double[4]; |
|
52 |
int[] minmaxHour = new int[] { 7, 18 }; |
|
53 |
int range = minmaxHour[1] - minmaxHour[0]; |
|
54 |
|
|
55 |
// procházet data ze souboru |
|
56 |
DateTime lastStartDay = new DateTime(list[0].dateTime.Year, list[0].dateTime.Month, list[0].dateTime.Day); |
|
57 |
int values = 0; |
|
58 |
for (int i = 0; i < list.Count; i++) |
|
59 |
{ |
|
60 |
int currHour = list[i].dateTime.Hour; |
|
61 |
if (currHour < minmaxHour[0] || currHour > minmaxHour[1]) |
|
62 |
continue; |
|
63 |
|
|
64 |
// v každym dni agreguju |
|
65 |
DateTime date = new DateTime(list[i].dateTime.Year, list[i].dateTime.Month, list[i].dateTime.Day); |
|
66 |
if (!date.Equals(lastStartDay)) |
|
67 |
{ |
|
68 |
Console.WriteLine(values); |
|
69 |
Console.WriteLine(recordedAmount[0]); |
|
70 |
|
|
71 |
WeatherInfo dayInfo = new WeatherInfo(lastStartDay, recordedAmount[0] / values, (int)(recordedAmount[1] / values * 100), recordedAmount[2] / values, recordedAmount[3] / values, range); |
|
72 |
weatherInfo.Add(dayInfo); |
|
73 |
|
|
74 |
recordedAmount = new double[4]; |
|
75 |
lastStartDay = date; |
|
76 |
values = 0; |
|
77 |
} |
|
78 |
|
|
79 |
// tady nasčítávát data |
|
80 |
recordedAmount[0] += list[i].temp; |
|
81 |
recordedAmount[1] += list[i].rain; |
|
82 |
recordedAmount[2] += list[i].wind; |
|
83 |
recordedAmount[3] += list[i].lum * 1000; |
|
84 |
values++; |
|
85 |
} |
|
86 |
|
|
87 |
WeatherInfo dayInfo2 = new WeatherInfo(lastStartDay, recordedAmount[0] / values, (int)(recordedAmount[1] / values * 100), recordedAmount[2] / values, recordedAmount[3] / values, range); |
|
88 |
weatherInfo.Add(dayInfo2); |
|
89 |
|
|
90 |
return weatherInfo; |
|
91 |
} |
|
92 |
|
|
93 |
private static List<JisInfo> ProcessOneWeatherFileAsIntervals(string path, int interval) |
|
94 |
{ |
|
95 |
throw new NotImplementedException(); |
|
96 |
} |
|
11 | 97 |
} |
12 | 98 |
} |
Také k dispozici: Unified diff
Parse data as days - averages, sums