Project

General

Profile

Redistributing leap-day precipitation over February

Added by Fabian Stenzel almost 4 years ago

Hi everyone,

I want to remove the leap-days from a multi-year-daily precipitation netcdf input, while preserving the total rainfall.
Therefore I want to delete the leap days and redistribute the precipitation of that day to February 1-28.
I could do this with a large loop for example in R, but I was hoping for a faster solution via cdo.

Right now, I am stuck after extracting the leap-days.
cdo -selday,29 -selmon,2 prec_daily_1991_2000.nc prec_feb29.nc
cdo delete,month=2,day=29 prec_daily_1991_2000.nc prec_leapdays_deleted.nc

Is there a way to multiply a constant (in my case 1/28*Feb29prec) to only a selective range (Februaries of leapyears) in the nc while preserving the other values? I tried a few things, but either I multiply the constant to every time-frame, or only get the February-values as output, after the multiplication. A small loop in bash over the actual leap-days would be fine, while I suspect there might be an even easier ways, since "prec_feb29.nc" contains the leap-year-stamps already.

I appreciate any support,
Best Fabian


Replies (6)

RE: Redistributing leap-day precipitation over February - Added by Fabian Stenzel almost 4 years ago

I assume no answer means that there is no easy solution using cdo.
I will treat the precipitation with a separate script then.

Best,
Fabian

RE: Redistributing leap-day precipitation over February - Added by Ralf Mueller almost 4 years ago

hi Fabian!
First of all I would use the -del29feb operator to remove the corresponding days. Then I would select only the leap days with the commands you wrote above.

The the following might work:

  1. devide the values by 28
  2. use splityear to have yearly files
  3. use splityear on your original input
  4. loop over the leap years and add the small February values to those years with cdo -add ...
  5. use cdo -cat on all years to create a single output file again (if you need that)

hth
ralf

RE: Redistributing leap-day precipitation over February - Added by Fabian Stenzel almost 4 years ago

Dear Ralf,

thanks for your suggestion.
I replaced splityear with splityearmon and believe this works now (I attach my code in case someone else runs into the same issue):

cdo -del29feb prec_daily_1991_2000.nc prec_daily_1991_2000_feb29deleted.nc #create new target dataset with feb29 deleted
cdo -splityearmon prec_daily_1991_2000_feb29deleted.nc prec_daily_1991_2000_feb29deleted_splityearmon_ #split into months to only add the values in feb
cdo -selday,29 -selmon,2 prec_daily_1991_2000.nc prec_daily_1991_2000_feb29.nc #extract leapday values
cdo -divc,28 prec_daily_1991_2000_feb29.nc prec_daily_1991_2000_feb29_div28.nc #divide by 28 to get the additive for each feb-day
cdo -splityear prec_daily_1991_2000_feb29_div28.nc prec_daily_1991_2000_feb29_div28_splityear_ #split up to treat each leapyear separately
for y in `ls *div28_splityear_*` #loop over all leapyear-files
do 
  year=${y:${#y}-7:4} #extract the year
  cdo -add prec_daily_1991_2000_feb29_div28_splityear_${year}.nc prec_daily_1991_2000_feb29deleted_splityearmon_${year}02.nc prec_daily_1991_2000_feb29deleted_splityearmon_${year}02_treated.nc #add a 1/28th of the leapday-precipitation to only the leapyear feb values
  mv prec_daily_1991_2000_feb29deleted_splityearmon_${year}02_treated.nc prec_daily_1991_2000_feb29deleted_splityearmon_${year}02.nc #overwrite the original feb values
done
cdo -cat *splityearmon*.nc prec_daily_1991_2000_final.nc #merge the months and years again

Additionally, I noticed that the file size after applying del29feb goes up by nearly a factor of 2. How does that come? Is the float precision increasing?

Again, thank you so much Ralf.

Best,
Fabian

RE: Redistributing leap-day precipitation over February - Added by Ralf Mueller almost 4 years ago

difference in file size can occur when you use compresses netcdf input. usually the CDO output is uncompressed.

don't you get months twice in your output by the last command?

RE: Redistributing leap-day precipitation over February - Added by Fabian Stenzel almost 4 years ago

I see - well for now I won't bother about compressing the files, thanks!

I don't see why I would get months twice - they are overwritten by the mv command.
So what I get looks fine!

RE: Redistributing leap-day precipitation over February - Added by Ralf Mueller almost 4 years ago

cool - I was just guessing because I cannot reproduce the output with the input. But if you are happy with it, it fine for me, of course.

cheers
ralf

    (1-6/6)