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

How can I change the units 'm' to 'mm' for ERA5 precipitation dataset. #1543

Closed
1 task done
CGL5230 opened this issue Nov 30, 2023 · 5 comments
Closed
1 task done
Labels
support Questions and help for users/developers

Comments

@CGL5230
Copy link

CGL5230 commented Nov 30, 2023

Generic Issue

  • xclim version:0.46.0
  • Python version:3.11
  • Operating System:Linux

Description

I download a ERA5 daily_mean precipitation dataset. I want to use this dataset to calculate the AnnualPrecipitation. I use the command as below. Here is the information of precipitation dataset after I revise the units. Before the changing activity, the units is 'm'.
image

What I Did

pre_ERA5.tp.values = pre_ERA5.tp.values*24*1000
pre_ERA5['tp'].attrs['units'] = 'mm'

What I Received

ValidationError: Data units millimeter are not compatible with requested [precipitation].

What I found

I don't konw why I got this error, even I changed the units to 'mm'. I want to know how can I fix this problem.
Then I search the xclim user guide and find the function xclim.core.units.convert_units_to(source, target, context=None)
Can I change the units of precipitation like:

pre_ERA5 = xclim.core.units.convert_units_to(pre_ERA5, 'mm', context=None)

Code of Conduct

  • I agree to follow this project's Code of Conduct
@aulemahal
Copy link
Collaborator

aulemahal commented Nov 30, 2023

Hi @CGL5230 !
This is because xclim calls "precipitation" the precipitation flux. We follow language from the "Climate and Forecast" (CF) conventions where "flux" is a mass per area per time, usually "kg m-2 s-1". Xclim can also receive a "rate", i.e. "length per time", like "mm/day", because the transformation from rate to flux is always the same for liquid water : multiply by the density of water which is 1000 kg/m³.

In your case, the data you have seems to be a total thickness of liquid water for the current timestep, which seems to be a day. If you confirm that the dataset is daily then an explicit and easy way out would be to patch the units like this:

pre_ERA5['tp'].attrs['units'] = 'm / day'

Xclim will convert to "kg m-2 s-1" under the hood.

But in this precise case, convert_units_to should be able to handle it, you simply need to request a precipitation flux unit:

pr = xclim.core.units.convert_units_to(pre_ERA5.tp, 'kg m-2 s-1')

Be aware however, that this works because the input has all needed metadata for xclim to understand how to handle the conversion! Xclim will read the standard_name and understand that this variable speaks of liquid water. It will also look at the frequency of the data. It will then enable the conversion from "[length]" to "[mass]/([area][time])".

@CGL5230
Copy link
Author

CGL5230 commented Dec 1, 2023

Thanks for quickly reply. I use the command as pre_ERA5['tp'].attrs['units'] = 'm / day' to change the units. And I got the waring like below when I run the ERA5_rx1day = xclim.indicators.atmos.max_1day_precipitation_amount(pr='tp',freq='YS',ds=pre_ERA5). I think the reason is the units and variable name not the standard name for xclim. So,the result will be correct for R1XDAY index ?

/home/anaconda3/envs/py311/lib/python3.11/site-packages/xclim/core/cfchecks.py:41:
UserWarning: Variable does not have a `cell_methods` attribute.
 _check_cell_methods(
/home/anaconda3/envs/py311/lib/python3.11/site-packages/xclim/core/cfchecks.py:45: UserWarning: Variable has a non-conforming standard_name: Got `lwe_thickness_of_precipitation_amount`, expected `['precipitation_flux']`
 check_valid(vardata, "standard_name", data["standard_name"])

@CGL5230
Copy link
Author

CGL5230 commented Dec 1, 2023

I want to clarify this command pre_ERA5['tp'].attrs['units'] = 'm / day'. I got the daily statistic precipitation dataset from ERA5 data pool. You can find the calculate step from document. The precipitation dataset is a daily mean dataset.

Notice my comand:pre_ERA5.tp.values = pre_ERA5.tp.values*24*1000. I change the precipitation dataset as a daily sum dataset and now the unit of values actually is 'mm'. So I use this command to convert the units.

pre_ERA5.tp.values = pre_ERA5.tp.values*24*1000
pre_ERA5['tp'].attrs['units'] = 'mm / day'

But I still got the waring like before.

/home/anaconda3/envs/py311/lib/python3.11/site-packages/xclim/core/cfchecks.py:41:
UserWarning: Variable does not have a `cell_methods` attribute.
 _check_cell_methods(
/home/anaconda3/envs/py311/lib/python3.11/site-packages/xclim/core/cfchecks.py:45: UserWarning: Variable has a non-conforming standard_name: Got `lwe_thickness_of_precipitation_amount`, expected `['precipitation_flux']`
 check_valid(vardata, "standard_name", data["standard_name"])

Hi @CGL5230 ! This is because xclim calls "precipitation" the precipitation flux. We follow language from the "Climate and Forecast" (CF) conventions where "flux" is a mass per area per time, usually "kg m-2 s-1". Xclim can also receive a "rate", i.e. "length per time", like "mm/day", because the transformation from rate to flux is always the same for liquid water : multiply by the density of water which is 1000 kg/m³.

In your case, the data you have seems to be a total thickness of liquid water for the current timestep, which seems to be a day. If you confirm that the dataset is daily then an explicit and easy way out would be to patch the units like this:

pre_ERA5['tp'].attrs['units'] = 'm / day'

@aulemahal
Copy link
Collaborator

aulemahal commented Dec 1, 2023

The warnings are indeed simply because the metadata of your dataset is not exactly what xclim expected, especially the standard_name that becomes wrong after our unit change.

If you use convert_units_to, the standard name will be modified as well and the warning1 should go away. But it's only a warning, and if you know that your data is valid, then it's safe to ignore.

I don't have access to the first link you gave, but from the metadata you showed, it seems that pre_ERA5['tp'] is the total precipitation accumulated over one timestep. So the liquid-water-equivalent thickness, in "m". To transform to a rate, you simply need to divide by the length of time of that timestep, which here is a single day.

precipitation_rate = total_precipitation  / 1 day

Which is why simply changing the units from "m" to "m/day" should work. I'm not sure I understand why you wanted to multiply by 24 ? That would mean the value is the mean hourly precipitation accumulation, which is not what I infer from the attributes.

Footnotes

  1. That is the warning about the standard_name. The other one about the cell_methods will stay, but again, this is safe to ignore, it's just xclim being a bit over-careful.

@CGL5230
Copy link
Author

CGL5230 commented Dec 1, 2023

Thanks for your quick reply. I offer the document link to show the precipitation dataset actually is daily mean dataset which would mean the value is the mean hourly precipitation accumulation. Because I want to get a daily-sum precipitation but hourly precipitation. The daily-sum precipitation can be used to calculate the annual precipitation.

Which is why simply changing the units from "m" to "m/day" should work. I'm not sure I understand why you wanted to multiply by 24 ? That would mean the value is the mean hourly precipitation accumulation, which is not what I infer from the attributes.

@Zeitsperre Zeitsperre added the support Questions and help for users/developers label Dec 1, 2023
@CGL5230 CGL5230 closed this as completed Dec 8, 2023
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