Page tree

How would you convert a netCDF file to/from HDF4?

The netCDF library from Unidata contains utilities ncdump and ncgen to convert between the netCDF binary format and a text format called CDL. The HDF4 library from the HDF Group also contains utilities ncdump and ncgen that do the same thing, but this time the underlying binary file format is HDF. It is a little tricky to install both, because by default the HDF commands will overwrite the netCDF commands or vice versa. You may want to rename the HDF commands. (In the paragraphs below, the HDF versions of these tools are referred to as hdf_ncdump and hdf_ncgen.)

To do the "conversion" you would follow these steps:

          ncdump -l 80 > foo.cdl
          # Edit CDL file with a text editor to work around various
          # problems encountered by hdf_ncgen
          hdf_ncgen -b -o foo.hdf foo.cdl

HDF uses netCDF 2.3.2 when building the HDF versions of these tools, which is a very old version of netCDF. The need to edit the CDL file arises because hdf_ncgen cannot read everything that ncdump can write.

The -l 80 option above is intended to work around one of hdf_ncgen's limitations: a maximum line length of 80 characters. When hdf_ncgen encounters a problem, it halts and prints out the relevant line number in the CDL file. The error message will not necessarily make much sense, but when you go to that line you will usually find that the problem is reasonably obvious and easily corrected.

You may also have to do things like strip out attributes containing zero-length strings, remove netCDF fill values (represented in CDL as an underscore) and replace NaNs with finite floating-point values. (NaNs are illegal in netCDF anyway, though some platforms allow them.)

There is a Windows executable called ncdf2hdf from Fortner Software (defunct) that does the conversion. It is no longer available, but may be found by searching the web. However, the HDF files that it generates have been reported to lack some of the structure that is found in the ones produced by the route above.