-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathERDDAPY_Intro_IOOS.py
366 lines (233 loc) · 6.81 KB
/
ERDDAPY_Intro_IOOS.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# coding: utf-8
# # Accessing ERDDAP from Python
#
# ERDDAP rich responses and RESTful API is makes it **THE** most convenient way to serve data.
#
# One can build URLs manually or programmatically like:
#
# <small>`https://erddap-uncabled.oceanobservatories.org/uncabled/erddap/tabledap/CP05MOAS-GL336-02-FLORTM000-flort_m_glider_instrument-telemetered-deployment0005-tabledap.csv?ctdgv_m_glider_instrument_sci_water_temp,time&time>=2017-02-10T00:00:00Z`</small>
# - server: `https://data.ioos.us/gliders/erddap/`
# - protocol: `tabledap`
# - dataset_id: `cp_336-20170817T1159`
# - variables: `time,latitude,longitude,temperature`
# - constraints:
# - `time>=2017-10-11T00:00:00Z`
# - `time<=2017-10-18T00:00:00Z`
# - `latitude>=38.0`
# - `latitude<=41.0`
# - `longitude>=-72.0`
# - `longitude<=-69.0`
# In[1]:
from erddapy import ERDDAP
server = 'https://data.ioos.us/gliders/erddap'
dataset_id = 'cp_336-20170817T1159'
constraints = {
'time>=': '2017-10-11T00:00:00Z',
'time<=': '2017-10-18T08:16:57Z',
'latitude>=': 38.0,
'latitude<=': 41.0,
'longitude>=': -72.0,
'longitude<=': -69.0
}
depth = 'depth'
salinity = 'salinity'
temperature = 'temperature'
variables = [
depth,
'latitude',
'longitude',
salinity,
temperature,
'time',
]
# In[2]:
e = ERDDAP(
server=server,
dataset_id=dataset_id,
constraints=constraints,
variables=variables,
protocol='tabledap',
response='mat',
)
print(e.get_download_url())
# # Obtaining the data
#
# There are a few methods to obtain the data with *to_pandas()* and *to_xarray()*:
# In[3]:
df = e.to_pandas(
index_col='time',
parse_dates=True,
skiprows=(1,) # units information can be dropped.
).dropna()
# In[4]:
df.head()
# # Let's plot the data
# # Exploring an ERDDAP server
# In[5]:
from erddapy import ERDDAP
e = ERDDAP(server='https://data.ioos.us/gliders/erddap')
# In[6]:
import pandas as pd
df = pd.read_csv(e.get_search_url(response='csv', search_for='all'))
# In[7]:
'We have {} tabledap, {} griddap, and {} wms endpoints.'.format(
len(set(df['tabledap'].dropna())),
len(set(df['griddap'].dropna())),
len(set(df['wms'].dropna()))
)
# # ERDDAP Advanced Search
#
# Let's narrow the search area, time span, and look for *sea_water_temperature* only.
# In[8]:
bbox = [-72.0, -69.0, 38.0, 41.0]
min_time = '2018-02-01T00:00:00Z'
max_time = '2018-02-08T00:00:00Z'
kw = {
'standard_name': 'sea_water_temperature',
'search_for': 'glider',
'min_lon': bbox[0],
'max_lon': bbox[1],
'min_lat': bbox[2],
'max_lat': bbox[3],
'min_time': min_time,
'max_time': max_time,
'cdm_data_type': 'trajectory'
}
# In[9]:
search_url = e.get_search_url(response='csv', **kw)
search = pd.read_csv(search_url)
gliders = search['Dataset ID'].values
msg = 'Found {} Glider Datasets:\n\n{}'.format
print(msg(len(gliders), '\n'.join(gliders)))
# With the Dataset IDs we can explore the metadata with the *get_info_url*
# In[10]:
print(gliders[0])
info_url = e.get_info_url(dataset_id=gliders[0], response='csv')
info = pd.read_csv(info_url)
info.head()
# In[11]:
cdm_profile_variables = info.loc[
info['Attribute Name'] == 'cdm_profile_variables', 'Value'
]
print(''.join(cdm_profile_variables))
# # Selecting variables by attributes
# In[12]:
e.get_var_by_attr(
dataset_id='cp_336-20180126T0000',
standard_name='sea_water_temperature'
)
# # Easy to use CF conventions standards
# In[13]:
t_vars = [
e.get_var_by_attr(
dataset_id=glider, standard_name='sea_water_temperature'
)[0] for glider in gliders
]
t_vars
# In[14]:
s_vars = [
e.get_var_by_attr(
dataset_id=glider, standard_name='sea_water_practical_salinity'
)[0] for glider in gliders
]
s_vars
# In[15]:
d_vars = [
e.get_var_by_attr(
dataset_id=glider, standard_name='sea_water_pressure'
)[0] for glider in gliders
]
d_vars
# In[16]:
# FIX: should not really assume that variables are the same for each dataset
depth = d_vars[0]
salinity = s_vars[0]
temperature = t_vars[0]
# # Putting everything together
# In[17]:
from requests.exceptions import HTTPError
constraints = {
'time>=': min_time,
'time<=': max_time,
'longitude>=': bbox[0],
'longitude<=': bbox[1],
'latitude>=': bbox[2],
'latitude<=': bbox[3]
}
def download_csv(url):
return pd.read_csv(
url, index_col='time', parse_dates=True, skiprows=[1]
)
dfs = {}
for glider in gliders:
try:
download_url = e.get_download_url(
dataset_id=glider,
protocol='tabledap',
variables=['time', 'latitude', 'longitude', depth, salinity, temperature],
response='csv',
constraints=constraints
)
except HTTPError:
print('Failed to download {}'.format(glider))
continue
dfs.update({glider: download_csv(download_url)})
# In[18]:
import numpy as np
for glider in dfs.keys():
dfs[glider].loc[dfs[glider][salinity] <= .1, salinity] = np.NaN
dfs[glider].loc[dfs[glider][temperature] <= .1, temperature] = np.NaN
# In[19]:
import folium
zoom_start = 7
lon = (bbox[0] + bbox[1]) / 2
lat = (bbox[2] + bbox[3]) / 2
m = folium.Map(width='100%', height='100%',
location=[lat, lon], zoom_start=zoom_start)
url = 'https://gis.ngdc.noaa.gov/arcgis/services/gebco08_hillshade/MapServer/WMSServer'
w = folium.WmsTileLayer(
url,
name='GEBCO Bathymetry',
fmt='image/png',
layers='GEBCO_08 Hillshade',
attr='GEBCO',
overlay=True,
transparent=True)
w.add_to(m)
colors = ['orange','pink','yellow']
k=0
for glider, df in dfs.items():
line = folium.PolyLine(locations=list(zip(df['latitude'],df['longitude'])),
color=colors[k],
weight=8,
opacity=0.6,
popup=glider[:22]).add_to(m)
k = k+1
m
# In[20]:
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
def glider_scatter(df, ax, glider):
ax.scatter(df[temperature], df[salinity],
s=10, alpha=0.5, label=glider)
fig, ax = plt.subplots(figsize=(12, 7))
ax.set_ylabel('salinity')
ax.set_xlabel('temperature')
ax.grid(True)
for glider, df in dfs.items():
glider_scatter(df, ax, glider)
leg = ax.legend()
# ## Plot one of the glider transects
# In[21]:
df = next(iter(dfs.values()))
# In[22]:
import matplotlib.dates as mdates
fig, ax = plt.subplots(figsize=(17, 2))
cs = ax.scatter(df.index, df[depth], s=15, c=df[temperature], marker='o', edgecolor='none')
ax.invert_yaxis()
ax.set_xlim(df.index[0], df.index[-1])
xfmt = mdates.DateFormatter('%H:%Mh\n%d-%b')
ax.xaxis.set_major_formatter(xfmt)
cbar = fig.colorbar(cs, orientation='vertical', extend='both')
cbar.ax.set_ylabel('Temperature ($^\circ$C)')
ax.set_ylabel('Depth (m)');