Cannot create 2D datasets for strings

Hi,

How do I create a 2 dimensional dataset for strings in Java? I have it
working fine for integers, but once I change the type to
HDF5Constants.H5T_C_S1, the dataset seems to become a 1D array with a size
of the specified Y-dimension. I copied and pasted my code below for anyone
who can point out what I'm doing wrong.

Thanks in advance!

                final int RANK = 2;
        int filespace_id = -1;
        int type_id = -1;
        int dcpl_id = -1;
        int dataset_id = -1;
        Dataset dset = null;
        final int DIM_X = 32;
        final int DIM_Y = 100;
        final int CHUNK_X = 4;
        final int CHUNK_Y = 8;
        long[] chunk_dims = { CHUNK_X, CHUNK_Y };
        long[] dims = { DIM_X, DIM_Y };
        
        final int NDIMS = 2;
        
        // Create dataspace.
        try {
            filespace_id = H5.H5Screate_simple(RANK, dims, maxdims);
            type_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
            H5.H5Tset_size(type_id, HDF5Constants.H5T_VARIABLE);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        
        // Create the dataset creation property list, add the gzip
compression
        // filter.
        try {
            dcpl_id = H5.H5Pcreate(HDF5Constants.H5P_DATASET_CREATE);
            if(dcpl_id >= 0){
                H5.H5Pset_deflate(dcpl_id, 9);
                // Set the chunk size.
                H5.H5Pset_chunk(dcpl_id, NDIMS, chunk_dims);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        
        // Create the dataset.
        try {
            if ((file_id >= 0) && (filespace_id >= 0) && (dcpl_id >= 0))
                  dataset_id = H5.H5Dcreate(file_id, DATASETNAME,
                          type_id, filespace_id, HDF5Constants.H5P_DEFAULT,
dcpl_id, HDF5Constants.H5P_DEFAULT);
              dset = new H5ScalarDS(file, DATASETNAME, "/");
              Group pgroup = (Group) file.get("/");
              pgroup.addToMemberList(dset);
        }
        catch (Exception e) {
              e.printStackTrace();
        }
            // End access to the dataset and release resources used by it.
        try {
            if (dcpl_id >= 0)
                H5.H5Pclose(dcpl_id);
            if (type_id >= 0)
                  H5.H5Tclose(type_id);
            if (dataset_id >= 0)
                  dset.close(dataset_id);
            if (filespace_id >= 0)
                  H5.H5Sclose(filespace_id);
        }
          catch (Exception e) {
              e.printStackTrace();
        }

···

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Cannot-create-2D-datasets-for-strings-tp4025908.html
Sent from the hdf-forum mailing list archive at Nabble.com.

The following two examples create a 2D array of strings in file.
Once example uses the library calls directly, the other one use
the object layer. It would be better not to mix the two. Just
use one way or another.

  ================================Using the Library================================================
     private static void testH5Vlen(final String filename) throws Exception
     {
         String buf[] = {"Parting", "is such", "sweet", "sorrow."};

         // Case 1, may run into infinite loop
         // int tid = H5.H5Tvlen_create(HDF5Constants.H5T_C_S1);

         // Case 2, differnt failure on differnt platforms
         int tid = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
         H5.H5Tset_size(tid, HDF5Constants.H5T_VARIABLE);

         int fid = H5.H5Fcreate(filename, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
         int sid = H5.H5Screate_simple(2, new long[] {2,2}, null);
         int did = H5.H5Dcreate(fid, "/str", tid, sid, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);

         // write() fails on both case 1 and 2
         H5.H5Dwrite(did, tid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, buf);

         // clean up
         H5.H5Dclose(did);
         H5.H5Sclose(sid);
         H5.H5Tclose(tid);
         H5.H5Fclose(fid);
     }

  ================================Using the Object================================================
     private static void testH5VlenObj(final String fname) throws Exception
     {
         int strLen = -1;
         long[] dims = {2,2};
         String buf[] = {"Parting", "is such", "sweet", "sorrow."};

         // create a new file with a given file name.
         H5File testFile = new H5File(fname, H5File.CREATE);

         testFile.open();
         Group root = (Group)((javax.swing.tree.DefaultMutableTreeNode)testFile.getRootNode()).getUserObject();
         Datatype dtype = testFile.createDatatype(Datatype.CLASS_STRING, strLen, Datatype.NATIVE, Datatype.NATIVE);
         Dataset dataset = testFile.createScalarDS ("/str", root, dtype, dims, null, null, 0, buf);

         testFile.close();
      }

···

On 2/20/2013 5:40 PM, heatherk wrote:

Hi,

How do I create a 2 dimensional dataset for strings in Java? I have it
working fine for integers, but once I change the type to
HDF5Constants.H5T_C_S1, the dataset seems to become a 1D array with a size
of the specified Y-dimension. I copied and pasted my code below for anyone
who can point out what I'm doing wrong.

Thanks in advance!

                 final int RANK = 2;
         int filespace_id = -1;
         int type_id = -1;
         int dcpl_id = -1;
         int dataset_id = -1;
         Dataset dset = null;
         final int DIM_X = 32;
         final int DIM_Y = 100;
         final int CHUNK_X = 4;
         final int CHUNK_Y = 8;
         long[] chunk_dims = { CHUNK_X, CHUNK_Y };
         long[] dims = { DIM_X, DIM_Y };
                  final int NDIMS = 2;
                  // Create dataspace.
         try {
             filespace_id = H5.H5Screate_simple(RANK, dims, maxdims);
             type_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
             H5.H5Tset_size(type_id, HDF5Constants.H5T_VARIABLE);
         }
         catch (Exception e) {
             e.printStackTrace();
         }
                  // Create the dataset creation property list, add the gzip
compression
         // filter.
         try {
             dcpl_id = H5.H5Pcreate(HDF5Constants.H5P_DATASET_CREATE);
             if(dcpl_id >= 0){
                 H5.H5Pset_deflate(dcpl_id, 9);
                 // Set the chunk size.
                 H5.H5Pset_chunk(dcpl_id, NDIMS, chunk_dims);
             }
         }
         catch (Exception e) {
             e.printStackTrace();
         }
                  // Create the dataset.
         try {
             if ((file_id >= 0) && (filespace_id >= 0) && (dcpl_id >= 0))
                   dataset_id = H5.H5Dcreate(file_id, DATASETNAME,
                           type_id, filespace_id, HDF5Constants.H5P_DEFAULT,
dcpl_id, HDF5Constants.H5P_DEFAULT);
               dset = new H5ScalarDS(file, DATASETNAME, "/");
               Group pgroup = (Group) file.get("/");
               pgroup.addToMemberList(dset);
         }
         catch (Exception e) {
               e.printStackTrace();
         }
             // End access to the dataset and release resources used by it.
         try {
             if (dcpl_id >= 0)
                 H5.H5Pclose(dcpl_id);
             if (type_id >= 0)
                   H5.H5Tclose(type_id);
             if (dataset_id >= 0)
                   dset.close(dataset_id);
             if (filespace_id >= 0)
                   H5.H5Sclose(filespace_id);
         }
           catch (Exception e) {
               e.printStackTrace();
         }

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Cannot-create-2D-datasets-for-strings-tp4025908.html
Sent from the hdf-forum mailing list archive at Nabble.com.

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

Thanks Peter, but how would I write a buffer defined as String[][] buff?

···

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Cannot-create-2D-datasets-for-strings-tp4025908p4025910.html
Sent from the hdf-forum mailing list archive at Nabble.com.

we currently do you support writing 2 or more dimensional string arrays in hdf-java.
The work is still in our to-do list (we do not have the resource to raise its priority).

For now, you may have to use 1D array. 1D array has better performance when it
is mapped from Java to C.

Thanks
--pc

···

On 2/21/2013 11:05 AM, heatherk wrote:

Thanks Peter, but how would I write a buffer defined as String[][] buff?

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Cannot-create-2D-datasets-for-strings-tp4025908p4025910.html
Sent from the hdf-forum mailing list archive at Nabble.com.

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