-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_data_definitions.py
280 lines (236 loc) · 7.37 KB
/
_data_definitions.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
import datetime
import json
import logging.config
import os
from pathlib import Path
from typing import Dict, List, Mapping, Optional, Union
from miranda.scripting import LOGGING_CONFIG
from miranda.storage import report_file_size
logging.config.dictConfig(LOGGING_CONFIG)
__all__ = [
"era5_variables",
"gather_agcfsr",
"gather_agmerra",
"gather_ecmwf",
"gather_grnch",
"gather_nrcan_gridded_obs",
"gather_raw_rdrs_by_years",
"gather_rdrs",
"gather_sc_earth",
"gather_wfdei_gem_capa",
"nasa_ag_variables",
"nrcan_variables",
"project_institutes",
"sc_earth_variables",
"wfdei_gem_capa_variables",
"xarray_frequencies_to_cmip6like",
]
_data_folder = Path(__file__).parent / "data"
eccc_rdrs_variables = json.load(open(_data_folder / "eccc_rdrs_cf_attrs.json"))[
"variables"
].keys()
era5_variables = json.load(open(_data_folder / "ecmwf_cf_attrs.json"))[
"variables"
].keys()
grnch_variables = ["T", "Tmin", "Tmax", "P"]
nrcan_variables = ["tasmin", "tasmax", "pr"]
nasa_ag_variables = json.load(open(_data_folder / "nasa_cf_attrs.json"))[
"variables"
].keys()
sc_earth_variables = ["prcp", "tdew", "tmean", "trange", "wind"]
wfdei_gem_capa_variables = json.load(open(_data_folder / "usask_cf_attrs.json"))[
"variables"
].keys()
project_institutes = {
"cfsr": "ncar",
"era5": "ecmwf",
"era5-land": "ecmwf",
"era5-land-monthly-means": "ecmwf",
"era5-monthly": "ecmwf",
"era5-pressure-levels": "ecmwf",
"era5-pressure-levels-preliminary-back-extension": "ecmwf",
"era5-pressure-monthly-means-levels-preliminary-back-extension": "ecmwf",
"era5-single-levels": "ecmwf",
"era5-single-levels-monthly-means": "ecmwf",
"era5-single-levels-monthly-means-preliminary-back-extension": "ecmwf",
"era5-single-levels-preliminary-back-extension": "ecmwf",
"merra2": "nasa",
"nrcan-gridded-10km": "nrcan",
"wfdei-gem-capa": "usask",
}
# Manually map xarray frequencies to CMIP6/CMIP5 controlled vocabulary.
# see: https://github.com/ES-DOC/pyessv-archive
xarray_frequencies_to_cmip6like = {
"H": "hr",
"D": "day",
"W": "sem",
"M": "mon",
"Q": "qtr", # TODO does this make sense? does not exist in cmip6 CV
"A": "yr",
"Y": "yr",
}
def _gather(
name: str,
variables: List[str],
source: Union[str, os.PathLike],
glob_pattern: str,
suffix: Optional[str] = None,
) -> Dict[str, List[Path]]:
source = Path(source).expanduser()
logging.info(f"Gathering {name} files from: {source.as_posix()}")
in_files = list()
for variable in variables:
if suffix:
pattern = glob_pattern.format(variable, suffix)
if suffix == "zarr":
in_files.extend(list(sorted(source.glob(pattern))))
continue
else:
pattern = glob_pattern.format(variable)
in_files.extend(list(sorted(source.rglob(pattern))))
logging.info(
f"Found {len(in_files)} files, totalling {report_file_size(in_files)}."
)
return {name: in_files}
def gather_ecmwf(
project: str,
path: Union[str, os.PathLike],
back_extension: bool = False,
monthly_means: bool = False,
) -> Dict[str, List[Path]]:
"""
Parameters
----------
project : {"era5-single-levels", "era5-pressure-levels", "era5-land"}
path : str or os.PathLike
back_extension : bool
monthly_means : bool
Returns
-------
dict(str, list[pathlib.Path])
"""
# ERA5-Single-Levels source data
name = (
f"{project}"
f"{'-monthly-means' if monthly_means else ''}"
f"{'-preliminary-back-extension' if back_extension else ''}"
)
glob_pattern = "".join(["{variable}", f"_*_{name}_*.nc"])
return _gather(name, era5_variables, source=path, glob_pattern=glob_pattern)
def gather_agmerra(path: Union[str, os.PathLike]) -> Dict[str, List[Path]]:
"""Gather agMERRA source data.
Parameters
----------
path : str or os.PathLike
Returns
-------
dict(str, list[pathlib.Path])
"""
return _gather(
"merra", nasa_ag_variables, source=path, glob_pattern="AgMERRA_*_{variable}.nc4"
)
def gather_agcfsr(path: Union[str, os.PathLike]) -> Dict[str, List[Path]]:
"""Gather agCFSR source data.
Parameters
----------
path: str or os.PathLike
Returns
-------
dict(str, list[pathlib.Path])
"""
return _gather(
"cfsr", nasa_ag_variables, source=path, glob_pattern="AgCFSR_*_{variable}.nc4"
)
def gather_nrcan_gridded_obs(path: Union[str, os.PathLike]) -> Dict[str, List[Path]]:
"""Gather NRCan Gridded Observations source data.
Parameters
----------
path: str or os.PathLike
Returns
-------
dict(str, list[pathlib.Path])
"""
return _gather(
"nrcan", nrcan_variables, source=path, glob_pattern="*{variable}_*.nc"
)
def gather_wfdei_gem_capa(path: Union[str, os.PathLike]) -> Dict[str, List[Path]]:
"""Gather WFDEI-GEM-CaPa source data.
Parameters
----------
path: str or os.PathLike
Returns
-------
dict(str, list[pathlib.Path])
"""
return _gather(
"wfdei-gem-capa",
wfdei_gem_capa_variables,
source=path,
glob_pattern="{variable}_*.nc",
)
def gather_sc_earth(path: Union[str, os.PathLike]) -> Dict[str, List[Path]]:
"""Gather SC-Earth source data
Parameters
----------
path: str or os.PathLike
Returns
-------
dict(str, list[pathlib.Path])
"""
return _gather(
"sc-earth",
sc_earth_variables,
source=path,
glob_pattern="SC-Earth_{variable}_*.nc",
)
def gather_rdrs(path: Union[str, os.PathLike], suffix: str) -> Dict[str, List[Path]]:
"""Gather RDRS processed source data.
Parameters
----------
path: str or os.PathLike
suffix : str
Returns
-------
dict(str, list[pathlib.Path])
"""
return _gather(
"rdrs-v21",
eccc_rdrs_variables,
source=path,
glob_pattern="{variable}_*_rdrs_*.{suffix}",
suffix=suffix,
)
def gather_raw_rdrs_by_years(
path: Union[str, os.PathLike]
) -> Dict[str, Dict[str, List[Path]]]:
"""Gather raw RDRS files for preprocessing.
Parameters
----------
path: str or os.PathLike
Returns
-------
dict(str, dict(str, list[Path])) or None
"""
# Time stamps starts at noon and flow into subsequent months
# Need full year plus previous december in order to easily produce complete hourly frequency monthly files
path = Path(path)
year_sets = dict()
for year in range(1950, datetime.datetime.now().year + 1):
files = sorted(list(path.glob(f"*_{year - 1}12*.nc")))
if files:
files = [files[-1]]
files.extend(sorted(list(path.glob(f"*_{year}*.nc"))))
year_sets[str(year)] = files
return {"rdrs-v21": year_sets}
def gather_grnch(path: Union[str, os.PathLike]) -> Dict[str, List[Path]]:
# GRNCH-ETS source data
source_grnch = Path(path)
logging.info(f"Gathering GRNCH from: {source_grnch.as_posix()}")
in_files_grnch = list()
for v in grnch_variables:
for yyyy in range(1970, 2020):
in_files_grnch.extend(list(source_grnch.rglob(f"{v}_{yyyy}.nc")))
logging.info(
f"Found {len(in_files_grnch)} files, totalling {report_file_size(in_files_grnch)}."
)
return dict(cfsr=sorted(in_files_grnch))