Monday, February 8, 2010

HDF Reader C++ Code Is Written!

The code to read HDF files is up. The code should be considered a beta release, not so much because of the quality (the code is actually pretty good, if I do say so myself), but because I reserve the right to modify it in the near future.

After having various problems with the code NASA supplies (missing libraries, pre-built binaries that don't link), I decided to just write the reader myself. This post will take a quick look at the code. A follow up post will show some of the results of using the code to read raw UAH data.

The code has been placed in the public domain and you can get it here:

Previous Posts In This Series
Prepping The Data File
The AMSR code reads un-compacted ASCII files, and HDF file are compacted binary. So you'll need to change that before reading them with the code. To do that, just use the ncdump tool. The only argument you supply to ncdump in this case is the filename. Redirect the output to a file and use that file as input to the code. And example command line is shown below:

ncdump AIRS.2010.01.01.240.L1B.AMSU_Rad.v5.0.0.0.G10005063822.hdf > MyFile.txt

If you don't yet have ncdump on your computer, see this post for instructions on downloading and using it.

The Code
The underlying code is fairy complex, so we'll skip discussing it. The code is well commented, so you can look there for specific details. Here we'll just discuss how to use the code to read data.

The code sample below shows how to create the reader, declare memory to hold data you'll get from the reader, and read the raw antenna temperatures.

// Supply location of HDF file.
HDFReader oHDFReader(string("./MyFile.txt"));
// 45 scan lines, 30 footprints, 15 channels.
float aData[45][30][15];

// Load data.
oHDFReader.getVariableData(string("antenna_temp"), aData);

Yup. That's it. Of course, with this version of the code, you need to know that the name of the antenna temperature data is antenna_temp and that it's an array of floats 45 by 30 by 15. Fear not. You can open the ASCII version of the HDF file and see all the variables listed in the variables section near the beginning of the file. These variables will have dimensions listed as constants and those constants are defined in the dimensions section at the beginning of the file.

There's also some hierarchical data in the HDF file and that data is stored in a node structure in HDFReader. To get the root of the node structure, use the getRootNode() method.

Future versions of the code will build on what's here now. Specifically, I want to add methods to access the variables without forcing the programmer to dig through the data file to see what variables are there. Code to do that should be ready soon.

Also coming soon is a look at some of the raw UAH data that gets extracted from these files.

Some Useful Climate Code


  1. having some questions in HDF5. is there a way to conatact by email?

  2. Hey, having problem reading satellite HDF files, can i get your contact?