Usage of NCO to Access NetCDF Files

1. Introduction

Although NCO is mainly for NetCDF files, NetCDF-4 uses HDF5 as storage format and some NASA MEaSUREs products use NetCDF-4. In addition, the H4CF Conversion Toolkit can benefit from NCO in editing the converted NetCDF files. The NASA HDF files served by CF-enabled OPeNDAP handlers can be manipulated easily with NCO. Thus, we highlight some basic and useful usage on how to access NetCDF files using NCO.

2. Dump File Contents in ASCII

One of the basic usage is to dump the contents of NetCDF files in ASCII output. Although ncdump can do the similar job, the ncks in NCO prints data in one datum per line, with all dimension subscripts and coordinate values. This makes users to easily search for the data you want. To dump the file contents of an NetCDF file, use ncks as shown in Figure 1.

Figure 1 How to dump NetCDF file in ASCII using ncks operator.
ncks AIRS.2002.08.01.L3.RetStd_H031.v4.0.21.0.G06104133732.hdf.nc
You can test the sample file, AIRS.2002.08.01.L3.RetStd_H031.v4.0.21.0.G06104133732.hdf.nc (size:176M), which is an NetCDF-3 file converted by h4tonccf tool in H4CF Conversion Toolkit.

The same command works for some NASA files served by CF-option enabled OPeNDAP hdf4 handler and hdf5 handler as shown in Figure 2.

Figure 2 How to dump OPeNDAP URL in ASCII using ncks operator.
ncks https://eosdap.hdfgroup.org:8080/opendap/data/NASAFILES/hdf4/AIRS.2002.08.01.L3.RetStd_H031.v4.0.21.0.G06104133732.hdf

3. Save Remote OPeNDAP Resource as Local NetCDF

The ncks operator in the previous section can also save the contents of remote OPeNDAP resource that follows the CF-conventions in a local NetCDF file.

Figure 3 How to save OPeNDAP resource as a local NetCDF file.
ncks https://eosdap.hdfgroup.org:8080/opendap/data/NASAFILES/hdf4/AIRS.2002.08.01.L3.RetStd_H031.v4.0.21.0.G06104133732.hdf -o opendap.nc
Please note that your NCO must be built with DAP library to make the above command work.

4. Edit an attribute in NetCDF

One nice thing about NCO is that you can edit an attribute easily with the ncatted operator. This is particularly useful for NASA products that do not follow the CF conventions in naming attributes and their values. For example, our sample file opendap.nc (size:175M) from the previous section does not have "units" attribute for "TotO3_A" variable. If you want to add "units" attribute with value "DU" for "TotO3_A" variable, use ncatted as shown in Figure 4.

Figure 4 How to add an attribute using ncatted.
ncatted -a 'units,TotO3_A,c,c,DU' opendap.nc opendap_du.nc
If you don't want to keep the original NetCDF file, you don't have to specify new file name (i.e., opendap_du.nc in the Figure 4). You can also modify or delete an existing attribute by following the examples here.

5. Convert NetCDF-3 to NetCDF-4

Finally, you can convert NetCDF-3 into NetCDF-4 file. Since the NetCDF-4 uses HDF5 for its storage format, the converted file becomes an HDF5 file that any HDF5 tool like HDFView can easily access it. Use the ncks operator as shown in Figure 5.

Figure 5 How to Convert NetCDF-3 to NetCDF-4
ncks -4 opendap.nc opendap.nc4
The converted file, opendap.nc4 (size:176M), is also available so that you can check it out. Please note that your NCO must be built with HDF5 library to make the above conversion work.


Last modified: 06/02/2017
About Us | Contact Info | Archive Info | Disclaimer
Sponsored by Subcontract number 4400528183 under Raytheon Contract number NNG15HZ39C, funded by NASA / Maintained by The HDF Group