How do I check if a Xarray Dataarray has units assigned #2359
-
I have come across the problem that during filling of "nans" the previously assigned units are getting lost. Prior to the call units are assigned using good_var= good_var*units.degC good_var=good_var.interpolate_na(dim='time',method='slinear',max_gap='3H') # In order to reassign units my program needs to check if units have been assigned or not. How can I do this? When I use the metpy.quantify() is there a way to specify the units? How are units determined when calling the method? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
For data being read into xarray, the units usually come from metadata stored in a In this particular case, it looks like The direct answer to your question is you can look at var = ds.Temperature_surface * units.K
var = var.metpy.dequantify().interpolate_na(dim='time', method='slinear', max_gap='3H', keep_attrs=True)
# Can "requantify" if necessary at this point
var = var.metpy.quantify() EDIT: Fix |
Beta Was this translation helpful? Give feedback.
-
Thanks much Ryan Correction in your code suggestion (replace "units" with "True"... which threw me off for a moment in case somebody else runs into this. var=var.metpy.dequantify().interpolate_na(dim='time',method='slinear',max_gap='3H',keep_attrs=True) I was also able to this to solve the problem if isinstance(var.data,units.Quantity)==False:
var=var*units.degC I noticed that when I don't do this check and the variable already has a Quantity assigned. I get Kelvin^2 ... Axel |
Beta Was this translation helpful? Give feedback.
-
Also, yeah |
Beta Was this translation helpful? Give feedback.
-
You may also be interested in using |
Beta Was this translation helpful? Give feedback.
You may also be interested in using
pint-xarray
for this: https://pint-xarray.readthedocs.io/en/latest/generated/xarray.DataArray.pint.interpolate_na.html