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 >Organization: UNC >Keywords: 200503021900.j22J0xv2002105 netCDF Hi Ming, > I am a new user of netcdf data. I have a big nc file which contains > tens of variables. What I want to do now is to extract one of the > variables from the whole nc file and generate a new nc file containing > this varaible only. Do you know what is the most efficient way to do this? One way is to install the NCO package, freely available from: http://nco.sourceforge.net/ and use the "ncks" program that is part of that package. For example, if you just want to extract the variable "foo" (along with all its attributes and whatever dimensions are used by foo) from the netCDF file "file.nc", you would just invoke: ncks -v foo file.nc foo.nc and it would extract and write the requested output file. Another freely available package that does it is CDO from http://www.mpimet.mpg.de/~cdo/ in which you can use the program "selvar" (select variable) invoked as: selvar foo file.nc foo.nc If you know Python, Ruby, or Perl, you could use the netCDF interface for one of those scripting languages to develop a fairly small script that would do what you want. Or there's always writing a program in C, Fortran, Java, or C++, using the appropriate netCDF interface. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu/staff/russ