[ncl-talk] convert the time proleptic gregorian calendar to UTC date in NCL
Dave Allured - NOAA Affiliate
dave.allured at noaa.gov
Thu Dec 17 14:57:31 MST 2020
Your input time format is valid, but it is non-standard, so it must be
converted with code. Function *cd_calendar* does not understand this
format. Here is some code that creates two different formats, depending on
what you want to do next. One format is year, month, day in separate
arrays. The other is standard relative time offsets, like you would read
in from a CF conforming Netcdf data set.
Note that proleptic_gregorian is exactly the same as normal Gregorian
calendar in this time range.
Thanks to Dennis Shea for assisting with this reply.
load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/cd_string.ncl"
begin
; This part simulates the original input data, for demo only.
; It should be deleted and replaced with the original input code.
; This is a non-standard format, decimal coded integers plus fraction of
day.
time = (/18500131.99444444d0, 18500228.99444444d0 \
,20041031.99444444d0, 20041130.99444444d0 \
,20041231.99444444d0, 20050131.99444444d0 \
,20051130.99444444d0, 20051231.99444444d0 /)
time!0 = "time"
time at units = "day as %Y%m%d.%f" ; udunits does not understand this
time at calendar = "proleptic_gregorian"
; This part converts the decimal coded dates to year, month, day arrays.
YYYY = toint(time/10000)
MMDD = toint(time-YYYY*10000)
MM = MMDD/100
DD = MMDD-MM*100
frac = time-toint(time) ; this is fraction of day, 0.0 to 1.0
; Now convert the year, month, day arrays to CF standard relative times.
H = DD ; dummy time of day arrays = 00:00:00, will be replaced
H = 0
M = H
S = H
units2 = "days since 1850-1-1"
opt = 0
opt at calendar = "gregorian"
time2 = cd_inv_calendar (YYYY, MM, DD, H, M, S, units2, opt)
time2 = time2 + frac ; add fraction of day back in, no conversion
needed
print (time2)
; Decode final result, and display.
print ("")
strings = cd_string (time2, "%Y-%N-%D %H:%M:%S")
print (strings+"")
end
On Thu, Dec 17, 2020 at 6:15 AM Sri nandini via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:
> Hello dear ncl-users
>
> I am trying to convert the time proleptic_gregorian calendar in the time
> format of my data to UTC date, so that irs easier to extract the years
> to work with.
>
> The original print time output is this:
>
> Variable: time
> Type: double
> Total Size: 14976 bytes
> 1872 values
> Number of Dimensions: 1
> Dimensions and sizes: [time | 1872]
> Coordinates:
> time: [18500131.99444444..20051231.99444444]
> Number Of Attributes: 4
> standard_name : time
> units : day as %Y%m%d.%f
> calendar : proleptic_gregorian
> axis : T
> (0) 18500131.99444444
> (1) 18500228.99444444
> (2) 18500331.99444444
> (3) 18500430.99444444
> (4) 18500531.99444444
> (5) 18500630.99444444
> (6) 18500731.99444444
> (7) 18500831.99444444
> (8) 18500930.99444444
> (9) 18501031.99444444
> (10) 18501130.99444444
> (11) 18501231.99444444
> (12) 18510131.99444444
> (13) 18510228.99444444
> (14) 18510331.99444444
> (15) 18510430.99444444
> (16) 18510531.99444444
> (17) 18510630.99444444
> ................continued
>
> (1852) 20040531.99444444
> (1853) 20040630.99444444
> (1854) 20040731.99444444
> (1855) 20040831.99444444
> (1856) 20040930.99444444
> (1857) 20041031.99444444
> (1858) 20041130.99444444
> (1859) 20041231.99444444
> (1860) 20050131.99444444
> (1861) 20050228.99444444
> (1862) 20050331.99444444
> (1863) 20050430.99444444
> (1864) 20050531.99444444
> (1865) 20050630.99444444
> (1866) 20050731.99444444
> (1867) 20050831.99444444
> (1868) 20050930.99444444
> (1869) 20051031.99444444
> (1870) 20051130.99444444
> (1871) 20051231.99444444
>
> yrStrt = 1986
> yrLast = 2005
>
> f = addfile ("hist_1850-2005_wind10.nc", "r")
> time = f->time
> print(time) see above output
>
> ; convert the time proleptic_gregorian calendar to UTC date
> YYYY = cd_calendar(time,-1)/100 ;-- convert date to
> UT-referenced date
> ; Set date variable names
> year = tointeger(utc_date(:,0)) ;-- get year as
> integer value
> month = tointeger(utc_date(:,1)) ;-- get month as
> integer value
> day = tointeger(utc_date(:,2)) ;-- get day as
> integer value
> hour = tointeger(utc_date(:,3)) ;-- get hour as
> integer value
> minute = tointeger(utc_date(:,4)) ;-- get minute as
> integer value
> ; Write date as a string (YYYY.MM)
> date_str = sprinti("%0.4i",year) + "/" + sprinti("%0.2i", month)
> return(date_str)
>
> ;-- retrieve the indices of the selected years
>
> iYYYY = ind(YYYY.ge.yrStrt .and. YYYY.le.yrLast)
> print(iYYYY)
> hist_anom=f->wind10(iYYYY,:,:,:)
>
> but i get the error:
>
> CDMS error: Error on relative units conversion, string = day as %Y%m%d.%f
>
> CDMS error: Error on time conversion: invalid month = 10085
>
> Can someone help me to solve this?
>
> Sri
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20201217/9ffab1b2/attachment.html>
More information about the ncl-talk
mailing list