[[IT memo]]
[[IT memo/CDO memo]]

* NCL memo [#fc2be665]

#contents

*** Vertical interpolation of CCSM3.0 hybrid output [#re7a0284]

 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
 ;************************************************
 begin
 ;************************************************
 ; file handling
 ;************************************************
   dec = "198"
   yr = "5"
   year = dec+yr
   fnps = "fromesg/tmpPS"+dec+"0s_00"+yr+".nc"
   fn  = "fromesg/b30.030e.cam2.h3.U."+year+"-01-01_cat_"+year+"-12-31.nc" ; define filename
   inU  = addfile(fn,"r")                         ; open netcdf file
   inPS  = addfile(fnps,"r")                         ; open netcdf file
   fnout = "fromesg/b30.030e.cam2.h3.UPRS."+year+"-01-01_cat_"+year+"-12-31.nc"
   system("/bin/rm -f "+fnout)
   fout = addfile(fnout,"c")
 ;************************************************
 ; read needed variables from file
 ;************************************************
   U = inU->U                                    ; select variable
   P0mb =1000.
   hyam = inU->hyam                             ; get a coefficients
   hybm = inU->hybm                              ; get b coefficients
   PS   = inPS->PS                                ; get sfc pressure
 ;************************************************
 ; define other arguments required by vinth2p
 ;************************************************
 ; type of interpolation: 1 = linear, 2 = log, 3 = loglog
   interp = 2
 ; is extrapolation desired if data is outside the range of PS
   extrap = False
 ; create an array of desired pressure levels:
   pnew = (/1000.0,925,850.0,700.0,600,500.0,400,300.,250,200.0,150,100,70,50,30,20,10 /)
 ;************************************************
 ; calculate T on pressure levels
 ;************************************************
 ; note, the 7th argument is not used, and so is set to 1.
 ;************************************************
   uprs = vinth2p(U,hyam,hybm,pnew,PS,interp,P0mb,1,extrap)
   filedimdef(fout,"time",-1,True)
   uprs!0 = "time"
   uprs!1 = "lev"
   uprs!2 = "lat"
   uprs!3 = "lon"
   pnew@units = "hPa" ; required for grads compatible
   uprs&lev = pnew
   fout->uprs = uprs
 end
*** Other tips [#pefef7a3]
- file status (e.g. addfile)

- file status (e.g. addfile)~
"r": read only, "w": writable, "c": create
- Creating GrADS-readable netCDF file

- Creating GrADS-readable netCDF file~
x,y,z variables (i.e., "lon", "lat", "lev") should have attribution of their units. Usually, lon and lat will be ok. For lev, you need to do something like this below. (pnew is vertical pressure level and ugrd is a 4D variable)
   uprs!0 = "time"
   uprs!1 = "lev"
   uprs!2 = "lat"
   uprs!3 = "lon"
   pnew@units = "hPa" 
   uprs&lev = pnew
   fout->uprs = uprs