Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement]: Add X and Y bounds to 2d coordinates #676

Open
acordonez opened this issue Jul 2, 2024 · 4 comments
Open

[Enhancement]: Add X and Y bounds to 2d coordinates #676

acordonez opened this issue Jul 2, 2024 · 4 comments
Labels
type: enhancement New enhancement request

Comments

@acordonez
Copy link
Contributor

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:
Screenshot 2024-07-02 at 3 11 10 PM
Desired result:
Screenshot 2024-07-02 at 3 11 24 PM

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.

@pochedls
Copy link
Collaborator

pochedls commented Jul 2, 2024

@acordonez – could you point to an example dataset where this exists (or a dataset where this behavior would apply if you opened the dataset and then dropped the bounds via ds = ds.drop_vars('lat_bnds')?

A couple things that might be considered for this enhancement:

  • Should xcdat just call the cf-xarray functionality to add bounds (for this feature and in general)?
  • What happens now if someone tried to regrid this dataset with xcdat + xesmf? I'm curious if the bounds are not needed, are generated, or if xcdat can't currently regrid this dataset (unless the user adds the bounds with cf-xarray). Aside from regridding, other spatial functionality that uses bounds (e.g., spatial averaging), does not work on non-rectilinear data.

@acordonez
Copy link
Contributor Author

@pochedls Regarding regridding, in my experience xcdat + xesmf cannot regrid this dataset without the bounds. I actually stumbled across this by trying to regrid a non-rectilinear dataset.
The dataset I was working with is on NERSC, but I can try to find something local to point you to.

@pochedls
Copy link
Collaborator

pochedls commented Jul 2, 2024

@pochedls Regarding regridding, in my experience xcdat + xesmf cannot regrid this dataset without the bounds. I actually stumbled across this by trying to regrid a non-rectilinear dataset. The dataset I was working with is on NERSC, but I can try to find something local to point you to.

Would a dataset like this work (if bounds are dropped): /p/css03/esgf_publish/CMIP6/CMIP/CSIRO/ACCESS-ESM1-5/historical/r10i1p1f1/SImon/siconc/gn/v20200817/siconc_SImon_ACCESS-ESM1-5_historical_r10i1p1f1_gn_185001-201412.nc

Thank you for clarifying on regridding. Since this issue would prevent a user from regridding, it seems like it would be useful to add functionality to add bounds for 2D coordinates.

@acordonez
Copy link
Contributor Author

/p/css03/esgf_publish/CMIP6/CMIP/CSIRO/ACCESS-ESM1-5/historical/r10i1p1f1/SImon/siconc/gn/v20200817/siconc_SImon_ACCESS-ESM1-5_historical_r10i1p1f1_gn_185001-201412.nc

@pochedls Yes it looks like the file you suggested would reproduce this behavior if the bounds were dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New enhancement request
Projects
Status: Todo
Development

No branches or pull requests

2 participants