Closed tlogan2000 closed 3 years ago
This shouldn't be too tough I don't think... a small check and manipulation of output dataset coords should be simple to implement
I found this solution, but it's the only one I tried, so there might be a more elegant one:
Xb = mask.bfill('lon').sum('lat')
Xf = mask.ffill('lon').sum('lat')
Xm = (Xb == Xb.max()) | (Xf == Xf.max()) # 1 in outer zone, 0 in the inner, only for lon
Yb = mask.bfill('lat').sum('lon')
Yf = mask.ffill('lat').sum('lon')
Ym = (Yb == Yb.max()) | (Yf == Yf.max())
final_mask = mask.notnull() | ( ( Ym == 0 ) & ( Xm == 0 ) ) # 1 in the strictly inner zone or on the shapes, 0 elsewhere.
sub_data = data.where(final_mask, drop=True)
Before: After:
(And there's some fancy dimension finding missing)
Description
subset.shape() doesn't work properly for polygons that are not contiguous. Example geojson used in test code available here :https://ouranos-my.sharepoint.com/:u:/g/personal/tralog1_ouranos_ca/EfIY4IARTthNkFkfEL5csu0BmEVLQjPxgP8WpHA0zVOBfg?e=fdBtjj
Notes: When polygon borders are not touching the
.dropna()
calls in subset_shape() results in chunks (horizontal or vertical) of the dataset being removed. When we examine output lon, lat coords they are not longer monotonously increasing or decreasing. Technically the data has been properly subsetted (from what I can tell at least) but the dataset coordinates become unintuitive as well as causing issues for mapping / plots (pixels stretching)What I Did