-
Notifications
You must be signed in to change notification settings - Fork 0
/
noaa.py
296 lines (263 loc) · 10.2 KB
/
noaa.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
import urllib
import numpy as np
import pandas as pd
import pytz
import requests
import xarray as xr
from dateutil import parser
def get_coops_data(
station,
start_date,
end_date,
product="hourly_height",
units="metric",
datum="MLLW",
time_zone="GMT",
interval=False,
bin=False,
):
"""
units can be 'english' or 'metric'
start_date and end_date must be formatted like:
yyyyMMdd, yyyyMMdd HH:mm, MM/dd/yyyy, or MM/dd/yyyy HH:mm
product options include 'water_level', 'hourly_height', 'predictions'
from https://tidesandcurrents.noaa.gov/api/
Option Description
water_level Preliminary or verified water levels, depending on availability.
air_temperature Air temperature as measured at the station.
water_temperature Water temperature as measured at the station.
wind Wind speed, direction, and gusts as measured at the station.
air_pressure Barometric pressure as measured at the station.
air_gap Air Gap (distance between a bridge and the water's surface) at the station.
conductivity The water's conductivity as measured at the station.
visibility Visibility from the station's visibility sensor. A measure of atmospheric clarity.
humidity Relative humidity as measured at the station.
salinity Salinity and specific gravity data for the station.
hourly_height Verified hourly height water level data for the station.
high_low Verified high/low water level data for the station.
daily_mean Verified daily mean water level data for the station.
monthly_mean Verified monthly mean water level data for the station.
one_minute_water_level One minute water level data for the station.
predictions 6 minute predictions water level data for the station.
datums datums data for the stations.
currents Currents data for currents stations.
"""
url = (
"https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product="
+ product
+ "&application=NOS.COOPS.TAC.WL&begin_date="
+ urllib.parse.quote(str(start_date))
+ "&end_date="
+ urllib.parse.quote(str(end_date))
+ "&datum="
+ datum
+ "&station="
+ str(station)
+ "&time_zone="
+ time_zone
+ "&units="
+ units
+ "&format=json"
)
if interval:
url = url + "&interval=" + interval
if bin:
url = f"{url}&bin={bin}"
if product == "currents_predictions":
url = f"{url}&vel_type=speed_dir"
payload = requests.get(url).json()
if "error" in payload.keys():
raise ValueError("Error in returning dataset: " + payload["error"]["message"])
t = []
v = []
wind = {"s": [], "d": [], "dr": [], "g": [], "f": []}
cp = {"Speed": [], "Bin": [], "Direction": [], "Depth": []}
cur = {"s": [], "d": [], "b": []}
if product in [
"water_level",
"hourly_height",
"air_pressure",
"air_temperature",
"water_temperature",
]:
d = payload["data"]
elif product == "monthly_mean":
d = payload["data"]
monthly = {x: [] for x in d[0].keys() if x != "month" and x != "year"}
elif product == "predictions":
d = payload["predictions"]
elif product == "wind":
d = payload["data"]
elif product == "currents_predictions":
d = payload["current_predictions"]["cp"]
elif product == "currents":
d = payload["data"]
elif product == "datums":
datums = {}
for k in payload["datums"]:
datums[k["n"]] = float(k["v"])
return datums
for n in range(len(d)):
if product == "currents_predictions":
t.append(pytz.utc.localize(parser.parse(d[n]["Time"])))
elif product != "monthly_mean":
t.append(pytz.utc.localize(parser.parse(d[n]["t"])))
else:
t.append(parser.parse(d[n]["year"] + "-" + d[n]["month"] + "-01"))
if product == "wind":
for k in ["s", "d", "dr", "g", "f"]:
if k == "dr" or k == "f":
wind[k].append(d[n][k])
else:
try:
wind[k].append(float(d[n][k]))
except:
wind[k].append(np.nan)
elif product == "monthly_mean":
for k in monthly.keys():
try:
monthly[k].append(float(d[n][k]))
except:
monthly[k].append(np.nan)
elif product == "currents_predictions":
for k in cp:
cp[k].append(float(d[n][k]))
elif product == "currents":
for k in cur:
cur[k].append(float(d[n][k]))
else:
try:
v.append(float(d[n]["v"]))
except:
v.append(np.nan)
ds = xr.Dataset()
n = {}
n["time"] = np.array(t)
if product == "wind":
for k in ["s", "d", "dr", "g", "f"]:
n[k] = np.array(wind[k])
elif product == "monthly_mean":
for k in monthly.keys():
n[k] = np.array(monthly[k])
elif product == "currents_predictions":
for k in cp:
n[k] = np.array(cp[k])
if "units" in payload["current_predictions"]:
ds.attrs["units"] = payload["current_predictions"]["units"]
elif product == "currents":
for k in cur:
n[k] = np.array(cur[k])
else:
n["v"] = np.array(v)
for k in n:
ds[k] = xr.DataArray(n[k], dims="time")
if product == "currents":
# also get metadata for bins
bins = requests.get(
f"https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations/{station}/bins.json"
).json()
ds["depth"] = bins["bins"][bin - 1]["depth"]
if "metadata" in payload:
for k in payload["metadata"]:
ds.attrs[k] = payload["metadata"][k]
products = [
"water_level",
"hourly_height",
"high_low",
"daily_mean",
"monthly_mean",
"one_minute_water_level",
"predictions",
]
if product in products:
ds.attrs["datum"] = datum
ds["time"] = pd.DatetimeIndex(ds["time"].values)
ds["time"] = pd.DatetimeIndex(
ds["time"].values
) # don't know why we need to do it twice, but we do in order for it to return as datetim64[ns]
return ds
def get_long_coops_data(
station,
start_date,
end_date,
product="hourly_height",
units="metric",
datum="MLLW",
time_zone="GMT",
interval=False,
bin=False,
):
"""
Get NOAA CO-OPS data for longer than 1 month.
This function makes recursive calls to get_coops_data() for time ranges
longer than one month.
units can be 'english' or 'metric'
start_date and end_date must be formatted like:
yyyyMMdd, yyyyMMdd HH:mm, MM/dd/yyyy, or MM/dd/yyyy HH:mm
product options include 'water_level', 'hourly_height', 'predictions'
from https://tidesandcurrents.noaa.gov/api/
Option Description
water_level Preliminary or verified water levels, depending on availability.
air_temperature Air temperature as measured at the station.
water_temperature Water temperature as measured at the station.
wind Wind speed, direction, and gusts as measured at the station.
air_pressure Barometric pressure as measured at the station.
air_gap Air Gap (distance between a bridge and the water's surface) at the station.
conductivity The water's conductivity as measured at the station.
visibility Visibility from the station's visibility sensor. A measure of atmospheric clarity.
humidity Relative humidity as measured at the station.
salinity Salinity and specific gravity data for the station.
hourly_height Verified hourly height water level data for the station.
high_low Verified high/low water level data for the station.
daily_mean Verified daily mean water level data for the station.
monthly_mean Verified monthly mean water level data for the station.
one_minute_water_level One minute water level data for the station.
predictions 6 minute predictions water level data for the station.
datums datums data for the stations.
currents Currents data for currents stations.
"""
# date ranges in 1-month chunks
dates = pd.date_range(start_date, end_date, freq="MS")
# need to add the beginning and end since pd.date_range normalizes to start of month
if pd.Timestamp(start_date) < dates[0]:
dates = dates.insert(0, pd.Timestamp(start_date))
if pd.Timestamp(end_date) > dates[-1]:
dates = dates.append(pd.DatetimeIndex([end_date]))
data = []
for n in range(len(dates) - 1):
print(dates[n], dates[n + 1])
try:
data.append(
get_coops_data(
station,
dates[n].strftime("%Y%m%d %H:%M"),
dates[n + 1].strftime("%Y%m%d %H:%M"),
product=product,
units=units,
datum=datum,
time_zone=time_zone,
interval=interval,
bin=bin,
)
)
except ValueError as e:
# sometimes a station goes offline
if "No data was found" in repr(e):
print("no data in {} - {}; skipping".format(dates[n], dates[n + 1]))
continue
else:
print(e)
continue
ds = xr.concat(data, dim="time")
# deduplicate times if necessary
_, index = np.unique(ds["time"], return_index=True)
ds = ds.isel(time=index)
ds["time"] = pd.DatetimeIndex(ds["time"].values)
return ds
def get_coops_metadata(station):
"""
Get NOAA CO-OPS metadata for a single station
"""
url = f"https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/stations/{station}.json"
payload = requests.get(url).json()
return payload