-
Notifications
You must be signed in to change notification settings - Fork 59
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
SPEI : can't figure it out #1564
Comments
Sorry to hear you rushed using SPEI. As you perform an operation on an arithmetic operation on your datasets in xarray, ( # a dataset with units
pr.attrs["units"]
>>> 'kg m-2 s-1'
# arithmetic op kills units
out = 0.5*pr
out.attrs["units"]
>>> KeyError: 'units'
# specify that ops should not change units
with xr.set_options(keep_attrs=True):
out = (0.5*pr)
out.attrs["units"]
>>> 'kg m-2 s-1' If you wrap your computation of As for the I guess if you solve the problem in the first step, you won't be doing this change |
Hi @coxipi, thank for your answer. I did what you said, but I get "KeyError: 'units'". I think I have to set some attributes manually because in the xarray.datarray I use for the SPEI computation there are no attribute at all. I don't understand which kind of attributes I need to set. |
Hi! Can you show the code you use to get this error? It seems you might have invoked |
Yes it's not defined in my initial dataset. How can I define it? And how can I define every attributes I need to compute the SPEI? My code is exactly the code I wrote above, plus the code you advise me to add. |
water budget is Now, suppose the units are agERA5_202302['bic_daily'] = agERA5_202302['ppn'] - agERA5_202302['pev']
bic_daily_foramonth = agERA5_202302['bic_daily']
bic_daily_foramonth.attrs["units"] = "kg m-2 s-1" You could also define units for agERA5_202302['ppn'].attrs["units"] = "kg m-2 s-1"
agERA5_202302['pev'].attrs["units"] = "kg m-2 s-1" If you find out that the implicit units aren't the same, you can use our conversion tools to change units of one datasets agERA5_202302['ppn'].attrs["units"] = "kg m-2 s-1"
agERA5_202302['pev'].attrs["units"] = "kg mm-2 d-1"
agERA5_202302['pev'] = xclim.core.units.convert_units_to(agERA5_202302['pev'], agERA5_202302['ppn']) # 'pev' is now in "kg m-2 s-1" Cheers |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Setup Information
Context
Hello, I have been trying to calculate the SPEI for a long time unsuccessfully, because every time I tried to fix an error I got a new one . I've been using agERA5 dataset, rearranged from GRIB to netCDF, adding the "pev" variable, that is the potential evapotranspiration calculated with the Penman-Monteith equation. The netCDF file contains several variables with daily values (for february 2023, as an example only), including those necessary for computing the SPEI: in particular the "ppn" (that is the daily total precipitation [mm]) and the "pev" (namely the daily potential evapotranspiration [mm]) in order to compute the daily hydroclimatic balance [mm] (precipitation minus evapotraspiration) named "bic_daily". Below the first (simplest) version of the python code i wrote:
I got this error: KeyError: 'units' (same error if I change "indices" with "atmos"). The error des not depends on the parameters inside the SPEI function.
I decided to try with some changes. Below the new python code:
If I run:
spei_feb = atmos.standardized_precipitation_evapotranspiration_index(wb = bic_daily_foramonth, freq = None, window = 1, dist = 'gamma', method = 'APP')
I got: ValueError: Inputs have different frequencies. Got : [].To mute this, set xclim's option data_validation='log'.
If I run:
spei_feb = atmos.standardized_precipitation_evapotranspiration_index(wb = bic_daily_foramonth, freq = None, window = 1, dist = 'gamma', method = 'APP')
I got: KeyError: 'units'
I decided to try again with some other changes. Below the last python code:
And I got this: AttributeError: 'DataArray' object has no attribute 'time'
I do not understand if the problem comes from the data, because I have not found examples of code for the SPEI computation
I'm sorry for being wordy. Any suggestions?
Thank you in advance
Giulia
Dataset.zip
Code of Conduct
The text was updated successfully, but these errors were encountered: