Monthly sum of wet days from daily data using Climate Data Operators (CDO)
Asked Answered
H

3

5

I have climate data with a daily temporal resolution and would like a count of days that have precipitation (e.g., greater than 1mm/day) by month and by year.

I've tried eca_pd,1 and eca_rr1, but these commands return wet-day totals for all years.

For example, cdo eca_pd,1 infile outfile

Is there a command to return wet-days for each month and/or year?

Holmic answered 23/4, 2020 at 2:44 Comment(0)
D
6

You can accomplish this task with CDO's masking function, for more details beyond the answer below, you can also refer to my video guide on masking using cdo.

The first step is to make an equivalent file with 1 if P>threshold (1mm/day in your case) and 0 otherwise. For this we use the "greater than or equal to a constant" gec function (or ge="greater than" if you prefer):

cdo gec,1 input.nc mask.nc 

(assuming units are mm/day in your input file).

Then you can simply sum this mask over the period (months, years etc) that you want your statistic

cdo monsum mask.nc nwetdays_mon.nc 
cdo yearsum mask.nc nwetdays_year.nc

Of course you can pipe this if you like to do this on one line: e.g.

cdo monsum -gec,1 input.nc nwetdays_mon.nc 

We can take this even further if you want to work out the climatology for a particular month. If you have a multiyear dataset then you can use the wonderful "ymonstat" commands. So for example, once you have calculated your monthly series of wet days above, you can calculate the average for each month with

cdo ymonmean nwetdays_mon.nc nwetdays_mon_clim.nc

You can then difference the series from this monthly climatology to give you the anomaly of wet days in each month over the series

cdo ymonsub nwetdays_mon.nc nwetdays_mon_clim.nc nwetdays_mon_anom.nc

I hope that helps!

(ps: I usually always find it is easier to calculate these kinds of statistics directly with CDO in this way, I rarely find that the built in climate functions calculate exactly the statistic as/how I want).

Downing answered 13/5, 2020 at 13:40 Comment(3)
Thank you. I was able to solve this the other day using the same approach.Holmic
sorry I was slow, missed this cdo-climate tagged question somehow.Downing
no worries, and thanks for the 'bonus' ymonmean section!Holmic
L
6

With NCO's ncap2, create a binary flag then total it in the desired dimension(s):

ncap2 -s 'rainy=(precip > 1);rainy_days=rainy.total($time)' in.nc out.nc
Lamoureux answered 23/4, 2020 at 17:54 Comment(1)
Thank you. I haven't used NCO, but I will check it out.Holmic
D
6

You can accomplish this task with CDO's masking function, for more details beyond the answer below, you can also refer to my video guide on masking using cdo.

The first step is to make an equivalent file with 1 if P>threshold (1mm/day in your case) and 0 otherwise. For this we use the "greater than or equal to a constant" gec function (or ge="greater than" if you prefer):

cdo gec,1 input.nc mask.nc 

(assuming units are mm/day in your input file).

Then you can simply sum this mask over the period (months, years etc) that you want your statistic

cdo monsum mask.nc nwetdays_mon.nc 
cdo yearsum mask.nc nwetdays_year.nc

Of course you can pipe this if you like to do this on one line: e.g.

cdo monsum -gec,1 input.nc nwetdays_mon.nc 

We can take this even further if you want to work out the climatology for a particular month. If you have a multiyear dataset then you can use the wonderful "ymonstat" commands. So for example, once you have calculated your monthly series of wet days above, you can calculate the average for each month with

cdo ymonmean nwetdays_mon.nc nwetdays_mon_clim.nc

You can then difference the series from this monthly climatology to give you the anomaly of wet days in each month over the series

cdo ymonsub nwetdays_mon.nc nwetdays_mon_clim.nc nwetdays_mon_anom.nc

I hope that helps!

(ps: I usually always find it is easier to calculate these kinds of statistics directly with CDO in this way, I rarely find that the built in climate functions calculate exactly the statistic as/how I want).

Downing answered 13/5, 2020 at 13:40 Comment(3)
Thank you. I was able to solve this the other day using the same approach.Holmic
sorry I was slow, missed this cdo-climate tagged question somehow.Downing
no worries, and thanks for the 'bonus' ymonmean section!Holmic
C
3

You can also do this in cf-python, essentially using the same methodology as the CDO example above, but in a Python environment, using the where and collapse methods:

import cf

# Read the dataset
f = cf.read('filename.nc')[0]

# Mask out dry days (assuming that your data
#                    units are 'mm day-1' or 'kg m-2 day-1', etc.)
wet = f.where(cf.le(1), cf.masked)

# If the data are in units of 'metres/day', say, then you could do:
#   wet = f.where(cf.le(0.001), cf.masked)
# or
#   wet = f.where(cf.le(1, 'mm day-1'), cf.masked)
# etc.

# Count the wet day occurrences by month
count_monthly = wet.collapse('T: sample_size', group=cf.M())

# Count the wet day occurrences by year
count_yearly = wet.collapse('T: sample_size', group=cf.Y())

# Get the data as numpy arrays
print(count_monthly.array)
print(count_yearly.array)


# Count the wet day totals by month
wet_day_sum_monthly = wet.collapse('T: sum', group=cf.M())

# Count the wet day totals by year
wet_day_sum_yearly = wet.collapse('T: sum', group=cf.Y())
Cullie answered 27/5, 2020 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.