Multidimensional xarray where dorps coordinates?

I have a multidimensional variable which I would like to mask it using a multidimensional mask. However, using xarray.where drops the coordinates of the variables. Here is an example using very small files (download var file here and mask file here):

import xarray as xr
mask = xr.open_dataarray("mask.nc")
var = xr.open_dataarray("var.nc")

masked_var = var.where(mask,drop=False)

>>> print(mask.shape)
(3, 3, 101, 101)
>>> print(var.shape)
(3, 3, 101, 101)
>>> print(masked_var.shape)
(1, 3, 101, 101)  # Note expected dimension is (3, 3, 101, 101)

As you can see where dropped coordinates. My expected behavior is:

var.isel(lat=0,lon=0).where(mask.isel(lat=0,lon=0))
var.isel(lat=1,lon=1).where(mask.isel(lat=1,lon=1))
.
.
.
for all lat and lon, resulting in masked_var with (3, 3, 101, 101) shape.


Read more here: https://stackoverflow.com/questions/67376150/multidimensional-xarray-where-dorps-coordinates

Content Attribution

This content was originally published by ReSS at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: