Project

General

Profile

Help for adding leap days to multiyear daily netcdf file

Added by Kendy Silvério about 4 years ago

Hi dear all,
I have daily data (get it from link https://drive.google.com/file/d/1I9OeOPxBJV_tD7n3Ga-f8jl4DHdLZcop/view?usp=sharing) with no leap days over the period 1979-2005 (365day*27year=9855days). The problem is that I am using a script which consider leap days as I am dealing with forecasts. To this end, it is necessary to add to this data the leap days. I went through the internet and I found similar question (https://code.mpimet.mpg.de/boards/1/topics/3706), and I tried to go through the suggestions presented in just mentioned link, but the results seems somehow strange although it seems that the lead days have been added.

cdo -setmon,2 -selday,29 -selmon,1 mydata.nc aux_feb29.nc
cdo -mergetime mydata.nc aux_feb29.nc aux_2.nc. After that I tried to remove the leap days to get data with noleap days, it became impossible.

Is there any suggestion?

Thanks in advance for your guidance.

Sincerely,

KCS


Replies (1)

RE: Help for adding leap days to multiyear daily netcdf file - Added by Brendan DeTracey about 4 years ago

Kendy,
The time axis in that file is a dog's breakfast. It will give cdo indigestion.

        double time(time) ;
                time:standard_name = "time" ;
                time:units = "hours since 1-1-1 00:00:00" ;
                time:calendar = "standard" ;
                time:axis = "T" ;

It is counting hours since Jesus, and although you say it is a 365 day year the time attribute time:calendar is specified as standard. Your data file needs a small surgery that cdo alone can not perform. See the bash script below. First I reset the time axis to be relative to the start of the data. Then I use nco tool ncatted to manually change time:calendar to the correct value 365_day. Finally, I copy the file using cdo option -a which converts the time axis from relative to absolute. An absolute time axis is required in order to correctly select and timemerge your data. Finally, I set time:calendar back to standard which also converts the time axis back to relative. See section 1.5 in the cdo manual.

I hope that others find this example useful. Solution works in the bash shell on Linux and Cygwin. You must have NCO (http://nco.sourceforge.net/) installed for its ncatted command.

infile_bad='mydata.nc'
infile='mydata_365day.nc'
outfile='mydata_with_leap_years.nc'

isleap() { date -d $1-02-29 &>/dev/null && echo 0 || echo 1 ;}

cdo -setreftime,1979-01-01,00:00:00,days "$infile_bad" tmp1.nc
ncatted -a calendar,time,m,c,365_day tmp1.nc tmp2.nc
cdo -a copy tmp2.nc "$infile" 
#\rm tmp1.nc tmp2.nc

years=$(cdo -s showyear "$infile")

for year in $years; do
    echo "$year" 
    if [ $(isleap "$year") -eq 0 ] ; then
        echo "$year" 
          cdo  -setday,29 -setmon,2 -divc,2 -add -selday,28 -selmon,2 -selyear,"$year" "$infile" -selday,1 -selmon,3 -selyear,"$year" "$infile" tmp."$year".nc
    fi    
done
cdo mergetime "$infile" 'tmp.????.nc' tmp.nc
cdo setcalendar,'standard' tmp.nc "$outfile" 
#\rm tmp.nc
#\rm tmp.????.nc

    (1-1/1)