<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Hi Jinqiang,<br>
      <br>
      &nbsp; I've had this problem in the past, in which I need to read in a
      netcdf file that may contain hundreds of disparate variables. The
      solution of querying the file to get all the variables, then
      reading them in, as Wei suggests is certainly a good way to go,
      but there's a clever way to bypass this task altogether -- I've
      written a module that will automatically read the contents of
      everything in the netcdf file into memory. The module works by
      dynamically generating additional modules and then executing them.
      <br>
      <br>
      &nbsp; Please see the attached:
      module_netcdf_reader_multiple_prefix_v1.0.ncl to see how it works.
      To use this module, simply load it before your main program block
      and set the input filename and path, as I've illustrated in the
      attached script: example_main.ncl. Then run the example_main.ncl
      and you should be able to start using the variables which were
      read right away in the main program block.<br>
      <br>
      &nbsp; The module can also read all the contents of <i>multiple</i>
      netcdf files into memory, which is double nice. If your files
      contain variables with the same name, there will be problems
      however. to get around this, I've written a separate module which
      prefixes a key to each variable name that can be set based on a
      pattern in the filename. Simply swap the load statement to load:
      module_netcdf_reader_multiple_prefix_v1.0.ncl instead and then
      modify the part of the module where the keys are set based on the
      file patterns. <br>
      <br>
      &nbsp; A caveat -- I believe the variables may be global when read this
      way. This can be an advantage or a disadvantage, depending on your
      subsequent program structure. <br>
      <br>
      &nbsp; It may be possible to read the input filenames and paths as
      Command Line Arguments if you don't know these in advance, but I
      haven't tested this yet to know if it would actually work. <br>
      <br>
      &nbsp; I've only tested these modules for my own codes and data. I'll
      be interested to hear if other people find them useful. Of course,
      I can't promise to provide any support for these. Eventually, when
      and if NCL has an "eval" statement, this telescoped dynamic module
      approach won't be necessary. <br>
      <br>
      Best regards,<br>
      &nbsp;&nbsp;&nbsp; Jonathan<br>
      <br>
      <br>
      <br>
      <br>
      <br>
      On 07/20/2012 04:33 PM, Jinqiang Chen wrote:<br>
    </div>
    <blockquote
      cite="mid:50670.131.215.103.227.1342802027.squirrel@webmail.caltech.edu"
      type="cite">
      <pre wrap="">

Hi there,

I want to know how to read netcdf variable automatically. Now I am
analyzing tons of CMIP5 data and I write an array of all the variable
names and I want to use the loop to let ncl to read the specific variable
name but I don't know how to do that. For example,
I build a vector named variable s.t. variable = (/"cct","ccb"/)
but it doesn't work for the case that: data = f-&gt;variable(i)
in which i is the loop index and f is the addfile netcdf variable. Every
time ncl read f-&gt;variable(i) it never change variable(i) to its real name,
like cct, or ccb. Could you tell me how to deal with that?

Thanks!

Jinqiang Chen



</pre>
    </blockquote>
    <br>
    <br>
    <pre class="moz-signature" cols="72">-- 
Jonathan Vigh                                                
Project Scientist I, Joint Numerical Testbed         
Research Applications Laboratory (RAL)
National Center for Atmospheric Research (NCAR)
P.O. Box 3000            tel: +1 303 497 8205
Boulder, CO 80307-3000   fax: +1 (303) 497-8171
<a class="moz-txt-link-freetext" href="http://www.ral.ucar.edu/staff/jvigh/">http://www.ral.ucar.edu/staff/jvigh/</a>
<a class="moz-txt-link-freetext" href="http://www.ral.ucar.edu/hurricanes/">http://www.ral.ucar.edu/hurricanes/</a>
</pre>
  </body>
</html>