Search code examples
netcdfcdo-climate

Calculate monthly average and annual sum of precipitation from netcdf with CDO


I am learning netCDF and CDO with a CRU ts_4.04 data. I want to calculate the monthly average and annual sum of precipitation in London. I wrote:

#!/usr/bin/bash
lon=-0.11
lat=51.49
PREFNAME="/myHD/CRU4.04/pre/cru_ts4.04.1901.2019.pre.dat.nc"
OUTFNAME="outfile-"
echo "1970-2000 monthly average and annual sum of precipitations in London"
cdo remapnn,lon=$lon/lat=$lat $PREFNAME               $OUTFNAME"place.nc"
cdo selvar,pre                $OUTFNAME"place.nc"     $OUTFNAME"var.nc"
cdo selyear,1970/2000         $OUTFNAME"var.nc"       $OUTFNAME"years.nc"
cdo ymonmean                  $OUTFNAME"years.nc"     $OUTFNAME"yearsmean.nc"
cdo timcumsum                 $OUTFNAME"yearsmean.nc" $OUTFNAME"yearsum.nc"
cdo info $OUTFNAME"yearsum.nc"
cdo info $OUTFNAME"yearsmean.nc"
exit

I get:

MyPC:~/workbench01$ ./gotit2.sh 
1970-2000 monthly average and annual sum of precipitations in London
cdo    remapnn: Nearest neighbor weights from lonlat (720x360) to lonlat (1x1) grid, with source mask (67420)
cdo    remapnn: Processed 2 variables over 1428 timesteps [2.64s 60MB].
cdo    selname: Processed 2 variables over 1428 timesteps [0.01s 52MB].
cdo    selyear: Processed 1 variable over 1428 timesteps [0.00s 51MB].
cdo    ymonmean: Processed 1 variable over 372 timesteps [0.00s 51MB].
cdo    timcumsum: Processed 1 variable over 12 timesteps [0.00s 51MB].
    -1 :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter ID
     1 : 2000-01-16 00:00:00       0        1       0 :                  81.229             : -1            
     2 : 2000-02-15 00:00:00       0        1       0 :                  132.26             : -1            
     3 : 2000-03-16 00:00:00       0        1       0 :                  189.70             : -1            
     4 : 2000-04-16 00:00:00       0        1       0 :                  244.82             : -1            
     5 : 2000-05-16 00:00:00       0        1       0 :                  298.52             : -1            
     6 : 2000-06-16 00:00:00       0        1       0 :                  356.92             : -1            
     7 : 2000-07-16 00:00:00       0        1       0 :                  402.39             : -1            
     8 : 2000-08-16 00:00:00       0        1       0 :                  456.03             : -1            
     9 : 2000-09-16 00:00:00       0        1       0 :                  527.19             : -1            
    10 : 2000-10-16 00:00:00       0        1       0 :                  605.04             : -1            
    11 : 2000-11-16 00:00:00       0        1       0 :                  682.58             : -1            
    12 : 2000-12-16 00:00:00       0        1       0 :                  762.59             : -1            
cdo    info: Processed 1 variable over 12 timesteps [0.00s 50MB].
    -1 :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter ID
     1 : 2000-01-16 00:00:00       0        1       0 :                  81.229             : -1            
     2 : 2000-02-15 00:00:00       0        1       0 :                  51.032             : -1            
     3 : 2000-03-16 00:00:00       0        1       0 :                  57.439             : -1            
     4 : 2000-04-16 00:00:00       0        1       0 :                  55.116             : -1            
     5 : 2000-05-16 00:00:00       0        1       0 :                  53.700             : -1            
     6 : 2000-06-16 00:00:00       0        1       0 :                  58.400             : -1            
     7 : 2000-07-16 00:00:00       0        1       0 :                  45.471             : -1            
     8 : 2000-08-16 00:00:00       0        1       0 :                  53.642             : -1            
     9 : 2000-09-16 00:00:00       0        1       0 :                  71.161             : -1            
    10 : 2000-10-16 00:00:00       0        1       0 :                  77.845             : -1            
    11 : 2000-11-16 00:00:00       0        1       0 :                  77.545             : -1            
    12 : 2000-12-16 00:00:00       0        1       0 :                  80.006             : -1            
cdo    info: Processed 1 variable over 12 timesteps [0.00s 51MB].

It looks very well, but they aren't the same results as shown in https://climatecharts.net/ for London between 1970-2000 years with CRU ts4.04.

My question is: Am I calculating the monthly average and the yearly sum of precipitations?.

Thank you for any help.


Solution

  • cdo function:

    ymonmean 
    

    calculates the average of each calendar month, i.e. the average of all the Januarys, the average of all the February etc... the resulting file will have 12 time steps.

    timcumsum
    

    then produces the cumulative sum over these 12 steps. So step 1 is still your January average, then the 2nd step has the sum of the January and February averages and so on, the resulting file still has 12 timesteps, and the result you need should be the last step.

    However, if you simply want to know what the mean average annual rainfall is, then you can simply calculate it directly with

    cdo yearsum in.nc out.nc  # calculate total for each year
    cdo timmean out.nc year_average.nc # average over the totals
    

    or piped using one line:

    cdo timmean -yearsum in.nc year_average.nc
    

    A word of warning with the above, make sure your series has full calendar years. If the first year starts in e.g. July, then that year's sum would obviously only have 6 months worth of rainfall, which would impact your statistics, likewise with the final month of the final year.

    Last thing, I see on the data page of the climatecharts that it uses observations directly and not the gridded cru, so you can anyway not expect the results to be exactly the same.

    Edit 2021: I have now made video guides on these topics:

    Calculating temporal statistics

    Calculating diurnal and seasonal cycles