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

interpolate_na raises TypeError when using rioxarray.open_rasterio instead of xarray.open_rasterio #250

Closed
vedal opened this issue Feb 21, 2021 · 5 comments · Fixed by #256
Labels
bug Something isn't working

Comments

@vedal
Copy link

vedal commented Feb 21, 2021

Code Sample

The following causes a crash:

import xarray
import rioxarray

file_url = 'https://neo.sci.gsfc.nasa.gov/archive/geotiff.float/MY1DMW_CHLORA/MY1DMW_CHLORA_2020-03-21.FLOAT.TIFF'
ds = rioxarray.open_rasterio(file_url)
ds = ds.rio.write_crs("EPSG:4326")
ds = ds.sel(
    y=slice(55, 30), x=slice(165, 180)
)
ds = ds.rio.interpolate_na(method="linear")

while replacing rioxarray.open_rasterio with xarray.open_rasterio works fine.

Problem description

The problem is a crash with the error:
TypeError: float() argument must be a string or a number, not 'NoneType':
This also happens for other TIFFs.

Environment Information

rioxarray (0.3.0) deps:
  rasterio: 1.1.8
    xarray: 0.16.2
      GDAL: 2.4.4

Other python deps:
     scipy: 1.5.4
    pyproj: 3.0.0.post1

System:
    python: 3.8.3 (default, Nov  5 2020, 11:18:23)  [Clang 9.1.0 (clang-902.0.39.1)]
executable: /Users/user/.pyenv/versions/3.8.3/envs/myenv/bin/python3.8
   machine: macOS-10.13.6-x86_64-i386-64bit

Installation method

pypi

@vedal vedal added the bug Something isn't working label Feb 21, 2021
@snowman2
Copy link
Member

The issue is related to this: #212 (comment)

The solution is either have the code do nothing if there is no nodata value or to raise a clearer exception.

@snowman2
Copy link
Member

The dataset you have is missing a nodata value. Due to that, there is nothing to do.

My guess is your nodata value is 99999 based on the file you have. If that is the case rio.write_nodata will get it to work as expected.

Here is the updated script:

file_url = 'https://neo.sci.gsfc.nasa.gov/archive/geotiff.float/MY1DMW_CHLORA/MY1DMW_CHLORA_2020-03-21.FLOAT.TIFF'
ds = rioxarray.open_rasterio(file_url)
ds.rio.write_crs("EPSG:4326", inplace=True)
ds.rio.write_nodata(99999., inplace=True)
ds = ds.sel(
    y=slice(55, 30), x=slice(165, 180)
)
ds = ds.rio.interpolate_na(method="linear")

@vedal
Copy link
Author

vedal commented Feb 22, 2021

Thanks alot @snowman2 , this works great! 😄
Related to your comment above, I would definitely prefer a clearer exception rather than failing "silently" in this case. In my original code, I was using xarray.open_rasterio along with a where-statement replacing 99999. with np.nan before running interpolation, which works well. A silent fail is very easy to miss.
Maybe another alternative could be to have np.nan as default nodata-value?

@snowman2
Copy link
Member

Maybe another alternative could be to have np.nan as default nodata-value?

You can actually write NaN as the notata value in your dataset. For clarity, I think leaving it as None makes more sense so you know that there isn't a nodata value. Additionally, NaN does not work with integer data types.

I would definitely prefer a clearer exception rather than failing "silently" in this case.

I agree. I think raising an exception with the suggestion to use rio.write_nodata is the better solution in this scenario.

@vedal
Copy link
Author

vedal commented Feb 26, 2021

Great! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants