Project

General

Profile

convert monthly averages to monthly totals

Added by Stuart Brown over 4 years ago

Hi, I have some monthly precipitation data (12 months) which is recorded in kg.m-2.s-1 averaged over the entire month - i.e. the average daily precipitation for the month.

Can I use CDO to convert these into monthly totals? At the moment I simply multiply the values by the number of seconds in a month (2.628e+6):

cdo mulc,2.628e+6 avgPrec.nc sumPrecip.nc

But this obviously assumes that all months are ~30.4 days long - is there a way to do this whereby the number of seconds depends on which month is being processed? So a 31 day month would be multiplied by 2.678e+6, February (28 days) would be multiplied by 2.419e+6

Thanks.


Replies (1)

RE: convert monthly averages to monthly totals - Added by Ralf Mueller over 4 years ago

Hi Stuart!

Here is what I would do:

  • check with with calendar the data was created: 360days or 365days, with/without leap years
  • compute a 1d-timeseries of seconds that represent the length of each month in your input
  • enlarge this timeseries to the input grid with
    cdo -enlarge ...
  • multiply this with your input by
    cdo mul ...

for the computation of seconds, I think the datetime libraries of python or ruby should be a good choice. here is a ruby example using the normal calendar

require 'time'
years  = (2001..2009)
months = (1..12)

years.each {|year|
  months.each {|month|

    dateStart = DateTime.new(year,month,1,0,0,0)
    if 12 != month then
      dateEnd = DateTime.new(year,month+1,1,0,0,0)
    else
      dateEnd = DateTime.new(year+1,1,1,0,0,0)
    end

    diffInSeconds = dateEnd.strftime('%s').to_i - dateStart.strftime('%s').to_i
    pp [diffInSeconds, dateStart.to_s, dateEnd.to_s]
  }
}

python should look very similar.

hth
ralf

m2s.rb (428 Bytes) m2s.rb
    (1-1/1)