ex_netcdf_2.ncl#

Example script#

;---------------------------------------------------------------
;-- DKRZ NCL Light Hands-On                   EGU 2014   SPM1.12
;--
;-- Example script:       ex_netcdf_2.ncl        (basic script)
;--
;-- Settings:             convert temperature in Kelvin to Celsius,
;--                       write variable to new netCDF file,
;--                       netCDF 3
;--
;-- 2014-03-22   Karin Meier-Fleischer (meier-fleischer@dkrz.de)
;---------------------------------------------------------------
begin

  outfile = "t_in_Celsius_2.nc"

  if (isfilepresent(outfile)) then
    system("rm -rf "+outfile)          ;-- make sure that file does not exist
  end if

;-- open data file
  fin  = addfile("ECHAM5_OM_A1B_t_20010101.nc","r")  ;-- open data file

;-- get variable t and its dimensions and dimension sizes
  tK    =  fin->t                      ;-- get variable

  time  =  fin->time                   ;-- get dimension time
  lev   =  fin->lev                    ;-- get dimension lev
  lat   =  fin->lat                    ;-- get dimension lat
  lon   =  fin->lon                    ;-- get dimension lon

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

;-- convert variable t: Kelvin to Celsius
  tC           =  tK                   ;-- copy variable and its dimensions and attributes
  tC           =  tK - 273.15          ;-- convert from Kelvin to Celsius
  tC@units     = "Celsius"             ;-- define new units

;-- create new netCDF file
  fout = addfile(outfile,"c")

;-- begin output file settings
  setfileoption(fout,"DefineMode",True) ;-- explicitly declare file definition mode

;-- create global attributes of the file
  fAtt                  =  True        ;-- assign file attributes
  fAtt@title            = "NCL Efficient Approach to netCDF Creation"
  fAtt@source_file      = "ECHAM5_OM_A1B_t_20010101.nc"
  fAtt@Conventions      = "CF"
  fAtt@creation_date    =  systemfunc ("date")
  fAtt@history          =  "NCL script: ex_netcdf_2.ncl"
  fAtt@comment          = "Convert variable t: Kelvin to Celsius"
  fileattdef(fout,fAtt)                ;-- copy file attributes

;-- predefine the coordinate variables and their dimensionality
  dimNames = (/"time", "lev", "lat", "lon"/)
  dimSizes = (/ -1   ,  nlev, nlat,  nlon /)
  dimUnlim = (/ True , False, False, False/)
  filedimdef(fout,dimNames,dimSizes,dimUnlim)

;-- predefine the the dimensionality of the variables to be written out
  filevardef(fout, "time" ,typeof(time),getvardims(time))
  filevardef(fout, "lev"  ,typeof(lev), getvardims(lev))
  filevardef(fout, "lat"  ,typeof(lat), getvardims(lat))
  filevardef(fout, "lon"  ,typeof(lon), getvardims(lon))
  filevardef(fout, "tC"   ,typeof(tK),  getvardims(tK))

;-- copy attributes associated with each variable to the file
  filevarattdef(fout,"time" ,time)       ;-- copy time attributes
  filevarattdef(fout,"lev"  ,lev)        ;-- copy lev attributes
  filevarattdef(fout,"lat"  ,lat)        ;-- copy lat attributes
  filevarattdef(fout,"lon"  ,lon)        ;-- copy lon attributes
  filevarattdef(fout,"tC",   tC)         ;-- copy tC attributes

;-- explicitly exit file definition mode (not required)
  setfileoption(fout,"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.
  fout->time   =  (/time/)               ;-- write time to new netCDF file
  fout->lev    =  (/lev/)                ;-- write lev to new netCDF file
  fout->lat    =  (/lat/)                ;-- write lat to new netCDF file
  fout->lon    =  (/lon/)                ;-- write lon to new netCDF file
  fout->tC     =  (/tC/)                 ;-- write variable to new netCDF file

end