HDF5:更新整数表中的单元格

时间:2014-07-22 09:21:02

标签: c hdf5

注意:在http://mail.lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2014-July/007960.html

发布

使用hdf5,我构建了一个5x4表。表格的每个单元格都包含3 integers 我正在尝试编写一个函数 setyx ,它会在y,x更新表格的单元格 这是我到目前为止写的C代码。

#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;
    }

但它引发了一些错误:

  #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

(y,x)更新给定单元格的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

问题已在HDF5邮件列表中得到解答:http://mail.lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2014-July/007966.html

以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include "hdf5.h"

#define VERIFY(a) do { if((a)<0) { fprintf(stderr,"Failure line %d.\n",__LINE__); exit(-1);}}while(0)

/** 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,3};

    hsize_t  dims_memory[3]={1,1,3};



    hid_t filespace = H5Dget_space(dataset);
    hid_t  memspace  = H5Screate_simple(3, dims_memory, NULL); 
    VERIFY(memspace);

    VERIFY(H5Sselect_hyperslab(dataspace,
            H5S_SELECT_SET,
            starts, NULL, 
            counts, NULL));

    VERIFY(H5Dwrite(dataset,
        H5T_NATIVE_INT,
        memspace,
        dataspace,
        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;
    }