[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 20050302: How to extract one variable from the whole Netcdf file



>To: address@hidden
>From: Ming Yang <address@hidden>
>Subject: How to extract one variable from the whole Netcdf file
>Cc: address@hidden
>Organization: UNC
>Keywords: 200503102145.j2ALjgv2025675

Hi Ming,

>    Thank you for your help. I have another question for you. Currently I
> have a 400 year long NC file and each record in the file is monthly
> averaged so that totally I have 4800 points in time axis. Now I would like
> to calcuate the annual average for each year to reduce the size of my
> file. What is the best efficient way?

The most efficient way in machine time, if you have enough memory to
hold a record's worth of data, would probably be to write a C program
that called the netCDF interface to do something like:

   open the input file for reading
   open the output file for writing
   for(outrec = 0; outrec < 400; outrec++) {
      month = 0;
      zero out the output variables (which will contain the averages)
      for (inrec = 12*outrec + month; month < 12; month++) {
         for each input record variable invar {
            read in invar data for record number inrec
            add the invar data to the corresponding output variable
         }
      }
      divide the values of the output variables by 12.0
      output all the output variables in record outrec
   }

Of course you could do the same thing with any of the other language
interfaces for Fortran, C++, Java, Python, etc.

If you're asking about human time rather than machine time, it would
be good to find a utility that already did this.  I'm not that
familiar with all the NCO or CDO utilities, but it may be that one of
the NCO averaging programs could be invoked in a way that does the
above for you.  The most likely candidate, ncra, doesn't look like it
does quite what you want unless you rearranged the input files first
to use "month number" as the record dimension:

  http://nco.sourceforge.net/nco.html#ncra%20netCDF%20Record%20Averager

Also, using a scripting language like Python, Ruby, or Perl is
generally a faster way to develop algorithms like this than using the
procedural interfaces for C, Fortran, C++, or Java.  But you would
have to consider how much time it takes to learn the language or
interface and how much more of this sort of thing you are likely to do
in the future to determine whether it's a good investment to switch
to a scripting language.

--Russ

_____________________________________________________________________

Russ Rew                                         UCAR Unidata Program
address@hidden          http://www.unidata.ucar.edu/staff/russ