reading all the variables from a netCDF when the variable names are unknown (or too many)

From: Jonathan Vigh <vigh_at_nyahnyahspammersnyahnyah>
Date: Tue, 24 Feb 2009 19:19:47 +0000

Greetings NCL'ers,
    I'd like to read in all the variables from a netCDF file in a
general way because there are 177 of them. I understand how to query the
file and get a list of variable names, and I even understand how to
reference the variable using:
fin->$varnames$

What I am confused about is what to assign this to. I'd like to avoid
having to type out 177 lines to read in each variable if possible. But
if I didn't even know the variable names in advance, I'd still like to
know how to do this.

Maybe I've missed something really basic, but I couldn't seem to find
anything from the documentation. If anyone has an idea, I'd be most
grateful. It would be nice if NCL could simply make all the variables in
a netCDF file available without having to go through this step, so maybe
this could be the prototype for a new function that does this.

Thanks,
   Jonathan

begin
  processed_filename = systemfunc("cd " + processed_data_directory + ";
ls " + stormid + "*.nc")
  fin = addfile(processed_data_directory + processed_filename,"r") ;
open output netCDF file

; query the file to see what variables it contains and what the sizes of
the file dimensions are
  varnames = getfilevarnames(fin) ; Returns an array of file
variable names in the file
  filedimsizes = getfiledimsizes(fin) ; Returns a list of the
sizes of all the dimensions in the file.

; query all the named variables in the file
  vartypes = getfilevartypes(fin,varnames) ; Returns the type of
each variable name listed. A missing value is returned for any variable
name that doesn't exist in the file.

; loop through all the variables, query them, and read them in
   nvars = dimsizes(varnames)
   do ivar = 0, nvars-1
      print("Reading variable ivar = "+ivar+" "+vartypes(ivar)+"
"+varnames(ivar))
      ???? = fin->$varnames(ivar)$ ; I understand how to
read in a variable from a file if I don't know the variable name in
advance, but how can I assign it to a variable without typing the
variable's name?
   end do

end
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Feb 24 2009 - 12:19:47 MST

This archive was generated by hypermail 2.2.0 : Mon Mar 02 2009 - 16:45:42 MST