@@ -462,7 +462,9 @@ def grid_to_polygons(ds: xr.Dataset) -> xr.DataArray:
462
462
463
463
grid = ds .cf [["latitude" , "longitude" ]].load ()
464
464
bounds = grid .cf .bounds
465
+ coordinates = grid .cf .coordinates
465
466
dims = grid .cf .dims
467
+ bounds_dim = grid .cf .get_bounds_dim_name ("latitude" )
466
468
467
469
if "latitude" in dims or "longitude" in dims :
468
470
# for 1D lat, lon, this allows them to be
@@ -471,13 +473,23 @@ def grid_to_polygons(ds: xr.Dataset) -> xr.DataArray:
471
473
472
474
assert "latitude" in bounds
473
475
assert "longitude" in bounds
476
+ (lon ,) = coordinates ["longitude" ]
474
477
(lon_bounds ,) = bounds ["longitude" ]
478
+ (lat ,) = coordinates ["latitude" ]
475
479
(lat_bounds ,) = bounds ["latitude" ]
476
480
477
481
with xr .set_options (keep_attrs = True ):
478
- (points ,) = xr .broadcast (grid )
482
+ broadcasted = xr .broadcast (
483
+ grid [lon ],
484
+ grid [lat ],
485
+ grid [lon_bounds ],
486
+ grid [lat_bounds ],
487
+ exclude = bounds_dim ,
488
+ )
489
+ asdict = dict (zip ([lon , lat , lon_bounds , lat_bounds ], broadcasted ))
490
+ # display(asdict)
491
+ points = xr .Dataset (asdict )
479
492
480
- bounds_dim = grid .cf .get_bounds_dim_name ("latitude" )
481
493
points = points .transpose (..., bounds_dim )
482
494
lonbnd = points [lon_bounds ].data
483
495
latbnd = points [lat_bounds ].data
0 commit comments