Re: can a procedure in NCL receive variables from another NCL script?

From: Dave Allured - NOAA Affiliate <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Wed Dec 04 2013 - 20:10:55 MST

Yi-Chih,

Since you are trying to return three variables from one call, it is more
complicated. You can fix your exising procedure call by pre-dimensioning
the three variables. However, it would be easier to change the procedure
to a function, and return three variables in an NCL list variable, because
then you do not need to pre-dimension. Please try the example below, and
you will need a recent version of NCL.

Documentation on NCL list variables:
http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclVariables.shtml#ListVariables

Also, there is a potential problem in your existing procedure vShear_nmm.
 To avoid possible conflicts, you should declare ALL local variables, latS
througy xyres, in the "local" statement at the beginning. You have
included only a few.

#######################
load "func1.ncl"
begin
  out_list = func1 ()
  xx = out_list[0]
  yy = out_list[1]
  printVarSummary (xx)
  printVarSummary (yy)
end

#######################
function func1 ()
local x, y
begin
  x = new ( (/ 5, 10 /), float)
  y = new ( (/ 6, 12 /), float)
  return ([/ x, y /])
end

--Dave

On Wed, Dec 4, 2013 at 7:08 PM, Yi-Chih Huang <dscpln@gmail.com> wrote:

> Hello,
>
> I have a basic question about procedure in NCL. Can a procedure in
> NCL receive new variables from another NCL script? I used a procedure
> vShear_nmm to obtain three variables by "vShear_nmm
> (du_nmm,dv_nmm,duy_nmm)" (line 94). However, I got an error message
> "fatal:Variable (du_nmm) is undefined". I wonder how to fix such an error?
>
> Thanks much,
>
> Yi-Chih
>
> ######
> fatal:Variable (du_nmm) is undefined
> fatal:["Execute.c":8128]:Execute: Error occurred at or near line 94 in
> file vShear.ncl
>
> ######
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "/fs3/yhuang/nmm/vShear.ncl"
>
> begin
> latS = -30.0
> latN = 30.0
> lonE = 30.0
> lonW = 160.0
>
> dir1= "/fs3/SysE_DB/nmm/CFSR/Monthly/Mean/"
> yrStrt = 1981
> yrLast = 2006
> year = ispan(yrStrt, yrLast,1) ; integer
> yrDir = year + "/" ; string
> nyr = dimsizes(year)
>
> varName= (/"uwnd","vwnd","lat"/)
>
> dir = str_concat(dir1+yrDir(0))
> fil = systemfunc("cd "+dir+" ; ls *wnd_850.nc")
> f = addfile (dir+fil(0), "r")
>
> x = f->$varName(0)$
> dims= getfilevardims(f,varName(0))
> xs = x(5:8,{latS:latN},{lonE:lonW}) ; JJAS tropical Pacific
> xu = dim_avg_n_Wrap( xs,0)
> ds = dimsizes(xs) ; assume the same dimension of u and v at
> 850 and 200 hPa
> xx = new((/nyr,ds(1),ds(2)/),float,"No_FillValue") ; so only create
> xx once
> YYYYMM= new ( (/nyr,ds(0)/), double)
> printVarSummary(YYYYMM)
>
> do nf= 0,1
> do ny= 0,nyr-1
> dir = str_concat(dir1+yrDir(ny))
> fil = systemfunc("cd "+dir+" ; ls *wnd_850.nc") ; input file
> name(s)
>
> f = addfile (dir+fil(nf), "r")
> x = f->$varName(nf)$
> dims = getfilevardims(f,varName(nf))
> xs := x(5:8,{latS:latN},{lonE:lonW}) ; Indian Ocean
> xy = dim_avg_n_Wrap( xs, 0 )
> xx(ny,:,:)= xy
> YYYYMM(ny,:) = cd_calendar(xs&$xs!0$, 1)
> xx!0 = "year"
>
> if ( ny .eq. nyr-1 .and. nf .eq. 0 ) then
> u850= dim_avg_n_Wrap( xx, 0 )
> else if ( ny .eq. nyr-1 .and. nf .eq. 1 ) then
> v850= dim_avg_n_Wrap( xx, 0 )
> end if
> end if
> end do
> end do
>
> do nf= 0,1
> do ny= 0,nyr-1
> fil = systemfunc("cd "+dir+" ; ls *wnd_200.nc") ; input file
> name(s)
>
> f = addfile (dir+fil(nf), "r")
> x = f->$varName(nf)$
> dims = getfilevardims(f,varName(nf))
> xs := x(5:8,{latS:latN},{lonE:lonW}) ; Indian Ocean
> xy = dim_avg_n_Wrap( xs, 0 )
> xx(ny,:,:)= xy
> xx!0 = "year"
>
> if ( ny .eq. nyr-1 .and. nf .eq. 0 ) then
> u200= dim_avg_n_Wrap( xx, 0 )
> else if ( ny .eq. nyr-1 .and. nf .eq. 1 ) then
> v200= dim_avg_n_Wrap( xx, 0 )
> end if
> end if
> end do
> end do
>
> du= u200 - u850
> dv= v200 - v850
> copy_VarMeta(u850,du)
> copy_VarMeta(v850,dv)
>
> printVarSummary(du)
> dulon= du(:,{65:95})
> duy = dim_avg_n_Wrap( dulon, 1 )
> printVarSummary(dulon)
> printVarSummary(duy )
> f = addfile (dir+fil(0), "r")
> la = f->$varName(2)$
> dims = getfilevardims(f,varName(2))
> lat = la({$dims(1)$|30:-30}) ; get latitudes in the domain
> printVarSummary(lat)
>
> vShear_nmm (du_nmm,dv_nmm,duy_nmm)
> wks= gsn_open_wks("X11","vShear_CFSR")
> gsn_define_colormap(wks,"BlueRed") ; use the BlueRed colormap
> (instead of default colormap)
> plot = new(2,graphic) ; create plot array
>
> res = True
> res@gsnAddCyclic = False
> res@mpMinLatF = -30
> res@mpMaxLatF = 30
> res@mpMinLonF = 30
> res@mpMaxLonF = 160
> res@cnFillOn = True
> res@gsnSpreadColors = True ; use full colormap
> res@gsnSpreadColorStart = 10 ; for BlueRed
> res@gsnSpreadColorEnd = 250 ; for BlueRed
> res@cnLevelSelectionMode= "ManualLevels"
> res@cnMinLevelValF = -30.
> res@cnMaxLevelValF = 50.
> res@cnLevelSpacingF = 4.
> res@cnSpanFillPalette = True
> res@cnLinesOn = False ; turn off the contour
> lines
>
> res@lbLabelBarOn = True
> res@pmLabelBarWidthF= 0.8
> res@lbLabelStride = 4
> res@mpFillOn = False
> res@mpOutlineBoundarySets = "National" ; turns on country
> boundaries.
> res@gsnLeftString = "CFSR: Average Monthly vertical shear of
> U-component during JJAS"
> res@cnFillDrawOrder = "Predraw"
>
> res@gsnDraw = False
> res@gsnFrame = False
> plot(0) = gsn_csm_contour_map(wks,du,res)
> plot(1) = gsn_csm_contour_map(wks,du_nmm,res)
> gsn_panel(wks,plot,(/2,1/),False)
>
> res@cnMinLevelValF = -14.
> res@cnMaxLevelValF = 10.
> res@cnLevelSpacingF = 1.
> res@gsnLeftString = "CFSR: Average Monthly vertical shear of
> V-component during JJAS"
> plot(0) = gsn_csm_contour_map(wks,dv,res)
> plot(1) = gsn_csm_contour_map(wks,dv_nmm,res)
> gsn_panel(wks,plot,(/2,1/),False)
>
> wks = gsn_open_wks ("X11","vShearXY_CFSR")
> xyres = True
> xyres@xyLineColor = "Red"
> xyres@tiMainString = "CFSR: Average Monthly Easterly shear during JJAS"
> x2res@xyLineColor = "Blue"
> ; plot = gsn_csm_xy (wks,duy&lat,duy({lat|-30:30}),xyres)
> plot =
> gsn_csm_xy2(wks,duy&lat,duy({lat|-30:30}),duy_nmm({lat|-30:30}),xyres,x2res)
> end
>
> ######
> procedure vShear_nmm (du_nmm,dv_nmm,duy_nmm)
> local dir1, dir, fil, f, x, xs, xu, xx, u200, u850, v200, v850
>
> begin
> latS = -30.0
> latN = 30.0
> lonE = 30.0
> lonW = 160.0
>
> dir1= "/fs3/SysE_DB/nmm/NMM/Monthly/Mean/"
> yrStrt = 1981
> yrLast = 2006
> year = ispan(yrStrt, yrLast,1) ; integer
> yrDir = year + "/" ; string
> nyr = dimsizes(year)
>
> dir = str_concat(dir1+yrDir(0))
> fil = systemfunc("cd "+dir+" ; ls *wnd_850.nc")
> f = addfile (dir+fil(0), "r")
>
> varName= (/"uwnd","vwnd","lat"/)
> x = f->$varName(0)$
> dims= getfilevardims(f,varName(0))
> xs = x(:,{latS:latN},{lonE:lonW}) ; JJAS tropical Pacific
> xu = dim_avg_n_Wrap( xs,0)
> ds = dimsizes(xs) ; assume the same dimension of u and v at 850 and
> 200 hPa
> xx = new((/nyr,ds(1),ds(2)/),float,"No_FillValue") ; so only create xx
> once
> YYYYMM= new ( (/nyr,ds(0)/), double)
> printVarSummary(xs)
>
> do nf= 0,1
> do ny= 0,nyr-1
> dir = str_concat(dir1+yrDir(ny))
> fil= systemfunc("cd "+dir+" ; ls *wnd_850.nc") ; input file name(s)
>
> f= addfile (dir+fil(nf), "r")
>
> x= f->$varName(nf)$
> dims = getfilevardims(f,varName(nf))
> xs := x(:,{latS:latN},{lonE:lonW}) ; Indian Ocean
> xy = dim_avg_n_Wrap( xs, 0 )
> xx(ny,:,:)= xy
> YYYYMM(ny,:) = cd_calendar(xs&$xs!0$, 1)
> xx!0 = "year"
>
> if ( ny .eq. nyr-1 .and. nf .eq. 0 ) then
> u850= dim_avg_n_Wrap( xx, 0 )
> else if ( ny .eq. nyr-1 .and. nf .eq. 1 ) then
> v850= dim_avg_n_Wrap( xx, 0 )
> end if
> end if
> end do
> end do
>
> do nf= 0,1
> do ny= 0,nyr-1
> fil= systemfunc("cd "+dir+" ; ls *wnd_200.nc") ; input file name(s)
>
> f= addfile (dir+fil(nf), "r")
>
> x= f->$varName(nf)$
> dims = getfilevardims(f,varName(nf))
> xs := x(:,{latS:latN},{lonE:lonW}) ; Indian Ocean
> xy = dim_avg_n_Wrap( xs, 0 )
> xx(ny,:,:)= xy
> xx!0 = "year"
>
> if ( ny .eq. nyr-1 .and. nf .eq. 0 ) then
> u200= dim_avg_n_Wrap( xx, 0 )
> else if ( ny .eq. nyr-1 .and. nf .eq. 1 ) then
> v200= dim_avg_n_Wrap( xx, 0 )
> end if
> end if
> end do
> end do
> print(YYYYMM(0:2,0:3))
>
> du_nmm= u200-u850
> dv_nmm= v200-v850
> copy_VarMeta(u850,du_nmm)
> copy_VarMeta(v850,dv_nmm)
>
> dulon = du_nmm(:,{65:95})
> duy_nmm = dim_avg_n_Wrap( dulon, 1 )
> f = addfile (dir+fil(0), "r")
> la = f->$varName(2)$
> dims = getfilevardims(f,varName(2))
> lat = la({$dims(1)$|30:-30}) ; get latitudes in the domain
> printVarSummary(du_nmm)
> printVarSummary(duy_nmm)
> printVarSummary(lat)
>
> wks= gsn_open_wks("X11","vShear_nmm")
> gsn_define_colormap(wks,"BlueRed") ; use the BlueRed colormap
> (instead of default colormap)
> plot = new(2,graphic) ; create plot array
>
> res = True
> res@gsnAddCyclic = False
> res@mpMinLatF = -30
> res@mpMaxLatF = 30
> res@mpMinLonF = 30
> res@mpMaxLonF = 160
> res@cnFillOn = True
> res@gsnSpreadColors = True ; use full colormap
> res@gsnSpreadColorStart = 10 ; for BlueRed
> res@gsnSpreadColorEnd = 250 ; for BlueRed
> res@cnLevelSelectionMode= "ManualLevels"
> res@cnMinLevelValF = -30.
> res@cnMaxLevelValF = 50.
> res@cnLevelSpacingF = 4.
> res@cnSpanFillPalette = True
> res@cnLinesOn = False ; turn off the contour
> lines
>
> res@lbLabelBarOn = True
> res@pmLabelBarWidthF= 0.8
> res@lbLabelStride = 4
> res@mpFillOn = False
> res@mpOutlineBoundarySets = "National" ; turns on country
> boundaries.
> res@gsnLeftString = "NMM: Average Monthly vertical shear of
> U-component during JJAS"
> res@cnFillDrawOrder = "Predraw"
>
> res@gsnDraw = False
> res@gsnFrame = False
> print("a")
> ; plot(0) = gsn_csm_contour_map(wks,du_nmm,res)
> print("b")
> res@cnMinLevelValF = -14.
> res@cnMaxLevelValF = 10.
> res@cnLevelSpacingF = 1.
> res@gsnLeftString = "NMM: Average Monthly vertical shear of
> V-component during JJAS"
> ; plot(1) = gsn_csm_contour_map(wks,dv,res)
> ; gsn_panel(wks,plot,(/2,1/),False)
>
> wks = gsn_open_wks ("X11","vShearXY_nmm")
> xyres = True
> xyres@tiMainString = "NMM: Average Monthly Easterly shear during JJAS"
> ; plot = gsn_csm_xy (wks,duy_nmm&lat,duy_nmm({lat|-30:30}),xyres)
> end
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Dec 4 20:11:08 2013

This archive was generated by hypermail 2.1.8 : Fri Dec 13 2013 - 11:39:30 MST