forked from NVIDIA/physicsnemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurate_era5.py
More file actions
269 lines (238 loc) · 10.1 KB
/
Copy pathcurate_era5.py
File metadata and controls
269 lines (238 loc) · 10.1 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
# SPDX-FileCopyrightText: Copyright (c) 2023 - 2026 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
from pathlib import Path
from typing import List, Tuple, Union
import dask
import fsspec
import hydra
import numpy as np
import xarray as xr
from dask.diagnostics import ProgressBar
from omegaconf import DictConfig, OmegaConf
from transform.transform import transform_registry
from utils import get_filesystem
# Add eval to OmegaConf TODO: Remove when OmegaConf is updated
OmegaConf.register_new_resolver("eval", eval)
ZARR_FORMAT = 3
class CurateERA5:
"""
Curate a Zarr ERA5 dataset to a Zarr dataset used for training global weather models.
"""
def __init__(
self,
unpredicted_variables: List[str],
predicted_variables: List[str],
dataset_filename: Union[str, Path] = "./data.zarr",
fs: fsspec.filesystem = fsspec.filesystem("file"),
curated_dataset_filename: Union[str, Path] = "./curated_data.zarr",
curated_fs: fsspec.filesystem = fsspec.filesystem("file"),
transform: None = None,
date_range: Tuple[str, str] = ("2000-01-01", "2001-01-01"),
dt: int = 1, # 1 hour
chunk_channels_together: bool = True,
single_threaded: bool = False,
):
super().__init__()
# Store parameters
self.unpredicted_variables = unpredicted_variables
self.predicted_variables = predicted_variables
self.dataset_filename = dataset_filename
self.fs = fs
self.curated_dataset_filename = curated_dataset_filename
self.curated_fs = curated_fs
self.transform = transform
self.date_range = date_range
assert dt in [1, 3, 6, 12], "dt must be 1, 3, 6, or 12"
self.dt = dt
self.chunk_channels_together = chunk_channels_together
self.single_threaded = single_threaded
# Open dataset to do curation from
mapper = fs.get_mapper(self.dataset_filename)
self.era5 = xr.open_zarr(mapper, consolidated=True)
# Subset variables (this speeds up chunking)
needed_variables = ["latitude", "longitude", "time", "level"]
for variable in self.unpredicted_variables + self.predicted_variables:
if not isinstance(variable, str):
needed_variables.append(variable[0])
else:
needed_variables.append(variable)
for variable in self.era5.variables:
if variable not in needed_variables:
self.era5 = self.era5.drop_vars(variable)
# Chunk data
self.era5 = self.era5.sel(
time=slice(
datetime.datetime.strptime(date_range[0], "%Y-%m-%d"),
datetime.datetime.strptime(date_range[1], "%Y-%m-%d"),
)
)
self.era5 = self.era5.sel(
time=self.era5.time.dt.hour.isin(np.arange(0, 24, self.dt))
)
self.era5 = self.era5.chunk(
{"time": 1, "level": 1, "latitude": 721, "longitude": 1440}
)
# Gather all predicted variables
xarray_predicted_variables = []
for variable in self.predicted_variables:
if not isinstance(variable, str): # TODO: better way to check if list
pressure_variable = self.era5[variable[0]].sel(level=variable[1])
pressure_variable = pressure_variable.drop("level")
if "time" not in pressure_variable.dims:
pressure_variable = pressure_variable.expand_dims(
time=self.era5.time, axis=0
)
pressure_variable = pressure_variable.rename(
{"level": "predicted_channel"}
)
xarray_predicted_variables.append(pressure_variable)
else:
single_variable = self.era5[variable]
if "time" not in single_variable.dims:
single_variable = single_variable.expand_dims(
time=self.era5.time, axis=0
)
single_variable = single_variable.expand_dims(
"predicted_channel", axis=1
)
xarray_predicted_variables.append(single_variable)
# Gather all unpredicted variables
xarray_unpredicted_variables = []
for variable in self.unpredicted_variables:
if not isinstance(variable, str): # TODO: better way to check if list
pressure_variable = self.era5[variable[0]].sel(level=variable[1])
pressure_variable = pressure_variable.drop("level")
if "time" not in pressure_variable.dims:
pressure_variable = pressure_variable.expand_dims(
time=self.era5.time, axis=0
)
pressure_variable = pressure_variable.rename(
{"level": "unpredicted_channel"}
)
xarray_unpredicted_variables.append(pressure_variable)
else:
single_variable = self.era5[variable]
if "time" not in single_variable.dims:
single_variable = single_variable.expand_dims(
time=self.era5.time, axis=0
)
single_variable = single_variable.expand_dims(
"unpredicted_channel", axis=1
)
xarray_unpredicted_variables.append(single_variable)
# Concatenate all variables
self.era5_subset = xr.Dataset()
self.era5_subset["predicted"] = xr.concat(
xarray_predicted_variables, dim="predicted_channel"
).transpose("time", "predicted_channel", ...)
self.era5_subset["unpredicted"] = xr.concat(
xarray_unpredicted_variables, dim="unpredicted_channel"
).transpose("time", "unpredicted_channel", ...)
self.era5_subset["time"] = self.era5["time"]
# Chunk channels
if self.chunk_channels_together:
predicted_channel_chunk_size = self.era5_subset.predicted_channel.size
unpredicted_channel_chunk_size = self.era5_subset.unpredicted_channel.size
else:
predicted_channel_chunk_size = 1
unpredicted_channel_chunk_size = 1
self.era5_subset = self.era5_subset.chunk(
{
"time": 1,
"predicted_channel": predicted_channel_chunk_size,
"unpredicted_channel": unpredicted_channel_chunk_size,
}
)
def __call__(self):
"""
Generate the zarr array
"""
# Check if already exists
if self.fs.exists(self.curated_dataset_filename):
print(f"Zarr file {self.curated_dataset_filename} already exists")
return
# Run transform if specified
if self.transform is not None:
self.era5_subset = self.transform(self.era5_subset)
# Save
mapper = self.fs.get_mapper(self.curated_dataset_filename)
delayed_obj = self.era5_subset.drop_encoding().to_zarr(
mapper,
consolidated=True,
compute=False,
mode="w",
zarr_format=ZARR_FORMAT,
)
# Wait for save to finish (Single-threaded legacy issue)
with ProgressBar():
if self.single_threaded:
with dask.config.set(scheduler="single-threaded"):
delayed_obj.compute()
else:
delayed_obj.compute()
@hydra.main(version_base="1.2", config_path="conf", config_name="config")
def main(cfg: DictConfig) -> None:
# Resolve config so that all values are concrete
OmegaConf.resolve(cfg)
# Get transform function
try:
transform = transform_registry[cfg.transform.name]
except KeyError:
raise NotImplementedError(f"Transform {cfg.transform.name} not implemented")
if "kwargs" in cfg.transform:
def wrapper_transform(transform, **kwargs):
def _transform(x):
return transform(x, **kwargs)
return _transform
transform = wrapper_transform(transform, **cfg.transform.kwargs)
# Get filesystem
fs = get_filesystem(
cfg.filesystem.type,
cfg.filesystem.key,
cfg.filesystem.endpoint_url,
cfg.filesystem.region_name,
)
# Make train data
curate_train_era5 = CurateERA5(
unpredicted_variables=cfg.curated_dataset.unpredicted_variables,
predicted_variables=cfg.curated_dataset.predicted_variables,
dataset_filename=cfg.dataset.dataset_filename,
fs=fs,
curated_dataset_filename=cfg.curated_dataset.train_dataset_filename,
curated_fs=fs,
transform=transform,
date_range=cfg.curated_dataset.train_years,
dt=cfg.curated_dataset.dt,
chunk_channels_together=cfg.curated_dataset.chunk_channels_together,
)
curate_train_era5()
# Make validation data
curate_val_era5 = CurateERA5(
unpredicted_variables=cfg.curated_dataset.unpredicted_variables,
predicted_variables=cfg.curated_dataset.predicted_variables,
dataset_filename=cfg.dataset.dataset_filename,
fs=fs,
curated_dataset_filename=cfg.curated_dataset.val_dataset_filename,
curated_fs=fs,
transform=transform,
date_range=cfg.curated_dataset.val_years,
dt=cfg.curated_dataset.dt,
chunk_channels_together=cfg.curated_dataset.chunk_channels_together,
)
curate_val_era5()
if __name__ == "__main__":
main()