-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfetch_test_data.py
More file actions
executable file
·319 lines (289 loc) · 9.77 KB
/
fetch_test_data.py
File metadata and controls
executable file
·319 lines (289 loc) · 9.77 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
import pathlib
from pathlib import Path
import pandas as pd
import pooch
import typer
import xarray as xr
from loguru import logger
from ref_sample_data import CMIP6Request, DataRequest, Obs4REFRequest
OUTPUT_PATH = Path("data")
app = typer.Typer()
def _get_match(dataset: pd.DataFrame, source_type: str, key: str) -> pd.Series | None:
"""
Get the matching dataset from the processed datasets
Parameters
----------
dataset
The dataset to match against
source_type
The source type to match against
key
The key to match against
Returns
-------
The matching dataset
"""
matches = dataset.loc[(dataset.source_type == source_type) & (dataset.key == key)]
if len(matches) > 1:
raise ValueError(f"Found multiple datasets with the same key: {key}")
if len(matches) == 0:
return None
return matches.iloc[0]
def process_sample_data_request(
processed_datasets: pd.DataFrame,
request: DataRequest,
decimate: bool,
output_directory: Path,
) -> pd.DataFrame:
"""
Fetch and create sample datasets
Parameters
----------
processed_datasets
The datasets that have already been processed
request
The request to execute
This may be different types of requests, such as CMIP6Request or Obs4MIPsRequest.
decimate
Whether to decimate the datasets
output_directory
The directory to write the output to
Returns
-------
The processed datasets from this request
"""
datasets = request.fetch_datasets()
items = []
for _, dataset in datasets.iterrows():
match = _get_match(processed_datasets, request.source_type, dataset.key)
# Check if the dataset has already been processed and can be skipped
if match is not None and request.time_span is not None:
# Dataset has already been processed and a time span was specified
# Check if the dataset already covers the requested time span
if int(match.time_start) <= int(dataset["time_start"]) and int(match.time_end) >= int(
dataset["time_end"]
):
# Already have a dataset that covers the requested time span
logger.info(
f"Skipping regenerating {dataset.key} as it already covers the requested time span"
)
continue
# Update the request to match the superset of the time spans
time_start = (
dataset["time_start"] if dataset["time_start"] < match.time_start else match.time_start
)
time_end = dataset["time_end"] if dataset["time_end"] > match.time_end else match.time_end
request.time_span = (str(time_start), str(time_end))
logger.info(f"Regenerating dataset with new time span: {dataset.key} {request.time_span}")
for file in match.files:
file_path = pathlib.Path(file)
if file_path.exists():
logger.info(f"Removing existing file: {file}")
file_path.unlink()
output_filenames = []
for ds_filename in dataset["files"]:
ds_orig = xr.open_dataset(ds_filename)
if decimate:
ds_decimated = request.decimate_dataset(ds_orig)
else:
ds_decimated = ds_orig
if ds_decimated is None:
continue
output_filename = output_directory / request.generate_filename(dataset, ds_decimated, ds_filename)
output_filename.parent.mkdir(parents=True, exist_ok=True)
ds_decimated.to_netcdf(output_filename)
output_filenames.append(output_filename)
item = {
"source_type": request.source_type,
"key": dataset.key,
"files": output_filenames,
}
if request.time_span is not None:
item["time_start"] = request.time_span[0]
item["time_end"] = request.time_span[1]
items.append(item)
# Regenerate the registry.txt file
pooch.make_registry(str(OUTPUT_PATH), "registry.txt")
return pd.DataFrame(items)
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 Climate at global warmings levels data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "pr", "tas"],
experiment_id=["ssp126", "historical"],
),
remove_ensembles=True,
time_span=("1850", "2100"),
),
# ESMValTool Cloud radiative effects
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "rlut", "rlutcs", "rsut", "rsutcs"],
experiment_id="historical",
),
remove_ensembles=True,
time_span=("2005", "2014"),
),
# 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"),
),
# ESMValTool TCRE data
CMIP6Request(
facets=dict(
source_id="MPI-ESM1-2-LR",
frequency=["fx", "mon"],
variable_id=["areacella", "fco2antt", "tas"],
experiment_id=["esm-1pctCO2"],
),
remove_ensembles=True,
time_span=("1850", "1915"),
),
CMIP6Request(
facets=dict(
source_id="MPI-ESM1-2-LR",
frequency=["fx", "mon"],
variable_id=["areacella", "tas"],
experiment_id=["esm-piControl"],
),
remove_ensembles=True,
time_span=("1850", "1915"),
),
# ESMValTool ZEC data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "tas"],
experiment_id=["1pctCO2", "esm-1pct-brch-1000PgC"],
),
remove_ensembles=True,
time_span=("0158", "0268"),
),
# ESMValTool Sea Ice Area Seasonal Cycle data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacello", "siconc"],
experiment_id=["historical"],
),
remove_ensembles=True,
time_span=("1979", "2014"),
),
# ILAMB data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "sftlf", "gpp", "pr", "tas", "mrro", "mrsos", "cSoil", "lai"],
experiment_id=["historical"],
),
remove_ensembles=True,
time_span=("2000", "2025"),
),
# ILAMB data, nbp requires a longer time span
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["mon"],
variable_id=["nbp"],
experiment_id=["historical"],
),
remove_ensembles=True,
time_span=("1850", "2015"),
),
# IOMB data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacello", "sftof", "tos", "sos", "msftmz"],
experiment_id=["historical"],
),
remove_ensembles=True,
time_span=("2000", "2025"),
),
# PMP modes of variability data
CMIP6Request(
facets=dict(
source_id="ACCESS-ESM1-5",
frequency=["fx", "mon"],
variable_id=["areacella", "ts", "psl"],
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"),
# ),
# All unpublished obs4mips datasets
Obs4REFRequest(),
]
@app.command()
def create_sample_data(
decimate: bool = True,
output: Path = OUTPUT_PATH,
) -> None:
"""Fetch and create sample datasets"""
processed_datasets = pd.DataFrame(columns=["source_type", "key", "files", "time_start", "time_end"])
for dataset_requested in DATASETS_TO_FETCH:
# Process the request
new_datasets = process_sample_data_request(
processed_datasets,
dataset_requested,
decimate=decimate,
output_directory=pathlib.Path(output),
)
# Remove duplicate source_type and key values, but keep the latest one
processed_datasets = (
pd.concat([processed_datasets, new_datasets], ignore_index=True)
.drop_duplicates(subset=["source_type", "key"], keep="last")
.reset_index(drop=True)
)
if __name__ == "__main__":
app()