-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathinference.py
441 lines (382 loc) · 15.3 KB
/
inference.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
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
431
432
433
434
435
436
437
438
439
440
441
# SPDX-FileCopyrightText: Copyright (c) 2023 - 2024 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 matplotlib.pyplot as plt
import numpy as np
import os
import torch
from datetime import datetime
import xarray as xr
import zarr
import pandas as pd
import hydra
from physicsnemo.distributed import DistributedManager
from omegaconf import DictConfig
from physicsnemo.models import Module
from utils.nn import regression_model_forward, diffusion_model_forward
from utils.data_loader_hrrr_era5 import HrrrEra5Dataset
@hydra.main(version_base=None, config_path="config", config_name="stormcast_inference")
def main(cfg: DictConfig):
# Initialize
DistributedManager.initialize()
dist = DistributedManager()
device = dist.device
initial_time = datetime.fromisoformat(cfg.inference.initial_time)
n_steps = cfg.inference.n_steps
# Dataset prep
dataset = HrrrEra5Dataset(cfg.dataset, train=False)
base_hrrr_channels, hrrr_channels = dataset._get_hrrr_channel_names()
diffusion_channels = (
hrrr_channels
if cfg.dataset.diffusion_channels == "all"
else cfg.dataset.diffusion_channels
)
input_channels = (
hrrr_channels
if cfg.dataset.input_channels == "all"
else cfg.dataset.input_channels
)
diffusion_channel_indices = [
hrrr_channels.index(channel) for channel in diffusion_channels
]
input_channel_indices = [
list(hrrr_channels).index(channel) for channel in input_channels
]
hrrr_data = xr.open_zarr(
os.path.join(
cfg.dataset.location, cfg.dataset.conus_dataset_name, "valid", "2021.zarr"
)
)
invariant_array = dataset._get_invariants()
invariant_tensor = torch.from_numpy(invariant_array).to(device).repeat(1, 1, 1, 1)
if len(cfg.inference.output_hrrr_channels) == 0:
output_hrrr_channels = diffusion_channels.copy()
vardict: dict[str, int] = {
hrrr_channel: i for i, hrrr_channel in enumerate(hrrr_channels)
}
vardict_era5 = {
era5_channel: i for i, era5_channel in enumerate(dataset.era5_channels.values)
}
color_limits = {
"u10m": (-5, 5),
"v10": (-5, 5),
"t2m": (260, 310),
"tcwv": (0, 60),
"msl": (0.1, 0.3),
"refc": (-10, 30),
}
hours_since_jan_01 = int(
(initial_time - datetime(initial_time.year, 1, 1, 0, 0)).total_seconds() / 3600
)
hrrr_channel_indices = [
list(base_hrrr_channels).index(channel) for channel in hrrr_channels
]
means_hrrr = dataset.means_hrrr[hrrr_channel_indices]
stds_hrrr = dataset.stds_hrrr[hrrr_channel_indices]
means_era5 = dataset.means_era5
stds_era5 = dataset.stds_era5
# Load pretrained models
net = Module.from_checkpoint(cfg.inference.regression_checkpoint)
regression_model = net.to(device)
net = Module.from_checkpoint(cfg.inference.diffusion_checkpoint)
diffusion_model = net.to(device)
# initialize zarr
zarr_output_path = os.path.join(cfg.inference.rundir, "data.zarr")
group = zarr.open_group(zarr_output_path, mode="w")
group.array("latitude", data=hrrr_data["latitude"].values)
group.array("longitude", data=hrrr_data["longitude"].values)
edm_prediction_group = group.create_group("edm_prediction")
noedm_prediction_group = group.create_group("noedm_prediction")
target_group = group.create_group("target")
hrrr, _ = dataset[0]["hrrr"]
assert hrrr.ndim == 3
grid_size = hrrr.shape[1:]
for name in output_hrrr_channels:
target_group.empty(
name, shape=(n_steps,) + grid_size, chunks=[1, *grid_size], compressor=None
)
edm_prediction_group.empty(
name, shape=(n_steps,) + grid_size, chunks=[1, *grid_size], compressor=None
)
noedm_prediction_group.empty(
name, shape=(n_steps,) + grid_size, chunks=[1, *grid_size], compressor=None
)
with torch.no_grad():
for i in range(n_steps):
data = dataset[i + hours_since_jan_01]
print(i)
if i == 0:
inp = data["hrrr"][0].cuda().float().unsqueeze(0)
boundary = data["era5"][0].cuda().float().unsqueeze(0)
out = inp
out_edm = out.clone()
out_noedm = out.clone()
assert out_edm.shape == (1, len(hrrr_channels)) + grid_size
assert out_noedm.shape == (1, len(hrrr_channels)) + grid_size
# write hrrr
denorm_out_edm = out_edm.cpu().numpy() * stds_hrrr + means_hrrr
denorm_out_noedm = out_noedm.cpu().numpy() * stds_hrrr + means_hrrr
for name in output_hrrr_channels:
k = vardict[name]
edm_prediction_group[name][i] = denorm_out_edm[0, k]
noedm_prediction_group[name][i] = denorm_out_noedm[0, k]
target_data = (
data["hrrr"][0][k].cpu().numpy() * stds_hrrr[k] + means_hrrr[k]
)
target_group[name][i] = target_data
if i > n_steps:
break
hrrr_0 = out
out = regression_model_forward(
regression_model, hrrr_0, boundary, invariant_tensor
)
out_noedm = out.clone()
hrrr_0 = torch.cat(
(
hrrr_0[:, input_channel_indices, :, :],
out[:, input_channel_indices, :, :],
),
dim=1,
)
edm_corrected_outputs = diffusion_model_forward(
diffusion_model,
hrrr_0,
diffusion_channel_indices,
invariant_tensor,
sampler_args=dict(cfg.sampler.args),
)
out[0, diffusion_channel_indices] += edm_corrected_outputs[0].float()
out_edm = out.clone()
boundary = data["era5"][0].cuda().float().unsqueeze(0)
varidx = vardict[cfg.inference.plot_var_hrrr]
fig, ax = plt.subplots(1, 4, figsize=(20, 5))
pred = out.cpu().numpy()
tar = data["hrrr"][1].unsqueeze(0).cpu().numpy()
era5 = data["era5"][0].unsqueeze(0).cpu().numpy()
pred = pred * stds_hrrr + means_hrrr
tar = tar * stds_hrrr + means_hrrr
era5 = era5 * stds_era5 + means_era5
error = pred - tar
if cfg.inference.plot_var_hrrr in color_limits:
im = ax[0].imshow(
pred[0, varidx],
origin="lower",
cmap="magma",
clim=color_limits[cfg.inference.plot_var_hrrr],
)
else:
im = ax[0].imshow(pred[0, varidx], origin="lower", cmap="magma")
fig.colorbar(im, ax=ax[0], fraction=0.046, pad=0.04)
ax[0].set_title(
"Predicted, {}, \n initial time {} \n lead_time {} hours".format(
cfg.inference.plot_var_hrrr, initial_time, i
)
)
if cfg.inference.plot_var_hrrr in color_limits:
im = ax[1].imshow(
tar[0, varidx],
origin="lower",
cmap="magma",
clim=color_limits[cfg.inference.plot_var_hrrr],
)
else:
im = ax[1].imshow(tar[0, varidx], origin="lower", cmap="magma")
fig.colorbar(im, ax=ax[1], fraction=0.046, pad=0.04)
ax[1].set_title("Actual, {}".format(cfg.inference.plot_var_hrrr))
if cfg.inference.plot_var_era5 in color_limits:
im = ax[2].imshow(
era5[0, vardict_era5[cfg.inference.plot_var_era5]],
origin="lower",
cmap="magma",
clim=color_limits[cfg.inference.plot_var_era5],
)
else:
im = ax[2].imshow(
era5[0, vardict_era5[cfg.inference.plot_var_era5]],
origin="lower",
cmap="magma",
)
fig.colorbar(im, ax=ax[2], fraction=0.046, pad=0.04)
ax[2].set_title("ERA5, {}".format(cfg.inference.plot_var_era5))
maxerror = np.max(np.abs(error[0, varidx]))
im = ax[3].imshow(
error[0, varidx],
origin="lower",
cmap="RdBu_r",
vmax=maxerror,
vmin=-maxerror,
)
fig.colorbar(im, ax=ax[3], fraction=0.046, pad=0.04)
ax[3].set_title("Error, {}".format(cfg.inference.plot_var_hrrr))
plt.savefig(f"{cfg.inference.rundir}/out_{i}.png")
level_names = [
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"13",
"15",
"20",
"25",
"30",
"35",
"40",
]
vertical_vars = ["u", "v", "t", "q", "z", "p", "w"]
horizontal_vars = ["msl", "refc", "u10m", "v10m"]
zarr_group = group
lons = zarr_group["longitude"][:, :]
lats = zarr_group["latitude"][:, :]
initial_time_pd = pd.to_datetime(initial_time)
val_times = []
for i in range(n_steps):
val_times.append(initial_time_pd + pd.Timedelta(seconds=i * hours_since_jan_01))
def convert_strings_to_ints(string_list):
return [int(i) for i in string_list]
model_levels = convert_strings_to_ints(level_names)
ds_pred_edm = xr.Dataset()
ds_pred_noedm = xr.Dataset()
ds_targ = xr.Dataset()
for var in vertical_vars:
dsp_edm = xr.Dataset()
dsp_noedm = xr.Dataset()
dst = xr.Dataset()
for i, level in enumerate(level_names):
key = f"{var}{level}"
if key in zarr_group["edm_prediction"]:
# Extract the data from zarr_group
data_pred_edm = zarr_group["edm_prediction"][key][:, :]
else:
data_pred_edm = np.full(
(len(val_times), lats.shape[0], lats.shape[1]), np.nan
)
print(f"Key {key} not found in Zarr group. Filling with NaNs.")
if key in zarr_group["noedm_prediction"]:
# Extract the data from zarr_group
data_pred_noedm = zarr_group["noedm_prediction"][key][:, :]
else:
data_pred_noedm = np.full(
(len(val_times), lats.shape[0], lats.shape[1]), np.nan
)
print(f"Key {key} not found in Zarr group. Filling with NaNs.")
if key in zarr_group["target"]:
data_targ = zarr_group["target"][key][:, :]
else:
data_targ = np.full(
(len(val_times), lats.shape[0], lats.shape[1]), np.nan
)
print(f"Key {key} not found in Zarr group. Filling with NaNs.")
dap_edm = xr.DataArray(
data_pred_edm,
dims=("time", "y", "x"),
coords={
"time": val_times,
"y": np.arange(lats.shape[0]),
"x": np.arange(lats.shape[1]),
"levels": model_levels[i],
},
)
dap_noedm = xr.DataArray(
data_pred_noedm,
dims=("time", "y", "x"),
coords={
"time": val_times,
"y": np.arange(lats.shape[0]),
"x": np.arange(lats.shape[1]),
"levels": model_levels[i],
},
)
dat = xr.DataArray(
data_targ,
dims=("time", "y", "x"),
coords={
"time": val_times,
"y": np.arange(lats.shape[0]),
"x": np.arange(lats.shape[1]),
"levels": model_levels[i],
},
)
dsp_edm[f"var_{i}"] = dap_edm
dsp_noedm[f"var_{i}"] = dap_noedm
dst[f"var_{i}"] = dat
combined_pred_edm = xr.concat(
[dsp_edm[var] for var in dsp_edm.data_vars], dim="levels"
)
combined_pred_noedm = xr.concat(
[dsp_noedm[var] for var in dsp_noedm.data_vars], dim="levels"
)
combined_targ = xr.concat([dst[var] for var in dst.data_vars], dim="levels")
reshaped_pred_edm = combined_pred_edm.transpose("time", "levels", "y", "x")
reshaped_pred_noedm = combined_pred_noedm.transpose("time", "levels", "y", "x")
reshaped_targ = combined_targ.transpose("time", "levels", "y", "x")
ds_pred_edm[f"{var}_comb"] = reshaped_pred_edm
ds_pred_noedm[f"{var}_comb"] = reshaped_pred_noedm
ds_targ[f"{var}_comb"] = reshaped_targ
for var in horizontal_vars:
data_pred_edm = zarr_group["edm_prediction"][var][:, :]
data_pred_noedm = zarr_group["noedm_prediction"][var][:, :]
data_targ = zarr_group["target"][var][:, :]
dap_edm = xr.DataArray(
data_pred_edm,
dims=("time", "y", "x"),
coords={
"time": val_times,
"y": np.arange(lats.shape[0]),
"x": np.arange(lats.shape[1]),
},
)
dap_noedm = xr.DataArray(
data_pred_noedm,
dims=("time", "y", "x"),
coords={
"time": val_times,
"y": np.arange(lats.shape[0]),
"x": np.arange(lats.shape[1]),
},
)
dat = xr.DataArray(
data_targ,
dims=("time", "y", "x"),
coords={
"time": val_times,
"y": np.arange(lats.shape[0]),
"x": np.arange(lats.shape[1]),
},
)
ds_pred_edm.update({var: dap_edm})
ds_pred_noedm.update({var: dap_noedm})
ds_targ.update({var: dat})
ds_pred_edm["longitude"] = xr.DataArray(lons, dims=("y", "x"))
ds_pred_edm["latitude"] = xr.DataArray(lats, dims=("y", "x"))
ds_pred_edm = ds_pred_edm.assign_coords(levels=model_levels)
ds_pred_noedm["longitude"] = xr.DataArray(lons, dims=("y", "x"))
ds_pred_noedm["latitude"] = xr.DataArray(lats, dims=("y", "x"))
ds_pred_noedm = ds_pred_noedm.assign_coords(levels=model_levels)
ds_targ["longitude"] = xr.DataArray(lons, dims=("y", "x"))
ds_targ["latitude"] = xr.DataArray(lats, dims=("y", "x"))
ds_targ = ds_targ.assign_coords(levels=model_levels)
ds_out_path = cfg.inference.rundir
ds_pred_edm.to_netcdf(f"{ds_out_path}/ds_pred_edm.nc", format="NETCDF4")
ds_pred_noedm.to_netcdf(f"{ds_out_path}/ds_pred_noedm.nc", format="NETCDF4")
ds_targ.to_netcdf(f"{ds_out_path}/ds_targ.nc", format="NETCDF4")
if __name__ == "__main__":
main()