Dynamic variable names, alternate method

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Thu Mar 04 2010 - 17:22:27 MST

All,

Here is an alternate method for dynamic variable names. This is for
the purpose of reading all variables from a Netcdf file, and keeping
the same names within an NCL program.

The trick is to use a special program in advance of your
application, to make an include file with the necessary input
statements. This is not a general purpose solution for dynamic
names, but it does save the effort of writing all the input
statements by hand.

See two attached scripts. The first reads a Netcdf file and makes
the include file. The second demonstrates how to use the include
file in an application program.

I am not sure if this is what the original poster on this topic
needed, but it might be useful for some purposes.

Dave Allured
CU/CIRES Climate Diagnostics Center (CDC)
http://cires.colorado.edu/science/centers/cdc/
NOAA/ESRL/PSD, Climate Analysis Branch (CAB)
http://www.esrl.noaa.gov/psd/psd1/

;---------------------------------------------------------------------------
;
; Generate a set of NCL statements to read all variables in a
; Netcdf file, keeping the same names within the target NCL program.
;
; 2010-mar-04 by Dave Allured, NOAA/PSD/CIRES.
;
; The read statements can then be used in another NCL program.
; The statement file can be used as an include file, making the
; process automatic. In effect, all file variables are read
; when the load statement is encountered.
;
; in = addfile (infile, "r")
; load "statements.ncl" ; reads all variables with their own names
;
; This is a demo version that includes an optional threshold.
; Variables larger than a specified size limit are excluded.
; If you do not need the limit or the status report, then this
; program can be greatly simplified.
;
; Usage:
;
; 1. Change the input file name to your own data file.
;
; 2. Change file_var_name to the var name that your application
; program uses on the left side of the addfile statement.
;
; 3. Change max_nelements to prevent reading variables larger
; than a certain size. Set it to a huge number to read all
; variables from the input file.
;
; 4. Run this program to make the include file "statements.ncl".
;
; NCL version 5.1.1 or greater is required for built-in functions.
;
;---------------------------------------------------------------------------

function blank_pad (str, length) ; extend a string to fixed length
begin
   nextra = length - strlen (str)
   if (nextra .ge. 1) then
      bbb = new (nextra, string)
      bbb(:) = " "
      return (str + str_concat (bbb))
   else
      return (str)
   end if
end

begin
   infile = "test-data.nc"
   outfile = "statements.ncl"
   file_var_name = "in" ; file variable name in target NCL program
   max_nelements = 1e6 ; vars larger than this size are excluded

   print ("Open input file.")
   in = addfile (infile, "r")
   vars = getfilevarnames (in)
   nvars = dimsizes (vars)
   
   width = max (strlen (vars)) + 2 ; longest name length, plus spacer
   width = max ((/ width, 11 /)) ; minimum with for title
   print ("Generate NCL input statements.")
   print (" " + blank_pad ("Var name", width) \
      + "Rank Total elements Included")
   
   stmts = new (nvars, string)
   include = new (nvars, logical)
   
   do i = 0, nvars-1
      dims = getfilevardimsizes (in, vars(i))
      rank = dimsizes (dims)
      nelements = product (dims + 0.0)
      include(i) = (nelements .le. max_nelements)
      yesno = where (include(i), "Yes", "No")
      print (" " + blank_pad (vars(i), width) + sprinti ("%4i", rank) \
         + sprintf ("%17.0f", nelements) + " " + yesno)
      stmts(i) = " " + vars(i) + " = " + file_var_name + "->" + vars(i)
      delete (dims)
   end do
   
   print ("Number of variables in file = " + nvars)
   print ("Number included in output = " + num (include))
   print ("Write output file: " + outfile)
   
   asciiwrite (outfile, stmts(ind(include)))
end

;---------------------------------------------------------------------------
;
; readvars.demo.ncl -- Read all variables in Netcdf file, and preserve names.
; 2010-mar-04 by Dave Allured, NOAA/PSD/CIRES.
;
; Usage:
;
; 1. Use make-read-statements.ncl to make include file statements.ncl.
; 2. Change the input file name to your own data file.
; 3. Insert your own variable names prinVarSummary statements below.
; 4. Run this demo program.
;
;---------------------------------------------------------------------------

begin
   infile = "test-data.nc"
   print ("Open input file.")
   in = addfile (infile, "r")
   
   print ("Check for possible conflicts with existing variables.")
   varnames = getfilevarnames (in)
   conflicts = isdefined (varnames)
   if (any (conflicts)) then
      print ("*** These variables are already in use in this program,")
      print ("*** so they can't be read in with the same names:")
      print (" " + varnames(ind (conflicts)))
      print ("*** Abort.")
      exit
   else
      print ("... No conflicts detected.")
   end if
   
   print ("Read all variables via include file.")
   load "statements.ncl" ; in effect this reads all included variabes,
                            ; preserving the names from the input file.

   printVarSummary (lat) ; check status of expected data variables
   printVarSummary (lon)
   printVarSummary (monthtot)
end

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Mar 4 17:22:37 2010

This archive was generated by hypermail 2.1.8 : Mon Mar 08 2010 - 12:07:42 MST