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

BUG: Preserve original data type for writing to disk #306

Closed
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Latest
- BUG: Return correct transform in `rio.transform` with non-rectilinear transform (discussions #280)
- BUG: Update to handle WindowError in rasterio 1.2.2 (issue #286)
- BUG: Don't generate x,y coords in `rio` methods if not previously there (pull #294)
- BUG: Preserve original data type for writing to disk (issue #305)

0.3.2
-----
Expand Down
2 changes: 2 additions & 0 deletions rioxarray/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ def open_rasterio(
encoding = {}
if mask_and_scale and "_Unsigned" in attrs:
unsigned = variables.pop_to(attrs, encoding, "_Unsigned") == "true"
elif masked:
encoding["dtype"] = str(riods.dtypes[0])

da_name = attrs.pop("NETCDF_VARNAME", default_name)
data = indexing.LazilyOuterIndexedArray(
Expand Down
5 changes: 5 additions & 0 deletions rioxarray/raster_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,11 @@ def to_raster(
if driver is None and LooseVersion(rasterio.__version__) < LooseVersion("1.2"):
driver = "GTiff"

dtype = (
self._obj.encoding.get("dtype", str(self._obj.dtype))
if dtype is None
else dtype
)
dtype = str(self._obj.dtype) if dtype is None else dtype
# get the output profile from the rasterio object
# if opened with xarray.open_rasterio()
Expand Down
19 changes: 16 additions & 3 deletions test/integration/test_integration__io.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ def test_open_rasterio_mask_chunk_clip():
assert np.isnan(xdi.values).sum() == 52119
test_encoding = dict(xdi.encoding)
assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
assert test_encoding == {"_FillValue": 0.0, "grid_mapping": "spatial_ref"}
assert test_encoding == {
"_FillValue": 0.0,
"grid_mapping": "spatial_ref",
"dtype": "uint16",
}
attrs = dict(xdi.attrs)
assert_almost_equal(
tuple(xdi.rio._cached_transform())[:6],
Expand Down Expand Up @@ -307,7 +311,11 @@ def test_open_rasterio_mask_chunk_clip():
_assert_xarrays_equal(clipped, comp_subset)
test_encoding = dict(clipped.encoding)
assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
assert test_encoding == {"_FillValue": 0.0, "grid_mapping": "spatial_ref"}
assert test_encoding == {
"_FillValue": 0.0,
"grid_mapping": "spatial_ref",
"dtype": "uint16",
}

# test dataset
clipped_ds = xdi.to_dataset(name="test_data").rio.clip(
Expand All @@ -317,7 +325,11 @@ def test_open_rasterio_mask_chunk_clip():
_assert_xarrays_equal(clipped_ds, comp_subset_ds)
test_encoding = dict(clipped.encoding)
assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
assert test_encoding == {"_FillValue": 0.0, "grid_mapping": "spatial_ref"}
assert test_encoding == {
"_FillValue": 0.0,
"grid_mapping": "spatial_ref",
"dtype": "uint16",
}


##############################################################################
Expand Down Expand Up @@ -912,6 +924,7 @@ def test_no_mask_and_scale(open_rasterio):
"_FillValue": 32767.0,
"missing_value": 32767,
"grid_mapping": "crs",
"dtype": "uint16",
}
attrs = rds.air_temperature.attrs
assert attrs == {
Expand Down
1 change: 1 addition & 0 deletions test/integration/test_integration_rioxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,7 @@ def test_to_raster(
assert_array_equal(rds.read(1), xds.fillna(xds.rio.encoded_nodata).values)
assert rds.count == 1
assert rds.tags() == {"AREA_OR_POINT": "Area", **test_tags, **xds_attrs}
assert rds.dtypes == ("int16",)


@pytest.mark.parametrize(
Expand Down