xcdat
xcdat copied to clipboard
[Enhancement]: Add X and Y bounds to 2d coordinates
Is your feature request related to a problem?
I don't see any similar issues. I would like the bounds.add_missing_bounds() function to be able to add bounds to two-dimensional coordinates.
Describe the solution you'd like
I've added a code example here to demonstrate.
import cf_xarray
import numpy as np
import xarray as xr
import xcdat as xc
# Create 2-d lat and lon arrays
lat=np.arange(90,-91,-5)
lon=np.arange(0,361,5)
lon = np.transpose(np.broadcast_to(np.squeeze(lon),(37,73)))
lat = np.transpose(np.transpose(np.broadcast_to(lat,(73,37))))
data = np.random.rand(73,37)
ds = xr.DataArray(data=data, coords={"lon":(["x","y"],lon),"lat":(["x","y"],lat)}, dims=["x","y"], attrs=None)
ds = ds.to_dataset(name="data")
# The printed result will not have lat lon bounds
ds.bounds.add_missing_bounds()
# The printed result will include lat lon bounds
# I would like to get this result using xcdat.bounds.add_missing_bounds()
ds.cf.add_bounds(["lat","lon"])
Current result with xcdat:
Desired result:
Describe alternatives you've considered
This can be done with cf_xarray, as I've shown in my example. Because so many xcdat features require datasets to have bounds, however, I think it would be very helpful for xcdat to be able to add those bounds without asking the user to research and incorporate another library.
Another alternative would be to point users to cf_xarray for 2d bounds in the add_missing_bounds() documentation.
Additional context
Two dimensional coordinates are very common in ocean and sea ice datasets.