[ncl-talk] Dynamic Variable Names (with unique prefix/suffix)

Mary Haley haley at ucar.edu
Tue Oct 20 16:11:32 MDT 2015


AR,

You can't do dynamic variables the way you've written it, but there may be
a way to do what you want, if I could know more about what you need to do
with such a variable. The special "$...$" type of syntax can be used when
referencing file variables, both reading and writing.

For example, if you need to write the results of stat_dispersion to a
NetCDF file, then you might do something like:

;------------------------------------------------------
;  OPEN NEW NETCDF FOR WRITING
;------------------------------------------------------
  output_filename = "test.nc"
 system("rm -rf " + output_filename)
 fout = addfile(output_filename,"c")

;------------------------------------------------------
;  STAT DISPERSION
;------------------------------------------------------
 opt = True
 opt at PrintStat = True

 vars = (/"SNOWFALL","SNOWC","SWE","TSA"/)
 elev = (/"500","1000","1500","2000"/)
 nvars = dimsizes(vars)-1
 nelev = dimsizes(elev)-1

 do n=0,nvars
 do k=0,nelev
   varname = "Sierra_STAT_VR_CESM_28_HIST_"+vars(n)+elev(k)
 print("VR-CESM28 - HISTORICAL - AVG DJF Sierra Nevada "+elev(k)+"
"+vars(n)+"")

;---Write calculation directly to NetCDF file
 fout->$varname$ =
stat_dispersion(oned_+vars(n)+_VR_CESM_28_HIST_+elev(k)+,opt)

 end do
 end do

Note that this is a very inefficient way to write a NetCDF file. Mostly I
just want to show how to use the $...$ syntax.

If this doesn't answer your question, please post back to ncl-talk.

--Mary



On Tue, Oct 20, 2015 at 12:33 PM, Alan Rhoades <alan.m.rhoades at gmail.com>
wrote:

> Hello,
>
> I'm currently trying to make dynamic variable names within NCL (i.e., with
> unique prefix and/or suffix, but consistent patterns for each variable).
> I've tried to track down a solution for my current problem via the NCL
> archives, but can't seem to find it.  A post in 2010 said that this type of
> NCL scripting wasn't yet possible, but I was curious if an update had
> occurred.
>
> I'm trying to do the following...
>
> Assess several different mountain ranges
> With four different variables (SNOWFALL, SNOWC, SWE, TSA)
> Across four different elevation classes (500 m, 1000 m, 1500 m, 2000 m)
>
> This is my current stab at making the dynamic variable names within NCL,
> but of course, it isn't working (although the print statement is working,
> the NCL functions just don't like the syntax used to name the dynamic
> variable names).  Here is a snippet of the code...
>
> ;------------------------------------------------------
> ;  STAT DISPERSION
> ;------------------------------------------------------
>  opt = True
>  opt at PrintStat = True
>
>  vars = (/"SNOWFALL","SNOWC","SWE","TSA"/)
>  elev = (/"500","1000","1500","2000"/)
>  nvars = dimsizes(vars)-1
>  nelev = dimsizes(elev)-1
>
>  do n=0,nvars
>  do k=0,nelev
>  print("VR-CESM28 - HISTORICAL - AVG DJF Sierra Nevada "+elev(k)+"
> "+vars(n)+"")
>  Sierra_STAT_VR_CESM_28_HIST_+vars(n)+elev(k)+ =
> stat_dispersion(oned_+vars(n)+_VR_CESM_28_HIST_+elev(k)+,opt)
>
>  end do
>  end do
>
> Do you have any advice on how to move forward on this?  I have heard that
> it is possible to read in variable names via a shell script into NCL, but I
> can't track down an example of how to do this either.
>
> Thanks in advance,
>
> AR
>
> --
>
> *Alan Rhoades*
> *PhD Student, Atmospheric Science Graduate Group*
> *Climate Change Water and Society (CCWAS) NSF IGERT Trainee*
> *University of California, Davis*
> *LinkedIn <https://www.linkedin.com/pub/alan-rhoades/22/5bb/52a>*
> *alan.m.rhoades at gmail.com <alan.m.rhoades at gmail.com> *
> *amrhoades at ucdavis.edu <amrhoades at ucdavis.edu>*
>
> *"It’s all really there. That’s what really gets you. But you gotta stop
> and think about it to really get the pleasure about the complexity, the
> inconceivable nature of nature."*
> *Richard Feynman*
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20151020/1a0da01c/attachment.html 


More information about the ncl-talk mailing list