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

From: <brownrig_at_nyahnyahspammersnyahnyah>
Date: Wed Dec 04 2013 - 20:12:10 MST

Hi Yi-Chih,

Line 94 is your 1st script (which I presume is in file "vShear.ncl")
is:

     vShear_nmm (du_nmm,dv_nmm,duy_nmm)

Prior to this in that script, none of those arguments have ever been
defined/referenced, and hence the error. Am I correct that what you'd
like is for the procedure vShear_nmm to define and return those
variables populated with data back to you, i.e., vShear_nmm() works by
side-effects?

Well, yes and no. The following works by side-effect:

   procedure foo(a, b, c)
   begin
     a = 5
     b = 10
     c = 15
   end
  
   x = 1
   y = 2
   z = 3
   foo(x, y, z)
   print(x + "," + y + "," + z)

But that's because x/y/z are defined before going into the call to
foo(), *AND* they are of the same type (integer) as the assignments to
a/b/c inside foo.

To do something like that in a more general case, you'd have to know
in advance what the shape and types are of the variables
du_nmm/dv_nmm/duy_nmm *before* you make the call to vShear_nmm, and
define them in advance in the outer script to serve as placeholders
for calling vShear_nmm.

That seems painful, likely impossible to know in advance.

Alternatives might be:

i) three distinct *functions* to return what you want

ii) possibly the use of NCL's list-type in a function to return a
3-element list of what you want. Caveat: I think the list type can
hold elements of disparate type, but I'm not certain offhand. If not,
this won't work.

So -- hopefully that answers your question and helps...
Rick

On Thu, 5 Dec 2013 11:08:07 +0900
  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
Received on Wed Dec 4 20:12:18 2013

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