Closed bertcoerver closed 2 years ago
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:
scale_factor
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)
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?
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
Environment (conda list):
conda list
Details about conda and system ( conda info ):
conda
conda info
See #581
Code Sample, a copy-pastable example if possible
Create a dummy netcdf file:
Opening the file, the
scale_factor
only gets applied to the first row of the data:Opening the file like this, seems to work:
But selecting the band doesn't seem to work:
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
):Details about
conda
and system (conda info
):