Information

This package extends the basic functions of the HDF5` package from the basic functions of the HDF5group.org (http://www.hdfgroup.org/HDF5/doc/RM/) into a more Mathematica-oriented syntax. One main purpose of these extended functions is to bury the three-line NETNew[...] formulation that is needed for some function calls into a single convenient function (cf. H5SExtendGetSimpleExtentDims).

Prepared 14 July 2011 by Scot Martin for Windows 7, 64-bit, Mathematica version 8.0.1. Email: scot_martin@harvard.edu.

http://www.hdfgroup.org/HDF5/doc/RM/
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5Front.html
Source code: http://www.hdfgroup.org/ftp/HDF5/current/src/unpacked/src/

Package

BeginPackage Context[...]

BeginPackage["HDF5Extend`",{"HDF5`","NETLink`"}]

Definitions

$HDF5ExtendPackageVersion="V1.00"

Usage Statements

H5ExtendGetUlibVersion::usage=
"H5ExtendGetUlibVersion[] returns the version of the H5 library as a list of {major, minor, revision}."

H5SExtendGetSimpleExtentDims::usage=
"H5SExtendGetSimpleExtentDims[\"DataSpaceID\"] returns as a list the dimensions of the DataSpaceID."

H5DExtendRead::usage=
"H5DExtendRead[\"DataSpaceID\", \"MemoryTypeID\", \"MemorySpaceID\", \"FileSpaceID\", \"xferPListID\", \"Dimensions\"] reads data from a file. The first 5 arguments are as defined for H5Dread. The \"Dimensions\" argument should be a list of dimensions of the object to be read, e.g., \"{10}\" for a rank 1 data set of length 10, \"{10,5}\" for a rank 2 data set of dimensions 10 by 5, and so forth. This function takes the option \"DataType\", which is Integer32 by default. Allowed DataTypes include Byte, Integer32, Integer64, SingleFloat, and DoubleFloat. (Other allowed values can be easily added by additional programming of H5Dread in \"HDF5`\" and of H5DExtendRead in \"HDF5Extend`\".)"

H5DExtendWrite::usage=
"H5DExtendRead[\"DataSpaceID\", \"MemoryTypeID\", \"MemorySpaceID\", \"FileSpaceID\", \"xferPListID\", \"Data\"] writes data to a file. The first 5 arguments are as defined for H5Dwrite. The \"Data\" argument should be a list or a list of lists, depending on dimensions of data. This function takes the option \"DataType\", which is Integer32 by default. Other allowed values are Byte, SingleFloat, and DoubleFloat. (Other allowed values can be easily added by additional programming of H5Dwrite in \"HDF5`\" and of H5DExtendWrite in \"HDF5Extend`\".)"

H5DExtendOpen::usage=
"H5DExtendOpen[{\"dataSetIDsymbol\" = H5Dopen[....]}, body] sets the dataSetIDsymbol and then runs the body. The purpose of this function is that it provides an automatic H5Dclose[dataSetIDsymbol] after executing body. The return value is that of the body."

H5FExtendOpen::usage=
"H5FExtendOpen[{\"fileIDsymbol\" = H5Fopen[....]}, body] sets the fileIDsymbol and then runs the body. The purpose of this function is that it provides an automatic H5Fclose[fileIDsymbol] after executing body. The return value is that of the body."

H5GExtendOpen::usage=
"H5GExtendOpen[{\"groupIDsymbol\" = H5Gopen[....]}, body] sets the groupIDsymbol and then runs the body. The purpose of this function is that it provides an automatic H5Dclose[dataSetIDsymbol] after executing body. The return value is that of the body."

H5GExtendGetNumObjs::usage=
"H5GExtendGetNumObjs[\"GroupOrFileID\"] returns the number of objects at one node of the GroupOrFileID."

H5GExtendGetObjNameByIndex::usage=
"H5GExtendGetObjNameByIndex[\"GroupOrFileID\", \"Index\"] returns the String name for the indicated Index of the GroupOrFileID."

H5AExtendGetName::usage=
"H5AExtendGetName[\"AttributeID\"] returns the String name for the indicated AttributeID."

H5AExtendRead::usage=
"H5AExtendRead[\"AttributeID\", \"MemoryTypeID\", \"Dimensions\"] reads the value of an attribute. \"Dimensions\" is typically {1} for most common applications. This function takes the option \"DataType\", which is Integer32 by default. Allowed DataTypes include Byte, Integer32, Integer64, SingleFloat, and DoubleFloat."

Begin Private Context[...]

Begin["Private`"]

H5

H5EXTENDGETULIBVERSION

H5ExtendGetUlibVersion[]:=

Module[

{major=minor=revision=0},

CompoundExpression[
H5getUlibversion[major,minor,revision],
(*return*) {major,minor,revision}
]

]

H5A

H5AExtendGetName

H5AExtendGetName[attributeID_Integer]:=

NETBlock[
Module[
{size=H5AgetName[attributeID,0,Null]+1},
Module[
{buffer=NETNew["System.Text.StringBuilder",size]},
CompoundExpression[
H5AgetName[attributeID,size,buffer],
(*return*)  buffer@ToString[]
]]]]

H5AEXTENDREAD

Options[H5AExtendRead]={"DataType"->Integer32}

H5AExtendRead[attributeID_Integer,memoryTypeID_Integer,dimensions_List,opts:OptionsPattern[]]:=

NETBlock[

Module[

{readDataObject,dataAsVector,dataAsMatrix},

CompoundExpression[

(*make the correct buffer type and size for reading in data as one long vector *)
Switch[
OptionValue["DataType"],

Byte,
readDataObject=NETNew["System.Byte[]",Times@@dimensions],

Integer32,
readDataObject=NETNew["System.Int32[]",Times@@dimensions],

Integer64,
readDataObject=NETNew["System.Int64[]",Times@@dimensions],

SingleFloat,
readDataObject=NETNew["System.Single[]",Times@@dimensions],

DoubleFloat,
readDataObject=NETNew["System.Double[]",Times@@dimensions]
](*close Switch*),

(* read in the data to the buffer *)
H5Aread[OptionValue["DataType"]][attributeID,memoryTypeID,readDataObject],

(* data is read in as one long List[..] *)
dataAsVector=NETObjectToExpression[readDataObject],
(* use this fancy function to get data back into lists of lists *)
dataAsMatrix=Fold[Partition,dataAsVector,Reverse@Rest@dimensions]
] (* close CompoundExpression *)

] (* close Module *)

] (* close NETBlock *)

H5D

H5DEXTENDREAD

Options[H5DExtendRead]={"DataType"->Integer32}

H5DExtendRead[dataSetID_Integer,memoryTypeID_Integer,memorySpaceID_Integer,fileSpaceID_Integer,xferPListID_Integer,dimensions_List,opts:OptionsPattern[]]:=

NETBlock[

Module[

{readDataObject,dataAsVector,dataAsMatrix},

CompoundExpression[

(*make the correct buffer type and size for reading in data as one long vector *)
Switch[
OptionValue["DataType"],

Byte,
readDataObject=NETNew["System.Byte[]",Times@@dimensions],

Integer32,
readDataObject=NETNew["System.Int32[]",Times@@dimensions],

Integer64,
readDataObject=NETNew["System.Int64[]",Times@@dimensions],

SingleFloat,
readDataObject=NETNew["System.Single[]",Times@@dimensions],

DoubleFloat,
readDataObject=NETNew["System.Double[]",Times@@dimensions]
](*close Switch*),

(* read in the data to the buffer *)
H5Dread[OptionValue["DataType"]][dataSetID,memoryTypeID,memorySpaceID,fileSpaceID,xferPListID,readDataObject],

(* data is read in as one long List[..] *)
dataAsVector=NETObjectToExpression[readDataObject],
(* use this fancy function to get data back into lists of lists *)
dataAsMatrix=Fold[Partition,dataAsVector,Reverse@Rest@dimensions]
] (* close CompoundExpression *)

] (* close Module *)

] (* close NETBlock *)

H5DEXTENDWRITE

Options[H5DExtendWrite]={"DataType"->Integer32}

H5DExtendWrite[dataSetID_Integer,memoryTypeID_Integer,memorySpaceID_Integer,fileSpaceID_Integer,xferPListID_Integer,data_List,opts:OptionsPattern[]]:=

(* use the correct function for the type of data *)
H5Dwrite[OptionValue["DataType"]][dataSetID,memoryTypeID,memorySpaceID,fileSpaceID,xferPListID,Flatten@data]

H5DEXTENDOPEN

SetAttributes[H5DExtendOpen,HoldAll]

H5DExtendOpen[groupOrFileIDspecification_List,body_]:=

Module[
{output},

With[
(* e.g., dataSetID = H5DOpen[....]*)
groupOrFileIDspecification,
With[
{
(* pickout 'dataSetID' from user input and insert it into the statement for H5Dclose *)
dataSetIDsymbol=Replace[Hold[groupOrFileIDspecification],Hold[List[Set[symbol:_,_]]]:> symbol]
},
(* execute the main body and then close the file *)
CompoundExpression[output=body,H5Dclose[dataSetIDsymbol],output]
]
]
]

H5E

H5F

H5FEXTENDOPEN

SetAttributes[H5FExtendOpen,HoldAll]

H5FExtendOpen[fileIDspecification_List,body_]:=

Module[
{output},

With[
(* e.g., fileID = H5FOpen[....]*)
fileIDspecification,
With[
{
(* pickout 'fileID' from user input and insert it into the statement for H5Fclose *)
fileIDsymbol=Replace[Hold[fileIDspecification],Hold[List[Set[symbol:_,_]]]:> symbol]
},
(* execute the main body and then close the file *)
CompoundExpression[output=body,H5Fclose[fileIDsymbol],output]
]
]
]

H5G

H5GEXTENDOPEN

SetAttributes[H5GExtendOpen,HoldAll]

H5GExtendOpen[groupOrFileIDspecification_List,body_]:=

Module[
{output},

With[
(* e.g., dataSetID = H5DOpen[....]*)
groupOrFileIDspecification,
With[
{
(* pickout 'dataSetID' from user input and insert it into the statement for H5Dclose *)
groupIDsymbol=Replace[Hold[groupOrFileIDspecification],Hold[List[Set[symbol:_,_]]]:> symbol]
},
(* execute the main body and then close the file *)
CompoundExpression[output=body,H5Gclose[groupIDsymbol],output]
]
]
]

H5GEXTENDGETNUMOBJS

H5GExtendGetNumObjs[GroupOrFileID_Integer]:=

Module[

{num=0},

CompoundExpression[
H5GgetNumObjs[GroupOrFileID,num],
(*return*) num
]

]

H5GEXTENDGETOBJNAMEBYINDEX

H5GExtendGetObjNameByIndex[GroupOrFileID_Integer,index_Integer]:=

NETBlock[
Module[
{size=H5GgetObjNameByIndex[GroupOrFileID,index,Null,0]+1},
Module[
{buffer=NETNew["System.Text.StringBuilder",size]},
CompoundExpression[
H5GgetObjNameByIndex[GroupOrFileID,index,buffer,size],
(*return*)  buffer@ToString[]
]]]]

H5I

H5L

H5O

H5P

H5R

H5S

H5SEXTENDGETSIMPLEEXTENTDIMS

H5SExtendGetSimpleExtentDims[dataSpaceID_Integer]:=

NETBlock[

Module[

{dataSpaceDimensionsObject=NETNew["System.Int64[]",H5SgetSimpleExtentNDims@dataSpaceID]},

CompoundExpression[
H5SgetSimpleExtentDims[dataSpaceID,dataSpaceDimensionsObject,Null],
(*return*) NETObjectToExpression@dataSpaceDimensionsObject
]

]

]

H5T

H5Z

End Private

End[]

SetAttributes[
{H5ExtendGetUlibVersion,H5DExtendRead,H5DExtendWrite,H5DExtendOpen,H5FExtendOpen,H5GExtendOpen,H5SExtendGetSimpleExtentDims,H5GExtendGetNumObjs,H5GExtendGetObjNameByIndex,H5AExtendGetName,H5AExtendRead},
ReadProtected
]

EndPackage Context[...]

EndPackage[]