Contact: zeng  @  zegraph.com      Last update: 10 May 2011

HDF-5 Library

This library includes only part of the functions of Hierarchical Data Format library. Supported data types include simple array of char, uchar, short, ushort, int, uint, float, and double. Supported attribute types include string, integer, double, and simple array of integer and double.

Function Parameter Type Remark
hdf(filename[,mode]) string[, string] Returns a HDF object. The optional mode parameter may be "w" for writing (and creating if the file does not exist) or "c" for creating. The root "/" is set as the target group. Returns null if failed.
.version()   Returns HDF version as string.
.group(name) string Sets the named group as the target group. The function first tries to open the group if it exists and creates the group if not.
.group()   Returns sub-group names of the target group in an array. Returns a null if there is no sub-group in the group.
.dataset(name) string Sets the named dataset in the target group as the target dataset. The dataset name must include group name. It returns false if failed or true otherwise.
.dataset(name, type, dim1[, dim2...]) string, string, integers Defines a dataset in the target group and set it as the target dataset. The dataset name must include group name. The dataset type name may be "char", "uchar", "short", "ushort", "int", "uint", "float", or "double". The number of integers after the type argument determines the number of dimensions and the integer values define dimension sizes.
.dataset()   Returns the dataset names of the target group in an array. Returns a null if there is no dataset in the group.
.dims()   Returns dimension sizes of the current dataset in an array if the number of dimension is greater than 1; returns an integer otherwise.
.size()   Returns an array containing the number of data, datum size, and type name of the target dataset.
.cmm()   Returns the comment of the target group or dataset.
.cmm(comment) string Writes comment to the target group or dataset.
.attribute(name, i, value) string, integer, number Sets the value to the ith element of the named array attribute.
.list([fname]) string List contents in the target group recursively. If the opentional file name parameter is given, the contents will be saved to the file.
.__get(name) string To be called by such an expression as hdf.name to get the named attribute in the target group or dataset. It may return a string, a number, or an array of numbers.
.__get(index...) null or integer or array To be called by such an expression as hdf[*, 1] to read data from the target dataset. A null index (may be represented by *) specifies reading all data in that dimension; an integer index (must be positive) specifies reading at that position in that dimension; and an array index (e.g., [1,2]) specifies reading range for that dimension. If the number of indices is smaller than the number of dimensions of the dataset, unspecified dimension indices are defaulted to null. The function returns an integer or real if indices are fully specified by integers; otherwise, it returns a pointer to data.
.__set(name, value) string, string or number or array of numbers To be called by such an expression as hdf.name = value to set the named attribute of the target group or dataset.
.__set(index.., data.) null or integers or array, user or number To be called by such an expression as hdf[*, 1] = data to write data to the target dataset. If the data is a user type, you are responsible to ensure that its pointer points to correct number of data.

HDF Example

load("hdf.dll");

h = hdf("test.hdf", "c");

h.dataset("/data1", "int", 10, 10);

csv(h.size());
csv(h.dims());

h.cmm("test group comment");
csv(h.cmm());

h.att1 = "attribute of data1";
csv(h.att1);

h.att2 = 1.5;
csv(h.att2);

h.att3 = [1, 2.1, 3];
csv(h.att3);


h.group("/group1");
h.att1 = "attribute of group1";
csv(h.att1);

h.att2 = 15;
csv(h.att2);

h.att3 = [1, 2, 3];
csv(h.att3);

h.group("/");

h.cmm("test group comment");
csv(h.cmm());

/////////////////////////////////////////////////////////////////////

load("matrix.dll", "hdf.dll");

h = hdf("test.hdf", "w");
h.dataset("/data1");


a = matrix("int", 10, 10);
a.fill(0, 1);
[ptr, n, e] = a.ptr();

h[*] = ptr;
h[1,*] = 100;
h[*,2] = 200;
h[[1,5],[2,5]] = 0;

h[9,[2,5]] = 0;

a.fill(0,0);
a.import(h[*]);
a.print();