newbie: updating a cell in a table of integers

(cross posted http://stackoverflow.com/questions/24883461 )

Hi all,

I'm a newbie with HDF5.

I built a *5x4* table.

Each cell of the table contains *3* integers

I'm trying to write a function `setyx` that would update a cell of the table at y,x

I wrote the C code below but it raises some errors:

  #000: H5Dio.c line 225 in H5Dwrite(): can't prepare for writing data

     major: Dataset
     minor: Write failed
   #001: H5Dio.c line 347 in H5D__pre_write(): can't write data
     major: Dataset
     minor: Write failed
   #002: H5Dio.c line 685 in H5D__write(): src and dest data spaces have different sizes
     major: Invalid arguments to routine
     minor: Bad value|

what is the correct way to update a given cell at |(y,x)| ?

/* -------------------------------------------------------------------------------------------------------------*/
include "hdf5.h"

/** set 3 'int' values at y/x */
static void setyx(hid_t dataset, hid_t dataspace,
                   int y,int x,
                   int i1,int i2,int i3)
     {
     int values[3]={i1,i2,i3};
     hsize_t starts[3]={y,x,0};
     hsize_t counts[3]={1,1,1};
     hsize_t dims_memory[3]={1,1,3};

     H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, starts, NULL, counts, NULL);
     hid_t filespace = H5Dget_space(dataset);
     hid_t memspace = H5Screate_simple(3, dims_memory, NULL);

     H5Dwrite(dataset,
         H5T_NATIVE_INT,
         memspace,
         filespace,
         H5P_DEFAULT,
         values);

      H5Sclose(memspace);
      H5Sclose(filespace);
     }

int main (void)
      {
      hid_t file, dataset;
      hid_t dataspace;
      hsize_t dims[3]={5,4,3};

      file = H5Fcreate("test.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
      dataspace = H5Screate_simple(3, dims, NULL);
      dataset = H5Dcreate(file, "/test", H5T_NATIVE_INT, dataspace,
                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
      setyx(dataset,dataspace,2,1, 100,200,300);

      H5Sclose(dataspace);
      H5Dclose(dataset);
      H5Fclose(file);
      return 0;
     }
/* -------------------------------------------------------------------------------------------------------------*/

Hi Pierre,

The code should work (it works for me) after fixing 2 problems.

1.) hsize_t counts[3]={1,1,1}; in line 12 should be hsize_t counts[3]={1,1,3};

The hyperslab that you want to write is the 3 elements at y,x,0 - y,x,2, so you start at y,x,0, write 1x1x1 blocks for a count of 3 in the third dimension while not changing the first 2 dimensions.

2.) Your code selects a hyperslab in "dataspace" in line 15, which will be the correct hyperslab after making correction #1. But then it gets a new space "filespace" which is used for the call to HDwrite(). While the "filespace" id identifies the same dataspace as "dataspace", no hyperslab has been selected for it, hence the error "src and dest data spaces have different sizes". You should eliminate "filespace" entirely and in its place in the call to H5Dwrite use the "dataspace" that you have passed in and used to select the hyperslab.

While the program will then work, the memory dataspace could also be simplified since 2 of the dimensions are 1:
    change line 13 to hsize_t dims_memory[1]={3};
          and line 18 to hid_t memspace = H5Screate_simple(1, dims_memory, NULL);

For reference, there are example programs that read and write hyperslabs in hdf5 at these links:
http://www.hdfgroup.org/HDF5/Tutor/select.html
http://www.hdfgroup.org/HDF5/Tutor/phype.html
http://www.hdfgroup.org/ftp/HDF5/examples/misc-examples/#dspc

Hope this helps,
Larry

···

________________________________________
From: Hdf-forum <hdf-forum-bounces@lists.hdfgroup.org> on behalf of Pierre Lindenbaum <pierre.lindenbaum@univ-nantes.fr>
Sent: Tuesday, July 22, 2014 7:09 AM
To: hdf-forum@lists.hdfgroup.org
Subject: [Hdf-forum] newbie: updating a cell in a table of integers

(cross posted http://stackoverflow.com/questions/24883461 )

Hi all,

I'm a newbie with HDF5.

I built a *5x4* table.

Each cell of the table contains *3* integers

I'm trying to write a function `setyx` that would update a cell of the
table at y,x

I wrote the C code below but it raises some errors:

  #000: H5Dio.c line 225 in H5Dwrite(): can't prepare for writing data

     major: Dataset
     minor: Write failed
   #001: H5Dio.c line 347 in H5D__pre_write(): can't write data
     major: Dataset
     minor: Write failed
   #002: H5Dio.c line 685 in H5D__write(): src and dest data spaces have different sizes
     major: Invalid arguments to routine
     minor: Bad value|

what is the correct way to update a given cell at |(y,x)| ?

/*
-------------------------------------------------------------------------------------------------------------*/
include "hdf5.h"

/** set 3 'int' values at y/x */
static void setyx(hid_t dataset, hid_t dataspace,
                   int y,int x,
                   int i1,int i2,int i3)
     {
     int values[3]={i1,i2,i3};
     hsize_t starts[3]={y,x,0};
     hsize_t counts[3]={1,1,1};
     hsize_t dims_memory[3]={1,1,3};

     H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, starts, NULL,
counts, NULL);
     hid_t filespace = H5Dget_space(dataset);
     hid_t memspace = H5Screate_simple(3, dims_memory, NULL);

     H5Dwrite(dataset,
         H5T_NATIVE_INT,
         memspace,
         filespace,
         H5P_DEFAULT,
         values);

      H5Sclose(memspace);
      H5Sclose(filespace);
     }

int main (void)
      {
      hid_t file, dataset;
      hid_t dataspace;
      hsize_t dims[3]={5,4,3};

      file = H5Fcreate("test.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
      dataspace = H5Screate_simple(3, dims, NULL);
      dataset = H5Dcreate(file, "/test", H5T_NATIVE_INT, dataspace,
                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
      setyx(dataset,dataspace,2,1, 100,200,300);

      H5Sclose(dataspace);
      H5Dclose(dataset);
      H5Fclose(file);
      return 0;
     }
/*
-------------------------------------------------------------------------------------------------------------*/

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@lists.hdfgroup.org
http://mail.lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5