forked from ECMWFCode4Earth/wildfire-forecasting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfwi_forecast.py
60 lines (49 loc) · 1.78 KB
/
fwi_forecast.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
"""
The dataset class to be used with fwi-forcings and fwi-forecast data.
"""
from glob import glob
import xarray as xr
from dataloader.base_loader import ModelDataset as BaseDataset
class ModelDataset(BaseDataset):
"""
The dataset class responsible for loading the data and providing the samples for
training.
:param BaseDataset: Base Dataset class to inherit from
:type BaseDataset: base_loader.BaseDataset
"""
def __init__(
self, dates, forecast_dir, hparams=None, **kwargs,
):
"""
Constructor for the ModelDataset class
:param dates: The t=0 dates
:type dates: list
:param forecast_dir: The directory containing the FWI-Forecast data, defaults to
None
:type forecast_dir: str, optional
:param hparams: Holds configuration values, defaults to None
:type hparams: Namespace, optional
"""
super().__init__(
forecast_dir=forecast_dir, hparams=hparams, **kwargs,
)
# Create new `lead` dimension to avoid duplication along `time` dimension
preprocess = (
lambda x: x.rename_dims(time="lead")
.rename_vars(name_dict={"time": "lead"})
.expand_dims("time")
.assign_coords(time=("time", [x.time[0].values]))
.assign_coords(lead=("lead", range(10)))
)
out_files = glob(f"{forecast_dir}/ECMWF_FWI_20*_1200_hr_fwi.nc")
with xr.open_mfdataset(
out_files,
preprocess=preprocess,
engine="h5netcdf",
parallel=True,
combine="by_coords",
coords="minimal",
data_vars="minimal",
compat="override",
) as ds:
self.output = ds.sortby("time").sel(time=dates).load()