2

I am having trouble setting dimensions in a xarray object. I have created a xarray object (data, lats, and lons are 2D ndarray) and set a coordinate system as follows, and it is working.

da0 = xr.DataArray(
      data = data,
      dims = ["x","y"],
      coords = dict(
            lon = (["x","y"], lons),
            lat = (["x","y"], lats)
      )
)
da = da0.rio.write_crs("epsg:4326", inplace=True)

However, when I tried to set the spatial dimension for reprojection

da.rio.set_spatial_dims("lon", "lat", inplace=True)

it raises the following error.

   raise DimensionError(
rioxarray.exceptions.DimensionError: x dimension (lon) not found.

Why "lon" is not found although "da" has "lon"? I want to set dimensions for using xr.rio.reproject() in the end.

1 Answer 1

3

Your dims and coords need to have the same name. This should work:

da0 = xr.DataArray(
      data = data,
      dims = ["x","y"],
      coords = dict(
            x = (["x","y"], lons),
            y = (["x","y"], lats)
      )
)
da = da0.rio.write_crs("epsg:4326", inplace=True)
6
  • Then it raises an error that ` raise MissingDimensionsError( xarray.core.variable.MissingDimensionsError: 'x' has more than 1-dimension and the same name as one of its dimensions ('x', 'y'). xarray disallows such variables because they conflict with the coordinates used to label dimensions. `
    – raraki
    Jul 13, 2021 at 20:53
  • Even if I change the dimension name to x1, it still raises error ``` da = xr.DataArray( data = data, dims = ["x1","y1"], coords = dict( x = (["x1","y1"], lons), y = (["x1","y1"], lats) ) ) ``` rioxarray.exceptions.DimensionError: x dimension (x) not found.
    – raraki
    Jul 13, 2021 at 20:58
  • rioxarray expects 1D coordinate arrays. I think having 2D coordinate arrays is your problem.
    – snowman2
    Jul 13, 2021 at 22:02
  • This might be a helpful reference: github.com/corteva/rioxarray/issues/209
    – snowman2
    Jul 13, 2021 at 22:03
  • Thank you for your quick reply!! I will try it
    – raraki
    Jul 13, 2021 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.