corteva / rioxarray

geospatial xarray extension powered by rasterio
https://corteva.github.io/rioxarray
Other
518 stars 82 forks source link

Scale_factor is only applied to first row. #580

Closed bertcoerver closed 2 years ago

bertcoerver commented 2 years ago

Code Sample, a copy-pastable example if possible

Create a dummy netcdf file:

import numpy as np
import xarray as xr
import rioxarray

data = np.hypot(*np.meshgrid(np.linspace(-100, 500, 50), np.linspace(-150,700, 60)))
ds = xr.Dataset({"var": (["y", "x"],data)}, coords = {"x": np.linspace(40, 51, 50), "y": np.linspace(55, 62, 60)})
ds = ds.rio.write_crs(4326)
ds = ds.rio.write_coordinate_system()
encoding = {"var": {"scale_factor": 0.01, "_FillValue": -9999, "dtype": "int32"}}
output_nc = r"/Users/hmcoerver/Desktop/test.nc"
ds.to_netcdf(output_nc, encoding = encoding)
ds = ds.close()

Opening the file, the scale_factor only gets applied to the first row of the data:

xr.open_dataset(output_nc, engine = "rasterio").isel(band=0)["var"]
<xarray.DataArray 'var' (y: 60, x: 50)>
array([[  707.11,   705.48,   704.06, ...,   846.23,   853.17,   860.23],
       [69285.  , 69119.  , 68974.  , ..., 83435.  , 84139.  , 84855.  ],
       [67860.  , 67690.  , 67542.  , ..., 82256.  , 82970.  , 83695.  ],
       ...,
       [15712.  , 14962.  , 14279.  , ..., 49071.  , 50258.  , 51448.  ],
       [16848.  , 16151.  , 15520.  , ..., 49446.  , 50625.  , 51806.  ],
       [18028.  , 17378.  , 16793.  , ..., 49861.  , 51030.  , 52202.  ]])
Coordinates:
    band         int64 1
  * x            (x) float64 40.0 40.22 40.45 40.67 ... 50.33 50.55 50.78 51.0
  * y            (y) float64 62.0 61.88 61.76 61.64 ... 55.36 55.24 55.12 55.0
    spatial_ref  int64 0

Opening the file like this, seems to work:

rioxarray.open_rasterio(output_nc, mask_and_scale=True)
<xarray.DataArray 'var' (band: 1, y: 60, x: 50)>
array([[[707.11, 705.48, ..., 853.17, 860.23],
        [692.85, 691.19, ..., 841.39, 848.55],
        ...,
        [168.48, 161.51, ..., 506.25, 518.06],
        [180.28, 173.78, ..., 510.3 , 522.02]]])
Coordinates:
  * band         (band) int64 1
  * x            (x) float64 40.0 40.22 40.45 40.67 ... 50.33 50.55 50.78 51.0
  * y            (y) float64 62.0 61.88 61.76 61.64 ... 55.36 55.24 55.12 55.0
    spatial_ref  int64 0

But selecting the band doesn't seem to work:

rioxarray.open_rasterio(output_nc, mask_and_scale=True).isel(band=0)
<xarray.DataArray 'var' (y: 60, x: 50)>
array([[  707.11,   705.48,   704.06, ...,   846.23,   853.17,   860.23],
       [69285.  , 69119.  , 68974.  , ..., 83435.  , 84139.  , 84855.  ],
       [67860.  , 67690.  , 67542.  , ..., 82256.  , 82970.  , 83695.  ],
       ...,
       [15712.  , 14962.  , 14279.  , ..., 49071.  , 50258.  , 51448.  ],
       [16848.  , 16151.  , 15520.  , ..., 49446.  , 50625.  , 51806.  ],
       [18028.  , 17378.  , 16793.  , ..., 49861.  , 51030.  , 52202.  ]])
Coordinates:
    band         int64 1
  * x            (x) float64 40.0 40.22 40.45 40.67 ... 50.33 50.55 50.78 51.0
  * y            (y) float64 62.0 61.88 61.76 61.64 ... 55.36 55.24 55.12 55.0
    spatial_ref  int64 0

Expected Output

Not sure if I'm doing something wrong, but I'd expect the output to be the same whether or not I select a band?

Environment Information

rioxarray (0.12.0) deps: rasterio: 1.3.2 xarray: 2022.6.0 GDAL: 3.5.1 GEOS: 3.11.0 PROJ: 9.0.1 PROJ DATA: /Users/hmcoerver/opt/miniconda3/envs/pywapor/share/proj GDAL DATA: /Users/hmcoerver/opt/miniconda3/envs/pywapor/share/gdal

Other python deps: scipy: 1.9.1 pyproj: 3.3.1

System: python: 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:41:22) [Clang 13.0.1 ] executable: /Users/hmcoerver/opt/miniconda3/envs/pywapor/bin/python machine: macOS-12.5.1-arm64-arm-64bit

Installation method

Conda environment information (if you installed with conda):


Environment (conda list):

``` gdal 3.5.1 py310hc67b115_5 conda-forge libgdal 3.5.1 he1a18a7_5 conda-forge rasterio 1.3.2 py310ha36aacf_0 conda-forge rioxarray 0.12.0 pyhd8ed1ab_0 conda-forge xarray 2022.6.0 pyhd8ed1ab_1 conda-forge ```


Details about conda and system ( conda info ):

``` active environment : pywapor active env location : /Users/hmcoerver/opt/miniconda3/envs/pywapor shell level : 2 user config file : /Users/hmcoerver/.condarc populated config files : conda version : 4.13.0 conda-build version : 3.21.9 python version : 3.9.12.final.0 virtual packages : __osx=12.5.1=0 __unix=0=0 __archspec=1=arm64 base environment : /Users/hmcoerver/opt/miniconda3 (writable) conda av data dir : /Users/hmcoerver/opt/miniconda3/etc/conda conda av metadata url : None channel URLs : https://repo.anaconda.com/pkgs/main/osx-arm64 https://repo.anaconda.com/pkgs/main/noarch https://repo.anaconda.com/pkgs/r/osx-arm64 https://repo.anaconda.com/pkgs/r/noarch package cache : /Users/hmcoerver/opt/miniconda3/pkgs /Users/hmcoerver/.conda/pkgs envs directories : /Users/hmcoerver/opt/miniconda3/envs /Users/hmcoerver/.conda/envs platform : osx-arm64 user-agent : conda/4.13.0 requests/2.28.1 CPython/3.9.12 Darwin/21.6.0 OSX/12.5.1 UID:GID : 501:20 netrc file : None offline mode : False ```
snowman2 commented 2 years ago

See #581