Closed yt87 closed 10 months ago
A quick workaround is to pass ds.copy()
to regridder
@huard This looks like the same issue you were raising on slack!
The copy of attributes from ds_out
is a feature indeed. As xESMF only handles the spatial dimensions, the regridding
can only assign attributes to lat
and lon
in your case.
But the loss of attributes on ds
is a problem. We need to investigate.
I think it would make sense to preserve the non-spatial coordinate attributes, such as time
in the above example. Regridding does change their meaning.
The issue I see hee, is that the regridding does not know which coordinates of ds_out
will appear on the regridded output, except for the spatial dims. Also, in you specific case, time
was simply absent from ds_out
.
I think it makes more sense to preserve the attributes from the input (ds
) exactly, instead of trying to guess how to "update" them following the regridding.
But if you have an idea on the logic behind this hypothetical update, this could be implemented!
Sorry, I missed the keep_attrs
argument to BaseRegridder.__call__
. Interestingly, if set to True
, it also preserves attributes in the argument, that is, my problem disappears. I still think there is a bug, the input dataset should not be modified in either case.
This seems to be xarray issue:
import xarray as xr
def magnitude(a, b):
func = lambda x, y: np.sqrt(x**2 + y**2)
return xr.apply_ufunc(func, a, b, keep_attrs=False)
array = xr.DataArray([1, 2, 3], coords=[("x", [0.1, 0.2, 0.3], {'axis': 'X'})])
array
magnitude(array, -array)
array['x'].attrs
The last line prints an empty dictionary. The documentation states https://github.com/pydata/xarray/blob/main/xarray/core/computation.py#L836-L1210
keep_attrs : {"drop", "identical", "no_conflicts", "drop_conflicts", "override"} or bool, optional
- 'drop' or False: empty attrs on returned xarray object.
Returned, not passed, so likely this is not an intentional behaviour.
I opened an issue on xarray regarding this bug.
Should we close this here, then ?
I have nothing against closing it here.
I am not sure it is a bug or an undocumented feature. Consider the following code:
The output is:
Actually, all coordinate attributes are gone. Attributes for data variables, in this case
air
, are preserved. Interestingly,regridder
assigns attributes tolat
andlon
for the output (but not totime
), copied fromds_out