Problem writing array to an HDF5 file.

Hi all,
I've just started using the HDF5 C interface and I've been having a few
problems. I'm sure it's some simple thing that I've overlooked, but it'd be
great if you guys could help find what I've done wrong!!

I can read data with no problems, but when I want to write my results to an
HDF5 file things go wrong. I'm trying to write a 2000x2200 array, but the
lines being output to the H5 file are not what I expect.
I have created an example that is supposed to write the line number to each
column in an H5 file (datatype: H5T_NATIVE_USHORT) but rather than the
expected results I get something like this:
0 0 0 0 0 0 0 .....
0 0 4009 0 1 1 1 .....
1 1 1 1 0 0 4009 .....
2 2 2 2 2 2 2 .....
3 3 3 3 3 3 3 .....

The '4009' moves 4 columns to the right with each line.

Any ideas what the problem could be? The same issue exists (with different
numbers, of course) when I try with a floating point data type.

I've included the sample code I used to generate the data below. Originally
there was lots of error checking (based on the status returned from the HDF
commands) for each step of the HDF process, but for brevity I remove those
checks as they showed no problems:

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

int Save_f_Data(hid_t,char *,short unsigned int **,int,int);
void alloc_f_2dim (short unsigned int ***,int,int);

int main()
{
  char *name="TESTARR";
  int xsize=2200;
  int ysize=2000;
  int retval;
  hid_t outfile;
  herr_t status;
    
outfile=H5Fcreate("/home/simon/Desktop/TEST/outtest.h5",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
  short unsigned int **testarr;
  int i,j;
  
  alloc_f_2dim (&testarr,xsize,ysize);
  for (i=0;i<xsize;i++) for (j=0;j<ysize;j++) testarr[i][j]=i;
  
  retval=Save_f_Data(outfile,name,testarr,xsize,ysize);
  status=H5Fclose(outfile);

  printf("Running!\n");
  return 0;
}

int Save_f_Data(hid_t outfile,char *setname,short unsigned int **
procarr,int xsize, int ysize)
{

  herr_t status;
  hid_t dataspace,dataset,datatype;
  hsize_t dims[2]={xsize,ysize};
  
  dataspace=H5Screate_simple(2,dims,dims);
  dataset=H5Dcreate(outfile,setname,H5T_NATIVE_USHORT,dataspace,H5P_DEFAULT);

status=H5Dwrite(dataset,H5T_NATIVE_USHORT,H5S_ALL,H5S_ALL,H5P_DEFAULT,procarr[0]);
  status=H5Sclose(dataspace);
  status=H5Dclose(dataset);
  return 0;
}

void alloc_f_2dim (short unsigned int ***ptr,int nrh,int nch)
{
   int i = 0;
   short unsigned int **p = NULL;

   p = (short unsigned int **) calloc ((nrh + 1), sizeof (short unsigned int
*));
   if (!p) {
      fprintf(stderr,"allocation failure dim0 in alloc_d_2dim()");
       
   }
   for (i = 0; i <= nrh; i++) {
      p[i] = (short unsigned int *) calloc ((nch + 1), sizeof (short
unsigned int));
      if (!p[i]) {
   fprintf(stderr,"allocation dim1 in alloc_d_2dim()");
          
      }
   }
   *ptr = p;
   return;
}

···

--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Problem-writing-array-to-an-HDF5-file-tp1464609p1464609.html
Sent from the hdf-forum mailing list archive at Nabble.com.

Hi,
  Your alloc_f_2dim() routine is allocating a nested 2-D array of pointers-to-arrays, instead of a contiguous 2-D array of shorts. It's likely that changing that will address your problem.

  Quincey

···

On Sep 13, 2010, at 12:05 PM, Simonpro wrote:

Hi all,
I've just started using the HDF5 C interface and I've been having a few
problems. I'm sure it's some simple thing that I've overlooked, but it'd be
great if you guys could help find what I've done wrong!!

I can read data with no problems, but when I want to write my results to an
HDF5 file things go wrong. I'm trying to write a 2000x2200 array, but the
lines being output to the H5 file are not what I expect.
I have created an example that is supposed to write the line number to each
column in an H5 file (datatype: H5T_NATIVE_USHORT) but rather than the
expected results I get something like this:
0 0 0 0 0 0 0 .....
0 0 4009 0 1 1 1 .....
1 1 1 1 0 0 4009 .....
2 2 2 2 2 2 2 .....
3 3 3 3 3 3 3 .....

The '4009' moves 4 columns to the right with each line.

Any ideas what the problem could be? The same issue exists (with different
numbers, of course) when I try with a floating point data type.

I've included the sample code I used to generate the data below. Originally
there was lots of error checking (based on the status returned from the HDF
commands) for each step of the HDF process, but for brevity I remove those
checks as they showed no problems:

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

int Save_f_Data(hid_t,char *,short unsigned int **,int,int);
void alloc_f_2dim (short unsigned int ***,int,int);

int main()
{
  char *name="TESTARR";
  int xsize=2200;
  int ysize=2000;
  int retval;
  hid_t outfile;
  herr_t status;
    
outfile=H5Fcreate("/home/simon/Desktop/TEST/outtest.h5",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
  short unsigned int **testarr;
  int i,j;
  
  alloc_f_2dim (&testarr,xsize,ysize);
  for (i=0;i<xsize;i++) for (j=0;j<ysize;j++) testarr[i][j]=i;
  
  retval=Save_f_Data(outfile,name,testarr,xsize,ysize);
  status=H5Fclose(outfile);

  printf("Running!\n");
  return 0;
}

int Save_f_Data(hid_t outfile,char *setname,short unsigned int **
procarr,int xsize, int ysize)
{

  herr_t status;
  hid_t dataspace,dataset,datatype;
  hsize_t dims[2]={xsize,ysize};
  
  dataspace=H5Screate_simple(2,dims,dims);
  dataset=H5Dcreate(outfile,setname,H5T_NATIVE_USHORT,dataspace,H5P_DEFAULT);

status=H5Dwrite(dataset,H5T_NATIVE_USHORT,H5S_ALL,H5S_ALL,H5P_DEFAULT,procarr[0]);
  status=H5Sclose(dataspace);
  status=H5Dclose(dataset);
  return 0;
}

void alloc_f_2dim (short unsigned int ***ptr,int nrh,int nch)
{
  int i = 0;
  short unsigned int **p = NULL;

  p = (short unsigned int **) calloc ((nrh + 1), sizeof (short unsigned int
*));
  if (!p) {
     fprintf(stderr,"allocation failure dim0 in alloc_d_2dim()");
       
  }
  for (i = 0; i <= nrh; i++) {
     p[i] = (short unsigned int *) calloc ((nch + 1), sizeof (short
unsigned int));
     if (!p[i]) {
   fprintf(stderr,"allocation dim1 in alloc_d_2dim()");
          
     }
  }
  *ptr = p;
  return;
}
--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Problem-writing-array-to-an-HDF5-file-tp1464609p1464609.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 for your response.

So rather then creating a theoretical 2000x2200 array I should instead create a single 4400000 array?

Simon.

Quincey Koziol 09/13/10 12:28 PM >>>

Hi,
    Your alloc_f_2dim() routine is allocating a nested 2-D array of pointers-to-arrays, instead of a contiguous 2-D array of shorts. It's likely that changing that will address your problem.

    Quincey

···

On Sep 13, 2010, at 12:05 PM, Simonpro wrote:

Hi all,
I've just started using the HDF5 C interface and I've been having a few
problems. I'm sure it's some simple thing that I've overlooked, but it'd be
great if you guys could help find what I've done wrong!!

I can read data with no problems, but when I want to write my results to an
HDF5 file things go wrong. I'm trying to write a 2000x2200 array, but the
lines being output to the H5 file are not what I expect.
I have created an example that is supposed to write the line number to each
column in an H5 file (datatype: H5T_NATIVE_USHORT) but rather than the
expected results I get something like this:
0 0 0 0 0 0 0 .....
0 0 4009 0 1 1 1 .....
1 1 1 1 0 0 4009 .....
2 2 2 2 2 2 2 .....
3 3 3 3 3 3 3 .....

The '4009' moves 4 columns to the right with each line.

Any ideas what the problem could be? The same issue exists (with different
numbers, of course) when I try with a floating point data type.

I've included the sample code I used to generate the data below. Originally
there was lots of error checking (based on the status returned from the HDF
commands) for each step of the HDF process, but for brevity I remove those
checks as they showed no problems:

#include
#include

int Save_f_Data(hid_t,char *,short unsigned int **,int,int);
void alloc_f_2dim (short unsigned int ***,int,int);

int main()
{
    char *name="TESTARR";
    int xsize=2200;
    int ysize=2000;
    int retval;
    hid_t outfile;
    herr_t status;
        
outfile=H5Fcreate("/home/simon/Desktop/TEST/outtest.h5",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
    short unsigned int **testarr;
    int i,j;
    
    alloc_f_2dim (&testarr,xsize,ysize);
    for (i=0;i
    retval=Save_f_Data(outfile,name,testarr,xsize,ysize);
    status=H5Fclose(outfile);

    printf("Running!\n");
    return 0;
}

int Save_f_Data(hid_t outfile,char *setname,short unsigned int **
procarr,int xsize, int ysize)
{

    herr_t status;
    hid_t dataspace,dataset,datatype;
    hsize_t dims[2]={xsize,ysize};
    
    dataspace=H5Screate_simple(2,dims,dims);
    dataset=H5Dcreate(outfile,setname,H5T_NATIVE_USHORT,dataspace,H5P_DEFAULT);

status=H5Dwrite(dataset,H5T_NATIVE_USHORT,H5S_ALL,H5S_ALL,H5P_DEFAULT,procarr[0]);
    status=H5Sclose(dataspace);
    status=H5Dclose(dataset);
    return 0;
}

void alloc_f_2dim (short unsigned int ***ptr,int nrh,int nch)
{
  int i = 0;
  short unsigned int **p = NULL;

  p = (short unsigned int **) calloc ((nrh + 1), sizeof (short unsigned int
*));
  if (!p) {
     fprintf(stderr,"allocation failure dim0 in alloc_d_2dim()");
           
  }
  for (i = 0; i <= nrh; i++) {
     p[i] = (short unsigned int *) calloc ((nch + 1), sizeof (short
unsigned int));
     if (!p[i]) {
     fprintf(stderr,"allocation dim1 in alloc_d_2dim()");
              
     }
  }
  *ptr = p;
  return;
}
--
View this message in context: http://hdf-forum.184993.n3.nabble.com/Problem-writing-array-to-an-HDF5-file-tp1464609p1464609.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

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

Hi Simon,

Thanks for your response.

So rather then creating a theoretical 2000x2200 array I should instead create a single 4400000 array?

  Yup. Or, I suppose, you could have many calls to H5Dwrite(), but I wouldn't suggest that.

    Quincey

···

On Sep 13, 2010, at 1:19 PM, Simon R. Proud wrote:

Simon.

>>> Quincey Koziol <koziol@hdfgroup.org> 09/13/10 12:28 PM >>>
Hi,
    Your alloc_f_2dim() routine is allocating a nested 2-D array of pointers-to-arrays, instead of a contiguous 2-D array of shorts. It's likely that changing that will address your problem.

    Quincey

On Sep 13, 2010, at 12:05 PM, Simonpro wrote:

>
> Hi all,
> I've just started using the HDF5 C interface and I've been having a few
> problems. I'm sure it's some simple thing that I've overlooked, but it'd be
> great if you guys could help find what I've done wrong!!
>
> I can read data with no problems, but when I want to write my results to an
> HDF5 file things go wrong. I'm trying to write a 2000x2200 array, but the
> lines being output to the H5 file are not what I expect.
> I have created an example that is supposed to write the line number to each
> column in an H5 file (datatype: H5T_NATIVE_USHORT) but rather than the
> expected results I get something like this:
> 0 0 0 0 0 0 0 .....
> 0 0 4009 0 1 1 1 .....
> 1 1 1 1 0 0 4009 .....
> 2 2 2 2 2 2 2 .....
> 3 3 3 3 3 3 3 .....
>
> The '4009' moves 4 columns to the right with each line.
>
> Any ideas what the problem could be? The same issue exists (with different
> numbers, of course) when I try with a floating point data type.
>
> I've included the sample code I used to generate the data below. Originally
> there was lots of error checking (based on the status returned from the HDF
> commands) for each step of the HDF process, but for brevity I remove those
> checks as they showed no problems:
>
>
>
> #include <stdlib.h>
> #include <hdf5.h>
>
> int Save_f_Data(hid_t,char *,short unsigned int **,int,int);
> void alloc_f_2dim (short unsigned int ***,int,int);
>
> int main()
> {
> char *name="TESTARR";
> int xsize=2200;
> int ysize=2000;
> int retval;
> hid_t outfile;
> herr_t status;
>
>
> outfile=H5Fcreate("/home/simon/Desktop/TEST/outtest.h5",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
> short unsigned int **testarr;
> int i,j;
>
> alloc_f_2dim (&testarr,xsize,ysize);
> for (i=0;i<xsize;i++) for (j=0;j<ysize;j++) testarr[i][j]=i;
>
> retval=Save_f_Data(outfile,name,testarr,xsize,ysize);
> status=H5Fclose(outfile);
>
> printf("Running!\n");
> return 0;
> }
>
> int Save_f_Data(hid_t outfile,char *setname,short unsigned int **
> procarr,int xsize, int ysize)
> {
>
> herr_t status;
> hid_t dataspace,dataset,datatype;
> hsize_t dims[2]={xsize,ysize};
>
> dataspace=H5Screate_simple(2,dims,dims);
> dataset=H5Dcreate(outfile,setname,H5T_NATIVE_USHORT,dataspace,H5P_DEFAULT);
>
>
> status=H5Dwrite(dataset,H5T_NATIVE_USHORT,H5S_ALL,H5S_ALL,H5P_DEFAULT,procarr[0]);
> status=H5Sclose(dataspace);
> status=H5Dclose(dataset);
> return 0;
> }
>
> void alloc_f_2dim (short unsigned int ***ptr,int nrh,int nch)
> {
> int i = 0;
> short unsigned int **p = NULL;
>
> p = (short unsigned int **) calloc ((nrh + 1), sizeof (short unsigned int
> *));
> if (!p) {
> fprintf(stderr,"allocation failure dim0 in alloc_d_2dim()");
>
> }
> for (i = 0; i <= nrh; i++) {
> p[i] = (short unsigned int *) calloc ((nch + 1), sizeof (short
> unsigned int));
> if (!p[i]) {
> fprintf(stderr,"allocation dim1 in alloc_d_2dim()");
>
> }
> }
> *ptr = p;
> return;
> }
> --
> View this message in context: http://hdf-forum.184993.n3.nabble.com/Problem-writing-array-to-an-HDF5-file-tp1464609p1464609.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

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