Here is a C# class that i created for my application to read / write some 2D
and 3D arrays and basic data types. It has functions to read / write
strings. I have not used compound data. Hope this helps a bit.
Shashi
class HDFFileHandler
{
public enum DataTypes
{
STRING,
INTEGER,
DOUBLE,
BYTE,
USHORT,
FLOAT
}
public H5FileId CreateFile(string filename)
{
return H5F.create(filename, H5F.CreateMode.ACC_TRUNC);
}
public H5GroupId CreateGroup(string groupName, H5LocId parentGroup)
{
return H5G.create(parentGroup, groupName);
}
public H5DataSetId Create1DDataSet(string datasetName,
H5FileOrGroupId parentGroup, DataTypes datatype, int size)
{
long[] dims = new long[1];
dims[0] = size;
H5DataSpaceId spaceId = H5S.create_simple(1, dims);
H5DataTypeId typeId = null;
switch (datatype)
{
case DataTypes.BYTE:
typeId = H5T.copy(H5T.H5Type.NATIVE_UCHAR);
break;
case DataTypes.DOUBLE:
typeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);
break;
case DataTypes.INTEGER:
typeId = H5T.copy(H5T.H5Type.NATIVE_INT);
break;
case DataTypes.STRING:
typeId = H5T.copy(H5T.H5Type.C_S1);
break;
case DataTypes.FLOAT:
typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
break;
case DataTypes.USHORT:
typeId = H5T.copy(H5T.H5Type.NATIVE_USHORT);
break;
default:
break;
}
return H5D.create(parentGroup, datasetName, typeId, spaceId);
}
public H5DataSetId Create2DDataSet(string datasetName,
H5FileOrGroupId parentGroup, DataTypes datatype, int sizeX, int sizeY)
{
long[] dims = new long[2];
dims[0] = sizeX;
dims[1] = sizeY;
H5DataSpaceId spaceId = H5S.create_simple(2, dims);
H5DataTypeId typeId = null;
switch (datatype)
{
case DataTypes.BYTE:
typeId = H5T.copy(H5T.H5Type.NATIVE_UCHAR);
break;
case DataTypes.DOUBLE:
typeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);
break;
case DataTypes.INTEGER:
typeId = H5T.copy(H5T.H5Type.NATIVE_INT);
break;
case DataTypes.STRING:
typeId = H5T.copy(H5T.H5Type.C_S1);
break;
case DataTypes.FLOAT:
typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
break;
case DataTypes.USHORT:
typeId = H5T.copy(H5T.H5Type.NATIVE_USHORT);
break;
default:
break;
}
return H5D.create(parentGroup, datasetName, typeId, spaceId);
}
public H5DataSetId Create3DDataSet(string datasetName,
H5FileOrGroupId parentGroup, DataTypes datatype, int sizeX, int sizeY, int
nSizeZ)
{
long[] dims = new long[3];
dims[0] = sizeX;
dims[1] = sizeY;
dims[2] = nSizeZ;
H5DataSpaceId spaceId = H5S.create_simple(3, dims);
H5DataTypeId typeId = null;
switch (datatype)
{
case DataTypes.BYTE:
typeId = H5T.copy(H5T.H5Type.NATIVE_UCHAR);
break;
case DataTypes.DOUBLE:
typeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);
break;
case DataTypes.INTEGER:
typeId = H5T.copy(H5T.H5Type.NATIVE_INT);
break;
case DataTypes.STRING:
typeId = H5T.copy(H5T.H5Type.C_S1);
break;
case DataTypes.FLOAT:
typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
break;
case DataTypes.USHORT:
typeId = H5T.copy(H5T.H5Type.NATIVE_USHORT);
break;
default:
break;
}
return H5D.create(parentGroup, datasetName, typeId, spaceId);
}
public void CloseFile(H5FileId fileId)
{
H5F.close(fileId);
}
public void CloseGroup(H5GroupId grpId)
{
H5G.close(grpId);
}
public void CloseDataset(H5DataSetId datasetId)
{
H5D.close(datasetId);
}
public H5FileId OpenFile(string filename, H5F.OpenMode mode)
{
return H5F.open(filename, mode);
}
public H5GroupId OpenGroup(string grpName, H5LocId parentID)
{
return H5G.open(parentID, grpName);
}
public H5DataSetId OpenDataset(string datasetName, H5FileOrGroupId
parentID)
{
return H5D.open(parentID, datasetName);
}
public void WriteByteArray(H5DataSetId dataSetId, byte[] byteArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_UCHAR);
H5D.write(dataSetId, typeId, new H5Array<byte>(byteArray));
}
public void ReadByteArray(H5DataSetId dataSetId, byte[] byteArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_UCHAR);
H5D.read(dataSetId, typeId, new H5Array<byte>(byteArray));
}
public void WriteIntegerArray(H5DataSetId dataSetId, int[] intArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_INT);
H5D.write(dataSetId, typeId, new H5Array<int>(intArray));
}
public void ReadIntegerArray(H5DataSetId dataSetId, int[] intArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_INT);
H5D.read(dataSetId, typeId, new H5Array<int>(intArray));
}
public void WriteFloatArray(H5DataSetId dataSetId, float[]
floatArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
H5D.write(dataSetId, typeId, new H5Array<float>(floatArray));
}
public void ReadFloatArray(H5DataSetId dataSetId, float[]
floatArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
H5D.read(dataSetId, typeId, new H5Array<float>(floatArray));
}
public void WriteDoubleArray(H5DataSetId dataSetId, double[]
dblArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);
H5D.write(dataSetId, typeId, new H5Array<double>(dblArray));
}
public void ReadDoubleArray(H5DataSetId dataSetId, double[]
dblArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);
H5D.read(dataSetId, typeId, new H5Array<double>(dblArray));
}
public void WriteUShortArray(H5DataSetId dataSetId, ushort[]
ushortArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_USHORT);
H5D.write(dataSetId, typeId, new H5Array<ushort>(ushortArray));
}
public void ReadUShortArray(H5DataSetId dataSetId, ushort[]
ushortArray)
{
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_USHORT);
H5D.read(dataSetId, typeId, new H5Array<ushort>(ushortArray));
}
public void WriteInteger(H5DataSetId dataSetId, int nValue)
{
int[] data = new int[1];
data[0] = nValue;
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_INT);
H5D.write(dataSetId, typeId, new H5Array<int>(data));
}
public int ReadInteger(H5DataSetId dataSetId)
{
int[] data = new int[1];
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_INT);
H5D.read(dataSetId, typeId, new H5Array<int>(data));
return data[0];
}
public void WriteFloat(H5DataSetId dataSetId, float fValue)
{
float[] data = new float[1];
data[0] = fValue;
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
H5D.write(dataSetId, typeId, new H5Array<float>(data));
}
public float ReadFloat(H5DataSetId dataSetId)
{
float[] data = new float[1];
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
H5D.read(dataSetId, typeId, new H5Array<float>(data));
return data[0];
}
public void WriteDouble(H5DataSetId dataSetId, ref double dValue)
{
double[] data = new double[1];
data[0] = dValue;
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_DOUBLE);
H5D.write(dataSetId, typeId, new H5Array<double>(data));
}
public double ReadDouble(H5DataSetId dataSetId)
{
double[] data = new double[1];
H5DataTypeId typeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
H5D.read(dataSetId, typeId, new H5Array<double>(data));
return data[0];
}
public void WriteString(H5DataSetId dataSetId, string strData)
{
byte[] byteArray = Encoding.ASCII.GetBytes(strData);
H5DataTypeId typeId = H5T.copy(H5T.H5Type.C_S1);
H5D.write(dataSetId, typeId, new H5Array<byte>(byteArray));
}
public string ReadString(H5DataSetId dataSetId, int nSize)
{
byte[] byteArray = new byte[nSize];
H5DataTypeId typeId = H5T.copy(H5T.H5Type.C_S1);
H5D.read(dataSetId, typeId, new H5Array<byte>(byteArray));
return System.Text.Encoding.ASCII.GetString(byteArray);
}
}
···
--
View this message in context: http://hdf-forum.184993.n3.nabble.com/c-read-different-datatypes-tp4026057p4026087.html
Sent from the hdf-forum mailing list archive at Nabble.com.