Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xclim.core.utils.ValidationError: Value's dimension [length] does not match expected units [mass] / [length] ** 2 / [time]. #1453

Closed
1 task done
millet5818 opened this issue Aug 9, 2023 · 7 comments
Labels
support Questions and help for users/developers

Comments

@millet5818
Copy link

Setup Information

  • Xclim version: 0.25.0
  • Python version: 3.6
  • Operating System: windows 10

Context

Dear developers:
I tried to use the function (xclim.indicators.atmos.wetdays) to calculate the total number of days during the period with precipitation over the threshold, but an issue happened. the code and error are listed below:
"Code":

file="F:\\file\\total_precipitation_1984.nc"
ds=xr.open_dataset(file)
ds=xclim.core.units.convert_units_to(ds.tp,'mm')
a=xclim.indices.wetdays(ds,thresh='1 mm/day', freq='YS')

"Error":
xclim.core.utils.ValidationError: Value's dimension [length] does not match expected units [mass] / [length] ** 2 / [time].
I don't know what's wrong with this code and NC file. The above file was downloaded from the ERA5 website. The units of origin file is m.

Thank you very much for your help, and I look forward to your reply!

Zheng Zhao

Code of Conduct

  • I agree to follow this project's Code of Conduct
@millet5818 millet5818 added the support Questions and help for users/developers label Aug 9, 2023
@aulemahal
Copy link
Collaborator

Hi @millet5818 !

In the first versions of xclim, after much discussion and trials, the dev team decided that "precipitation" data would be supported only with units (dimensions) of flux (mass per area per time, kg m-2 s-1, CMIP6 standard) or rate (length per time, mm/d). This already results in somewhat complex code, so we decided NOT to support units of length only (m, like your case). It is too ambiguous.

But, if you know what your data is, there are a few ways to adapt it for xclim:

  • Assuming you know the sampling timestep, simply swap the attribute: ds.tp.attrs['units'] = 'mm/day'
  • Assuming the lengths are the accumulation during the timesteps of ds.time : xc.core.units.amount2rate(ds.tp)

See the doc of the second function here.

Both are similar, but the second is a but more flexible and less arbitrary.

Hope that helps!

@millet5818
Copy link
Author

Hi @aulemahal !
Thank you for your valuable suggestions. I have adopted the function xc.core.units.amount2rate(ds.tp) to convert units of precipitation.
Best wishes!

@millet5818
Copy link
Author

Hello, @aulemahal
I tried to calculate R95P by using function xclim.indicators.icclim.R95p. For definition of R95P, a wet day with precipitation is above 1mm. So i first filtered the data, and then calculate the 95th percentile of a wet day over a given period, and finally calculated the R95P by using the xclim.indicators.icclim.R95p function. Can you help me if the following code is correct?

Due to the first use of xclim packgae, many functions are not well understood for me. I'm sorry to disturb you again. Looking forward to your reply.

file="F:\\file\\total_precipitation_1984.nc"
ds=xr.open_dataset(file)
ds=xclim.core.units.amount2rate(ds.tp, out_units="mm/d")
wetdays_Array=ds.where(ds> 1)
R95T = wetdays_Array.quantile(0.95, dim='time', keep_attrs=True)
R95P=xclim.indicators.icclim.R95p(ds, pr_per=R95T,freq='YS')

@aulemahal
Copy link
Collaborator

Yes this looks good!

@Zeitsperre
Copy link
Collaborator

Hi @millet5818

One thing I noticed is that you are running a much older version of xclim (v0.25.0) and Python (v3.6; currently past end-of-life). Is there a possibility that you can perform an update of these tools? Xclim has significantly changed in this time.

@millet5818
Copy link
Author

Hi @Zeitsperre
Yeah, thanks so much for your reminder, I have updated the newer version of xclim (v0.44) and python (3.9). The tool (xclim) is very helpful for me to develop the procedures for the identification of compound events.

@millet5818
Copy link
Author

Hi @aulemahal
In the calculation of the percentile of the precipitation and temperature, I found the function differences (percentile_doy and quantile) between them in the source code, which makes me confused, especially in the coords "day of year". Do you have a more detailed definition about function percentile_doy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions and help for users/developers
Projects
None yet
Development

No branches or pull requests

3 participants