rioxarray
rioxarray copied to clipboard
Saving netcdf file as an ASC grid flips images
Using the following netcdf file for input: https://drive.google.com/file/d/1GNWuRPm1mWVXgQsqtgl1wiZFv0evTw2x/view?usp=sharing
If I read in the netcdf file an plot it, I get this image
import xarray as xr
original_file = test_nc.nc'
original_data = xr.open_dataset(original_file)['Rain Rate']
original_data = original_data.rio.set_crs('WGS84')
plt.figure()
original_data.plot()
plt.title('Original Data')
original_data.close()

If I then save the file as an ASCII grid (.asc), reopen it and plot it, the image is flipped about the y-axis. Note that the y-axis coordinates are different too. The bottom of the original image (~36.5) is the top of the ASC image below.
asc_file = 'test_asc.asc'
original_data.rio.to_raster(asc_file)
asc_data = xr.open_dataset(asc_file)['band_data']
plt.figure()
asc_data.plot()
plt.title('ASC Data')
asc_data.close()

However, if I save the image as a TIFF and reopen/plot it, the result is correct (matches the original image)
tiff_file = 'test_tiff.tiff'
original_data.rio.to_raster(tiff_file)
tiff_data = xr.open_dataset(tiff_file)['band_data']
plt.figure()
tiff_data.plot()
plt.title('TIFF Data')
tiff_data.close()

Why does saving the file as an ASCII grid flip the image?
Environment Information
(base) C:\Users\kchudler>python -c "import rioxarray; rioxarray.show_versions()"
rioxarray (0.12.2) deps:
rasterio: 1.3.2
xarray: 2022.9.0
GDAL: 3.5.2
GEOS: 3.11.0
PROJ: 9.0.1
PROJ DATA: C:\ProgramData\Miniconda3\Library\share\proj
GDAL DATA: C:\ProgramData\Miniconda3\Library\share\gdal
Other python deps:
scipy: 1.9.1
pyproj: 3.4.0
System:
python: 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:50:36) [MSC v.1929 64 bit (AMD64)]
executable: C:\ProgramData\Miniconda3\python.exe
machine: Windows-10-10.0.19044-SP0
Installation method
conda install -c conda-forge rioxarray
Conda environment information (if you installed with conda):
Environment (
conda list):
(base) C:\Users\kchudler>conda list | findstr "rasterio xarray gdal"
gdal 3.5.2 py39h34c8707_1 conda-forge
libgdal 3.5.2 hc386656_1 conda-forge
rasterio 1.3.2 py39h223f425_0 conda-forge
rioxarray 0.12.2 pyhd8ed1ab_0 conda-forge
xarray 2022.9.0 pyhd8ed1ab_0 conda-forge
Details about
conda and system ( conda info ):
(base) C:\Users\kchudler>conda info
active environment : base
active env location : C:\ProgramData\Miniconda3
shell level : 1
user config file : C:\Users\kchudler\.condarc
populated config files :
conda version : 22.9.0
conda-build version : not installed
python version : 3.9.13.final.0
virtual packages : __win=0=0
__archspec=1=x86_64
base environment : C:\ProgramData\Miniconda3 (writable)
conda av data dir : C:\ProgramData\Miniconda3\etc\conda
conda av metadata url : None
channel URLs : https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : C:\ProgramData\Miniconda3\pkgs
C:\Users\kchudler\.conda\pkgs
C:\Users\kchudler\AppData\Local\conda\conda\pkgs
envs directories : C:\ProgramData\Miniconda3\envs
C:\Users\kchudler\.conda\envs
C:\Users\kchudler\AppData\Local\conda\conda\envs
platform : win-64
user-agent : conda/22.9.0 requests/2.28.1 CPython/3.9.13 Windows/10 Windows/10.0.19044
administrator : True
netrc file : None
offline mode : False
This is indeed strange. Because it works on a GeoTiff, it makes me wonder if there is an underlying GDAL issue instead of a rioxarray issue. Probably need to find a test case using GDAL that does the same thing to verify.
Same issue if I do this:
gdal_translate -of AAIGrid test_tiff.tiff test_tiff.asc
When I open the ascii data, it is flipped.
Issue addressed upstream: https://github.com/OSGeo/gdal/issues/6946