-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfetch_test_data.py
More file actions
430 lines (359 loc) · 13 KB
/
fetch_test_data.py
File metadata and controls
430 lines (359 loc) · 13 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import os
import pathlib
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any
import pandas as pd
import pooch
import xarray as xr
from intake_esgf import ESGFCatalog
OUTPUT_PATH = Path("data")
class DataRequest(ABC):
"""
Represents a request for a dataset
A polymorphic association is used to capture the different types of datasets as each
dataset type may have different metadata fields and may need to be handled
differently to generate the sample data.
"""
def __init__(self, remove_ensembles: bool, time_span: tuple[str, str]):
self.remove_ensembles = remove_ensembles
self.time_span = time_span
@abstractmethod
def decimate_dataset(self, dataset: xr.Dataset, time_span: tuple[str, str] | None) -> xr.Dataset | None:
"""Downscale the dataset to a smaller size."""
pass
@abstractmethod
def create_out_filename(self, metadata: pd.Series, ds: xr.Dataset, ds_filename: str) -> pathlib.Path:
"""Create the output filename for the dataset."""
pass
class CMIP6Request(DataRequest):
"""
Represents a CMIP6 dataset request
"""
def __init__(self, facets: dict[str, Any], remove_ensembles: bool, time_span: tuple[str, str] | None):
self.avail_facets = [
"mip_era",
"activity_drs",
"institution_id",
"source_id",
"experiment_id",
"member_id",
"table_id",
"variable_id",
"grid_label",
"version",
"data_node",
]
self.facets = facets
super().__init__(remove_ensembles, time_span)
self.cmip6_path_items = [
"mip_era",
"activity_drs",
"institution_id",
"source_id",
"experiment_id",
"member_id",
"table_id",
"variable_id",
"grid_label",
]
self.cmip6_filename_paths = [
"variable_id",
"table_id",
"source_id",
"experiment_id",
"member_id",
"grid_label",
]
assert all(key in self.avail_facets for key in self.cmip6_path_items), "Error message"
assert all(key in self.avail_facets for key in self.cmip6_filename_paths), "Error message"
def decimate_dataset(self, dataset: xr.Dataset, time_span: tuple[str, str] | None) -> xr.Dataset | None:
"""
Downscale the dataset to a smaller size.
Parameters
----------
dataset
The dataset to downscale
time_span
The time span to extract from a dataset
Returns
-------
xr.Dataset
The downscaled dataset
"""
has_latlon = "lat" in dataset.dims and "lon" in dataset.dims
has_ij = "i" in dataset.dims and "j" in dataset.dims
if has_latlon:
assert len(dataset.lat.dims) == 1 and len(dataset.lon.dims) == 1
result = dataset.interp(lat=dataset.lat[:10], lon=dataset.lon[:10])
elif has_ij:
# 2d lat/lon grid (generally ocean variables)
# Choose a starting point around the middle of the grid to maximise chance that it has values
# TODO: Be smarter about this?
j_midpoint = len(dataset.j) // 2
result = dataset.interp(i=dataset.i[:10], j=dataset.j[j_midpoint : j_midpoint + 10])
else:
raise ValueError("Cannot decimate this grid: too many dimensions")
if "time" in dataset.dims and time_span is not None:
result = result.sel(time=slice(*time_span))
if result.time.size == 0:
result = None
return result
def create_out_filename(self, metadata: pd.Series, ds: xr.Dataset, ds_filename: str) -> pathlib.Path:
"""
Create the output filename for the dataset.
Parameters
----------
ds
Loaded dataset
Returns
-------
The output filename
"""
output_path = (
Path(os.path.join(*[metadata[item] for item in self.cmip6_path_items]))
/ f"v{metadata['version']}"
)
filename_prefix = "_".join([metadata[item] for item in self.cmip6_filename_paths])
if "time" in ds.dims:
time_range = (
f"{ds.time.min().dt.strftime('%Y%m').item()}-{ds.time.max().dt.strftime('%Y%m').item()}"
)
filename = f"{filename_prefix}_{time_range}.nc"
else:
filename = f"{filename_prefix}.nc"
return output_path / filename
class Obs4MIPsRequest(DataRequest):
"""
Represents a Obs4MIPs dataset request
"""
def __init__(self, facets: dict[str, Any], remove_ensembles: bool, time_span: tuple[str, str] | None):
self.avail_facets = [
"activity_id",
"institution_id",
"source_id",
"frequency",
"variable_id",
"grid_label",
"version",
"data_node",
]
self.facets = facets
super().__init__(remove_ensembles, time_span)
self.obs4mips_path_items = [
"activity_id",
"institution_id",
"source_id",
"variable_id",
"grid_label",
]
self.obs4mips_filename_paths = [
"variable_id",
"source_id",
"grid_label",
]
assert all(key in self.avail_facets for key in self.obs4mips_path_items), "Error message"
assert all(key in self.avail_facets for key in self.obs4mips_filename_paths), "Error message"
def decimate_dataset(self, dataset: xr.Dataset, time_span: tuple[str, str] | None) -> xr.Dataset | None:
"""
Downscale the dataset to a smaller size.
Parameters
----------
dataset
The dataset to downscale
time_span
The time span to extract from a dataset
Returns
-------
xr.Dataset
The downscaled dataset
"""
has_latlon = "lat" in dataset.dims and "lon" in dataset.dims
has_ij = "i" in dataset.dims and "j" in dataset.dims
if has_latlon:
assert len(dataset.lat.dims) == 1 and len(dataset.lon.dims) == 1
result = dataset.interp(lat=dataset.lat[:10], lon=dataset.lon[:10])
elif has_ij:
# 2d lat/lon grid (generally ocean variables)
# Choose a starting point around the middle of the grid to maximise chance that it has values
# TODO: Be smarter about this?
j_midpoint = len(dataset.j) // 2
result = dataset.interp(i=dataset.i[:10], j=dataset.j[j_midpoint : j_midpoint + 10])
else:
raise ValueError("Cannot decimate this grid: too many dimensions")
if "time" in dataset.dims and time_span is not None:
result = result.sel(time=slice(*time_span))
if result.time.size == 0:
result = None
return result
def create_out_filename(self, metadata: pd.Series, ds: xr.Dataset, ds_filename: str) -> pathlib.Path:
"""
Create the output filename for the dataset.
Parameters
----------
ds
Loaded dataset
Returns
-------
The output filename
"""
output_path = (
Path(os.path.join(*[metadata[item] for item in self.obs4mips_path_items]))
/ f"v{metadata['version']}"
)
if ds_filename.name.split("_")[0] == ds.variable_id:
filename_prefix = "_".join([metadata[item] for item in self.obs4mips_filename_paths])
else:
filename_prefix = ds_filename.name.split("_")[0] + "_"
filename_prefix += "_".join(
[metadata[item] for item in self.obs4mips_filename_paths if item != "variable_id"]
)
if "time" in ds.dims:
time_range = (
f"{ds.time.min().dt.strftime('%Y%m').item()}-{ds.time.max().dt.strftime('%Y%m').item()}"
)
filename = f"{filename_prefix}_{time_range}.nc"
else:
filename = f"{filename_prefix}.nc"
return output_path / filename
def fetch_datasets(request: DataRequest) -> pd.DataFrame:
"""
Fetch the datasets from ESGF.
Parameters
----------
search_facets
Facets to search for
remove_ensembles
Whether to remove ensembles from the dataset
(i.e. include only a single ensemble member)
Returns
-------
List of paths to the fetched datasets
"""
cat = ESGFCatalog()
cat.search(**request.facets)
if request.remove_ensembles:
cat.remove_ensembles()
path_dict = cat.to_path_dict(prefer_streaming=False, minimal_keys=False)
merged_df = cat.df.merge(pd.Series(path_dict, name="files"), left_on="key", right_index=True)
if request.time_span:
merged_df["time_start"] = request.time_span[0]
merged_df["time_end"] = request.time_span[1]
return merged_df
def deduplicate_datasets(request: DataRequest) -> pd.DataFrame:
"""
Deduplicate a dataset collection.
Uses the metadata from the first dataset in each group,
but expands the time range to the min/max timespan of the group.
Parameters
----------
datasets
The dataset collection
Returns
-------
pd.DataFrame
The deduplicated dataset collection spanning the times requested
"""
datasets = fetch_datasets(request)
def _deduplicate_group(group: pd.DataFrame) -> pd.DataFrame:
first = group.iloc[0].copy()
first.time_start = group.time_start.min()
first.time_end = group.time_end.max()
return first
return datasets.groupby("key").apply(_deduplicate_group, include_groups=False).reset_index()
def create_sample_dataset(request: DataRequest):
"""
Create the output filename for the dataset.
Parameters
----------
ds
Loaded dataset
Returns
-------
The output filename
"""
datasets = deduplicate_datasets(request)
for _, dataset in datasets.iterrows():
for ds_filename in dataset["files"]:
ds_orig = xr.open_dataset(ds_filename)
ds_decimated = request.decimate_dataset(ds_orig, request.time_span)
if ds_decimated is None:
continue
output_filename = OUTPUT_PATH / request.create_out_filename(dataset, ds_decimated, ds_filename)
output_filename.parent.mkdir(parents=True, exist_ok=True)
ds_decimated.to_netcdf(output_filename)
# Regenerate the registry.txt file
pooch.make_registry(OUTPUT_PATH, "registry.txt")
if __name__ == "__main__":
datasets_to_fetch = [
# Example metric data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "tas", "tos", "rsut", "rlut", "rsdt"],
experiment_id=["ssp126", "historical"],
),
remove_ensembles=True,
time_span=("2000", "2025"),
),
# ESMValTool ECS data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "rlut", "rsdt", "rsut", "tas"],
experiment_id=["abrupt-4xCO2", "piControl"],
),
remove_ensembles=True,
time_span=("0101", "0125"),
),
# ESMValTool TCR data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "tas"],
experiment_id=["1pctCO2", "piControl"],
),
remove_ensembles=True,
time_span=("0101", "0180"),
),
# ILAMB data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "sftlf", "gpp", "pr"],
experiment_id=["historical"],
),
remove_ensembles=True,
time_span=("2000", "2025"),
),
# PMP PDO data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "ts"],
experiment_id=["historical", "hist-GHG"],
variant_label=["r1i1p1f1", "r2i1p1f1"],
),
remove_ensembles=False,
time_span=("2000", "2025"),
),
# Obs4MIPs AIRS data
Obs4MIPsRequest(
facets=dict(
project="obs4MIPs",
institution_id="NASA-JPL",
frequency="mon",
source_id="AIRS-2-1",
variable_id="ta",
),
remove_ensembles=False,
time_span=("2002", "2016"),
),
]
for dataset_requested in datasets_to_fetch:
create_sample_dataset(dataset_requested)