DKRZ NCL time conversion Julian to standard calendarΒΆ

Example script:

;---------------------------------------------------------------
; DKRZ NCL example script: convert_julian_calendar.ncl
;---------------------------------------------------------------
;-- open file
 f = addfile("data.nc","r")                        ;-- contains only variable prcp

;-- read variable and time
 var = f->prcp ;-- read variable
 time = f->T ;-- read time

;-- proof if units are in days;
;-- the date of the time units must be equal or earlier than the first time step
;-- ncdump -v T data.nc --> time(0) = 2444606 then use the Julian Calendar Converter
;-- http://aa.usno.navy.mil/data/docs/JulianDate.php

 rest = time(0) - floor(time(0))
 print("--> First timestep: "+time(0)+" rest: "+rest)
 if(rest .eq. 0) then
    print("--> days")
    time@units = "days since -4712-1-1 12:00"        ;-- set correct units of julian calendar
    new_units  = "days since 1981-01-01 12:00"       ;-- = time(0): 2444606
 else
    print("--> anything else")
    time@units = "hours since -4712-1-1 12:00"       ;-- set correct units of julian calendar
    new_units  = "hours since 1981-01-01 00:00"      ;-- = time(0): 2444606
 end if

;-- convert time to standard calendar; t1 = 2444606 is 1981-01-01 12:00
 t = cd_convert( time, new_units )

;-- define a correct time coordinate variable
 t!0 = "time"
 t&time = t
 t@units = t@units
 t@standard_name = "time"
 t@long_name = "time"
 t@calendar = "standard"
 t@axis = "T"

;-- overwrite time of variable
 var&T := t

;-- create new netCDF file and write variable and its associated dimension variables to file
 system("rm -rf prcp_julian_calendar.nc")        ;-- delete file if existing
 outf = addfile("prcp_julian_calendar.nc","c")   ;-- create file
 outf->prcp = var   ;-- write var to file