Skip to content

Preserve input float dtype through resample() #1467

@brendancol

Description

@brendancol

Describe the bug

resample() casts every input to float64 internally and emits float32 regardless of input dtype. That silently throws away precision for float64 rasters and forces an unnecessary copy when input is already float64.

import numpy as np, xarray as xr
from xrspatial.resample import resample

data = np.linspace(0, 1, 64 * 64, dtype=np.float64).reshape(64, 64)
da = xr.DataArray(
    data, dims=('y', 'x'),
    coords={'y': np.arange(64), 'x': np.arange(64)},
    attrs={'res': (1.0, 1.0)},
)
out = resample(da, scale_factor=0.5)
print(out.dtype)  # float32 -- precision lost

Expected behavior

  • float64 input gives float64 output (no silent precision loss).
  • float32 input gives float32 output (existing default).
  • integer input gives float32 output, since NaN-sentinel resampling needs a float type.

Proposal

Pick a working dtype inside each _run_* runner: float64 if input itemsize is at least 8, else float32. Skip the astype copy when input dtype already matches. Apply the same rule across all four backend runners (_run_numpy, _run_cupy, _run_dask_numpy, _run_dask_cupy).

Output dtype:

  • float input: match input dtype.
  • integer input: float32.

Update the docstring return-type description to match.

Tests

  • float64 input gives float64 output, values match a high-precision reference (atol=1e-12).
  • float32 input gives float32 output.
  • int32 input gives float32 output.
  • Dask paths follow the same rules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions