Closed robbibt closed 10 months ago
Comparison of the arrays... somehow xx_cropped
and rasterized
combine to make xx_masked
...
that would be due to floating point precision most likely.
In theory we should have an invariant like this
xr_coords(gbox)['y'][3:7].data == xr_coord(gbox[3:7, :])['y'].data
but this might not be the case due to numeric precision.
changing
- xx_masked = xx_cropped.where(rasterized)
+ xx_masked = xx_cropped.where(rasterized.data)
gives expected result as it ignores tiny differences in lon/lat:
rasterized.latitude.data - xx_cropped.latitude.data
>>> array([ 0.00000000e+00, 2.22044605e-16, 2.22044605e-16, 0.00000000e+00,
0.00000000e+00, -2.22044605e-16, 0.00000000e+00, 0.00000000e+00,
-2.22044605e-16, 0.00000000e+00])
in general, when using xarray.where()
use plain numpy array that matches data dimensions to be sure to bypass coordinate matching logic.
Thanks Kirill!
While testing #114, I've been running into a really confusing issue that seems to cause
xarray
's.where
to change the output size of my arrays.Usually,
a.where(b)
simply masks out values froma
that are False inb
, replacing them withNaN
. This normally doesn't have any effect on the shape ofa
(assuming the defaultdrop=False
) - you get back an array of the orginal shape after applying the mask.For example, I might want to rasterize a polygon, then apply this to my original data as a mask. This works fine - my masked data comes out the same size as my original data:
Problem
However, if I modify the shape of my array using
.isel()
or similar,.where()
starts to behave differently. This example is the same, except I select a subset of my array using.isel()
before rasterizing my polygon into its new geobox.The rasterization works fine - my rasterized polygon and clipped array have the same geobox and shape. However, when I attempt to combine them with
xx_cropped.where(rasterized)
, I get out an array with a different geobox, including different resolution and shape:Am I doing something silly here? Or is there some interaction going on between
xarray
andodc-geo
that is causing.where
to behave differently on data that has previously subsetted with.isel()
?The confusing thing is that
xx_cropped
andrasterized
seem to have completely identical pixel grids, i.e. same geobox and even the same spatial dim coordinates...