NCL Home > Documentation > Functions > General applied math

calculate_daily_values

Calculate daily values [avg, sum, min, max] from high frequency temporal values.

Available in version 6.4.0 and later.

Prototype

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"  ; This library is automatically loaded
                                                             ; from NCL V6.2.0 onward.
                                                             ; No need for user to explicitly load.

	function calculate_daily_values (
		x         : numeric,  
		arith [1] : string,   
		nDim  [1] : integer,  
		opt   [1] : logical   
	)

	return_val  :  float or double array with with the same rank as x.

Arguments

x

Array containing the high frequency data. The x must have an associated time coordinate variable in units recognized by the cd_calendar function.

The following array structures are supported. The dimension name 'time' is a place-holder. Any name can be used. The nDim argument specifies the dimension number to be used.

       (time)                     ; nDim=0        
       (time,npts)
       (time,ny,nx) 
       (time,nz,ny,nx)
       (time,ne,nz,ny,nx)         ; nDim=0; added for NCL version 6.5.0 
       (ne,time,nz,ny,nx)         ; nDim=1

arith

A scalar string value which specifies the operation to be performed. Valid values are: "avg" [also, "ave"], "sum", "min", "max". It is required that x have associated with it a coordinate variable named time (ie, x&time) where "time" is recognized by cd_calendar.

nDim

The dimension of x on which to calculate the statistic. Currently, only nDim=0 or 1 is allowed.

opt

A logical variable to which various optional arguments may be assigned as attributes. These optional arguments alter the default behavior of the function. Must be set to True prior to setting the attributes which are assigned using the @ operator. Currently, there is one attribute.

  • If opt=True; the attribute 'nval_crit' (integer) will specify the minimum number of values need to calculate the desired statistic. If fewer values than 'nval_crit' are available, the result will be set to _fillValue. The default is 1 (one).

Return value

An array of the same rank as x.

Description

The function uses cd_calendar to extract the year, month, day and hour. All values for a particular day are used. NOTE: Use of cd_calendar means the units of time are something like: "days/hours/minutes/seconds since ...". If these units are not present as an attribute of 'time', the function will not work.

As an alternative to this function, consider using the Climate Data Operators (CDO). These operators will process all variables on the file. For example:

       cdo daymean foo_hourly.nc  foo_daily_mean.nc
       cdo daymin  foo_hourly.nc  foo_daily_min.nc
       cdo daymax  foo_hourly.nc  foo_daily_max.nc
       cdo daysum  foo_hourly.nc  foo_daily_sum.nc

See Also

dim_avg_n, dim_sum_n, dim_min_n, dim_max_n, calculate_monthly_values, calculate_segment_values

Examples

Example 1

Let x(time,lat,lon) where x&time is recognized by cd_calendar. The values of x may contain values separated by any time step.

              nDim    = 0          
              xDayAvg = calculate_daily_values(x, "avg", nDim, False)
              xDaySum = calculate_daily_values(x, "sum", nDim, False)
              xDayMin = calculate_daily_values(x, "min", nDim, False)
              xDayMax = calculate_daily_values(x, "max", nDim, False)

Example 2:

Read 4x daily values for one year (2008; a leap year) and calculate the daily means. The value are type 'short' with the attributes 'scale_factor' and 'add-offset'. The 'time' dimension is: nDim=0.

              f = addfile("foo.2008.nc","r")
              x = short2flt(f->air)
              printVarSummary(x)   ; [time | 1464] x [level | 17] x [lat | 73] x [lon | 144]

              opt = True
              opt@nval_crit = 4    ; require at least 4 values per day for a daily mean to be calculated. 

              xDay = calculate_daily_values (x, "avg", 0, opt)
              printVarSummary(xDay)

The output looks like:
              Variable: xDay
              Type: float
              Total Size: 8577792 bytes
                          2144448 values
              Number of Dimensions: 4
              Dimensions and sizes:   [time | 366] x [level | 17] x [lat | 73] x [lon | 144]
              Coordinates:
                          time: [17593032..17601072]
                          level: [1000..10]
                          lat: [90..-90]
                          lon: [ 0..357.5]
              Number Of Attributes: 18
                _FillValue :  32766
                missing_value :       32766
                long_name :   mean Daily Air temperature
                valid_range : ( 150, 350 )
                actual_range :        ( 178.73, 318.5 )
                units :       degK
                precision :   2
                least_significant_digit :     1
                GRIB_id :     11
                GRIB_name :   TMP
                var_desc :    Air temperature
                dataset :     NCEP Reanalysis Daily Averages
                level_desc :  Multiple levels
                statistic :   Mean
                parent_stat : Individual Obs
                _FillValue_original : 32766
                missing_value_original :      32766
                NCL_tag = calculate_daily_values: arith=avg

Example 3:

Read hourly values for spanning multiple files and calculate the daily and monthly means. The 'time' dimension is: nDim=0.


   diri    = "../"
   fili    = systemfunc("cd "+diri+" ; ls ACCESS_SRF.*.nc")  ; all files beginning with 'ACCESS_SRF'
   nfili   = dimsizes(fili)
   print(fili)

   varName = (/"snv" , "ts"/)
   nName   = dimsizes(varName)

   opt     = True
   opt@nval_crit = 10              ; require at least 10 values

   ndim    = 0

   f = addfiles(diri+fili, "r")   ; read variables from all files

   do nv=0,nName-1
      print("")
      print("-----------------------------------------------")
      print("-----------  "+varName(nv)+"  -----------------")
      print("-----------------------------------------------")
      print("")

      xhr := f[:]->$varName(nv)$                      ; (time,lat,lon) 
      printVarSummary(xhr)
      print("---")

      xdd := calculate_daily_values (xhr, "avg", ndim, opt)
      printVarSummary(xdd)
      print("---")

      xmm := calculate_monthly_values (xhr, "avg", ndim, opt)
      printVarSummary(xmm)
      print("---")
   end do


The output for the 'snv' variables is:
 

    Variable: fili
    Type: string
    Total Size: 16 bytes
                2 values
    Number of Dimensions: 1
    Dimensions and sizes:   [2]
    Coordinates: 
    (0)     ACCESS_SRF.1980010100.nc
    (1)     ACCESS_SRF.1980020100.nc                                           ; leap year (29 days in Feb.
    (0)     
    (0)     -----------------------------------------------
    (0)     -----------> snv <-----------------
    (0)     -----------------------------------------------
    (0)     
    
    Variable: xhr
    Type: float
    Total Size: 176238720 bytes
                44059680 values
    Number of Dimensions: 3
    Dimensions and sizes:   [time | 1440] x [iy | 141] x [jx | 217]           ; 1440 = (24*31)+(24*29)
    Coordinates: 
                time: [263713..265152]
                iy: [-1750000..1750000]
                jx: [-2700000..2700000]
    Number Of Attributes: 7
      long_name :   Liquid water equivalent of snow thickness
      standard_name :       lwe_thickness_of_surface_snow_amount
      units :       kg m-2
      coordinates : xlat xlon
      grid_mapping :        rcm_map
      cell_methods :        time: mean
      _FillValue :  1e+20
    
    Variable: xdd
    Type: float
    Total Size: 7343280 bytes
                1835820 values
    Number of Dimensions: 3
    Dimensions and sizes:   [time | 60] x [iy | 141] x [jx | 217]             ;   60 = (31+29)
    Coordinates: 
                time: [263713..265128]
                iy: [-1750000..1750000]
                jx: [-2700000..2700000]
    Number Of Attributes: 9
      _FillValue :  1e+20
      long_name :   Liquid water equivalent of snow thickness
      standard_name :       lwe_thickness_of_surface_snow_amount
      units :       kg m-2
      coordinates : xlat xlon
      grid_mapping :        rcm_map
      cell_methods :        time: mean
      time :        263713
      NCL_tag :     calculate_daily_values: arith=avg
    
    Variable: xmm
    Type: float
    Total Size: 244776 bytes
                61194 values
    Number of Dimensions: 3
    Dimensions and sizes:   [time | 2] x [iy | 141] x [jx | 217]              ;  2 months [Jan , Feb] 
    Coordinates: 
                time: [263713..264456]
                iy: [-1750000..1750000]
                jx: [-2700000..2700000]
    Number Of Attributes: 9
      _FillValue :  1e+20
      long_name :   Liquid water equivalent of snow thickness
      standard_name :       lwe_thickness_of_surface_snow_amount
      units :       kg m-2
      coordinates : xlat xlon
      grid_mapping :        rcm_map
      cell_methods :        time: mean
      time :        263713
      NCL_tag :     calculate_monthly_values: arith=avg