NCL Website header
NCL Home> Application examples> File IO || Data files for some examples

Example pages containing: tips | resources | functions/procedures > output netCDF

Output netCDF4 data with groups


Below is a script which demonstrates how to write NetCDF4 data with group.

 setfileoption("nc", "Format",  "NetCDF4")

 fn = "nc4uvt.nc"

 fi = addfile(fn, "r")

;printVarSummary(fi)
;print(fi)

 time = fi->time
 lev = fi->lev
 lat = fi->lat
 lon = fi->lon

 t = fi->T
 u = fi->U
 v = fi->V

;printVarSummary(t)
;printVarSummary(u)
;printVarSummary(v)

;print("t(0,0,0,0) = " + t(0,0,0,0))
;print("u(0,1,1,1) = " + u(0,1,1,1))
;print("v(0,2,2,2) = " + v(0,2,2,2))

;print("t&lat(0) = " + t&lat(0))
;print("t@units = " + t@units)
;print("u@units = " + u@units)
;print("v@units = " + v@units)

;u1 = fi->U(::2)
;v1 = fi->V(3:121:3)

 ntim  = dimsizes(time)	; get dimension sizes
 nlev  = dimsizes(lev)
 nlat  = dimsizes(lat)
 nlon  = dimsizes(lon)

;------------------------------------------------------------

 setfileoption("nc", "Format",  "NetCDF4")
 fon = "nc4_out_" + fn
 system("/bin/rm -f " + fon) ; remove if exists
 fo = addfile(fon, "c")

;===================================================================
; explicitly declare file definition mode. Improve efficiency.
;===================================================================
 setfileoption(fo,"DefineMode",True)

;setfileoption(fo,"CompressionLevel", 6)
;setfileoption(fo,"CacheSize", 3200000)
;setfileoption(fo,"CacheNelems", 1027)
;setfileoption(fo,"CachePreemption", 0.25)

; create global attributes of the file
;===================================================================
 fAtt               = True            ; assign file attributes
 fAtt@title         = "NCL generated netCDF file"
 fAtt@source_file   = fn
 fAtt@Conventions   = "None"
;fAtt@creation_date = systemfunc ("date")
 fileattdef(fo, fAtt)      ; copy file attributes

;===================================================================
; predefine the coordinate variables and their dimensionality
; Note: to get an UNLIMITED record dimension, we set the dimensionality
; to -1 (or the actual size) and set the dimension name to True.
;===================================================================
 dimNames = (/"time", "lev", "lat", "lon"/)
 dimSizes = (/ 1 ,   nlev,  nlat,  nlon /)
 dimUnlim = (/ True , False, False, False/)
 filedimdef(fo, dimNames, dimSizes, dimUnlim)

;===================================================================

 mtim = 1
 mlat = nlat/2
 mlon = nlon/2
 mlev = nlev/2
 chunkSizes = (/ mtim, mlev, mlat, mlon /)
;dimUnlim(0) = False
 filechunkdimdef(fo,dimNames,chunkSizes,dimUnlim)

;===================================================================

 grpnames = (/"grp1", "group2", "g3"/)

 filegrpdef(fo, grpnames)

; predefine the the dimensionality of the variables to be written out
;===================================================================
; Here we are using NCL functions to facilitate defining
; each variable's dimension name(s) and type.
;===================================================================
 filevardef(fo, "time", typeof(time), getvardims(time))
 filevarattdef(fo,"time", time)                   ; copy time attributes
 fo->time   = (/time/)

 filevardef(fo, "lev", typeof(lev), getvardims(lev) )
 filevarattdef(fo,"lev", lev)                     ; copy lev attributes
 fo->lev    = (/lev/)

 filevardef(fo, "lat", typeof(lat), getvardims(lat))
 filevarattdef(fo,"lat", lat)                     ; copy lat attributes
 fo->lat    = (/lat/)

 filevardef(fo, "lon", typeof(lon), getvardims(lon))
 filevarattdef(fo,"lon", lon)                     ; copy lon attributes
 fo->lon    = (/lon/)

 filevardef(fo, "T", typeof(t), getvardims(t))
 filevarattdef(fo,"T", t)                         ; copy T attributes
 filevarchunkdef(fo, "T", chunkSizes)
 filevarcompressleveldef(fo, "T", 2)
 fo->T      = (/t/)
;print(fo)
;printVarSummary(t)
;exit

 filevardef(fo, "U", typeof(u), getvardims(u))
 filevarattdef(fo,"U", u)                         ; copy U attributes
;filevarchunkdef(fo, "U", chunkSizes)
;filevarcompressleveldef(fo, "U", 4)
 fo->U      = (/u/)

 filevardef(fo, "V", typeof(v), getvardims(v))
 filevarattdef(fo,"V", v)                         ; copy V attributes
;filevarchunkdef(fo, "V", chunkSizes)
;filevarcompressleveldef(fo, "V", 6)
;filevarchunkcachedef(fo, "V", 3200000, 1027, 0.75)
 fo->V      = (/v/)

;===================================================================

 g1 = fo=>/grp1

 print(g1)

 fileattdef(g1, fAtt)
 filedimdef(g1, dimNames, dimSizes, dimUnlim)
 filechunkdimdef(g1,dimNames,chunkSizes,dimUnlim)

 filevardef(g1, "time", typeof(time), getvardims(time))
 filevarattdef(g1,"time", time)                   ; copy time attributes
 g1->time   = (/time/)

 filevardef(g1, "lev", typeof(lev), getvardims(lev) )
 filevarattdef(g1,"lev", lev)                     ; copy lev attributes
 g1->lev    = (/lev/)

 filevardef(g1, "lat", typeof(lat), getvardims(lat))
 filevarattdef(g1,"lat", lat)                     ; copy lat attributes
 g1->lat    = (/lat/)

 filevardef(g1, "lon", typeof(lon), getvardims(lon))
 filevarattdef(g1,"lon", lon)                     ; copy lon attributes
 g1->lon    = (/lon/)

 filevardef(g1, "T", typeof(t), getvardims(t))
 filevarattdef(g1,"T", t)                         ; copy T attributes
 filevarchunkdef(g1, "T", chunkSizes)
 filevarcompressleveldef(g1, "T", 2)
 g1->T      = (/t/)

 filevardef(g1, "U", typeof(u), getvardims(u))
 filevarattdef(g1,"U", u)                         ; copy U attributes
;filevarchunkdef(g1, "U", chunkSizes)
;filevarcompressleveldef(g1, "U", 4)
 g1->U      = (/u/)

 filevardef(g1, "V", typeof(v), getvardims(v))
 filevarattdef(g1,"V", v)                         ; copy V attributes
;filevarchunkdef(g1, "V", chunkSizes)
;filevarcompressleveldef(g1, "V", 6)
;filevarchunkcachedef(g1, "V", 3200000, 1027, 0.75)
 g1->V      = (/v/)

;===================================================================
 setfileoption(fo,"DefineMode",False)
;===================================================================
; output only the data values since the dimensionality and such have
; been predefined. The "(/", "/)" syntax tells NCL to only output the
; data values to the predefined locations on the file.
;====================================================================

 printVarSummary(g1)
;print(g1)
;exit

 printVarSummary(fo)
;print(fo)

;to = fo->T
;uo = fo->U
;vo = fo->V

;printVarSummary(to)
;printVarSummary(uo)
;printVarSummary(vo)

;print("to(0,0,0,0) = " + to(0,0,0,0))
;print("uo(0,1,1,1) = " + uo(0,1,1,1))
;print("vo(0,2,2,2) = " + vo(0,2,2,2))

;print("to&lat(0) = " + to&lat(0))
;print("to@units = " + to@units)
;print("uo@units = " + uo@units)
;print("vo@units = " + vo@units)



Use "ncl_filedump nc4_out_nc4uvt.nc" to check what is in the data file.