NCL Home> Application examples> Data Analysis || Data files for some examples

Example pages containing: tips | resources | functions/procedures

Time

This page shows some examples of dealing with "time" coordinates. All "date" functions are documented here.

See the time axis labels page for examples of creating nice time labels on the X or Y axis.

CAUTION: The yyyy-mm within a CESM file name and the time variable contained within the file may not necessarily agree with one another. This can be confusing and lead to erroneous temporal assignment(s). Consider a file named "CESM.sample.h.0280-01.nc". The file 'yyyy-mm' is 280-01 (January, 280).

 f = addfile("CESM.sample.h.0280-01.nc","r")
 time = f->time
 print(time)

     Variable: time
     Type: double
              [snip] 
     Dimensions and sizes:	[time | 1]
     Coordinates: 
              time: [102231..102231]
     Number Of Attributes: 4
       long_name :	time
     units :	days since 0000-01-01 00:00:00
     bounds :	time_bound
     calendar :	noleap
     
     (0)     102231

 date = cd_calendar(time, 0)                  
 print(date)
     
     Variable: date
     Dimensions and sizes:   [1] x [6]     <== yyyy,mm,dd,hh,mn,sec
     Coordinates: 
     Number Of Attributes: 1
     calendar :    noleap
    (0,0)   280         
    (0,1)    2 
              [snip] 

The variable 'time' indicates 280-02 or February, 280 which is not consistent with the file name of 280-01 or January, 280. How to deal with this? One approach is to use the 'time_bound' variable.

 time_bound = f->time_bound  
 print(time_bound)

     Variable: time_bound
     Type: double
     Total Size: 16 bytes
                 2 values
     Number of Dimensions: 2
     Dimensions and sizes:   [time | 1] x [d2 | 2]
     Coordinates: 
                 time: [102231..102231]
     Number Of Attributes: 2
       long_name :   boundaries for time-averaging interval
       units :       days since 0000-01-01 00:00:00

  (0,0)   102200     <==== beginning of the averaging time
  (0,1)   102231     <==== this is the same as 'time'

 time = (/ time_bound(:,0) /)          ; override values with lower bound  
 print(time)                           ; <=== 102200
 date = cd_calendar(time, 0) 
 print(date)                           

     Variable: date
     Type: float
     Total Size: 24 bytes
                 6 values
     Number of Dimensions: 2
     Dimensions and sizes:   [1] x [6]
     Coordinates: 
     Number Of Attributes: 1
       calendar :    noleap
  (0,0)   280
  (0,1)    1 

------------------------------------------------------------------------------>
------------------------------------------------------------------------------>
Another (better ?) approach is to average the time_bnd variable to get the
'center-of-mass' of the observations. 

 time       = f->time
 time_bound = f->time_bound  
 time       = (/ (time_bound(:,0)+time_bound(:,1))*0.5 /)  ; replace values with average

For NCL applications, one could reassign the 'time' coordinate variable
associated with a variable:

 temp       = f->TEMP
 temp&time  = (/ time  /)     ; reasign with 'center-of-mass' time values              

time_1.ncl: (a) Create an integer time coordinate variable of the type 199801,199802 etc for multiple years. (b) Convert to type string via tostring and sprinti. (c) Convert integer yyyymm to year and fraction of year for graphics via yyyymm_to_yyyyfrac. (d) Create a time variable with the units "hours since ...".
                yyyymm    _s1     _s2     time    yrfrac

          (0)	199801  199801  199801   859032   1998
          (1)	199802  199802  199802   859776   1998.08
          (2)	199803  199803  199803   860448   1998.17
          (3)	199804  199804  199804   861192   1998.25
          (4)	199805  199805  199805   861912   1998.33
          (5)	199806  199806  199806   862656   1998.42
          (6)	199807  199807  199807   863376   1998.5
          (7)	199808  199808  199808   864120   1998.58
          (8)	199809  199809  199809   864864   1998.67
          (9)	199810  199810  199810   865584   1998.75
          (10)	199811  199811  199811   866328   1998.83
          (11)	199812  199812  199812   867048   1998.92
          (12)	199901  199901  199901   867792   1999

          [SNIP]

          (35)	200012  200012  200012   884592   2000.92
          (36)	200101  200101  200101   885336   2001
          (37)	200102  200102  200102   886080   2001.08
          (38)	200103  200103  200103   886752   2001.17
          (39)	200104  200104  200104   887496   2001.25
          (40)	200105  200105  200105   888216   2001.33
          (41)	200106  200106  200106   888960   2001.42
          (42)	200107  200107  200107   889680   2001.5
          (43)	200108  200108  200108   890424   2001.58
          (44)	200109  200109  200109   891168   2001.67
          (45)	200110  200110  200110   891888   2001.75
          (46)	200111  200111  200111   892632   2001.83
          (47)	200112  200112  200112   893352   2001.92
time_2.ncl: Demonstrates using cd_calendar to convert a mixed Julian/Gregorian date to a UT-referenced date, and then demonstrates using cd_inv_calendar to go from a UT-referenced date to a mixed Julian/Gregorian date with a different reference time and units. The sprintf is used to ensure consistent formatting of the yrfrac variable.

          Variable: time                              ; original 'time'
          Type: double
          Total Size: 6176 bytes
                      772 values
          Number of Dimensions: 1
          Dimensions and sizes:   [time | 772]       
          Coordinates: 
                      time: [17067072..17630280]             ; range of values
          Number Of Attributes: 7
            units :       hours since 1-1-1 00:00:0.0        ; reference time
            long_name :   Time
            actual_range :        ( 17067072, 17630280 )
            delta_t :     0000-01-00 00:00:00
            prev_avg_period :     0000-00-01 00:00:00
            standard_name :       time
            axis :        T
          
          -----
          
          Variable: time2
          Type: double
          Total Size: 6176 bytes
                      772 values
          Number of Dimensions: 1
          Dimensions and sizes:   [time2 | 772]
          Coordinates: 
                      time2: [53690..77157]                 ; new reference range
          Number Of Attributes: 2
            calendar :    standard
            units :       days since 1801-1-1 00:00:0.0     ; new reference units


OUTPUT:
          
                    time    yyyymm  yyyymmdd  yyyymmddhh  yyyy.frac  time2
          (0)     17067072  194801  19480101  1948010100  1948.0000  53690
          (1)     17067816  194802  19480201  1948020100  1948.0847  53721
          (2)     17068512  194803  19480301  1948030100  1948.1639  53750
          (3)     17069256  194804  19480401  1948040100  1948.2486  53781
          (4)     17069976  194805  19480501  1948050100  1948.3306  53811
          (5)     17070720  194806  19480601  1948060100  1948.4153  53842
          (6)     17071440  194807  19480701  1948070100  1948.4973  53872

          [SNIP]

          (764)   17625168  201109  20110901  2011090100  2011.6658  76944
          (765)   17625888  201110  20111001  2011100100  2011.7479  76974
          (766)   17626632  201111  20111101  2011110100  2011.8329  77005
          (767)   17627352  201112  20111201  2011120100  2011.9151  77035
          (768)   17628096  201201  20120101  2012010100  2012.0000  77066
          (769)   17628840  201202  20120201  2012020100  2012.0847  77097
          (770)   17629536  201203  20120301  2012030100  2012.1639  77126
          (771)   17630280  201204  20120401  2012040100  2012.2486  77157
    
time_3.ncl: Demonstrates how to use the contributed.ncl function yyyymm_to_yyyyfrac to set up a monthly time array useful for plotting purposes. The top panel shows a random-generated timeseries plotted along an X-axis set up using yyyymm_to_yyyyfrac. The middle plot shows the result of using tmXBPrecision to set up the number of significant digits shown in the bottom X-axis tickmark labels. The bottom panel shows how to include carriage returns in tick mark labels such that the formatting is similar to:
     Jan    Feb
    2000   2000   
    
Carriage returns are created in text strings by utilizing your text function code surrounding a C. (ex. ~C~)