Xarray : how to deal with irregular coordinates

Viewed 822

I am trying to get around with xarray but i am facing troubles.

Especially since I am trying to get it working with irregular lat lon grids.

I have a netCDF file with a variable, here slp, on a lat/lon grid. Which when loaded gives me this:

<xarray.Dataset>
Dimensions:         (Time: 61375, lat: 81, lon: 81)
Dimensions without coordinates: Time, lat, lon
Data variables:
    sea_level_pres  (Time, lat, lon) float64 ...

Up to here everything is fine. Now I want to add my irregular lat lon grid as coordinates. I have tried the following:

slp["latc"] = (("lat","lon"),coord.XLAT.values.squeeze())
slp["lonc"] = (("lat","lon"),coord.XLONG.values.squeeze())
slp.set_coords("lonc")
slp.set_coords("latc")

Where coord is another netCDF where my irregular grid is described. Now I get the following:

<xarray.Dataset>
Dimensions:         (Time: 61375, lat: 81, lon: 81)
Coordinates:
    latc            (lat, lon) float32 34.69266 34.73062 ... 54.56264 54.51206
    lonc            (lat, lon) float32 -7.4926453 -7.1955566 ... 21.634521
Dimensions without coordinates: Time, lat, lon
Data variables:
    sea_level_pres  (Time, lat, lon) float64 ...

Which seems to me that it is the most convenient way to deal with irregular grid : having physical coordinates and indexed cooridnates.

But here I am not sure that I am doing things properly since when I try to subset according to physical latitude and longitude I never manage to do it:

lat_bnd = [35,40]
slp.sel(lat=slice(*lat_bnd))

Gives:

<xarray.Dataset>
Dimensions:         (Time: 61375, lat: 5, lon: 81)
Coordinates:
    latc            (lat, lon) float32 43.30759 43.35192 ... 44.46182 44.41968
    lonc            (lat, lon) float32 -9.37085 -9.030823 ... 18.309692 18.65686
Dimensions without coordinates: Time, lat, lon
Data variables:
    sea_level_pres  (Time, lat, lon) float64 ...

So it seems that I am slicing over indexes not actually my latitude and longitudes... But something like:

slp.sel(latc=slice(*lat_bnd))

Raise the following error:

ValueError: dimensions or multi-index levels ['latc'] do not exist

Any help in understanding this would be greatly appreciated :)

Thanks in advance !

0 Answers
Related