Skip to content

Commit

Permalink
Combine ERA5 and ERA5T data
Browse files Browse the repository at this point in the history
Requesting cutout data spanning recent (ERA5T)
and data older than ~3 months (ERA5) results in an
additional dimension in `cutout.data`, called `expver`,
which `atlite` currently cannot handle gracefully.

This change collapses the two dimensions into a single dimension.

See discussion in PyPSA#190
  • Loading branch information
zoltanmaric authored Sep 30, 2022
1 parent a0bd4b0 commit 25a831a
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions atlite/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def wrapper(*args, **kwargs):


@maybe_remove_tmpdir
def cutout_prepare(cutout, features=None, tmpdir=None, overwrite=False):
def cutout_prepare(cutout, features=None, tmpdir=None, overwrite=False, flatten_expver=True):
"""
Prepare all or a selection of features in a cutout.
Expand All @@ -133,6 +133,11 @@ def cutout_prepare(cutout, features=None, tmpdir=None, overwrite=False):
overwrite : bool, optional
Whether to overwrite variables which are already included in the
cutout. The default is False.
flatten_expver: bool, default True
If true, data for time periods spanning both ERA5 and ERA5T data
will be combined into a single dimension.
See https://confluence.ecmwf.int/pages/viewpage.action?pageId=173385064
for details.
Returns
-------
Expand Down Expand Up @@ -182,7 +187,14 @@ def cutout_prepare(cutout, features=None, tmpdir=None, overwrite=False):
cutout.data.close()
cutout.path.unlink()
os.rename(tmp, cutout.path)

cutout.data = xr.open_dataset(cutout.path, chunks=cutout.chunks)

data = xr.open_dataset(cutout.path, chunks=cutout.chunks)

# Combine ERA5 and ERA5T data into a single dimension.
# See https://github.com/PyPSA/atlite/issues/190
if flatten_expver and 'expver' in data.dims.mapping.keys():
data = data.sel(expver=1).combine_first(data.sel(expver=5))

cutout.data = data

return cutout

0 comments on commit 25a831a

Please sign in to comment.