[hdf-forum] problem writing a dataset to a .h5 file

nfortne2 at hdfgroup.org nfortne2 at hdfgroup.org
Wed Oct 8 14:42:49 EDT 2008


Natalie,

H5Dwrite expects a pointer to the first element of a write buffer  
which is contiguous in memory.  The way the array is declared in  
memory here can result in non-contiguous data.  If you wish to keep  
the same array in memory you will have to write the dataset using  
hyperslabs.  In order to declare a contiguous array you could do  
something like:

float** Coordinates;
Coordinates = new float*[3];
Coordinates[0] = new float[xdims*3];
for(int l=1; l<3; l++)
     {Coordinates[l] = Coordinates[l-1] + xdims;
     }

Also, H5Dwrite expects a pointer to the first element in the write  
buffer, while Coordinates is a pointer to a pointer to the first  
element, so the H5Dwrite call should be changed to something like:

status = H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,  
H5P_DEFAULT, Coordinates[0]);

Finally, only Coordinates[0] and Coordinates need to be deleted.  I  
hope this is helpful.

Neil Fortner
The HDF Group

Quoting Natalie Happenhofer <nataliehapp at hotmail.com>:

>
> Hi everybody,
> I have problems writing a .h5 file. I want to write Coordinates of a  
>  3D-Mesh to the .h5 file, believing that I have to arrange the  
> points  in an array with 3 columns and rows as much as I have  
> points. As I  don´t know before how many points there are gonna be,  
> I have to  allocate my coordinate array dynamically. The code looks  
> like this  (it´s C++):
>
> float** Coordinates;
> Coordinates = new float*[3];
> for(int l=0; l<3; l++)
>     {Coordinates[l] = new float[xdims];
>     }
>
> for(int i=0; i<ydims; i++)
>         { Coordinates[0][i] = xPoints[i]; //xPoints, yPoints are   
> vectors, so I know how many points I´ve read in from a file
>           Coordinates[1][i] = yPoints[i];
>           Coordinates[2][i] = 0;
>         }//for(int i
>
>
> Now I try to write that to an .h5 file:
>
> hsize_t dims[2];
> dims[0] = 3;
> dims[1] = xdims;
> hid_t datatype, dataspace;
> hid_t file, dataset;
> herr_t status;
>
>
> //create H5File
> file = H5Fcreate(H5FileName.c_str(), H5F_ACC_TRUNC , H5P_DEFAULT,   
> H5P_DEFAULT);
> dataspace = H5Screate_simple(RANK1, dims, NULL); // RANK1 = 2
> datatype = H5Tcopy(H5T_NATIVE_FLOAT);
> status = H5Tset_order(datatype,H5T_ORDER_LE);
> dataset =   
> H5Dcreate2(file,DATASETNAME1,datatype,dataspace,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
> status = H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,   
> H5P_DEFAULT, Coordinates); //!!! the problem is here !!!!
> H5Sclose(dataspace);
> H5Tclose(datatype);
> H5Dclose(dataset);
> H5Fclose(file);
>
>
> Well, the problem occurs in the function H5Dwrite, and as I have   
> successfully writen files with an 1D-array, I believe that the   
> problem is  that my CoordinatesArray is 2D and dynamically allocated  
>  (looking at the declaration, it´s a double pointer).
> How can I write my coordinates array? Should I just write it as a   
> 1D-array where the points are coded like
> x1,y1,z1,x2,y2,z2,x3,.... ?
> Is it possible for my visualization programm (in my case ParaView)   
> to read this?
>
> Thx for any help,
> NH
>
>
>
>
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today it's FREE!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to hdf-forum-subscribe at hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe at hdfgroup.org.





More information about the Hdf-forum mailing list