    ; --------------  LOAD FUNCTIONS AND PROCEDURES ----------------

    load "/usr/share/ncarg/nclscripts/csm/gsn_code.ncl"
    load "/usr/share/ncarg/nclscripts/csm/gsn_csm.ncl"
    load "/usr/share/ncarg/nclscripts/csm/contributed.ncl"
    load "/usr/share/ncarg/nclscripts/csm/shea_util.ncl"
    load "/usr/share/ncarg/nclscripts/wrf/WRFUserARW.ncl"
    load "/usr/share/ncarg/nclscripts/acentos.ncl"

    ; --------------  BEGINING OF NCL SCRIPT ----------------

    begin

    outfile = "press_heig_teste.nc"

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

    ;-- open data file

    in_u = addfile("224331.U.1985.20thC.U.nc","r")
    in_v = addfile("224331.V.1985.20thC.V.nc","r")
    in_z = addfile("239928.Z3.1985.20thC.Z3.nc","r")
    in_config = addfile("1975_20thC_V_ORIGINAL.nc","r")
    in_phis   = addfile("PHIS_1985.nc","r")
    in_ps   = addfile("223835.PS.1985.20thC.PS.nc","r")
    in_t = addfile("243069.T.1144.1985.20thC.T.nc","r")
    ; in_ps     = addfile("/media/leilane/Faggiani/Data/CMIP5/CCSM4/historical/atm/raw/UV/239928.PSL.1985.20thC.PSL.nc","r")
    ;-- get variable t and its dimensions and dimension sizes
    ;-- as variáveis estão em midpoints, logo tenho que hyai e hybrid

    hyai = in_config->hyai      ; hybrid a coefficients
    hybi = in_config->hybi      ; hybrid b coefficients
    PSFC = in_ps->PS
    P0mb = 0.01*in_config->P0   ; passa Pa para milibar
    PHIS = in_phis->PHIS
    PHIS = lonFlip(PHIS)        ; convert from 0:360 to -180:180
    phis_sub  = PHIS(1,{-62:12},{-73:0})  ; separando um subdomínio pq PHIS é global



    U = in_u->U ;(time, lev, lat, lon)                       ;-- get variable u
    V = in_v->V ;(time, lev, lat, lon)                       ;-- get variable v
    Z = in_z->Z3 ;(time, lev, lat, lon)                      ;-- get variable u
    T = in_t->T                          ; temperature at hybrid levels no nivel mais próximo do mar

    tbot = T(:,0,:,:)               ; bot temp level [clarity]
    nlev = dimsizes(hyai)

    lev_p   = (/ 1000., 995., 990., 985., 980. /)
    lev_p!0         = "lev_p"            ; variable and dimension name the same
    lev_p&lev_p     = lev_p              ; create coordinate variable
    lev_p@long_name = "pressure"         ; attach some attributes
    lev_p@units     = "hPa"
    lev_p@positive  = "down"

    intyp = 1                             ; 1=linear, 2=log, 3=log-log
    kxtrp = True                          ; True=extrapolate

    varflg = 1                           ; temperature is variable
    Tp     = vinth2p_ecmwf(T,hyai,hybi,lev_p,PSFC,intyp,P0mb, \
                   1,kxtrp,varflg,tbot,phis_sub)
    varflg = -1                          ; geo pot hgt is variable [tbot is used]
    Zp     = vinth2p_ecmwf(Z, hyai,hybi, lev_p ,PSFC, intyp, P0mb, \
                   1,kxtrp,varflg,tbot,phis_sub)
    varflg = 0                           ; not T or Z
    Up     = vinth2p_ecmwf(U,hyai,hybi,lev_p,PSFC,intyp,P0mb,\
                   1,kxtrp,varflg,tbot,phis_sub)


   ; Pressure to height
   xlvl = (/10, 50, 100 /) ; height levels
   xlvl@units = "m"

  ;  Reorder to interpolation

    Uh = Up(time|:,lat|:,lon|:,lev_p|:)

   linlog = -1 ; or 1 and negative for extrapolation
   uHeight = int2p_n(lev_p \
                  ,Uh(time|:,lat|:,lon|:,lev_p|:) \
                  ,xlvl,linlog,3)

   uHeight@long_name = U@long_name
   uHeight@units = U@units

   uHeight!0 = "time"
   uHeight!1 = "lat"
   uHeight!2 = "lon"
   uHeight!3 = "height"
   printVarSummary(uHeight)

   uHeight&time = U&time
   uHeight&lat  = U&lat
   uHeight&lon  = U&lon
   uHeight&height  = xlvl

   height = xlvl
   height!0 = "height"
   printVarSummary(height)

 ; Reorder
   uH = uHeight(time|:,height|:,lat|:,lon|:)
   delete(uHeight)


   time  =  in_u->time                    ;-- get dimension time
   lat   =  in_u->lat                     ;-- get dimension lat
   lon   =  in_u->lon                     ;-- get dimension lon

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


   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   ;-- 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      = "CCSM4.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
   nhei  =  dimsizes(height)
   dimNames = (/"time", "height", "lat", "lon"/)
   dimSizes = (/ -1   , nhei, 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, "height",typeof(height), getvardims(height))
   filevardef(fout, "lat"  ,typeof(lat), getvardims(lat))
   filevardef(fout, "lon"  ,typeof(lon), getvardims(lon))
   filevardef(fout, "uH" ,typeof(uH),  getvardims(uH))
   ;  filevardef(fout, "v1000p" ,typeof(v1000p),  getvardims(v1000p))

   ;-- copy attributes associated with each variable to the file
   filevarattdef(fout,"time" ,time)       ;-- copy time attributes
   filevarattdef(fout,"height",height)       ;-- copy time attributes
   filevarattdef(fout,"lat"  ,lat)        ;-- copy lat attributes
   filevarattdef(fout,"lon"  ,lon)        ;-- copy lon attributes
   filevarattdef(fout,"uH" ,uH)       ;-- copy u10m attributes
   ; filevarattdef(fout,"v1000p" ,v1000p)       ;-- copy v10m 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->height =  (/height/)
   fout->lat    =  (/lat/)                ;-- write lat to new netCDF file
   fout->lon    =  (/lon/)                ;-- write lon to new netCDF file
   fout->uH   =  (/uH/)               ;-- write variable to new netCDF file
   ; fout->v1000p   =  (/v1000p/)               ;-- write variable to new netCDF file

 end