-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathupdateEventList.py
305 lines (249 loc) · 10.9 KB
/
updateEventList.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
# -*- coding: utf-8 -*-
import os
import json
import dateutil.parser
import argparse, sys
import shutil
def get_event_ids (data_path):
"""
Get event IDs from the list of folders in data folder
"""
dirlist = [ item for item in os.listdir(data_path) if os.path.isdir(os.path.join(data_path, item)) ]
return dirlist
def separate_time_date(time):
"""
Get time and date as separate variables from the time in ISO8601 format
"""
dateTime = dateutil.parser.parse(time)
year = dateTime.year
month = dateTime.month
day = dateTime.day
hour = dateTime.hour
minute = dateTime.minute
second = dateTime.second
return (year, month, day, hour, minute, second)
def overlay_to_json(event_id, data_path):
info_file_path = data_path + event_id + '/current/products/intensity_overlay.pngw'
with open(info_file_path, 'r') as overlay:
overlay_file = overlay.readlines()
overlay_file = list(map(str.strip, overlay_file))
overlay_file = list(map(float, overlay_file))
js_file = {
'dx': overlay_file[0],
'dy': overlay_file[3],
'upper_left_x': overlay_file[4],
'upper_left_y': overlay_file[5]
}
with open(data_path + event_id + '/current/products/overlay.json', 'w') as outfile:
json.dump(js_file, outfile)
return None
def get_bBox_dict():
if (os.path.isfile('bBox.txt')):
with open('bBox.txt') as json_file:
bBox = json.load(json_file)[0]
return bBox
else:
return False
def get_products_list(event_id, data_path):
"""
Get the list of products generated for an event and write them to file
"""
with open('productsDownloadList.json') as json_file:
productMeta = json.load(json_file)
products_path = data_path + event_id + '/current/products/'
fileList = [ item for item in os.listdir(products_path) if os.path.isfile(os.path.join(products_path, item)) ]
productsList = []
for product in productMeta:
if product['name'] in fileList:
productsList.append(product)
with open(products_path + 'productList.json', 'w') as outfile:
json.dump(productsList, outfile)
return None
def get_parameters (event_id, bBox, data_path):
"""
Get the event parameters from the info.json file of an event
"""
info_file_path = data_path + event_id + '/current/products/info.json'
with open(info_file_path) as f:
info_file = json.load(f)
year, month, day, hour, minute, second = separate_time_date(info_file['input']['event_information']['origin_time'])
parameter_dict = {
'id': event_id,
'description': info_file['input']['event_information']['event_description'],
'day': day,
'month': month,
'year': year,
'hour': hour,
'minute': minute,
'second': second,
'latitude': round(float(info_file['input']['event_information']['latitude']), 2),
'longitude': round(float(info_file['input']['event_information']['longitude']), 2),
'magnitude': round(float(info_file['input']['event_information']['magnitude']), 1),
'depth': round(float(info_file['input']['event_information']['depth']), 1)
}
if (bBox == False):
bBox = {"minLat": -90.0, "maxLat": 90.0, "minLon": -180.0, "maxLon" : 180.0}
if ( parameter_dict['latitude'] < bBox["minLat"] or parameter_dict['latitude'] > bBox["maxLat"] or
parameter_dict['longitude'] < bBox["minLon"] or parameter_dict['longitude'] > bBox["maxLon"]):
return False
else:
return parameter_dict
def write_list_to_file(event_list):
"""
Write event information to file.
"""
## This next line is written so the file is saved as a javascript variable
## so the ajax call in the website could be avoided
# with open('events.js.tmp', 'w') as f:
# print('var events =', file=f)
# with open('events.js.tmp', 'a') as outfile:
# json.dump(event_list, outfile)
# shutil.copyfile('events.js.tmp', 'events.js')
with open('events.json', 'w') as f:
json.dump(event_list, f, indent = 4)
# not used anymore
def write_version_file():
yaml_file_path = 'publiccode.yml'
with open(yaml_file_path, 'r') as yaml:
yaml_file = yaml.readlines()
versionElement = [s for s in yaml_file if "softwareVersion" in s][0][:-1]
versionElement = versionElement.replace('software', 'Website ')
with open('./scripts/softwareVersion.js', 'w') as f:
print('var softwareVersion = "<span class=\'go_left\'>' + versionElement +
'";softwareVersion = softwareVersion + ' +
'" </span><span class=\'go_right\'>Development of this portal has been made by INGV and it is publicly available at ' +
'<a href=\'https://github.com/INGV/shakemap4-web\' target=\'_blank\'>GitHub INGV/shakemap4-web </a></span>";' +
'document.getElementById("footer_text").innerHTML = softwareVersion;', file=f)
return
def do_for_all_events(bBox, data_path):
event_list = []
folders_list = get_event_ids(data_path)
events_num = len(folders_list)
for count, event in enumerate(folders_list, start=1):
print(str(count) + '/' + str(events_num) + ' - Processing event:' + event)
## Try to read the info.json file to put the events in a list for the website to read
try:
eventParameters = get_parameters(event, bBox, data_path)
if (eventParameters != False):
event_list.append(eventParameters)
except Exception as e:
print('Following error occurred for event ' + event + ':')
print(e)
## Try to extract overlay parameters and put them into a json file, so the website can read it
try:
overlay_to_json(event, data_path)
except Exception as e:
print('No intensity overlay file for event:' + event)
print(e)
## Try to get products list and put them into a json file, so the website can read it
try:
get_products_list(event, data_path)
except Exception as e:
print('Product file list error for event ' + event + ':')
print(e)
write_list_to_file(event_list)
#write_version_file() Sergio ... not used anymore
# def update_event_list(eventParameters, event_id, eventAction='add'):
#
# if os.path.isfile('events.json'):
# with open('events.json') as json_file:
# events_list = json.load(json_file)
# if not any(d['id'] == event_id for d in events_list) and eventAction=='add':
# events_list.append(eventParameters)
# elif eventAction == 'del':
# if not any(d['id'] == event_id for d in events_list):
# print('Event ' + event_id + ' does not exist in the event list.' )
# else:
# events_list = [x for x in events_list if x['id']!=str(event_id)]
# print('Event ' + event_id + ' deleted from event list.')
# else:
# events_list = [eventParameters if x['id']==str(event_id) else x for x in events_list]
# else:
# if eventAction=='del':
# print('The event list file does not exist.')
# return
# events_list = []
# events_list.append(eventParameters)
# print('Nešto ovdje')
# write_list_to_file(events_list)
def update_event_list(eventParameters, event_id, eventAction='add_or_update'):
events_list = []
if os.path.isfile('events.json'):
with open('events.json') as json_file:
events_list = json.load(json_file)
event_index = next((i for i, n in enumerate(events_list) if n['id'] == event_id), None)
if eventAction=='add_or_update':
if event_index == None:
events_list.append(eventParameters)
else:
events_list[event_index] = eventParameters
elif eventAction == 'del':
if event_index == None:
print('Event ' + event_id + ' does not exist in the event list.' )
else:
events_list.pop(event_index)
print('Event ' + event_id + ' deleted from event list.')
write_list_to_file(events_list)
def do_for_one_event(event_id, bBox):
print('Processing event: ' + event_id)
## Try to read the info.json file to put the events in a list for the website to read
try:
eventParameters = get_parameters(event_id, bBox, data_path)
if (eventParameters != False):
update_event_list(eventParameters, event_id)
except Exception as e:
print('Following error occurred for event ' + event_id + ':')
print(e)
## Try to extract overlay parameters and put them into a json file, so the website can read it
try:
overlay_to_json(event_id, data_path)
except Exception as e:
print('No intensity overlay file for event:' + event_id)
print(e)
try:
get_products_list(event_id, data_path)
except Exception as e:
print('Product file list error for event ' + event_id + ':')
print(e)
def delete_event(event_id, data_path):
print('Deleting event: ' + event_id)
try:
update_event_list(None, event_id, eventAction='del')
except Exception as e:
print('Could not delete from list for event' + event_id + ':')
print(e)
try:
shutil.rmtree(data_path + event_id)
print('Event folder removed from data.')
except Exception as e:
print('Could not delete folder for event: ' + event_id + ':')
print(e)
def get_data_path():
with open('config.js') as configFile:
configData = configFile.read()
dataPath = configData[configData.find('path:'): configData.rfind('\'')]
dataPath = dataPath[dataPath.find('\'')+1:]
return dataPath
def main(event_id, data_path):
bBox = get_bBox_dict()
if event_id == False:
do_for_all_events(bBox, data_path)
else:
if os.path.isdir(data_path + event_id):
do_for_one_event(event_id, bBox)
else:
print('Error: event ' + event_id + ' does not exist')
sys.exit(1)
if __name__ == "__main__":
parser=argparse.ArgumentParser()
parser.add_argument('--eventid', help='Provide the event ID under the script argument --eventid of the event for which the parameters or files have been changed. To run the script for all events do not pass any arguments"')
parser.add_argument('--deleteid', help='Provide the event ID under the script argument --eventid of the event which you want to delete (from event list and data folder)"')
args = parser.parse_args()
data_path = get_data_path()
if len(sys.argv[:]) < 2:
print('No event ID has been provided. The script will run for all the events')
main(False, data_path)
elif (args.deleteid != None):
delete_event(args.deleteid, data_path)
else:
main(args.eventid, data_path)