Re: how to read netcdf automatically

From: Jonathan Vigh <jvigh_at_nyahnyahspammersnyahnyah>
Date: Fri Jul 20 2012 - 18:47:26 MDT

Hi Jinqiang,

   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.

   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.

   The module can also read all the contents of /multiple/ 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.

   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.

   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.

   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.

Best regards,
     Jonathan

On 07/20/2012 04:33 PM, Jinqiang Chen wrote:
>
> 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->variable(i)
> in which i is the loop index and f is the addfile netcdf variable. Every
> time ncl read f->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
>
>
>

-- 
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
http://www.ral.ucar.edu/staff/jvigh/
http://www.ral.ucar.edu/hurricanes/




_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk

Received on Fri Jul 20 18:47:42 2012

This archive was generated by hypermail 2.1.8 : Tue Jul 24 2012 - 09:48:26 MDT