The following discussion assumes the use of a UNIX operating system.
When creating your own HDF5 product, there are two header files that should be included, read_vhlhdf.h and write_vhlhdf.h.
When compiling a binary, there are three libraries that must be linked in; these are libhlhdf.a, libhdf5.a and libz.a. It is also possible to link the shared library libhdf5.so instead of libhdf5.a.
The HL-HDF package was installed with a hldef.mk file that can be included in your own Makefile in order to get the correct paths to both the zlib and the hdf5 library. It also contains information on which C-compiler the HL-HDF package was compiled with and some other goodies.
A simple Makefile could look like this:
include /usr/local/hlhdf/mkf/hldef.mk
HLHDF_INCDIR = -I/usr/local/hlhdf/include
HLHDF_LIBDIR = -L/usr/local/hlhdf/lib
CFLAGS = $(OPTS) $(DEFS) -I. $(ZLIB_INCDIR) $(HDF5_INCDIR) \
$(HLHDF_INCDIR)
LDFLAGS = -L. $(ZLIB_LIBDIR) $(HDF5_LIBDIR) $(HLHDF_LIBDIR)
LIBS = -lhlhdf -lhdf5 -lz -lm
TARGET=myTestProgram
SOURCES=test_program.c
OBJECTS=$(SOURCES:.c=.o)
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) -o $@ $(LDFLAGS) $(OBJECTS) $(LIBS)
clean:
@\rm -f *.o *~ so_locations core
distclean: clean
@\rm -f $(TARGET)
distribution:
@echo "Would bring the latest revision upto date"
install:
@$(HL_INSTALL) -f -o -C $(TARGET) ${MY_BIN_PATH}/$(TARGET)
Now, when the Makefile has been created, it might be a good idea to write your own HDF5 product. The following example will create a dataset with a two-dimensional array of integers, and two attributes connected to this dataset. It will also create a group containing one attribute.
#include <read_vhlhdf.h>
#include <write_vhlhdf.h>
int main(int argc, char** argv)
{
HL_NodeList* aList=NULL;
HL_Node* aNode=NULL;
int* anArray=NULL;
int anIntValue;
float aFloatValue;
hsize_t dims[]={10,10};
int npts=100;
int i;
initHlHdf(); /* Initialize the HL-HDF library */
debugHlHdf(2); /* Activate debugging */
if(!(aList = newHL_NodeList())) {
fprintf(stderr,"Failed to allocate nodelist");
goto fail;
}
if(!(anArray = malloc(sizeof(int)*npts))) {
fprintf(stderr,"Failed to allocate memory for array.");
goto fail;
}
for(i=0;i<npts;i++)
anArray[i]=i;
addNode(aList,(aNode = newHL_Group("/group1")));
addNode(aList,(aNode = newHL_Attribute("/group1/attribute1")));
anIntValue=10;
setScalarValue(aNode,sizeof(anIntValue),(unsigned char*)&anIntValue,"int",-1);
addNode(aList,(aNode = newHL_Dataset("/dataset1")));
setArrayValue(aNode,sizeof(int),2,dims,(unsigned char*)anArray,"int",-1);
addNode(aList,(aNode = newHL_Attribute("/dataset1/attribute2")));
anIntValue=20;
setScalarValue(aNode,sizeof(anIntValue),(unsigned char*)&anIntValue,"int",-1);
addNode(aList,(aNode = newHL_Attribute("/dataset1/attribute3")));
aFloatValue=99.99;
setScalarValue(aNode,sizeof(aFloatValue),(unsigned char*)&aFloatValue,
"float",-1);
strcpy(aList->filename,"written_hdffile.hdf");
writeNodeList(aList,6);
freeHL_NodeList(aList);
exit(0);
return 0; /* Won't come here */
fail:
freeHL_NodeList(aList);
exit(1);
return 1; /* Won't come here */
}
When you have created your own HDF5 product, it might be a good idea to create some code for reading this file and checking its contents.
#include <read_vhlhdf.h>
#include <write_vhlhdf.h>
int main(int argc, char** argv)
{
HL_NodeList* aList=NULL;
HL_Node* aNode=NULL;
int* anArray=NULL;
int anIntValue;
float aFloatValue;
int npts;
int i;
initHlHdf(); /* Initialize the HL-HDF library */
debugHlHdf(2); /* Activate debugging */
if(!(aList = readHL_NodeList("written_hdffile.hdf"))) {
fprintf(stderr,"Failed to read nodelist\n");
goto fail;
}
selectAllNodes(aList); /* Select everything for retrival */
fetchMarkedNodes(aList);
if((aNode = getNode(aList,"/group1")))
printf("%s exists\n",aNode->name);
if((aNode = getNode(aList,"/group1/attribute1"))) {
memcpy(&anIntValue,aNode->data,aNode->dSize);
printf("%s exists and have value %d\n",aNode->name,anIntValue);
}
if((aNode = getNode(aList,"/dataset1"))) {
anArray = (int*)aNode->data;
npts = 1;
for(i=0;i<aNode->ndims;i++)
npts*=aNode->dims[i];
printf("%s exists and has the values:\n",aNode->name);
for(i=0;i<npts;i++) {
printf("%d ", anArray[i]);
if((i%aNode->dims[0])==0) {
printf("\n");
}
}
printf("\n");
}
if((aNode = getNode(aList,"/dataset1/attribute2"))) {
memcpy(&anIntValue,aNode->data,aNode->dSize);
printf("%s exists and have the value %d\n",aNode->name,anIntValue);
}
if((aNode = getNode(aList,"/dataset1/attribute3"))) {
memcpy(&aFloatValue,aNode->data,aNode->dSize);
printf("%s exists and have the value %f\n",aNode->name,aFloatValue);
}
freeHL_NodeList(aList);
exit(0);
return 0; /* Never reached */
fail:
freeHL_NodeList(aList);
exit(1);
return 1; /* Never reached */
}