Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Jan 13, 2024
1 parent d33610e commit c048410
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cf_xarray/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ def grid_to_polygons(ds: xr.Dataset) -> xr.DataArray:

grid = ds.cf[["latitude", "longitude"]].load()
bounds = grid.cf.bounds
coordinates = grid.cf.coordinates
dims = grid.cf.dims
bounds_dim = grid.cf.get_bounds_dim_name("latitude")

if "latitude" in dims or "longitude" in dims:
# for 1D lat, lon, this allows them to be
Expand All @@ -471,13 +473,23 @@ def grid_to_polygons(ds: xr.Dataset) -> xr.DataArray:

assert "latitude" in bounds
assert "longitude" in bounds
(lon,) = coordinates["longitude"]
(lon_bounds,) = bounds["longitude"]
(lat,) = coordinates["latitude"]
(lat_bounds,) = bounds["latitude"]

with xr.set_options(keep_attrs=True):
(points,) = xr.broadcast(grid)
broadcasted = xr.broadcast(
grid[lon],
grid[lat],
grid[lon_bounds],
grid[lat_bounds],
exclude=bounds_dim,
)
asdict = dict(zip([lon, lat, lon_bounds, lat_bounds], broadcasted))
# display(asdict)
points = xr.Dataset(asdict)

bounds_dim = grid.cf.get_bounds_dim_name("latitude")
points = points.transpose(..., bounds_dim)
lonbnd = points[lon_bounds].data
latbnd = points[lat_bounds].data
Expand Down

0 comments on commit c048410

Please sign in to comment.