JuliaIO / HDF5.jl

Save and load data in the HDF5 file format from Julia
https://juliaio.github.io/HDF5.jl
MIT License
383 stars 139 forks source link

Write out string attribute as ascii #1066

Closed lucasbanting closed 1 year ago

lucasbanting commented 1 year ago

How can I output a string attribute as ascii instead of utf-8?

I imagine it may involves creating a custom Datatype?

Forum post: https://discourse.julialang.org/t/writing-a-hdf5-attribute-as-an-ascii-string-in-hdf5-jl/98450

mkitti commented 1 year ago

Can you elaborate what you mean by "write out"? Do you mean you want to encode the attribute in the HDF5 as ASCII ?

lucasbanting commented 1 year ago

I’be added an attribute ‘Type’ to my file as:

GROUP “VTKHDF” { ATTRIBUTE “Type” { DATATYPE H5T_STRING { STRSIZE 16; STRPAD H5T_STR_NULLTERM; CSET H5T_CSET_UTF8; CTYPE H5T_C_S1; } DATASPACE SCALAR DATA { (0): “UnstructuredGrid” } }

And I would like CSET to be ASCII instead of UTF-8.

mkitti commented 1 year ago

Use H5Tset_cset: https://docs.hdfgroup.org/hdf5/v1_8/group___a_t_o_m.html#ga4909c0c3d97c3d212fee032cc8dc031a

mkitti commented 1 year ago

I'm not sure how you created the type, but here's the low level commands.

julia> h5t = HDF5.Datatype(HDF5.API.h5t_create(HDF5.API.H5T_STRING, 16))
HDF5.Datatype: H5T_STRING {
      STRSIZE 16;
      STRPAD H5T_STR_NULLTERM;
      CSET H5T_CSET_ASCII;
      CTYPE H5T_C_S1;
   }

julia> HDF5.API.h5t_set_cset(h5t, HDF5.API.H5T_CSET_UTF8); h5t
HDF5.Datatype: H5T_STRING {
      STRSIZE 16;
      STRPAD H5T_STR_NULLTERM;
      CSET H5T_CSET_UTF8;
      CTYPE H5T_C_S1;
   }

julia> HDF5.API.h5t_set_cset(h5t, HDF5.API.H5T_CSET_ASCII); h5t
HDF5.Datatype: H5T_STRING {
      STRSIZE 16;
      STRPAD H5T_STR_NULLTERM;
      CSET H5T_CSET_ASCII;
      CTYPE H5T_C_S1;
   }
lucasbanting commented 1 year ago

This is what I ended up doing:

type = "UnstructuredGrid"
        dspace = HDF5.dataspace(type)
        dtype = HDF5.datatype(type)
        HDF5.h5t_set_cset(dtype, HDF5.H5T_CSET_ASCII)
        attr = create_attribute(VTKHDF, "Type", dtype, dspace)
        write_attribute(attr, dtype, type)