[This issue is a migration of Ouranosinc/xclim#422]
The goal would be to compute spatial averages over a polygon. It would need to account for non-uniform grid area and partial overlap, holes in polygons.
The best would be to compute a weight mask for the array representing the area of each gridcell covered by the polygon (of the fractional area).
As mentioned on the original thread, a first way to do this would be to generate a grid of higher resolution and use the existing create_mask.
Or, we could iterate over all gridcells and generate Polygons for them, either using provided lat_bnds and lon_bnds or inferring them. Then, shapely's methods could be used to compute the intersection of each gridpolygon and the target polygon.
From some tests I made of both methods, the second can be quite fast and relatively easy to implement.
[This issue is a migration of Ouranosinc/xclim#422]
The goal would be to compute spatial averages over a polygon. It would need to account for non-uniform grid area and partial overlap, holes in polygons. The best would be to compute a weight mask for the array representing the area of each gridcell covered by the polygon (of the fractional area).
As mentioned on the original thread, a first way to do this would be to generate a grid of higher resolution and use the existing
create_mask
. Or, we could iterate over all gridcells and generate Polygons for them, either using providedlat_bnds
andlon_bnds
or inferring them. Then, shapely's methods could be used to compute the intersection of each gridpolygon and the target polygon.From some tests I made of both methods, the second can be quite fast and relatively easy to implement.