-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch_data_Chile.py
executable file
·397 lines (344 loc) · 13.2 KB
/
fetch_data_Chile.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/home/bfbrzn0q0yvx/.local/bin/python3
# coding: utf-8
"""
Created on Thu Mar 26 11:58:01 2020
@author: javier.concha
"""
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import csv
import json
import datetime
import glob
import os
import subprocess
from git import Repo
import sys
import argparse
parser = argparse.ArgumentParser(description="Create data to plot to in the index.html")
parser.add_argument('-v','--verbose',action='store_true' ,help='Verbose mode')
parser.add_argument('-f','--force',action='store_true' ,help='To force execute without checking Minsal website')
parser.add_argument('-n','--noupload',action='store_true' ,help='No uploading to the server')
args = parser.parse_args()
import fetch_data_minsal
if sys.platform == 'darwin':
path_main = '/Users/javier.concha/Desktop/Javier/2020_COVID-19_CHILE/covid-19-Chile'
elif sys.platform == 'linux':
path_main = '/home/bfbrzn0q0yvx/projects/covid-19-Chile'
#%%
geodata_json = os.path.join(path_main,'geodata.json') # name for json data output
data_csv = os.path.join(path_main,'data.csv') # name to output csv
data = []
coors_json = os.path.join(path_main,'coors_Chile.json')
has_duplicate_data = 'Chile'
def geocode(country, province, latitude=None, longitude=None):
# read existing data
if os.path.exists(coors_json):
with open(coors_json) as f:
coors = json.load(f)
else:
print('coors_json NOT found!')
coors = {}
if province == '':
location = country
else:
location = f'{province}, {country}'
latitude = coors[location]['latitude']
longitude = coors[location]['longitude']
return latitude, longitude
def merge_data():
global total_days
with open(os.path.join(path_main,'data/Chile.csv')) as file:
total_days = len(file.readlines()) - 1
print('TOTAL DAYS: '+str(total_days))
for filename in glob.glob(path_main+'/data/*.csv'):
# print(filename)
name = filename.split('/')[-1].replace('.csv', '')
# print(name)
if ',' in name:
x = name.split(',')
province = x[0].strip()
country = x[1].strip()
else:
province = ''
country = name
found = False
for rec in data:
if country == rec['country'] and province == rec['province']:
found = True
break
if found:
confirmed = rec['confirmed']
recovered = rec['recovered']
deaths = rec['deaths']
index = len(confirmed) - 1
time = confirmed[index]['time']
path_file = os.path.join(path_main,filename[2:])
print(path_file)
with open(path_file) as f:
reader = csv.reader(f)
for row in reader:
pass
last_updated = datetime.datetime.fromisoformat(row[0]).\
astimezone(datetime.timezone.utc)
if time > last_updated:
last_updated = time
c = int(row[1])
r = int(row[2])
d = int(row[3])
if c > confirmed[index]['count']:
print(f'data confirmed: {province}, {country}, {confirmed[index]["count"]} => {c}')
confirmed[index] = {
'time': last_updated,
'count': c
}
if r > recovered[index]['count']:
print(f'data recovered: {province}, {country}, {recovered[index]["count"]} => {r}')
recovered[index] = {
'time': last_updated,
'count': r
}
if d > deaths[index]['count']:
print(f'data deaths : {province}, {country}, {deaths[index]["count"]} => {d}')
deaths[index] = {
'time': last_updated,
'count': d
}
else:
latitude, longitude = geocode(country, province)
latitude = round(latitude, 4)
longitude = round(longitude, 4)
confirmed = []
recovered = []
deaths = []
with open(filename) as f:
reader = csv.reader(f)
reader.__next__()
for row in reader:
time = datetime.datetime.fromisoformat(row[0]).\
astimezone(datetime.timezone.utc)
time_str = f'{time.strftime("%Y/%m/%d %H:%M:%S UTC")}'
c = int(row[1])
r = int(row[2])
d = int(row[3])
confirmed.append({
'time': time,
'count': c
})
recovered.append({
'time': time,
'count': r
})
deaths.append({
'time': time,
'count': d
})
print(f'data confirmed: {province}, {country}, {c}')
print(f'data recovered: {province}, {country}, {r}')
print(f'data deaths : {province}, {country}, {d}')
data.append({
'country': country,
'province': province,
'latitude': latitude,
'longitude': longitude,
'confirmed': confirmed,
'recovered': recovered,
'deaths': deaths
})
def sort_data():
global data
# sort records by confirmed, country, and province
data = sorted(data, key=lambda x: (
-x['confirmed'][len(x['confirmed'])-1]['count'],
x['country'],
x['province']))
def report_data():
total_confirmed = total_recovered = total_deaths = 0
for rec in data:
country = rec['country']
province = rec['province']
latitude = rec['latitude']
longitude = rec['longitude']
index = len(rec['confirmed']) - 1
c = rec['confirmed'][index]['count']
r = rec['recovered'][index]['count']
d = rec['deaths'][index]['count']
if c == 0 or (country in has_duplicate_data and not province):
print('-------------------')
print('From Chile.csv:')
print(f'Total confirmed: {c}')
print(f'Total recovered: {r}')
print(f'Total deaths : {d}')
continue
print(f'final: {province}; {country}; {latitude}; {longitude}; {c}; {r}; {d}')
total_confirmed += c
total_recovered += r
total_deaths += d
print('Calculated from the provinces:')
print(f'Total confirmed: {total_confirmed}')
print(f'Total recovered: {total_recovered}')
print(f'Total deaths : {total_deaths}')
def write_geojson():
# create a new list to store all the features
features = []
# create a feature collection
for i in range(0, len(data)):
rec = data[i]
country = rec['country']
province = rec['province']
index = len(rec['confirmed']) - 1
if rec['confirmed'][index]['count'] == 0 and \
rec['recovered'][index]['count'] == 0 and \
rec['deaths'][index]['count'] == 0:
continue
features.append({
'id': i,
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [rec['longitude'], rec['latitude']]
},
'properties': {
'country': rec['country'],
'province': rec['province'],
'confirmed': rec['confirmed'],
'recovered': rec['recovered'],
'deaths': rec['deaths']
}
})
# finally, build the output GeoJSON object and save it
geodata = {
'type': 'FeatureCollection',
'features': features
}
def convert_time(x):
if isinstance(x, datetime.datetime):
return int(x.timestamp())
with open(geodata_json, 'w') as f:
f.write(json.dumps(geodata, default=convert_time))
if args.verbose:
print('geodata.json created.')
def write_csv():
with open(data_csv, 'w') as f:
f.write('province,country,latitude,longitude,category')
for x in data[0]['confirmed']:
date = x['time'].strftime("%Y%m%d")
f.write(f',utc_{date}')
f.write('\n')
for rec in data:
country = rec['country']
province = rec['province']
index = len(rec['confirmed']) - 1
if rec['confirmed'][index]['count'] == 0 and \
rec['recovered'][index]['count'] == 0 and \
rec['deaths'][index]['count'] == 0:
continue
if ',' in province:
province = f'"{province}"'
if ',' in country:
country = f'"{country}"'
latitude = rec['latitude']
longitude = rec['longitude']
for category in ('confirmed', 'recovered', 'deaths'):
f.write(f'{province},{country},{latitude},{longitude},{category}')
for i in range(len(rec[category]), total_days):
f.write(',0')
for x in rec[category]:
f.write(f',{x["count"]}')
f.write('\n')
def send_to_server():
if sys.platform == 'darwin': # for Mac
cmd1 = 'scp '+path_main+'/data.csv bfbrzn0q0yvx@www.sci-solve.com:/home/bfbrzn0q0yvx/public_html/covid-19-Chile'
print(cmd1)
prog = subprocess.Popen(cmd1, shell=True,stderr=subprocess.PIPE)
out, err = prog.communicate()
if err:
print(err)
else:
print('data.csv uploaded to server!')
cmd2 = 'scp '+path_main+'/geodata.json bfbrzn0q0yvx@www.sci-solve.com:/home/bfbrzn0q0yvx/public_html/covid-19-Chile'
print(cmd2)
prog = subprocess.Popen(cmd2, shell=True,stderr=subprocess.PIPE)
out, err = prog.communicate()
if err:
print(err)
else:
print('geodata.json uploaded to server!')
cmd3 = 'scp '+path_main+'/*.html bfbrzn0q0yvx@www.sci-solve.com:/home/bfbrzn0q0yvx/public_html/covid-19-Chile'
print(cmd3)
prog = subprocess.Popen(cmd3, shell=True,stderr=subprocess.PIPE)
out, err = prog.communicate()
if err:
print(err)
else:
print('/*.html uploaded to server!')
elif sys.platform == 'linux': # in the server
cmd1 = 'cp '+path_main+'/data.csv /home/bfbrzn0q0yvx/public_html/covid-19-Chile'
print(cmd1)
prog = subprocess.Popen(cmd1, shell=True,stderr=subprocess.PIPE)
out, err = prog.communicate()
if err:
print(err)
else:
print('data.csv uploaded to server!')
cmd2 = 'cp '+path_main+'/geodata.json /home/bfbrzn0q0yvx/public_html/covid-19-Chile'
print(cmd2)
prog = subprocess.Popen(cmd2, shell=True,stderr=subprocess.PIPE)
out, err = prog.communicate()
if err:
print(err)
else:
print('geodata.json uploaded to server!')
cmd3 = 'cp '+path_main+'/*.html /home/bfbrzn0q0yvx/public_html/covid-19-Chile'
print(cmd3)
prog = subprocess.Popen(cmd3, shell=True,stderr=subprocess.PIPE)
out, err = prog.communicate()
if err:
print(err)
else:
print('*.html uploaded to server!')
def git_push():
PATH_OF_GIT_REPO = os.path.join(path_main,'.git') # make sure .git folder is properly configured
COMMIT_MESSAGE = 'Last update from Minsal.'
try:
repo = Repo(PATH_OF_GIT_REPO)
repo.git.add(path_main+'/data/')
repo.git.add(path_main+'/data.csv')
repo.git.add(path_main+'/geodata.json')
repo.index.commit(COMMIT_MESSAGE)
origin = repo.remote(name='origin')
origin.push()
except:
print('Some error occured while pushing the code using GitPython')
cmd1 = 'git -C '+path_main+' push origin master'
print('Trying: '+cmd1)
prog = subprocess.Popen(cmd1, shell=True,stderr=subprocess.PIPE)
out, err = prog.communicate()
if err:
print(err)
else:
print(out)
#%% MAIN
if __name__ == '__main__':
if fetch_data_minsal.main() or args.force:
# if True:
merge_data()
sort_data()
report_data()
write_geojson()
write_csv()
if not args.noupload:
send_to_server()
git_push()
else:
print('Data not updated!')