Read and wrote string attributes

Hello!

How may I read and wrote string attributes od dataset?
I use HDF5DotNet and C# .net 2010.
I can't find an example and string type on H5T.H5Type.

Thank you.

Pavel, how are you? If you want to write a fixed-length string scalar
attribute,
here's a snippet of IronPython code that will get you started.

def addScalarStringAttribute(dset):

    string ='ABCD'

    dspace = H5S.create(H5S.H5SClass.SCALAR)
    dtype = H5T.copy(H5T.H5Type.C_S1)
    H5T.setSize(dtype, 5)
    attr = H5A.create(dset, 'Character attribute', dtype, dspace);

    enc = System.Text.ASCIIEncoding()
    H5A.write(attr, dtype, H5Array[System.Byte](enc.GetBytes(string)))

    H5A.close(attr)
    H5T.close(dtype)
    H5S.close(dspace)
    return None

Best, G.

···

-----Original Message-----
From: hdf-forum-bounces@hdfgroup.org [mailto:hdf-forum-bounces@hdfgroup.org]
On Behalf Of Pavel Rudchenko
Sent: Tuesday, March 01, 2011 1:16 PM
To: hdf-forum@hdfgroup.org
Subject: [Hdf-forum] Read and wrote string attributes

Hello!

How may I read and wrote string attributes od dataset?
I use HDF5DotNet and C# .net 2010.
I can't find an example and string type on H5T.H5Type.

Thank you.

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

Hi,

I'm having a similar problem but reading a string write by pytables from
C.

Any clue how to do this ?

More info here:
Thanks,

···

On Tue, 2011-03-01 at 15:46 -0600, Gerd Heber wrote:

Pavel, how are you? If you want to write a fixed-length string scalar
attribute,
here's a snippet of IronPython code that will get you started.

def addScalarStringAttribute(dset):

    string ='ABCD'

    dspace = H5S.create(H5S.H5SClass.SCALAR)
    dtype = H5T.copy(H5T.H5Type.C_S1)
    H5T.setSize(dtype, 5)
    attr = H5A.create(dset, 'Character attribute', dtype, dspace);

    enc = System.Text.ASCIIEncoding()
    H5A.write(attr, dtype, H5Array[System.Byte](enc.GetBytes(string)))

    H5A.close(attr)
    H5T.close(dtype)
    H5S.close(dspace)
    return None

Best, G.

-----Original Message-----
From: hdf-forum-bounces@hdfgroup.org [mailto:hdf-forum-bounces@hdfgroup.org]
On Behalf Of Pavel Rudchenko
Sent: Tuesday, March 01, 2011 1:16 PM
To: hdf-forum@hdfgroup.org
Subject: [Hdf-forum] Read and wrote string attributes

Hello!

How may I read and wrote string attributes od dataset?
I use HDF5DotNet and C# .net 2010.
I can't find an example and string type on H5T.H5Type.

Thank you.

_______________________________________________
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

Hello, thank you!

Now I can write string attributes.
But can't read it back.
My InPtr pointer is 0:

                H5FileId fileId = H5F.open("temp.hdf", H5F.OpenMode.ACC_RDONLY);
                H5DataSetId dsetIdGeo = H5D.open(fileId, DSET2_NAME);
                H5DataTypeId typeAttrIdGeo = H5T.copy(H5T.H5Type.C_S1);
                H5ObjectInfo oinfoGeo = H5O.getInfo(dsetIdGeo);

                string sourceName = null;

                H5AttributeId attrId = H5A.open(dsetIdGeo, ATTR2_1_NAME);

                unsafe
                {
                    Chararray[] carray = new Chararray[2];
                    H5A.read(attrId, typeAttrIdGeo, new
H5Array<Chararray>(carray));
                    IntPtr ipp = (IntPtr)carray[0].RecordedText;
                    sourceName = Marshal.PtrToStringAnsi(ipp);
                }

                H5A.close(attrId);

                H5T.close(typeAttrIdGeo);
                H5D.close(dsetIdGeo);
                H5F.close(fileId);

                if (sourceName != null)
                    System.Console.WriteLine("sourceName = " + sourceName);

Why?

I can read string datasets (as in primer Program.cs in 'string'
folder), but not string attributes of datasets.

···

2011/3/2 Gerd Heber <gheber@hdfgroup.org>:

Pavel, how are you? If you want to write a fixed-length string scalar
attribute,
here's a snippet of IronPython code that will get you started.

def addScalarStringAttribute(dset):

string ='ABCD'

dspace = H5S.create(H5S.H5SClass.SCALAR)
dtype = H5T.copy(H5T.H5Type.C_S1)
H5T.setSize(dtype, 5)
attr = H5A.create(dset, 'Character attribute', dtype, dspace);

enc = System.Text.ASCIIEncoding()
H5A.write(attr, dtype, H5Array[System.Byte](enc.GetBytes(string)))

H5A.close(attr)
H5T.close(dtype)
H5S.close(dspace)
return None

Best, G.

-----Original Message-----
From: hdf-forum-bounces@hdfgroup.org [mailto:hdf-forum-bounces@hdfgroup.org]
On Behalf Of Pavel Rudchenko
Sent: Tuesday, March 01, 2011 1:16 PM
To: hdf-forum@hdfgroup.org
Subject: [Hdf-forum] Read and wrote string attributes

Hello!

How may I read and wrote string attributes od dataset?
I use HDF5DotNet and C# .net 2010.
I can't find an example and string type on H5T.H5Type.

Thank you.

_______________________________________________
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

Sorry guys, CTRL+Enter is so close to CTRL+V

Here there is more info:
http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/2011-February/004293.html

Regards,

···

On Thu, 2011-03-03 at 15:38 +0000, Felipe Barriga Richards wrote:

Hi,

I'm having a similar problem but reading a string write by pytables from
C.

Any clue how to do this ?

More info here:
Thanks,
On Tue, 2011-03-01 at 15:46 -0600, Gerd Heber wrote:
> Pavel, how are you? If you want to write a fixed-length string scalar
> attribute,
> here's a snippet of IronPython code that will get you started.
>
> def addScalarStringAttribute(dset):
>
> string ='ABCD'
>
> dspace = H5S.create(H5S.H5SClass.SCALAR)
> dtype = H5T.copy(H5T.H5Type.C_S1)
> H5T.setSize(dtype, 5)
> attr = H5A.create(dset, 'Character attribute', dtype, dspace);
>
> enc = System.Text.ASCIIEncoding()
> H5A.write(attr, dtype, H5Array[System.Byte](enc.GetBytes(string)))
>
> H5A.close(attr)
> H5T.close(dtype)
> H5S.close(dspace)
> return None
>
> Best, G.
>
>
>
> -----Original Message-----
> From: hdf-forum-bounces@hdfgroup.org [mailto:hdf-forum-bounces@hdfgroup.org]
> On Behalf Of Pavel Rudchenko
> Sent: Tuesday, March 01, 2011 1:16 PM
> To: hdf-forum@hdfgroup.org
> Subject: [Hdf-forum] Read and wrote string attributes
>
> Hello!
>
> How may I read and wrote string attributes od dataset?
> I use HDF5DotNet and C# .net 2010.
> I can't find an example and string type on H5T.H5Type.
>
> Thank you.
>

--
Felipe Barriga Richards

Pavel, how are you?

From: hdf-forum-bounces@hdfgroup.org

[mailto:hdf-forum-bounces@hdfgroup.org] On Behalf Of Pavel Rudchenko

Sent: Sunday, March 06, 2011 4:59 PM
To: HDF Users Discussion List
Subject: Re: [Hdf-forum] Read and wrote string attributes

Hello, thank you!

Now I can write string attributes.
But can't read it back...

Here's another snippet of IronPython that will get you started.
The method iterates over a dataset's (dset) attributes and reads/prints
its fixed-length string attributes:

def findScalarFixedStringAttribute(dset):

    info = H5O.getInfo(dset)
    for i in range(info.nAttributes):
        attr = H5A.openByIndex(dset, '.', H5IndexType.CRT_ORDER,
                               H5IterationOrder.INCREASING, Int64(i))
        dtype = H5A.getType(attr)
        tclass = H5T.getClass(dtype)

        # ignore variable length strings
        if tclass == H5T.H5TClass.STRING and not
H5T.isVariableString(dtype):

            mtype = H5T.getNativeType(dtype, H5T.Direction.ASCEND)
            buffer = Array.CreateInstance(Byte, H5T.getSize(mtype))
            H5A.read(attr, mtype, H5Array[Byte](buffer))

            enc = System.Text.ASCIIEncoding()
            fmt = 'Found string attribute; its index is %d , value = %s'
            print fmt % (i, enc.GetString(buffer))

            H5T.close(mtype)

        H5T.close(dtype)
        H5A.close(attr)

    return None

Best, G.

···

-----Original Message-----

Hello, Gerd!

Thank you very much! Now all is working right! :slight_smile:

Good luck.

···

2011/3/7 Gerd Heber <gheber@hdfgroup.org>:

Pavel, how are you?

-----Original Message-----
From: hdf-forum-bounces@hdfgroup.org

[mailto:hdf-forum-bounces@hdfgroup.org] On Behalf Of Pavel Rudchenko

Sent: Sunday, March 06, 2011 4:59 PM
To: HDF Users Discussion List
Subject: Re: [Hdf-forum] Read and wrote string attributes

Hello, thank you!

Now I can write string attributes.
But can't read it back...

Here's another snippet of IronPython that will get you started.
The method iterates over a dataset's (dset) attributes and reads/prints
its fixed-length string attributes:

def findScalarFixedStringAttribute(dset):

info = H5O.getInfo(dset)
for i in range(info.nAttributes):
attr = H5A.openByIndex(dset, '.', H5IndexType.CRT_ORDER,
H5IterationOrder.INCREASING, Int64(i))
dtype = H5A.getType(attr)
tclass = H5T.getClass(dtype)

   \# ignore variable length strings
   if tclass == H5T\.H5TClass\.STRING and not

H5T.isVariableString(dtype):

       mtype = H5T\.getNativeType\(dtype, H5T\.Direction\.ASCEND\)
       buffer = Array\.CreateInstance\(Byte, H5T\.getSize\(mtype\)\)
       H5A\.read\(attr, mtype, H5Array\[Byte\]\(buffer\)\)

       enc = System\.Text\.ASCIIEncoding\(\)
       fmt = &#39;Found string attribute; its index is %d , value = %s&#39;
       print fmt % \(i, enc\.GetString\(buffer\)\)

       H5T\.close\(mtype\)

   H5T\.close\(dtype\)
   H5A\.close\(attr\)

return None

Best, G.

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org