Skip to content

Commit

Permalink
Added example for fill values
Browse files Browse the repository at this point in the history
Update #318
  • Loading branch information
eugenwintersberger committed Jan 10, 2019
1 parent 52e4de9 commit 4f44420
Showing 1 changed file with 19 additions and 59 deletions.
78 changes: 19 additions & 59 deletions examples/hdfgroup/H5D/h5ex_d_fillval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,14 @@ using Int32Matrix = Matrix<int32_t,DIM0,DIM1>;
int
main (void)
{

herr_t status;
hsize_t dims[2] = {DIM0, DIM1},
extdims[2] = {EDIM0, EDIM1},
maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED},
chunk[2] = {CHUNK0, CHUNK1};
int wdata[DIM0][DIM1], /* Write buffer */
rdata[DIM0][DIM1], /* Read buffer */
rdata2[EDIM0][EDIM1], /* Read buffer for
extenstion */
fillval,

Int32Matrix rdata,wdata;
Matrix<int32_t,EDIM0,EDIM1> rdata2;
/*
* Initialize data.
*/
for (size_t i=0; i<DIM0; i++)
for (size_t j=0; j<DIM1; j++)
wdata[i][j] = i * j - j;
wdata(i,j) = i * j - j;

/*
* Create a new file using the default properties.
Expand All @@ -62,12 +51,13 @@ main (void)
*/
dataspace::Simple space({DIM0,DIM1},
{dataspace::Simple::UNLIMITED,
dataspace::Simple::UNLIMITED});
dataspace::Simple::UNLIMITED});

/*
* Create the dataset creation property list, and set the chunk
* size.
*/
property::LinkCreationList lcpl;
property::DatasetCreationList dcpl;
dcpl.layout(property::DatasetLayout::CHUNKED);
dcpl.chunk({CHUNK0,CHUNK1});
Expand All @@ -82,83 +72,53 @@ main (void)
* that reading from the dataset immediately after creation will
* return the fill value.
*/
status = H5Pset_alloc_time (dcpl, H5D_ALLOC_TIME_EARLY);
dcpl.allocation_time(property::DatasetAllocTime::EARLY);

/*
* Create the dataset using the dataset creation property list.
*/
dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT, dcpl,
H5P_DEFAULT);

node::Dataset dset(file.root(),DATASET,datatype::create<int32_t>(),space,lcpl,dcpl);
/*
* Read values from the dataset, which has not been written to yet.
*/
status = H5Dread (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
rdata[0]);

/*
* Output the data to the screen.
*/
printf ("Dataset before being written to:\n");
for (i=0; i<dims[0]; i++) {
printf (" [");
for (j=0; j<dims[1]; j++)
printf (" %3d", rdata[i][j]);
printf ("]\n");
}
dset.read(rdata);
std::cout<<"Dataset before being written to:"<<std::endl;
std::cout<<rdata<<std::endl;

/*
* Write the data to the dataset.
*/
status = H5Dwrite (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
wdata[0]);
dset.write(wdata);

/*
* Read the data back.
*/
status = H5Dread (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
rdata[0]);
dset.read(rdata);

/*
* Output the data to the screen.
*/
printf ("\nDataset after being written to:\n");
for (i=0; i<dims[0]; i++) {
printf (" [");
for (j=0; j<dims[1]; j++)
printf (" %3d", rdata[i][j]);
printf ("]\n");
}
std::cout<<std::endl<<"Dataset after being written to:"<<std::endl;
std::cout<<rdata<<std::endl;


/*
* Extend the dataset.
*/
status = H5Dset_extent (dset, extdims);
dset.extent({EDIM0,EDIM1});


/*
* Read from the extended dataset.
*/
status = H5Dread (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
rdata2[0]);
dset.read(rdata2);

/*
* Output the data to the screen.
*/
printf ("\nDataset after extension:\n");
for (i=0; i<extdims[0]; i++) {
printf (" [");
for (j=0; j<extdims[1]; j++)
printf (" %3d", rdata2[i][j]);
printf ("]\n");
}
std::cout<<std::endl<<"Dataset after extension:"<<std::endl;
std::cout<<rdata2<<std::endl;

/*
* Close and release resources.
*/
status = H5Pclose (dcpl);
status = H5Dclose (dset);
status = H5Sclose (space);
status = H5Fclose (file);

return 0;
}

0 comments on commit 4f44420

Please sign in to comment.