Python Xarray – Fixing Wrong Coordinates When Saving DataArray

noaapythonrasterrioxarrayxarray

I have three 3×3 arrays that are downsampled data from NOAA's Global Hydro Estimator (smaller to make it quicker for testing). One array is lats, one is lons, and one is rainfall. Here's a sample

lons: 
[[-180    0  180]
 [-180    0  180]
 [-180    0  180]]

lats:
[[  60   60   60]
 [   0    0    0]
 [ -60  -60 -600]]

rainfall:
[[10 50  0]
 [ 0 20 20]
 [10 90  0]]

I would like to convert this into a raster with EPSG:4326 CRS. In order to do that I've chosen xarray and created a DataArray and saved to a raster like so

ds = xr.DataArray(rainfall,
                  coords={'lat':(['y','x'],lat_grid),
                          'lon':(['y','x'],lon_grid)},
                  dims=['y','x'])

ds.rio.write_crs("epsg:4326", inplace=True)
ds.rio.to_raster('test.tif')

However, when I look at the raster in qgis the lat/lon are on a scale from 0,3 instead of their respective values in the array above. I've tried messing around with the coords and dims and swapping them with no luck, does anyone have any idea of how to accomplish this?

Best Answer

Your latitude and longitude are 2D. They need to be converted to 1D. Here are some suggestions: https://github.com/corteva/rioxarray/issues/209