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

fix dtype complex for rasterio backend #5501

Closed
wants to merge 3 commits into from
Closed
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 xarray/backends/rasterio_.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def __init__(self, manager, lock, vrt_params=None):
dtypes = riods.dtypes
if not np.all(np.asarray(dtypes) == dtypes[0]):
raise ValueError("All bands should have the same dtype")
self._dtype = np.dtype(dtypes[0])
if dtypes[0] != 'complex_int16' :
self._dtype = np.dtype(dtypes[0])
else :
self._dtype = complex

@property
def dtype(self):
Expand Down
17 changes: 15 additions & 2 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -4137,6 +4137,7 @@ def create_tmp_geotiff(
crs=default_value,
open_kwargs=None,
additional_attrs=None,
specific_dtype=None
):
if transform_args is default_value:
transform_args = [5000, 80000, 1000, 2000.0]
Expand All @@ -4163,7 +4164,11 @@ def create_tmp_geotiff(
else:
data_shape = nz, ny, nx
write_kwargs = {}
data = np.arange(nz * ny * nx, dtype=rasterio.float32).reshape(*data_shape)
if specific_dtype is None:
specific_dtype = rasterio.float32
data = np.arange(nz * ny * nx, dtype=rasterio.float32).reshape(*data_shape)
else:
data = np.arange(nz * ny * nx,dtype=rasterio.float32).reshape(*data_shape)
if transform is None:
transform = from_origin(*transform_args)
if additional_attrs is None:
Expand All @@ -4180,7 +4185,7 @@ def create_tmp_geotiff(
count=nz,
crs=crs,
transform=transform,
dtype=rasterio.float32,
dtype=specific_dtype,
**open_kwargs,
) as s:
for attr, val in additional_attrs.items():
Expand Down Expand Up @@ -4697,6 +4702,14 @@ def test_rasterio_vrt_network(self):
assert actual_res == expected_res
assert expected_val == actual_val

def test_rasterio_complex_dtype( self ):
import rasterio
with create_tmp_geotiff(specific_dtype='complex_int16',
) as (tmp_file, _):
with rasterio.open(tmp_file) as riobj:
assert riobj.dtypes[0]=='complex_int16'
with xr.open_rasterio(tmp_file) as rioda:
assert rioda.dtype==complex

class TestEncodingInvalid:
def test_extract_nc4_variable_encoding(self):
Expand Down