Skip to content

Commit

Permalink
Change nodata from rioxarray default to nan in pyramid_reproject (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrjones authored Mar 12, 2024
1 parent 948534a commit 9d1d0cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit 9d1d0cd

Please sign in to comment.