Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change nodata from rioxarray default to nan in pyramid_reproject #110

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ndpyramid/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections import defaultdict

import datatree as dt
import numpy as np
import xarray as xr

from .common import Projection
Expand Down Expand Up @@ -131,12 +132,14 @@ def pyramid_reproject(
dst_transform = projection_model.transform(dim=dim)

def reproject(da, var):
return da.rio.reproject(
da.encoding['_FillValue'] = np.nan
da = da.rio.reproject(
projection_model._crs,
resampling=Resampling[resampling_dict[var]],
shape=(dim, dim),
transform=dst_transform,
)
return da

# create the data array for each level
plevels[lkey] = xr.Dataset(attrs=ds.attrs)
Expand Down
12 changes: 11 additions & 1 deletion tests/test_pyramids.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ def test_reprojected_pyramid(temperature, benchmark):
pytest.importorskip('rioxarray')
levels = 2
temperature = temperature.rio.write_crs('EPSG:4326')
pyramid = benchmark(lambda: pyramid_reproject(temperature, levels=2))
pyramid = benchmark(lambda: pyramid_reproject(temperature, levels=levels))
assert pyramid.ds.attrs['multiscales']
assert len(pyramid.ds.attrs['multiscales'][0]['datasets']) == levels
assert pyramid.ds.attrs['multiscales'][0]['datasets'][0]['crs'] == 'EPSG:3857'
pyramid.to_zarr(MemoryStore())


def test_reprojected_pyramid_fill(temperature, benchmark):
"""
Test for https://github.com/carbonplan/ndpyramid/issues/93.
"""
pytest.importorskip('rioxarray')
temperature = temperature.rio.write_crs('EPSG:4326')
pyramid = benchmark(lambda: pyramid_reproject(temperature, levels=1))
assert np.isnan(pyramid['0'].air.isel(time=0, x=0, y=0).values)


@pytest.mark.parametrize('regridder_apply_kws', [None, {'keep_attrs': False}])
def test_regridded_pyramid(temperature, regridder_apply_kws, benchmark):
pytest.importorskip('xesmf')
Expand Down
Loading