-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwind.py
executable file
·154 lines (125 loc) · 4.55 KB
/
wind.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
#!/usr/bin/env python
"""Download and process the latest wind data
Usage:
get-latest.py [--date=<YYYYMMDD>]
get-latest.py [--dest=<filepath>]
get-latest.py (-h | --help)
get-latest.py --version
Options:
-h --help Show this screen.
--version Show version.
--date=<YYYYMMDD> Process wind data from a specific date.
--dest=<filepath> Destination filepath for the resulting JSON file
"""
from docopt import docopt
import datetime
from subprocess import call
import os
import errno
import urllib.request as urllib2
from time import strftime
def download_data(date):
"""Download wind data from NOAA.
Downloads the GRIB (GRIdded Binary or General Regularly-distributed
Information in Binary form) wind data from NOAA.
Saves it in the data directory, adding a date string to the filename.
Args:
date: A string indicating the date of the data we want.
Returns:
A string of the downloaded GRIB filename.
"""
iso_date_frag = (datetime.datetime.strptime(date, '%Y%m%d')
.strftime('%Y-%m-%d'))
url = 'http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_1p00.pl' + '?' + \
'file=gfs.t00z.pgrb2.1p00.f000&' + \
'lev_10_m_above_ground=on&' + \
'var_UGRD=on&var_VGRD=on&' + \
'leftlon=0&rightlon=360&toplat=90&bottomlat=-90&' + \
'dir=%2Fgfs.' + date + '00'
print (url)
file_name = iso_date_frag + '_gfs.t00z.pgrbf00.grib2'
try:
u = urllib2.urlopen("%s" % (url))
f = open('data/' + file_name, 'wb')
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
f.write(buffer)
f.close()
return f.name
except (urllib2.HTTPError, e):
print('HTTPError = ' + str(e.code))
print
print ('Unknown internet connection problem.')
print
except (urllib2.URLError, e):
print ('URLError = ' + str(e.reason))
print
print ('Your internet connection is probably down.')
print
def grib_2_json(grib_file, datestring,
dest=None,
apppath='/Applications/grib2json-0.8.0-SNAPSHOT/bin'):
"""Convert GRIB file to JSON.
Converts the GRIB wind data into a JSON file that the Earth visualization
system can read. Saves the file in date directories where Earth can
access them. This assumes that you've setup Earth in your
~/src/wind/ directory.
Args:
grib_file: A string representing the filename of the GRIB file
to be converted.
datestring: A string representing the YYYYMMDD that this
GRIB file represents
dest: A string. The destination path for the JSON file.
apppath: A string. The path to the grib2json script.
Returns:
A string of the converted JSON filename.
"""
# Base dir for the weather data
if dest is None:
dest = (os.path.expanduser("~") +
os.sep + 'src/wind/public/data/weather')
current = (dest + os.sep + 'current' + os.sep +
'current-wind-surface-level-gfs-1.0.json')
# Data is read from year, month, and day directories,
# so we need to create them.
d = datetime.datetime.strptime(datestring, '%Y%m%d')
dest = (os.path.normpath(dest) + os.sep +
d.strftime('%Y') + os.sep +
d.strftime('%m') + os.sep +
d.strftime('%d') + os.sep)
create_path(dest)
# Convert files with the grib2json utility
cmd = os.path.normpath(apppath) + os.sep + 'grib2json'
dest = dest + '0000-wind-surface-level-gfs-1.0.json'
cmd = (cmd + ' -d -n -o ' + dest + ' ' + grib_file)
call(cmd, shell=True)
# Make a symlink to the new file from the "current" path
os.remove(current)
os.symlink(dest, current)
return dest
def create_path(path):
"""Try to create all the directories for a given filepath
Args:
path: A string of the filepath to create
"""
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
if __name__ == '__main__':
arguments = docopt(__doc__, version='Get Latest Data 0.0.1')
if arguments['--date'] is None:
date = datetime.datetime.now().strftime('%Y%m%d')
else:
date = arguments['--date']
grib_file = download_data(date)
if grib_file:
json_file = grib_2_json(grib_file, date, arguments['--dest'])
else:
print ('''
You won't be able to get the latest wind data until the internet is reconnected
''')