Skip to content

Commit

Permalink
Merge pull request #487 from astrofrog/fix-dask-big-endian
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog authored Feb 1, 2025
2 parents c4c7d0a + 9f4ad1c commit ed556da
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
28 changes: 28 additions & 0 deletions reproject/interpolation/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,3 +954,31 @@ def test_auto_block_size():

assert array_out.chunksize[0] == 350
assert footprint_out.chunksize[0] == 350


@pytest.mark.parametrize("itemsize", (4, 8))
def test_bigendian_dask(itemsize):

# Regression test for an endianness issue that occurred when the input was
# passed in as (dask_array, wcs) and the dask array was big endian.

array_in_le = da.ones((350, 250, 150), dtype=f">f{itemsize}")
array_in_be = da.ones((350, 250, 150), dtype=f"<f{itemsize}")
wcs_in = WCS(naxis=2)
wcs_out = WCS(naxis=2)

array_out_be, _ = reproject_interp(
(array_in_be, wcs_in),
wcs_out,
shape_out=(300, 300),
block_size=(100, 100),
)

array_out_le, _ = reproject_interp(
(array_in_le, wcs_in),
wcs_out,
shape_out=(300, 300),
block_size=(100, 100),
)

assert_allclose(array_out_be, array_out_le)
7 changes: 6 additions & 1 deletion reproject/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def _dask_to_numpy_memmap(dask_array, tmp_dir):
if isinstance(dask_array.ravel()[0].compute(), da.Array):
dask_array = dask_array.compute()

# Cast the dask array to regular float for two reasons - first, zarr 3.0.0
# and later doesn't support big-endian arrays, and also we need to anyway
# make a native float memory mapped array below.
dask_array = dask_array.astype(float, copy=False)

# NOTE: here we use a new TemporaryDirectory context manager for the zarr
# array because we can remove the temporary directory straight away after
# converting the input to a Numpy memory mapped array.
Expand All @@ -53,7 +58,7 @@ def _dask_to_numpy_memmap(dask_array, tmp_dir):

memmapped_array = np.memmap(
memmap_path,
dtype=zarr_array.dtype,
dtype=float,
shape=zarr_array.shape,
mode="w+",
)
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ deps =
#devdeps: asdf-astropy @ git+https://github.com/astropy/asdf-astropy.git
devdeps: gwcs @ git+https://github.com/spacetelescope/gwcs.git
devdeps: sunpy[map] @ git+https://github.com/sunpy/sunpy.git
devdeps: zarr<3

extras =
test
Expand Down

0 comments on commit ed556da

Please sign in to comment.