This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>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