/
noaa_winds.py
184 lines (165 loc) · 6.71 KB
/
noaa_winds.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
import urllib
# import urllib2
import re
import gzip
import os
from datetime import datetime
from math import sin, cos, pi
import glob
import pdb
import numpy as np
import pylab
def retrieve_noaa_winds(station_id, data_type='hourly'):
"""Read NOAA winds from web site or file
Rob's file, adjust by Kristen
Parameters
----------
station_id : string
The id of the station, like 'burl1' or '42040'
data_type : ('hourly' or 'h', 'continuous' or 'c')
The type of data to collect. Continuous winds are every 10 min.
Returns
-------
noaa_wind : ndarray
The NOAA wind object array contains the date keys:
dates : Dates of wind observations
u : (EW) wind component
v : (NS) wind component.
direction : Direction of wind []
speed : Wind speed [m/s]
"""
# Get data, historical and through almost present day
if data_type[0].lower() == 'c':
# historical data
url_root_his = 'http://www.ndbc.noaa.gov/data/historical/cwind/'
url_root_new = 'http://www.ndbc.noaa.gov/data/cwind/'
else:
# historical data
url_root_his = 'http://www.ndbc.noaa.gov/data/historical/stdmet/'
url_root_new = 'http://www.ndbc.noaa.gov/data/stdmet/'
mons = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec']
url_root_recent = 'http://www.ndbc.noaa.gov/data/realtime2/'
url_roots = []
url_roots.append(url_root_his)
for mon in mons:
url_roots.append(url_root_new + mon + '/')
url_roots.append(url_root_recent)
p = re.compile(station_id + '\w*\.txt\.gz', re.IGNORECASE)
noaa_files = []
for url_root in url_roots:
# if 'realtime' in url_root:
pdb.set_trace()
lines = urllib.request.urlopen(url_root).readlines()
for line in lines:
m = p.search(line)
if m:
# pdb.set_trace()
noaa_file = m.group()
noaa_files.append(noaa_file)
if not noaa_file in os.listdir('.'):
# if not os.path.exists(noaa_file):
print(' ### Downloading ', url_root + noaa_file)
urllib.urlretrieve(url_root + noaa_file, noaa_file)
else:
print(' ### %s already exists. ' % noaa_file)
else: # if txt.gz doesn't exist, check for just .txt
ptxt = re.compile(station_id + '\w*\.txt', re.IGNORECASE)
m = ptxt.search(line)
if m:
# pdb.set_trace()
noaa_file = m.group()
noaa_files.append(noaa_file)
if not noaa_file in os.listdir('.'):
# if not os.path.exists(noaa_file):
print(' ### Downloading ', url_root + noaa_file)
temp_fname, headers = urllib.urlretrieve(url_root + noaa_file, noaa_file)
# resave file to a new name
save_fname = url_root.split('/')[-2] + '_' + temp_fname
# pdb.set_trace()
os.system('mv ' + temp_fname + ' ' + save_fname)
noaa_files.pop(-1) # get rid of old file name
noaa_files.append(save_fname) # add on new file name
else:
print(' ### %s already exists. ' % noaa_file)
# pdb.set_trace()
# Process wind data (read straight from gzipped file)
wind_data =[]
for noaa_file in noaa_files:
print(' ... processing ', noaa_file)
# pdb.set_trace()
if '.gz' in noaa_file:
f = gzip.open(noaa_file)
else:
f = open(noaa_file)
line = f.readline()
if line[0] == '#':
line = line[1:]
dataline = line.split()
if 'YYYY' in dataline:
yr_idx = dataline.index('YYYY')
else:
yr_idx = dataline.index('YY')
mo_idx = dataline.index('MM')
da_idx = dataline.index('DD')
hr_idx = dataline.index('hh')
if 'mm' in dataline:
mn_idx = dataline.index('mm')
else:
mn_idx = None
if 'SPD' in dataline:
speed_idx = dataline.index('SPD')
else:
speed_idx = dataline.index('WSPD')
if 'WDIR' in dataline:
dir_idx = dataline.index('WDIR')
elif 'DIR' in dataline:
dir_idx = dataline.index('DIR')
else:
dir_idx = dataline.index('WD')
for line in f.readlines():
if line[0] == '#': continue
data = line.split()
if len(data) == 0:
continue
yr = int(data[yr_idx])
if yr < 1900:
yr = yr+1900
mo = int(data[mo_idx])
da = int(data[da_idx])
hr = int(data[hr_idx])
if mn_idx is None:
date = datetime(yr, mo, da, hr)
else:
mn = int(data[mn_idx])
date = datetime(yr, mo, da, hr, mn)
# if 'realtime2' in noaa_file and data[dir_idx]=='MM':
# pdb.set_trace()
if not data[dir_idx] == 'MM': # if =='MM' don't want line
direction = float(data[dir_idx])
speed = float(data[speed_idx])
u = -1.0 * speed * sin(direction*pi/180.0) # convert to Oceanographic convention
v = -1.0 * speed * cos(direction*pi/180.0)
if speed != 99.0:
wind_data.append( (date, u, v, direction, speed) )
wind_data = np.array( wind_data,
dtype=[('date','O'),
('u','double'), ('v','double'),
('direction', 'double'), ('speed', 'double')])
wind_data.sort(order='date')
ud = np.unique(wind_data['date'])
idx = wind_data['date'].searchsorted(ud)
return wind_data[idx]
if __name__ == '__main__':
noaa_42007c = retrieve_noaa_winds('42007', 'c')
np.save('noaa_42007c', noaa_42007c)
noaa_42007h = retrieve_noaa_winds('42007', 'h')
np.save('noaa_42007h', noaa_42007h)
noaa_burl1c = retrieve_noaa_winds('burl1', 'c')
np.save('noaa_burl1c', noaa_burl1c)
noaa_burl1h = retrieve_noaa_winds('burl1', 'h')
np.save('noaa_burl1h', noaa_burl1h)
noaa_42040c = retrieve_noaa_winds('42040', 'c')
np.save('noaa_42040c', noaa_42040c)
noaa_42040h = retrieve_noaa_winds('42040', 'h')
np.save('noaa_42040h', noaa_42040h)