-
Notifications
You must be signed in to change notification settings - Fork 0
/
synop_download.py
284 lines (241 loc) · 8.04 KB
/
synop_download.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
from datetime import datetime
import glob
import urllib3
import os
from os.path import expanduser
import pandas as pd
import shutil
def url_synop(lang='eng', header='yes'):
begin = str(input('Please enter the start time of the query of the format'
'(YYYYMMDDHHmm): '))
end = str(input('Please enter the end time of the query of the format'
'(YYYYMMDDHHmm). If you enter "N" then it will use current'
'time as the end time: '))
# state='Austri' for Austrian stations
state = str(input('If you want to download synops from a specific country'
'only then please specify the three letter acronym'
'(e.g. "Pol"). If no country enter "N": '))
list_names = ['begin', 'end', 'lang', 'header', 'state']
lis = [x for x in [begin, end, lang, header, state]]
dic = {}
for name, val in zip(list_names, lis):
if (val == 'N' or val == 'n'):
pass
else:
dic[name] = val
url = 'http://www.ogimet.com/cgi-bin/getsynop?'
i = 0
for key, value in dic.items():
# print(key)
# print(value)
if i == 0:
url += key
else:
url += '&'+key
url += '='+value
i += 1
print(url)
return dic, url
def url_last_hour(state=None, lang='eng', header='yes'):
'''
Function to create the url for latest (full hour) synop download from
Ogimet.
Arguments:
----------
state = None, lang = 'eng', header = 'yes'
Returns:
--------
url (to download file)
path (to save the file)
Examples:
---------
from synop_download import url_last_hour
url, path = url_last_hour()
'''
# Create the dates and strings
now = datetime.utcnow()
now = datetime(now.year, now.month, now.day, now.hour, 29)
end_str = now.strftime('%Y%m%d%H%M')
save_str = datetime(now.year, now.month, now.day, now.hour, 00)
save_str = save_str.strftime('%Y%m%d%H%M')
start = datetime(now.year, now.month, now.day, now.hour-1, 31)
start_str = start.strftime('%Y%m%d%H%M')
# set up the paths and test for existence
path = expanduser('~') + '/Documents/Synop_data'
try:
os.listdir(path)
except FileNotFoundError:
os.mkdir(path)
print('Created the path {}'.format(path))
if state is None:
path = path + '/synop_' + save_str + '.csv'
else:
path = path + '/synop_' + save_str + '_' + state + '.csv'
list_names = ['begin', 'end', 'lang', 'header', 'state']
lis = [x for x in [start_str, end_str, lang, header, state]]
dic = {}
for name, val in zip(list_names, lis):
if (val == 'N' or val == 'n' or val is None):
pass
else:
dic[name] = val
url = 'http://www.ogimet.com/cgi-bin/getsynop?'
i = 0
for key, value in dic.items():
# print(key)
# print(value)
if i == 0:
url += key
else:
url += '&'+key
url += '='+value
i += 1
print(url)
return url, path
def url_any_hour(year=None, month=None, day=None, hour=None, state=None, lang='eng',
header='yes'):
'''
Function to create the url for provided (full hour) synop download from
Ogimet.
Arguments:
----------
year=None, month=None, day=None, hour=None, state=None, state = None, lang =
'eng', header = 'yes'
Returns:
--------
url (to download file)
path (to save the file)
Examples:
---------
from synop_download import url_any_hour
url, path = url_any_hour()
'''
# Create the dates and strings
now = datetime(year, month, day, hour, 29)
end_str = now.strftime('%Y%m%d%H%M')
save_str = datetime(year, month, day, hour, 00)
save_str = save_str.strftime('%Y%m%d%H%M')
start = datetime(year, month, day, hour-1, 31)
start_str = start.strftime('%Y%m%d%H%M')
# set up the paths and test for existence
path = expanduser('~') + '/Documents/Synop_data'
try:
os.listdir(path)
except FileNotFoundError:
os.mkdir(path)
print('Created the path {}'.format(path))
if state is None:
path = path + '/synop_' + save_str + '.csv'
else:
path = path + '/synop_' + save_str + '_' + state + '.csv'
list_names = ['begin', 'end', 'lang', 'header', 'state']
lis = [x for x in [start_str, end_str, lang, header, state]]
dic = {}
for name, val in zip(list_names, lis):
if (val == 'N' or val == 'n' or val is None):
pass
else:
dic[name] = val
url = 'http://www.ogimet.com/cgi-bin/getsynop?'
i = 0
for key, value in dic.items():
# print(key)
# print(value)
if i == 0:
url += key
else:
url += '&'+key
url += '='+value
i += 1
print(url)
return url, path
def url_timeseries(year=None, month=None, day=None, hour=None,
year_end=None, month_end=None, day_end=None, hour_end=None,
station=None, state=None, lang='eng',
header='yes'):
'''
Function to download a time series of SYNOP observations from a station or a
block of station (i.e. station = 11, will download all stations starting with 11)
Arguments:
----------
year=None, month=None, day=None, hour=None,
year_end=None, month_end=None, day_end=None, hour_end=None,
station=None, state=None, lang='eng',
header='yes'
Returns:
--------
url (to download file)
path (to save the file)
Examples:
---------
from synop_download import url_any_hour
url, path = url_timeseries(2018,2,22,0,2018,2,23,17,'04301')
'''
station = str(station)
# Create the dates and strings
now = datetime(year_end, month_end, day_end, hour_end, 00)
end_str = now.strftime('%Y%m%d%H%M')
save_str_end = datetime(year_end, month_end, day_end, hour_end, 00)
save_str_end = save_str_end.strftime('%Y%m%d%H%M')
start = datetime(year, month, day, hour, 00)
start_str = start.strftime('%Y%m%d%H%M')
save_str = datetime(year, month, day, hour, 00)
save_str = save_str.strftime('%Y%m%d%H%M')
# set up the paths and test for existence
path = expanduser('~') + '/Documents/Synop_data/StationData/' + station + '/'
try:
os.listdir(path)
except FileNotFoundError:
os.makedirs(path)
print('Created the path {}'.format(path))
# Where to save the file
path = path + 'synop_' + station + '_' + save_str + '-' + save_str_end + '.csv'
list_names = ['block', 'begin', 'end', 'lang', 'header', 'state']
lis = [x for x in [station, start_str, end_str, lang, header, state]]
dic = {}
for name, val in zip(list_names, lis):
if (val == 'N' or val == 'n' or val is None):
pass
else:
dic[name] = val
url = 'http://www.ogimet.com/cgi-bin/getsynop?'
i = 0
for key, value in dic.items():
# print(key)
# print(value)
if i == 0:
url += key
else:
url += '&'+key
url += '='+value
i += 1
print(url)
return url, path
def download_and_save(path, url):
'''
Function to download and save the file from the url created by either
url_last_hour() or url_synop().
Arguments:
----------
path (where to save file on disk)
url (to download file)
Returns:
--------
File on disk
Examples:
---------
from synop_download import url_last_hour
url, path = url_last_hour()
download_and_save(path, url)
'''
if os.path.exists(path):
print('Using an existing file stored in {}.'.format(path))
else:
http = urllib3.PoolManager()
with http.request('GET', url, preload_content=False) as r, open(path, 'wb') \
as out_file:
shutil.copyfileobj(r, out_file)
print('Saved file to {}.'.format(path))
if __name__ == 'main':
url, path = url_last_hour(state=None)
download_and_save(path, url)