-
Notifications
You must be signed in to change notification settings - Fork 2
/
voto_erddap_utils.py
345 lines (308 loc) · 11.7 KB
/
voto_erddap_utils.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
import numpy as np
import pathlib
import xarray as xr
import pandas as pd
import requests
from erddapy import ERDDAP
from tqdm import tqdm
import xml.etree.ElementTree as ET
from collections import defaultdict
cache_dir = pathlib.Path('voto_erddap_data_cache')
def init_erddap(protocol="tabledap"):
# Setup initial ERDDAP connection
e = ERDDAP(
server="https://erddap.observations.voiceoftheocean.org/erddap",
protocol=protocol,
)
return e
def _clean_dims(ds):
if "timeseries" in ds.sizes.keys() and "obs" in ds.sizes.keys():
ds = ds.drop_dims("timeseries")
if "obs" in ds.sizes.keys():
ds = ds.swap_dims({"obs": "time"})
return ds
def _get_meta_griddap(dataset_id):
e = init_erddap(protocol="griddap")
e.dataset_id = dataset_id
e.griddap_initialize()
time = pd.read_csv(f"https://erddap.observations.voiceoftheocean.org/erddap/griddap/{dataset_id}.csvp?time")[
"time (UTC)"].values
e.constraints['time>='] = str(time[-20])
ds = e.to_xarray()
attrs = ds.attrs
# Clean up formatting of variables list
if "variables" in attrs.keys():
if "\n" in attrs["variables"]:
attrs["variables"] = attrs["variables"].split("\n")
# evaluate dictionaries
for key, val in attrs.items():
if type(val) == str:
if "{" in val:
attrs[key] = eval(val)
if "basin" not in attrs.keys():
attrs["basin"] = ""
return attrs
def _etree_to_dict(t):
d = {t.tag: {} if t.attrib else None}
children = list(t)
if children:
dd = defaultdict(list)
for dc in map(_etree_to_dict, children):
for k, v in dc.items():
dd[k].append(v)
d = {t.tag: {k: v[0] if len(v) == 1 else v
for k, v in dd.items()}}
if t.attrib:
d[t.tag].update(('@' + k, v)
for k, v in t.attrib.items())
if t.text:
text = t.text.strip()
if children or t.attrib:
if text:
d[t.tag]['#text'] = text
else:
d[t.tag] = text
return d
def get_meta(dataset_id, protocol="tabledap"):
if "adcp" in dataset_id or protocol=="griddap":
# Cannot use to_ncCF with griddap
return _get_meta_griddap(dataset_id)
e = init_erddap(protocol=protocol)
e.dataset_id = dataset_id
meta = e.to_ncCF()
attrs = {}
for key_name in dir(meta):
if key_name[0] != "_":
attrs[key_name] = meta.__getattribute__(key_name)
# Clean up formatting of variables list
if "variables" in attrs.keys():
if type(attrs["variables"]) is dict:
attrs["variables"] = list(attrs["variables"].keys())
# evaluate dictionaries
for key, val in attrs.items():
if type(val) == str:
if "{" in val:
attrs[key] = eval(val)
if "basin" not in attrs.keys():
attrs["basin"] = ""
return attrs
def date_from_iso(dataset_id):
req = requests.get(f'https://erddap.observations.voiceoftheocean.org/erddap/tabledap/{dataset_id}.iso19115')
with open('iso.xml', 'w') as file:
file.write(req.text)
tree = ET.parse('iso.xml')
root = tree.getroot()
ddict = _etree_to_dict(root)
id_info = ddict[list(ddict.keys())[0]]['{http://www.isotc211.org/2005/gmd}identificationInfo'][0]
citation_info = id_info['{http://www.isotc211.org/2005/gmd}MD_DataIdentification']['{http://www.isotc211.org/2005/gmd}citation']['{http://www.isotc211.org/2005/gmd}CI_Citation']
date_info = citation_info['{http://www.isotc211.org/2005/gmd}date'][0]['{http://www.isotc211.org/2005/gmd}CI_Date']
datestamp = date_info['{http://www.isotc211.org/2005/gmd}date']['{http://www.isotc211.org/2005/gco}Date']
return datestamp
def find_glider_datasets(nrt_only=True):
"""
Find the dataset IDs of all glider datasets on the VOTO ERDDAP server
nrt_only: if True, only returns nrt datasets
"""
e = init_erddap()
# Fetch dataset list
e.response = "csv"
e.dataset_id = "allDatasets"
df_datasets = e.to_pandas()
datasets = df_datasets.datasetID
# Select only nrt datasets
if nrt_only:
datasets = datasets[datasets.str[:3] == "nrt"]
return datasets.values
def add_profile_time(ds):
profile_num = ds.pressure.copy()
profile_num.attrs = {}
profile_num.name = "profile_num"
profile_num[:] = 0
start = 0
for i, prof_index in enumerate(ds.profile_index):
rowsize = ds.rowSize.values[i]
profile_num[start:start + rowsize] = prof_index
start = start + rowsize
ds["profile_num"] = profile_num
profile_time = ds.time.values.copy()
profile_index = ds.profile_num
for profile in np.unique(profile_index.values):
mean_time = ds.time[profile_index == profile].mean().values
new_times = np.empty((len(ds.time[profile_index == profile])), dtype='datetime64[ns]')
new_times[:] = mean_time
profile_time[profile_index == profile] = new_times
profile_time_var = ds.time.copy()
profile_time_var.values = profile_time
profile_time_var.name = "profile_mean_time"
ds["profile_mean_time"] = profile_time_var
ds = _clean_dims(ds)
return ds
def _cached_dataset_exists(ds_id, request):
"""
Returns True if all the following conditions are met:
1. A dataset corresponding to ds_id exists in the cache
2. The cached dataset was downloaded with the same request
3. The dataset has not been updated on the VOTO ERDDAP since it was last downloaded
Otherwise, returns False
"""
if not cache_dir.exists():
print(f"Creating directory to cache datasets at {cache_dir.absolute()}")
pathlib.Path(cache_dir).mkdir(parents=True, exist_ok=True)
return False
dataset_nc = cache_dir / f"{ds_id}.nc"
if not dataset_nc.exists():
print(f"Dataset {ds_id} not found in cache")
return False
try:
df = pd.read_csv(cache_dir / "cache_info.csv", index_col=0)
except:
print(f"no cache records file found")
return False
if ds_id in df.index:
stats = df.loc[ds_id]
else:
print(f"no cache record found for {ds_id}")
return False
if not stats["request"] == request:
print(f"request has changed for {ds_id}")
return False
nc_time = pd.to_datetime(stats["date_created"][:-6])
try:
created_date = date_from_iso(ds_id)
erddap_time = pd.to_datetime(created_date)
except:
print(f"Dataset {ds_id} date updated check failed. Will re-download")
return False
if nc_time < erddap_time:
print(f"Dataset {ds_id} has been updated on ERDDAP")
return False
return True
def _update_stats(ds_id, request):
"""
Update the stats for a specified dataset
"""
dataset_nc = cache_dir / f"{ds_id}.nc"
ds = xr.open_dataset(dataset_nc)
try:
df = pd.read_csv(cache_dir / "cache_info.csv", index_col=0)
except:
df = pd.DataFrame()
nc_time = ds.attrs["date_created"]
new_stats = {"request": request, "date_created": pd.to_datetime(nc_time)}
if ds_id in df.index:
df.loc[ds_id] = new_stats
else:
new_row = pd.DataFrame(new_stats, index=[ds_id])
df = pd.concat((df, new_row))
df = df.sort_index()
df.to_csv(cache_dir / "cache_info.csv")
ds.close()
def add_adcp_data(ds):
dataset_id = ds.attrs["dataset_id"]
parts = dataset_id.split("_")
adcp_id = f"adcp_{parts[1]}_{parts[2]}"
cached_ds = _cached_dataset_exists(adcp_id, "adcp")
dataset_nc = cache_dir / f"{adcp_id}.nc"
if cached_ds:
print(f"Found {dataset_nc}. Loading from disk")
adcp = xr.open_dataset(dataset_nc)
else:
dataset_ids = find_glider_datasets(nrt_only=False)
if adcp_id not in dataset_ids:
print(f"Requested ADCP dataset {adcp_id} does not exist on server! Returning standard dataset")
return ds
print(f"Downloading {adcp_id}")
e = ERDDAP(server="https://erddap.observations.voiceoftheocean.org/erddap/", protocol="griddap", )
e.dataset_id = adcp_id
e.griddap_initialize()
time = pd.read_csv(f"https://erddap.observations.voiceoftheocean.org/erddap/griddap/{adcp_id}.csvp?time")[
"time (UTC)"].values
e.constraints['time>='] = str(time[0])
adcp = e.to_xarray()
adcp = adcp.sortby("time")
adcp.to_netcdf(dataset_nc)
_update_stats(adcp_id, "adcp")
ds = _clean_dims(ds)
if parts[0] == "nrt":
print("WARNING: matching adcp data to nearest nrt timestamp. Potential missmatch of ~ 15 seconds. "
"Use delayed mode data for closer timestamp match")
adcp = adcp.reindex(time=ds.time, method="nearest")
for var_name in list(adcp):
ds[{var_name}] = adcp[var_name]
adcp_attrs_dict = {i: j for i, j in adcp.attrs.items() if i not in ds.attrs}
ds.attrs["adcp_attributes"] = str(adcp_attrs_dict)
return ds
def download_glider_dataset(dataset_ids, variables=(), constraints={}, nrt_only=False, delayed_only=False,
cache_datasets=True, adcp=False):
"""
Download datasets from the VOTO server using a supplied list of dataset IDs.
dataset_ids: list of datasetIDs present on the VOTO ERDDAP
variables: data variables to download. If left empty, will download all variables
"""
if nrt_only and delayed_only:
raise ValueError("Cannot set both nrt_only and delayed_only")
if nrt_only:
ids_to_download = []
for name in dataset_ids:
if "nrt" in name:
ids_to_download.append(name)
else:
print(f"{name} is not nrt. Ignoring")
elif delayed_only:
ids_to_download = []
for name in dataset_ids:
if "delayed" in name:
ids_to_download.append(name)
else:
print(f"{name} is not delayed. Ignoring")
else:
ids_to_download = dataset_ids
e = init_erddap()
# Specify variables of interest if supplied
if variables:
e.variables = variables
if constraints:
e.constraints = constraints
# Download each dataset as xarray
glider_datasets = {}
for ds_name in tqdm(ids_to_download):
if cache_datasets and "delayed" in ds_name:
e.dataset_id = ds_name
request = e.get_download_url()
cached_dataset = _cached_dataset_exists(ds_name, request)
dataset_nc = cache_dir / f"{ds_name}.nc"
if cached_dataset:
print(f"Found {ds_name} in {cache_dir}. Loading from disk")
ds = xr.open_dataset(dataset_nc)
if adcp:
ds = add_adcp_data(ds)
glider_datasets[ds_name] = ds
else:
print(f"Downloading {ds_name}")
try:
ds = e.to_xarray(requests_kwargs={"timeout": 300})
except BaseException as ex:
print(ex)
continue
ds = _clean_dims(ds)
print(f"Writing {dataset_nc}")
ds = ds.sortby("time")
ds.to_netcdf(dataset_nc)
if adcp:
ds = add_adcp_data(ds)
glider_datasets[ds_name] = ds
_update_stats(ds_name, request)
else:
print(f"Downloading {ds_name}")
e.dataset_id = ds_name
try:
ds = e.to_xarray()
except BaseException as ex:
print(ex)
continue
ds = _clean_dims(ds)
if adcp:
ds = add_adcp_data(ds)
ds = ds.sortby("time")
glider_datasets[ds_name] = ds
return glider_datasets