Skip to content

Commit

Permalink
Set FillValue before reprojection
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrjones committed Mar 12, 2024
1 parent a87eaa8 commit 7d74911
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
7 changes: 2 additions & 5 deletions ndpyramid/core.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations # noqa: F401

import contextlib
import typing
from collections import defaultdict

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

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

def reproject(da, var):
da.encoding['_FillValue'] = np.nan
da = da.rio.reproject(
projection_model._crs,
resampling=Resampling[resampling_dict[var]],
shape=(dim, dim),
transform=dst_transform,
)
da = da.where(da != da.rio.nodata)
with contextlib.suppress(KeyError):
del da.attrs['_FillValue']
del da.encoding['_FillValue']
return da

# create the data array for each level
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pyramids.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_reprojected_pyramid_fill(temperature, benchmark):
"""
pytest.importorskip('rioxarray')
temperature = temperature.rio.write_crs('EPSG:4326')
pyramid = benchmark(lambda: pyramid_reproject(temperature, levels=2))
pyramid = benchmark(lambda: pyramid_reproject(temperature, levels=1))
assert np.isnan(pyramid['0'].air.isel(time=0, x=0, y=0).values)


Expand Down

0 comments on commit 7d74911

Please sign in to comment.