[ncl-talk] flexible variable names in new

Alan Brammer abrammer at albany.edu
Fri Jun 30 08:41:52 MDT 2017


You can save variables to netcdf with dynamic names:
ncdf->$tostring("var"+i)$ = some_variable

But you can’t have dynamic variable names in the script.  The closet would be attaching attributes with dynamic names. e.g.

x = ispan(0,5,1)
holder = True
do i=0,5
   holder@$tostring("var"+i)$ = x(i)
end do

>  Variable: holder
>  Type: logical
>  Total Size: 4 bytes
>              1 values
>  Number of Dimensions: 1
>  Dimensions and sizes:	[1]
>  Coordinates:
>  Number Of Attributes: 6
>    var5 :	5
>    var4 :	4
>    var3 :	3
>    var2 :	2
>    var1 :	1
>    var0 :	0
>  (0)	True


This is could get messy pretty quickly though. 

Google "ncl-talk dynamic variable names” and you’ll find a bunch of old threads asking similar questions.  
It usually boils down to why would you want this?  For output there is the method above, otherwise save variables in an array, the names are somewhat irrelevant.  Also I imagine trying to debug a script with unknown variable names would be a nightmare. 


Good luck, 

Alan



##############################
Alan Brammer,
Post-Doc Researcher

Department of Atmospheric and Environmental Sciences,
University at Albany, State University of New York, Albany, NY, 12222
abrammer at albany.edu
##############################

> On 30 Jun 2017, at 10:01, Michael Notaro <mnotaro at wisc.edu> wrote:
> 
> Is there any way to create new variables with flexible names?  For example,
> 
> do i=0,5
>   var$i$= ........
> end do
> 
> So that would (if done right) define new variables named var0, var1, var2, var3, var4, and var5.
> 
> Michael
> 
> Michael Notaro
> Associate Director
> Nelson Institute Center for Climatic Research
> University of Wisconsin-Madison
> Phone: (608) 261-1503
> Email: mnotaro at wisc.edu <mailto:mnotaro at wisc.edu>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu <mailto:ncl-talk at ucar.edu>
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk <http://mailman.ucar.edu/mailman/listinfo/ncl-talk>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170630/7052e99d/attachment.html 


More information about the ncl-talk mailing list