Repository: corteva/rioxarray

Issue:
ValueError: cannot convert float NaN to integer on `open_rasterio`
#### Code Sample, a copy-pastable example if possible

```python
import rioxarray as rx
import xarray as xr

with xr.open_rasterio('https://seaice.uni-bremen.de/data/amsr2/asi_daygrid_swath/n3125/2021/oct/Arctic3125/asi-AMSR2-n3125-20211001-v5.4.tif') as img:
  print(img)

print ('----')

with rx.open_rasterio('https://seaice.uni-bremen.de/data/amsr2/asi_daygrid_swath/n3125/2021/oct/Arctic3125/asi-AMSR2-n3125-20211001-v5.4.tif') as img:
  print(img)
```

#### Problem description

```
/Users/xyz/Desktop/test.py:4: DeprecationWarning: open_rasterio is Deprecated in favor of rioxarray. For information about transitioning, see: https://corteva.github.io/rioxarray/stable/getting_started/getting_started.html
  with xr.open_rasterio('https://seaice.uni-bremen.de/data/amsr2/asi_daygrid_swath/n3125/2021/oct/Arctic3125/asi-AMSR2-n3125-20211001-v5.4.tif') as img:
<xarray.DataArray (band: 1, y: 3584, x: 2432)>
[8716288 values with dtype=uint8]
Coordinates:
  * band     (band) int64 1
  * y        (y) float64 5.848e+06 5.845e+06 5.842e+06 ... -5.345e+06 -5.348e+06
  * x        (x) float64 -3.848e+06 -3.845e+06 ... 3.745e+06 3.748e+06
Attributes: (12/18)
    transform:                 (3125.0, 0.0, -3850000.0, 0.0, -3125.0, 585000...
    crs:                       +init=epsg:3413
    res:                       (3125.0, 3125.0)
    is_tiled:                  0
    nodatavals:                (nan,)
    scales:                    (1.0,)
    ...                        ...
    x#long_name:               x
    y#actual_range:            {0.5,3583.5}
    y#long_name:               y
    z#actual_range:            {0,120}
    z#long_name:               z
    z#_FillValue:              nan
----
Traceback (most recent call last):
  File "/Users/xyz/Desktop/test.py", line 9, in <module>
    with rx.open_rasterio('https://seaice.uni-bremen.de/data/amsr2/asi_daygrid_swath/n3125/2021/oct/Arctic3125/asi-AMSR2-n3125-20211001-v5.4.tif') as img:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/rioxarray/_io.py", line 1241, in open_rasterio
    result.attrs["_FillValue"] = result.dtype.type(result.attrs["_FillValue"])
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot convert float NaN to integer
```

#### Expected Output

Would expect something in line with `xarray.open_rasterio` rather than crashing.

#### Environment Information

```
rioxarray (0.14.0) deps:
  rasterio: 1.3.6
    xarray: 2023.3.0
      GDAL: 3.5.3
      GEOS: 0.0.0
      PROJ: 9.0.1
 PROJ DATA: /opt/homebrew/lib/python3.11/site-packages/rasterio/proj_data
 GDAL DATA: /opt/homebrew/lib/python3.11/site-packages/rasterio/gdal_data

Other python deps:
     scipy: 1.10.0
    pyproj: 3.5.0

System:
    python: 3.11.3 (main, Apr  7 2023, 20:13:31) [Clang 14.0.0 (clang-1400.0.29.202)]
executable: /opt/homebrew/opt/python@3.11/bin/python3.11
   machine: macOS-13.3.1-arm64-arm-64bit
```

#### Installation method
Homebrew

Discussion:

Patch:
diff --git a/docs/history.rst b/docs/history.rst
index 5224cff6..c3946214 100644
--- a/docs/history.rst
+++ b/docs/history.rst
@@ -3,7 +3,8 @@ History
 
 Latest
 ------
-- BUG: Fix :mod:`rioxarray.merge` CRS check
+- BUG: Fix :mod:`rioxarray.merge` CRS check (pull #655)
+- BUG: Remove tags with metadata added by rasterio in :func:`rioxarray.open_rasterio` (issue #666)
 
 0.14.0
 ------
diff --git a/rioxarray/_io.py b/rioxarray/_io.py
index b9abe85f..f874c8b4 100644
--- a/rioxarray/_io.py
+++ b/rioxarray/_io.py
@@ -37,6 +37,8 @@ from xarray.core.variable import as_variable
 from rioxarray.exceptions import RioXarrayError
 from rioxarray.rioxarray import _generate_spatial_coords
 
+FILL_VALUE_NAMES = ("_FillValue", "missing_value", "fill_value", "nodata")
+UNWANTED_RIO_ATTRS = ("nodatavals", "is_tiled", "res")
 # TODO: should this be GDAL_LOCK instead?
 RASTERIO_LOCK = SerializableLock()
 NO_LOCK = contextlib.nullcontext()
@@ -654,6 +656,10 @@ def _get_rasterio_attrs(riods: RasterioReader):
     # pylint: disable=too-many-branches
     # Add rasterio attributes
     attrs = _parse_tags({**riods.tags(), **riods.tags(1)})
+    # remove attributes with informaiton
+    # that should be added by GDAL/rasterio
+    for unwanted_attr in FILL_VALUE_NAMES + UNWANTED_RIO_ATTRS:
+        attrs.pop(unwanted_attr, None)
     if riods.nodata is not None:
         # The nodata values for the raster bands
         attrs["_FillValue"] = riods.nodata
diff --git a/rioxarray/raster_array.py b/rioxarray/raster_array.py
index 353299e7..133fecbf 100644
--- a/rioxarray/raster_array.py
+++ b/rioxarray/raster_array.py
@@ -27,6 +27,7 @@ from rasterio.features import geometry_mask
 from xarray.backends.file_manager import FileManager
 from xarray.core.dtypes import get_fill_value
 
+from rioxarray._io import FILL_VALUE_NAMES, UNWANTED_RIO_ATTRS
 from rioxarray.crs import crs_from_user_input
 from rioxarray.exceptions import (
     MissingCRS,
@@ -34,12 +35,7 @@ from rioxarray.exceptions import (
     OneDimensionalRaster,
     RioXarrayError,
 )
-from rioxarray.raster_writer import (
-    FILL_VALUE_NAMES,
-    UNWANTED_RIO_ATTRS,
-    RasterioWriter,
-    _ensure_nodata_dtype,
-)
+from rioxarray.raster_writer import RasterioWriter, _ensure_nodata_dtype
 from rioxarray.rioxarray import (
     XRasterBase,
     _get_data_var_message,
diff --git a/rioxarray/raster_writer.py b/rioxarray/raster_writer.py
index ad65cca6..1ef0882f 100644
--- a/rioxarray/raster_writer.py
+++ b/rioxarray/raster_writer.py
@@ -15,7 +15,7 @@ import rasterio
 from rasterio.windows import Window
 from xarray.conventions import encode_cf_variable
 
-from rioxarray._io import _get_unsigned_dtype
+from rioxarray._io import FILL_VALUE_NAMES, UNWANTED_RIO_ATTRS, _get_unsigned_dtype
 from rioxarray.exceptions import RioXarrayError
 
 try:
@@ -31,8 +31,6 @@ except ImportError:
         return False
 
 
-FILL_VALUE_NAMES = ("_FillValue", "missing_value", "fill_value", "nodata")
-UNWANTED_RIO_ATTRS = ("nodatavals", "is_tiled", "res")
 # Note: transform & crs are removed in write_transform/write_crs
 
 


Test patch:
diff --git a/test/integration/test_integration__io.py b/test/integration/test_integration__io.py
index 72f5f95f..f6815a4b 100644
--- a/test/integration/test_integration__io.py
+++ b/test/integration/test_integration__io.py
@@ -1106,7 +1106,6 @@ def test_mask_and_scale(open_rasterio):
             "add_offset": 220.0,
             "scale_factor": 0.1,
             "_FillValue": 32767.0,
-            "missing_value": 32767,
             "grid_mapping": "crs",
             "dtype": "uint16",
             "rasterio_dtype": "uint16",
@@ -1137,7 +1136,6 @@ def test_no_mask_and_scale(open_rasterio):
         )
         assert test_encoding == {
             "_FillValue": 32767.0,
-            "missing_value": 32767,
             "grid_mapping": "crs",
             "dtype": "uint16",
             "rasterio_dtype": "uint16",