generated from carbonplan/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
123 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# flake8: noqa | ||
|
||
from .core import pyramid_coarsen, pyramid_reproject | ||
from .coarsen import pyramid_coarsen | ||
from .reproject import pyramid_reproject | ||
from .regrid import pyramid_regrid | ||
from ._version import __version__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from __future__ import annotations # noqa: F401 | ||
|
||
import datatree as dt | ||
import xarray as xr | ||
|
||
from .utils import get_version, multiscales_template | ||
|
||
|
||
def pyramid_coarsen( | ||
ds: xr.Dataset, *, factors: list[int], dims: list[str], **kwargs | ||
) -> dt.DataTree: | ||
"""Create a multiscale pyramid via coarsening of a dataset by given factors | ||
Parameters | ||
---------- | ||
ds : xarray.Dataset | ||
The dataset to coarsen. | ||
factors : list[int] | ||
The factors to coarsen by. | ||
dims : list[str] | ||
The dimensions to coarsen. | ||
kwargs : dict | ||
Additional keyword arguments to pass to xarray.Dataset.coarsen. | ||
""" | ||
|
||
# multiscales spec | ||
save_kwargs = locals() | ||
del save_kwargs['ds'] | ||
|
||
attrs = { | ||
'multiscales': multiscales_template( | ||
datasets=[{'path': str(i)} for i in range(len(factors))], | ||
type='reduce', | ||
method='pyramid_coarsen', | ||
version=get_version(), | ||
kwargs=save_kwargs, | ||
) | ||
} | ||
|
||
# set up pyramid | ||
plevels = {} | ||
|
||
# pyramid data | ||
for key, factor in enumerate(factors): | ||
# merge dictionary via union operator | ||
kwargs |= {d: factor for d in dims} | ||
plevels[str(key)] = ds.coarsen(**kwargs).mean() | ||
|
||
plevels['/'] = xr.Dataset(attrs=attrs) | ||
return dt.DataTree.from_dict(plevels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters