Grid.to_xarray() issue with lat/lon

Hello,

Latest versions of pyart (e.g. 1.16) now sets 2d lat and lon as coordinates, but 1d x, y coordinates. This makes using ds.swap_dims({“x”: “lon”, “y”: 'lat"}) fail and has broken a lot of code that uses Xarray downstream of pyart. Is there a way to specify a return of 1d lat/lon as Xarray coordinates?

Pyart Version 1.12

Dimensions: (time: 1, z: 20, y: 201, x: 201)
Coordinates:

  • time (time) object 2023-12-18 05:00:21.321000
  • z (z) float64 500.0 1e+03 1.5e+03 2e+03 … 9e+03 9.5e+03 1e+04
    lat (y) float64 37.81 37.82 37.82 37.83 37.83 … 38.7 38.7 38.71 38.71
    lon (x) float64 -75.91 -75.91 -75.9 -75.89 … -74.78 -74.78 -74.77
  • y (y) float64 -5e+04 -4.95e+04 -4.9e+04 … 4.9e+04 4.95e+04 5e+04
  • x (x) float64 -5e+04 -4.95e+04 -4.9e+04 … 4.9e+04 4.95e+04 5e+04

pyART Version: 1.16.1

<xarray.Dataset>
Dimensions: (time: 1, z: 20, y: 201, x: 201)
Coordinates:

  • time (time) object 2023-12-18 05:00:21.321000
  • z (z) float64 500.0 1e+03 1.5e+03 2e+03 … 9e+03 9.5e+03 1e+04
    lat (y, x) float64 37.81 37.81 37.81 37.81 … 38.71 38.71 38.71 38.71
    lon (y, x) float64 -75.91 -75.91 -75.9 -75.89 … -74.78 -74.77 -74.77
  • y (y) float64 -5e+04 -4.95e+04 -4.9e+04 … 4.9e+04 4.95e+04 5e+04
  • x (x) float64 -5e+04 -4.95e+04 -4.9e+04 … 4.9e+04 4.95e+04 5e+04
    Traceback (most recent call last):
    File “/Users/wolff/Desktop/Test/test_grid.py”, line 97, in
    ds = ds.swap_dims({“x”: “lon”, “y”: “lat”})
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “/Users/wolff/anaconda3/lib/python3.11/site-packages/xarray/core/dataset.py”, line 3909, in swap_dims
    raise ValueError(
    ValueError: replacement dimension ‘lon’ is not a 1D variable along the old dimension ‘x’

Thanks,

Dave

1 Like

Thanks for raising this discussion - Py-ART previously dealt with this incorrectly, assuming equal spacing between latitude and longitude, but it varies since we are using an Azimuthal equidistant projection (see the discussion on this issue pyart.core.Grid.to_xarray returns 1-dimensional lon-lat instead of 2-dimensional · Issue #1389 · ARM-DOE/pyart · GitHub)

1 Like

@wxwolff1 does that make sense? Do you think we should roll back changes until Py-ART 2.0 (to be released later this year)? Open to discussion here!