NetCDF
4.9.2
|
The netCDF example programs show how to use netCDF.
In the netCDF distribution, the “examples” directory contains examples in C and CDL. The examples create, and then read, example data files of increasing complexity.
There are three sets of netCDF classic example programs; corresponding examples are included with the netCDF Fortran and C++ APIs.
Additionally, there is a example program demonstrating how to use the filter API. This is C only and only accessible when built with automake currently.
Any existing netCDF applications can be converted to generate netCDF-4/HDF5 files. Simply change the file creation call to include the correct mode flag.
In one of the netCDF classic examples which write a data file, change the nc_create() call so that NC_NETCDF4 is one of the flags set on the create.
The corresponding read example will work without modification; netCDF will notice that the file is a NetCDF-4/HDF5 file, and will read it automatically, just as if it were a netCDF classic format file.
In the example in this section we show some of the advanced features of netCDF-4.
The examples are built and run with the “make check” command. (See Building netCDF-C).
The corresponding examples in each language create identical netCDF data files. For example, the C program sfc_pres_temp_wr.c produces the same data file as the Fortran 77 program sfc_pres_temp_wr.f.
This example is an unrealistically simple netCDF file, to demonstrate the minimum operation of the netCDF APIs. Users should seek to make their netCDF files more self-describing than this primitive example.
As in all the netCDF tutorial examples, this example file can be created by C and by ncgen, which creates it from a CDL script. Both ncgen and the C example create identical files, “simple_xy.nc.”
The simple_xy.nc data file contains two dimensions, “x” and “y”, and one netCDF variable, “data.”
The CDL for this example is shown below. For more information on ncdump and ncgen see NetCDF Utilities.
This example has been constructed for the meteorological mind.
Suppose you have some data you want to write to a netCDF file. For example, you have one time step of surface temperature and surface pressure, on a 6 x 12 latitude longitude grid.
To store this in netCDF, create a file, add two dimensions (latitude and longitude) and two variables (pressure and temperature).
In this example we add some netCDF attributes, as is typical in scientific applications, to further describe the data. In this case we add a units attribute to every netCDF variable.
In this example we also add additional netCDF variables to describe the coordinate system. These “coordinate variables” allow us to specify the latitudes and longitudes that describe the data grid.
The CDL version of the data file, generated by ncdump, is shown below (see The NetCDF Utilities).
This example expands on the previous example by making our two-dimensional data into four-dimensional data, adding a vertical level axis and an unlimited time step axis.
Additionally, in this example the data are written and read one time step at a time, as is typical in scientific applications that use the unlimited dimension.
The sample data file created by pres_temp_4D_wr can be examined with the utility ncdump (see The NetCDF Utilities).
This example, like the simple_xy netCDF-3 example above, is an overly simplified example which demonstrates how to use groups in a netCDF-4 file.
This example is only available in C for this version of netCDF-4. The example creates and then reads the file “simple_nc4.nc.”
The simple_xy.nc data file contains two dimensions, “x” and “y”, two groups, “grp1” and “grp2”, and two data variables, one in each group, both named: “data.” One data variable is an unsigned 64-bit integer, the other a user-defined compound type.
The example program simple_nc4_wr.c creates the example data file simple_nc4.nc. The example program simple_nc4_rd.c reads the data file.
This example, like the simple_xy netCDF-3 example above, is an overly simplified example. It is based on the simple_xy example, but used data chunking, compression, and the fletcher32 filter.
(These are all HDF5 features. For more information see http://hdfgroup.org/HDF5/).
This example is not yet available in C++. We hope to have the C++ example in a future release of netCDF.
The example creates and then reads the file “simple_xy_nc4.nc.”
The example program simple_xy_nc4_wr.c creates the example data file simple_xy_nc4.nc. The example program simple_xy_nc4_rd.c reads the data file.
This example demonstrates how to write and read a variable that is compressed using, in this example, bzip2 compression.