(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



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


$HDF5ExtendPackageVersion="V1.00"


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`"]


H5ExtendGetUlibVersion[]:=

Module[

{major=minor=revision=0},

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

]


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[]
]]]]


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 *)



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 *)



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]



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]
]
]
]


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]
]
]
]


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[GroupOrFileID_Integer]:=

Module[

{num=0},

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

]


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[]
]]]]


H5SExtendGetSimpleExtentDims[dataSpaceID_Integer]:=

NETBlock[

Module[

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

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

]

]


End[]


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


EndPackage[]
