William,
This works for me. Its somewhat of a pain, so I wrote a helper method in a static class. It also uses a separate class for working with variable length strings (if you need that).
Jesse
/// <summary>
/// Helper class for working with HDF5 files.
/// </summary>
public static class Hdf5Helper
{
public static string ReadAttributeString(H5ObjectWithAttributes groupId, string objectName, string attributeName)
{
H5AttributeId attributeId = null;
H5DataTypeId dataTypeId = null;
// Read the Type attribute
try
{
attributeId = H5A.openByName(groupId, objectName, attributeName);
dataTypeId = H5A.getType(attributeId);
bool isVariableLength = H5T.isVariableString(dataTypeId);
if (isVariableLength)
{
// Variable length string attribute
// NOTE: This section only works if the array length is 1
VariableLengthString value = new VariableLengthString[1];
H5A.read<VariableLengthString>(attributeId, dataTypeId, new H5Array<VariableLengthString>(value));
return value[0].ToString();
}
else
{
// Make length smaller so null termination character is not read
int length = (int)H5T.getSize(dataTypeId) - 1;
// Fixed length string attribute
byte valueBytes = new byte[length];
H5A.read<byte>(attributeId, dataTypeId, new H5Array<byte>(valueBytes));
string value = System.Text.ASCIIEncoding.ASCII.GetString(valueBytes);
return value;
}
}
catch (H5AopenByNameException)
{
// Attribute does not exist
}
catch (H5AreadException)
{
// Could not read the attribute correctly
}
finally
{
if (dataTypeId != null)
{
H5T.close(dataTypeId);
}
if (attributeId != null)
{
H5A.close(attributeId);
}
}
return null;
}
}
/// <summary>
/// Struct used to read in variable length string data from an HDF5
/// file. It is unsafe, therefore, the layout is used to define the
/// struct.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public unsafe struct VariableLengthString
{
/// <summary>
/// Pointer to the variable length string
/// </summary>
private char* recordedText;
/// <summary>
/// Gets or sets the pointer to the variable length string.
/// </summary>
[CLSCompliant(false)]
public char* RecordedText
{
get
{
return this.recordedText;
}
set
{
this.recordedText = value;
}
}
/// <summary>
/// Returns a System.String that represents the current System.Object.
/// </summary>
/// <returns>The value of the variable length string.</returns>
public override string ToString()
{
string s;
// The HDF5 STRING is not a string but in fact a char*
// Therefore, we need to translate the return into a pointer address
IntPtr ipp = (IntPtr)this.recordedText;
// This call is used to transform the pointer into the value of the pointer.
// NOTE: this only works with null-terminated strings.
s = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ipp);
//// FREE THE MEMORY TO THE POINTER??
//// System.Runtime.InteropServices.Marshal.FreeHGlobal(ipp);
return s;
}
}
···
On 7/23/2010 1:25 PM, whubler@as.arizona.edu wrote:
Ruth,
That I have tried and although I got the string dataset to work in C#, I was not
able to get it to work with string attributes in C#. When converted to VB I am
unable to direct cast a pointer to an intptr. Is is just a deficiency in the
dotnet wrappers?
Thanks,
--
William Hubler
Technical Services Specialist
Steward Observatory Mirror Lap
University of Arizona
Quoting Ruth Aydt <aydt@hdfgroup.org>:
You might take a look at the user-contributed example for reading variable-length strings that is found here:
ftp://ftp.hdfgroup.uiuc.edu/pub/outgoing/hdf5/HDF5dotNetEnv/examples/
On Jul 23, 2010, at 11:43 AM, whubler@as.arizona.edu wrote:
Hello all,
It seems that I have hit a wall here. I have a program that reads h5 files.
Datasets (numerical), groups and attributes (numerical), I can read fine. I
now have some string attributes to read.
I have tried:
Dim fileId As H5FileId = H5F.open('f:\newDataFormat.h5", H5F.OpenMode.ACC_RDWR)
Dim dsetId As H5DataSetId = H5D.open(fileId,"units")
Dim attrId As H5AttributeId = H5A.open(dsetId, "unit")
Dim strtype As New H5DataTypeId(H5T.H5Type.C_S1)
Dim read_data As string() = New string(0) {}
H5A.read(attrId, strtype, New H5Array(Of string)(read_data))
and when I look at read_data(0), it is nothing. I also get no errors.
I can make this work for integers, doubles and the like, but not strings. Am I
missing something.
I am using VS 2008, vb.net, XP 64 bit and the .net hdf5 wrappers after making
them work for 64bit.
Any help would be greatly appreciated.
Thanks,
--
William Hubler
Technical Services Specialist
Steward Observatory Mirror Lap
University of Arizona
_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org
_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org