-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
executable file
·358 lines (309 loc) · 10.6 KB
/
Main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function
from numpy import genfromtxt
from urllib.request import urlopen
import glob, os
from matplotlib.pyplot import figure, plot, xlabel, ylabel, title, show, legend
from datetime import date
list_province = [[1, 'Cherkasy'],
[2, 'Chernihiv'],
[3, 'Chernivtsi'],
[4, 'Crimea'],
[5, 'Dnipropetrovsk'],
[6, 'Donetsk'],
[7, 'Ivano-Frankivsk'],
[8, 'Kharkiv'],
[9, 'Kherson'],
[10, 'Khmelnytskyy'],
[11, 'Kiev'],
[12, 'Kiev City'],
[13, 'Kirovohrad'],
[14, 'Luhansk'],
[15, 'Lviv'],
[16, 'Mykolayiv'],
[17, 'Odessa'],
[18, 'Poltava'],
[19, 'Rivne'],
[20, 'Sevastopol'],
[21, 'Sumy'],
[22, 'Ternopil'],
[23, 'Transcarpathia'],
[24, 'Vinnytsya'],
[25, 'Volyn'],
[26, 'Zaporizhzhya'],
[27, 'Zhytomyr']]
def files_in_directory():
try:
os.chdir("vhi")
print("- vhi")
for file in glob.glob("*.csv"):
print(file)
except:
os.chdir("../vhi")
print("- vhi")
for file in glob.glob("*.csv"):
print(file)
os.chdir("../percent")
print("- percent")
for file in glob.glob("*.csv"):
print(file)
def fix_file_vhi(s_file):
fi = open(s_file)
line = fi.readlines()
with open(s_file, 'r+') as f:
f_str = f.read().replace(line[0],
'')
f.seek(0)
f.truncate()
f.write(f_str)
fi.close()
with open(s_file, 'r+') as f:
f_str = f.read().replace(',', ' ')
f.seek(0)
f.truncate()
f.write(f_str)
with open(s_file, 'r+') as f:
f_str = f.read().replace(' ', ' ')
f.seek(0)
f.truncate()
f.write(f_str)
with open(s_file, 'r+') as f:
f_str = f.read().replace(' ', ' ')
f.seek(0)
f.truncate()
f.write(f_str)
fi = open(s_file)
line = fi.readlines()
x = len(line)
with open(s_file, 'r+') as f:
f_str = f.read().replace(line[x - 1], '')
f.seek(0)
f.truncate()
f.write(f_str)
fi.close()
def fix_file_percent(s_file):
fi = open(s_file)
line = fi.readlines()
with open(s_file, 'r+') as f:
f_str = f.read().replace(line[0],
'')
f.seek(0)
f.truncate()
f.write(f_str)
fi.close()
with open(s_file, 'r+') as f:
f_str = f.read().replace(',', ' ')
f.seek(0)
f.truncate()
f.write(f_str)
with open(s_file, 'r+') as f:
f_str = f.read().replace(' ', ' ')
f.seek(0)
f.truncate()
f.write(f_str)
with open(s_file, 'r+') as f:
f_str = f.read().replace(' ', ' ')
f.seek(0)
f.truncate()
f.write(f_str)
with open(s_file, 'r+') as f:
f_str = f.read().replace(' ', ' ')
f.seek(0)
f.truncate()
f.write(f_str)
fi = open(s_file)
line = fi.readlines()
x = len(line)
with open(s_file, 'r+') as f:
f_str = f.read().replace(line[x - 1], '')
f.seek(0)
f.truncate()
f.write(f_str)
fi.close()
def parser_file(province_id):
url = "https://www.star.nesdis.noaa.gov/smcd/emb/vci/VH/get_provinceData.php?country=UKR&" \
+ "provinceID=" + str(province_id) \
+ "&year1=1981&year2=2017&type=Mean"
vhi_url = urlopen(url)
try:
out = open('../vhi/vhi_id_' + str(province_id) + '_' + str(date.today()) + '.csv', 'wb')
out.write(vhi_url.read())
out.close()
try:
s_file = '../vhi/vhi_id_' + str(province_id) + '_' + str(date.today()) + '.csv'
fix_file_vhi(s_file)
except:
s_file = 'vhi/vhi_id_' + str(province_id) + '_' + str(date.today()) + '.csv'
fix_file_vhi(s_file)
except:
out = open('vhi/vhi_id_' + str(province_id) + '_' + str(date.today()) + '.csv', 'wb')
out.write(vhi_url.read())
out.close()
try:
s_file = 'vhi/vhi_id_' + str(province_id) + '_' + str(date.today()) + '.csv'
fix_file_vhi(s_file)
except:
s_file = '../vhi/vhi_id_' + str(province_id) + '_' + str(date.today()) + '.csv'
fix_file_vhi(s_file)
url = "https://www.star.nesdis.noaa.gov/smcd/emb/vci/VH/get_provinceData.php?country=UKR&" \
+ "provinceID=" + str(province_id) \
+ "&year1=1981&year2=2017&type=VHI_Parea"
vhi_url = urlopen(url)
try:
out = open('percent/percent_id_' + str(province_id) + '_' + str(date.today()) + '.csv', 'wb')
out.write(vhi_url.read())
out.close()
try:
s_file = 'percent/percent_id_' + str(province_id) + '_' + str(date.today()) + '.csv'
fix_file_percent(s_file)
except:
s_file = '../percent/percent_id_' + str(province_id) + '_' + str(date.today()) + '.csv'
fix_file_percent(s_file)
except:
out = open('../percent/percent_id_' + str(province_id) + '_' + str(date.today()) + '.csv', 'wb')
out.write(vhi_url.read())
out.close()
try:
s_file = '../percent/percent_id_' + str(province_id) + '_' + str(date.today()) + '.csv'
fix_file_percent(s_file)
except:
s_file = 'percent/percent_id_' + str(province_id) + '_' + str(date.today()) + '.csv'
fix_file_percent(s_file)
def analyzing_by_year(year, csv_v):
list_week = []
list_vhi = []
c = 0
for bich in csv_v:
if (csv_v[c][0] == year):
list_week.append(csv_v[c][1])
list_vhi.append(csv_v[c][6])
c += 1
figure()
plot(list_week, list_vhi, 'r')
xlabel('week')
ylabel('vhi')
title('Graphic VHI for ' + str(year) + ' year')
show()
print(66 * "-")
print("1. Minimum VHI")
print("2. Maximum VHI")
print(66 * "-")
choose = int(input("Enter choose: "))
if choose == 1:
print(min(list_vhi))
elif choose == 2:
print(max(list_vhi))
def analyzing_by_two_year(year_1, year_2, csv_v):
list_week1 = []
list_vhi1 = []
list_week2 = []
list_vhi2 = []
c = 0
for bich in csv_v:
if (csv_v[c][0] == year_1):
list_week1.append(csv_v[c][1])
list_vhi1.append(csv_v[c][6])
elif (csv_v[c][0] == year_2):
list_week2.append(csv_v[c][1])
list_vhi2.append(csv_v[c][6])
c += 1
figure()
plot(list_week1, list_vhi1, 'r', label=str(year_1))
plot(list_week2, list_vhi2, 'g', label=str(year_2))
legend(loc=2)
xlabel('week')
ylabel('vhi')
title('Graphic VHI for ' + str(year_1) + ',' + str(year_2) + ' years')
show()
def choose_default_vhi(province_id, date_in):
try:
csv_v = genfromtxt('vhi/vhi_id_' + str(province_id) + '_' + str(date_in) + '.csv', delimiter=' ')
except:
csv_v = genfromtxt('../vhi/vhi_id_' + str(province_id) + '_' + str(date_in) + '.csv', delimiter=' ')
return csv_v
def choose_default_percent(province_id, date_in):
try:
csv_p = genfromtxt('percent/percent_id_' + str(province_id) + '_' + str(date_in) + '.csv', delimiter=' ')
except:
csv_p = genfromtxt('../percent/percent_id_' + str(province_id) + '_' + str(date_in) + '.csv', delimiter=' ')
return csv_p
def sorting_by_ext_md(year, csv_p):
print("*" * 10, "Moderate", "*" * 10)
x = []
w = []
c = 0
for bich in csv_p:
if (csv_p[c][0] == year):
x.append(csv_p[c][2] + csv_p[c][3] + csv_p[c][4] + csv_p[c][5])
w.append(csv_p[c][1])
c += 1
c = 0
for bich in x:
if (x[c] <= 30):
print("Week: " + str(w[c]) + "; Extreme!")
c += 1
print("*" * 10, "Moderate", "*" * 10)
x = []
w = []
c = 0
for bich in csv_p:
if (csv_p[c][0] == year):
x.append(csv_p[c][2] + csv_p[c][3] + csv_p[c][4] + csv_p[c][5] + csv_p[c][6] + csv_p[c][7] + csv_p[c][8]
+ csv_p[c][9])
w.append(csv_p[c][1])
c += 1
c = 0
for bich in x:
if (x[c] <= 50):
if (x[c] > 30):
print("Week: " + str(w[c]) + "; Moderate!")
c += 1
def clear_console():
try:
os.system('clear')
except:
os.system('cls')
def print_menu():
print(30 * "-", "MENU", 30 * "-")
print("1. Parse data")
print("2. Re-choose files")
print("3. Graphic for one year")
print("4. Graphic for two years")
print("5. Sorting")
print("0. Exit")
print(70 * "-")
if __name__ == '__main__':
csv_v = choose_default_vhi("4", "2017-03-29")
csv_p = choose_default_percent("4", "2017-03-29")
loop = True
while loop:
print_menu()
choose = int(input("Enter your choose: "))
if choose == 1:
print(*list_province, sep='\n')
province_id = int(input("Choose province and input him [id] (1st column): "))
parser_file(province_id)
files_in_directory()
province_id = int(input("Enter province id: "))
date_in = input("Input date (yyyy-mm-dd): ")
csv_v = choose_default_vhi(province_id, date_in)
csv_p = choose_default_percent(province_id, date_in)
elif choose == 2:
files_in_directory()
province_id = int(input("Enter province id: "))
date_in = input("Input date (yyyy-mm-dd): ")
csv_v = choose_default_vhi(province_id, date_in)
csv_p = choose_default_percent(province_id, date_in)
elif choose == 3:
year = int(input("Enter year: "))
analyzing_by_year(year, csv_v)
elif choose == 4:
year_1 = int(input("Enter first year: "))
year_2 = int(input("Enter second year: "))
analyzing_by_two_year(year_1, year_2, csv_v)
elif choose == 5:
year = int(input("Input year: "))
sorting_by_ext_md(year, csv_p)
elif choose == 0:
loop = False