Hello Chris,

Is there a way to extend the byte size of an existing string datatype?

You can't change the datatype of a dataset, but you can read your data in, convert your data to a new datatype (using H5Tconvert), and write that data to another dataset with the new datatype.

Attached is an example of that. (I'll include it at the bottom of this
message, too.)

Will that work for you?  (You could then delete the original dataset
(see H5Ldelete) and use the h5repack utility to get rid of the unused
space left by the deleted object.)

-Barbara Jones
HDF Helpdesk

For example, I have a string column where the byte size is 3, but I extend the dataset in order to add more values, and now want to write a value that is 5 bytes. I've looked through the documentation to see if this is possible but haven't had any luck. I do know that I could instead use the variable length string data type to handle this. However, I am working with very large datasets, and there seems to be a very significant read/write performance penalty using variable length string data type.

I have an example test case below illustrating what I am trying to do, any 
pointers would be greatly appreciated.

Thanks,
Chris

/********************************************************************/
/*
   Create a dataset with an array of strings of size 5.
   Convert the data to an array of strings of size 8, and write it
   to a new dataset of that size.
*/
/********************************************************************/

#include "hdf5.h"
#include <string.h>
#define FILE "cnvstr.h5"

main() {

   hid_t       file_id, dataset_id, dataspace_id, dataset_id1;
   herr_t      status;
   char        buf[]={"test left call mesh"};
   char        newbuf[50];

   hsize_t     dims[2] = {2,2};
   hid_t       dtype, dtype1;
   size_t      size;

   file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
   printf ("H5Fcreate returns: %i\n", file_id);
   dataspace_id = H5Screate_simple (2, dims, NULL);
   printf ("H5Screate_simple returns: %i\n", dataspace_id);

   dtype = H5Tcopy (H5T_C_S1);
   size = 5;
   status = H5Tset_size (dtype, size);

   dataset_id = H5Dcreate(file_id, "StrData", dtype, dataspace_id,
                   H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

   printf ("H5Dcreate returns: %i\n", dataset_id);
   status = H5Dwrite (dataset_id, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
   printf ("H5Dwrite returns: %i\n", status);

   dtype1 = H5Tcopy (H5T_C_S1);
   size = 8;
   status = H5Tset_size (dtype1, size);

   dataset_id1 = H5Dcreate(file_id, "StrDatLong", dtype1, dataspace_id,
                            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
   printf ("H5Dcreate returns: %i\n", dataset_id1);

   /* Copy buffer of data to new buffer and convert it to new datatype size */
   strncpy (newbuf, buf, 20);
   newbuf[20] = '\0';
   status = H5Tconvert (dtype, dtype1, size, newbuf, NULL, H5P_DEFAULT);
   printf ("H5Tconvert returns: %i\n", status);

   status = H5Dwrite (dataset_id1, dtype1, H5S_ALL, H5S_ALL, H5P_DEFAULT, 
newbuf);
   printf ("H5Dwrite returns: %i\n", status);

   status = H5Tclose (dtype);
   status = H5Tclose (dtype1);
   status = H5Dclose(dataset_id);
   status = H5Dclose(dataset_id1);
   status = H5Sclose(dataspace_id);
   status = H5Fclose(file_id);
}
/********************************************************************/
/*
   Create a dataset with an array of strings of size 5. 
   Convert the data to an array of strings of size 8, and write it
   to a new dataset of that size.
*/
/********************************************************************/

#include "hdf5.h"
#include <string.h>
#define FILE "cnvstr.h5"

main() {

   hid_t       file_id, dataset_id, dataspace_id, dataset_id1;
   herr_t      status;
   char        buf[]={"test left call mesh"};
   char        newbuf[50];

   hsize_t     dims[2] = {2,2};
   hid_t       dtype, dtype1;
   size_t      size;

   file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
   printf ("H5Fcreate returns: %i\n", file_id);
   dataspace_id = H5Screate_simple (2, dims, NULL);
   printf ("H5Screate_simple returns: %i\n", dataspace_id);

   dtype = H5Tcopy (H5T_C_S1);
   size = 5;
   status = H5Tset_size (dtype, size);

   dataset_id = H5Dcreate(file_id, "StrData", dtype, dataspace_id, 
                   H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

   printf ("H5Dcreate returns: %i\n", dataset_id);
   status = H5Dwrite (dataset_id, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
   printf ("H5Dwrite returns: %i\n", status);

   dtype1 = H5Tcopy (H5T_C_S1);
   size = 8;
   status = H5Tset_size (dtype1, size);

   dataset_id1 = H5Dcreate(file_id, "StrDatLong", dtype1, dataspace_id, 
                            H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
   printf ("H5Dcreate returns: %i\n", dataset_id1);

   /* Copy buffer of data to new buffer and convert it to new datatype size */
   strncpy (newbuf, buf, 20);
   newbuf[20] = '\0';
   status = H5Tconvert (dtype, dtype1, size, newbuf, NULL, H5P_DEFAULT);
   printf ("H5Tconvert returns: %i\n", status);

   status = H5Dwrite (dataset_id1, dtype1, H5S_ALL, H5S_ALL, H5P_DEFAULT, 
newbuf);
   printf ("H5Dwrite returns: %i\n", status);

   status = H5Tclose (dtype);
   status = H5Tclose (dtype1);
   status = H5Dclose(dataset_id);
   status = H5Dclose(dataset_id1);
   status = H5Sclose(dataspace_id);
   status = H5Fclose(file_id);
}

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

Reply via email to