[ncl-talk] Time dimension in output to NetCDF

Dr. Muhammad Afzaal afzaalkarori at gmail.com
Thu Nov 30 02:06:18 MST 2017


Dear NCL users

I am using NCL 6.4.0 on ubuntu. I need to write a variable to netcdf file.
I did it successfully using Method 1 given on ncl homepage. I have little
problem that when I open nc file in grads, the time is not realistic. I
mean its like;

Time values set: 2320:6:25:0 2320:6:25:0

while the variable has the time from 190101 to 201312

Can someone please help me how can add time dim as that attached with the
define variable? My code is given below

;====================================================
begin

  diri   = "./"
  fili   = "precip.mon.total.v7.nc"
  f      = addfile(diri+fili, "r")

  prc    = flt2dble(f->precip(:,{12:33},{30:60}))
  pmsg   = prc at _FillValue      ; convenience

;*********************************
; plot parameters
;*********************************
  dimprc = dimsizes(prc)
  ntim   = dimprc(0)
  nlat   = dimprc(1)
  mlon   = dimprc(2)

  tim  = f->time
  utc_date = cd_calendar(tim, 0)
  year    = tointeger(utc_date(:,0))
  yrStrt  = year(0)
  yrLast  = year(ntim-1)
  nyear   = yrLast-yrStrt+1
;print(year + " " + yrStrt + " "+ yrLast +" "+ nyear)
  yyyymm  = yyyymm_time(yrStrt, yrLast, "integer")

  yrfrac  = (/ yyyymm_to_yyyyfrac(yyyymm, 0.0) /)
  prc&time = (/ yyyymm /)

;===============================================================
;write to nc file
;================================================================

    system("/bin/rm -f prc_3mon.nc") ;remove any pre-existing file
    ncdf = addfile("prc_3mon.nc" ,"c") ; open output netCDF file

;===============================================================
; create global attributes of the file (optional)
;================================================================
       fAtt               = True        ; assign file attributes
       fAtt at title         = "GPCC Rainfall"
       fAtt at source_file   = "precip.mon.total.v7.nc"
       fAtt at Conventions   = "None"
       fAtt at creation_date = systemfunc ("date")
       fileattdef( ncdf, fAtt )           ; copy file attributes

;===============================================================
; make time an UNLIMITED dimension;recommended for most applications
;===============================================================

       filedimdef(ncdf,"time",-1,True)

;===============================================================
; output variables directly; NCL will call appropriate functions
; to write the meta data associated with each variable
;===============================================================
       ncdf->spi  = spi                          ; 3D
end


Regards
*Dr. MUHAMMAD AFZAAL KARORI*
Pakistan Meteorological Department
Sector H-8/2, Pitras Bukhari Road,
Islamabad - 44000
Pakistan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20171130/163903b7/attachment.html>


More information about the ncl-talk mailing list