Re: Convert daily data to monthly......

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Mon Feb 22 2010 - 11:21:41 MST

Attached is an unsupported function:
    calculate_monthly_values

Note the the "time" coordinate variable associated
with the variable "x" must be recognized via "ut_calendar"

;---------------------------------
; undocumented
;
undef ("calculate_monthly_values")
function calculate_monthly_values (x:numeric, arith:string,
nDim[1]:integer, opt
[1]:logical)

; calculate monthly values [avg, sum, min, max]
; x: numeric array of 4D or less [eg: time,lev,lat,lon]
; *must* have time coordinate recognized by ut_calendar
; if 5D [case,time,lev,lat,lon]
; arith: "avg" [also, "ave"], "sum","min","max" others may be added
later
; nDim : scalar integer that specifies the 'time' dimension [generally, 0]
; opt : option ... not used here
;
; Sample usage: x(time,lat,lon) where time are n-hrly or daily values.
; xMonthAvg = calculate_monthly_values(x, "avg", 0, False)
; xMonthSum = calculate_monthly_values(x, "sum", 0, False)
; xMonthMin = calculate_monthly_values(x, "min", 0, False)
; xMonthMax = calculate_monthly_values(x, "max", 0, False)
;

Sabeerali(sebi) wrote:
> Dear ncl users,
>
> I have daily data from 1951 to 2008...I want to convert that data into
> monthly...NCL does has any function/script to convert daily data to
> monthly. ?
>
> Any help would be appreciated...
>
> --
> **********************************
> Sebi
> Climate and Global Modeling Division
> Indian Institute of Tropical Meteorology
> Pashan, Pune, 411 008
> ****************************************
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

;---------------------------------
; undocumented
;
undef ("calculate_monthly_values")
function calculate_monthly_values (x:numeric, arith:string, nDim[1]:integer, opt[1]:logical)

; calculate monthly values [avg, sum, min, max]
; x: numeric array of 4D or less [eg: time,lev,lat,lon]
; *must* have time coordinate recognized by ut_calendar
; if 5D [case,time,lev,lat,lon]
; arith: "avg" [also, "ave"], "sum","min","max" others may be added later
; nDim : scalar integer that specifies the 'time' dimension [generally, 0]
; opt : option ... not used here
;
; Sample usage: x(time,lat,lon) where time are n-hrly or daily values.
; xMonthAvg = calculate_monthly_values(x, "avg", 0, False)
; xMonthSum = calculate_monthly_values(x, "sum", 0, False)
; xMonthMin = calculate_monthly_values(x, "min", 0, False)
; xMonthMax = calculate_monthly_values(x, "max", 0, False)
;
local dimx, rankx, utc_date, year, month, day, hour, ntim \
    , yrStrt, yrLast, nyrs, NTIM, dAvg, xReturn, xMonth \
    , NT, nmo, ii

begin
    dimx = dimsizes( x )
    rankx = dimsizes( dimx )

    if (rankx.gt.5) then
        print("calculate_monthly_values: rankx="+rankx +" [only 5D or fewer supported]")
        exit
    end if

    utc_date = ut_calendar(x&time, 0)
    year = floattointeger(utc_date(:,0))
    month = floattointeger(utc_date(:,1))
    day = floattointeger(utc_date(:,2))
    hour = floattointeger(utc_date(:,3))
   ;minute = floattointeger(utc_date(:,4))
   ;second = utc_date(:,5)

    if (rankx.le.4) then
        ntim = dimx(0)
    else
        ntim = dimx(1)
    end if

    yrStrt = year(0)
    yrLast = year(ntim-1)
    nyrs = yrLast-yrStrt+1

    NTIM = 12*nyrs ; size of monthly files
    dAvg = dimx
    if (rankx.le.4) then
        dAvg(0)= NTIM
    else
        dAvg(1)= NTIM
    end if

    xReturn= new ( dAvg , typeof(x), getFillValue(x))
    xMonth = new ( NTIM , typeof(x&time), "No_FillValue")

    NT = -1
    do yr=yrStrt,yrLast
      do nmo=0,11

         NT = NT+1
         if (isvar("ii")) then ; keep this here!!
             delete(ii)
         end if
         ii = ind(yr.eq.year .and. (nmo+1).eq.month)

         if (.not.ismissing(ii(0))) then
             xMonth(NT) = (/ x&time(ii(0)) /)

             if (rankx.eq.1) then
                 if (arith.eq."avg" .or. arith.eq."ave") then
                     xReturn(NT) = dim_avg_n(x(ii) , nDim)
                 end if
                 if (arith.eq."sum") then
                     xReturn(NT) = dim_sum_n(x(ii) , nDim)
                 end if
                 if (arith.eq."min") then
                     xReturn(NT) = dim_min_n(x(ii) , nDim)
                 end if
                 if (arith.eq."max") then
                     xReturn(NT) = dim_max_n(x(ii) , nDim)
                 end if
             end if

             if (rankx.eq.2) then
                 if (arith.eq."avg" .or. arith.eq."ave") then
                     xReturn(NT,:) = dim_avg_n(x(ii,:) , nDim)
                 end if
                 if (arith.eq."sum") then
                     xReturn(NT,:) = dim_sum_n(x(ii,:) , nDim)
                 end if
                 if (arith.eq."min") then
                     xReturn(NT,:) = dim_min_n(x(ii,:) , nDim)
                 end if
                 if (arith.eq."max") then
                     xReturn(NT,:) = dim_max_n(x(ii,:) , nDim)
                 end if
             end if

             if (rankx.eq.3) then
                 if (arith.eq."avg" .or. arith.eq."ave") then
                     xReturn(NT,:,:) = dim_avg_n(x(ii,:,:) , nDim)
                 end if
                 if (arith.eq."sum") then
                     xReturn(NT,:,:) = dim_sum_n(x(ii,:,:) , nDim)
                 end if
                 if (arith.eq."min") then
                     xReturn(NT,:,:) = dim_min_n(x(ii,:,:) , nDim)
                 end if
                 if (arith.eq."max") then
                     xReturn(NT,:,:) = dim_max_n(x(ii,:,:) , nDim)
                 end if
             end if

             if (rankx.eq.4) then
                 if (arith.eq."avg" .or. arith.eq."ave") then
                     xReturn(NT,:,:,:) = dim_avg_n(x(ii,:,:,:) , nDim)
                 end if
                 if (arith.eq."sum") then
                     xReturn(NT,:,:,:) = dim_sum_n(x(ii,:,:,:) , nDim)
                 end if
                 if (arith.eq."min") then
                     xReturn(NT,:,:,:) = dim_min_n(x(ii,:,:,:) , nDim)
                 end if
                 if (arith.eq."max") then
                     xReturn(NT,:,:,:) = dim_max_n(x(ii,:,:,:) , nDim)
                 end if
             end if

             if (rankx.eq.5) then ; note the location of time
                 if (arith.eq."avg" .or. arith.eq."ave") then
                     xReturn(:,NT,:,:,:) = dim_avg_n(x(:,ii,:,:,:) , nDim)
                 end if
                 if (arith.eq."sum") then
                     xReturn(:,NT,:,:,:) = dim_sum_n(x(:,ii,:,:,:) , nDim)
                 end if
                 if (arith.eq."min") then
                     xReturn(:,NT,:,:,:) = dim_min_n(x(:,ii,:,:,:) , nDim)
                 end if
                 if (arith.eq."max") then
                     xReturn(:,NT,:,:,:) = dim_max_n(x(:,ii,:,:,:) , nDim)
                 end if
             end if
         end if

         delete(ii)
      end do ; month
    end do ; year

    if (rankx.le.4) then
        xReturn!0= "time"
    else
        xReturn!1= "time"
    end if

    xMonth@units = x&time@units
    xReturn&time = xMonth

    if (isatt(x,"long_name")) then
        xReturn@long_name = x@long_name
    end if

    if (isatt(x,"units")) then
        xReturn@units = x@units
    end if

    if (rankx.eq.3) then
        copy_VarCoords(x(0,:,:), xReturn(0,:,:))
    end if
    if (rankx.eq.4) then
        copy_VarCoords(x(0,0,:,:), xReturn(0,0,:,:))
    end if
    if (rankx.eq.5) then
        copy_VarCoords(x(:,0,0,:,:), xReturn(:,0,0,:,:))
    end if

    xReturn@operation_tag = "calculate_monthly_values: "+arith

    return( xReturn )
end

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Feb 22 11:21:46 2010

This archive was generated by hypermail 2.1.8 : Tue Feb 23 2010 - 08:26:41 MST