The best way to write ref attributes is to use a byte buffer.
However, if an attribute is a single value of reference, using
the object path will also work. Here is an example of using
both ways.

=======================================
        H5File file = new H5File(fname, H5File.CREATE);
        file.open();

        // create a ref dataset
        Group grp = file.createGroup("grp", null);
Dataset ds = file.createScalarDS("dset", grp, new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), dims, maxdims, null, 0, data); ref_buf[0] = H5.H5Rcreate(file.getFID(), grp.getFullName(), HDF5Constants.H5R_OBJECT, -1); ref_buf[1] = H5.H5Rcreate(file.getFID(), ds.getFullName(), HDF5Constants.H5R_OBJECT, -1); ds = file.createScalarDS(dname, null, new H5Datatype(Datatype.CLASS_REFERENCE, -1, -1, -1), new long[] {2}, null, null, 0, ref_buf);

        // create ref attributes
Datatype attr_dtype = file.createDatatype( Datatype.CLASS_REFERENCE, Datatype.NATIVE, Datatype.NATIVE, Datatype.NATIVE);
        Attribute attr = new Attribute("ref", attr_dtype, new long[] {1});
        attr.setValue(ds.getFullName());
        file.writeAttribute(ds, attr, false);
        attr = new Attribute("refs", attr_dtype, new long[] {2});
        attr.setValue(ref_buf);
        ds.writeMetadata(attr);

        file.close();
===================================================

On 3/30/2011 9:11 AM, Konrad Hinsen wrote:
On 28 Mar, 2011, at 19:11 , Peter Cao wrote:

Try this:

===============
        // open the file and the dataset with refs
        file.open();
        ds = (H5ScalarDS)file.get(dname);
         long[] refs = (long[])ds.getData();

         // use low level API function, H5.H5Rget_name
           String[] name = {""};;
         for (int i=0; i<refs.length; i++) {
             H5.H5Rget_name(file.getFID(), HDF5Constants.H5R_OBJECT, 
HDFNativeData.longToByte(refs[i]), name, 32);
            System.out.println(name[0]);
        }

         // if file.open() was called, search objects in memory by high level 
function, findObject()
        long[] oid = new long[1];
        for (int i=0; i<refs.length; i++) {
            oid[0] = refs[i];
             HObject obj = FileFormat.findObject(file, oid);
            System.out.println(obj.getFullName());
        }
==============
I am now at the point where I would like to write reference attributes as well. 
But what do I supply as the attribute value? I first tried to provide the 
objects themselves, but that results in invalid references. A look at the 
source code (H5File.java) shows that there is a special case for a single 
reference which can be supplied as a string (path to the object). So I tried an 
array of strings, but that also leads to invalid references. Do I have to use 
the low-level routines again for some conversion?

Konrad.
--
---------------------------------------------------------------------
Konrad Hinsen
Centre de Biophysique Moléculaire, CNRS Orléans
Synchrotron Soleil - Division Expériences
Saint Aubin - BP 48
91192 Gif sur Yvette Cedex, France
Tel. +33-1 69 35 97 15
E-Mail: research AT khinsen DOT fastmail DOT net
---------------------------------------------------------------------




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


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

Reply via email to