2003-01-07 12:22:12 +08:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
2005-10-01 23:29:18 +08:00
|
|
|
* Copyright by the Board of Trustees of the University of Illinois. *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This file is part of HDF5. The full HDF5 copyright notice, including *
|
|
|
|
* terms governing use, modification, and redistribution, is contained in *
|
|
|
|
* the files COPYING and Copyright.html. COPYING can be found at the root *
|
|
|
|
* of the source code distribution tree; Copyright.html can be found at the *
|
|
|
|
* root level of an installed copy of the electronic HDF5 document set and *
|
|
|
|
* is linked from the top-level documents page. It can also be found at *
|
|
|
|
* http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html. If you do not have *
|
|
|
|
* access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2003-01-07 12:22:12 +08:00
|
|
|
|
2005-08-14 04:53:35 +08:00
|
|
|
/*
|
2003-06-26 01:25:11 +08:00
|
|
|
* This example writes a dataset to a new HDF5 file.
|
|
|
|
*/
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2000-11-15 05:36:14 +08:00
|
|
|
#ifdef OLD_HEADER_FILENAME
|
|
|
|
#include <iostream.h>
|
|
|
|
#else
|
|
|
|
#include <iostream>
|
|
|
|
#endif
|
2005-12-03 20:28:55 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#ifndef H5_NO_NAMESPACE
|
|
|
|
#ifndef H5_NO_STD
|
|
|
|
using std::string;
|
|
|
|
#endif // H5_NO_STD
|
|
|
|
#endif
|
|
|
|
|
2000-11-15 05:36:14 +08:00
|
|
|
#include "H5Cpp.h"
|
|
|
|
|
|
|
|
#ifndef H5_NO_NAMESPACE
|
|
|
|
using namespace H5;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const string FILE_NAME( "SDS.h5" );
|
|
|
|
const string DATASET_NAME( "IntArray" );
|
|
|
|
const int NX = 5; // dataset dimensions
|
|
|
|
const int NY = 6;
|
|
|
|
const int RANK = 2;
|
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
2005-08-14 04:53:35 +08:00
|
|
|
/*
|
|
|
|
* Data initialization.
|
2003-06-26 01:25:11 +08:00
|
|
|
*/
|
2000-11-15 05:36:14 +08:00
|
|
|
int i, j;
|
|
|
|
int data[NX][NY]; // buffer for data to write
|
|
|
|
for (j = 0; j < NX; j++)
|
|
|
|
{
|
|
|
|
for (i = 0; i < NY; i++)
|
|
|
|
data[j][i] = i + j;
|
2005-08-14 04:53:35 +08:00
|
|
|
}
|
2003-06-26 01:25:11 +08:00
|
|
|
/*
|
2005-08-14 04:53:35 +08:00
|
|
|
* 0 1 2 3 4 5
|
2003-06-26 01:25:11 +08:00
|
|
|
* 1 2 3 4 5 6
|
|
|
|
* 2 3 4 5 6 7
|
|
|
|
* 3 4 5 6 7 8
|
|
|
|
* 4 5 6 7 8 9
|
|
|
|
*/
|
2000-11-15 05:36:14 +08:00
|
|
|
|
|
|
|
// Try block to detect exceptions raised by any of the calls inside it
|
|
|
|
try
|
|
|
|
{
|
2003-06-26 01:25:11 +08:00
|
|
|
/*
|
|
|
|
* Turn off the auto-printing when failure occurs so that we can
|
|
|
|
* handle the errors appropriately
|
|
|
|
*/
|
|
|
|
Exception::dontPrint();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new file using H5F_ACC_TRUNC access,
|
|
|
|
* default file creation properties, and default file
|
|
|
|
* access properties.
|
|
|
|
*/
|
2000-11-15 05:36:14 +08:00
|
|
|
H5File file( FILE_NAME, H5F_ACC_TRUNC );
|
|
|
|
|
2003-06-26 01:25:11 +08:00
|
|
|
/*
|
|
|
|
* Define the size of the array and create the data space for fixed
|
2005-08-14 04:53:35 +08:00
|
|
|
* size dataset.
|
2003-06-26 01:25:11 +08:00
|
|
|
*/
|
2000-11-15 05:36:14 +08:00
|
|
|
hsize_t dimsf[2]; // dataset dimensions
|
|
|
|
dimsf[0] = NX;
|
|
|
|
dimsf[1] = NY;
|
2005-08-14 04:53:35 +08:00
|
|
|
DataSpace dataspace( RANK, dimsf );
|
2000-11-15 05:36:14 +08:00
|
|
|
|
2003-06-26 01:25:11 +08:00
|
|
|
/*
|
|
|
|
* Define datatype for the data in the file.
|
|
|
|
* We will store little endian INT numbers.
|
|
|
|
*/
|
2000-11-15 05:36:14 +08:00
|
|
|
IntType datatype( PredType::NATIVE_INT );
|
|
|
|
datatype.setOrder( H5T_ORDER_LE );
|
|
|
|
|
2003-06-26 01:25:11 +08:00
|
|
|
/*
|
|
|
|
* Create a new dataset within the file using defined dataspace and
|
|
|
|
* datatype and default dataset creation properties.
|
|
|
|
*/
|
2000-11-15 05:36:14 +08:00
|
|
|
DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace );
|
|
|
|
|
2003-06-26 01:25:11 +08:00
|
|
|
/*
|
|
|
|
* Write the data to the dataset using default memory space, file
|
|
|
|
* space, and transfer properties.
|
|
|
|
*/
|
2000-11-15 05:36:14 +08:00
|
|
|
dataset.write( data, PredType::NATIVE_INT );
|
|
|
|
} // end of try block
|
|
|
|
|
|
|
|
// catch failure caused by the H5File operations
|
|
|
|
catch( FileIException error )
|
|
|
|
{
|
|
|
|
error.printError();
|
2003-06-26 01:25:11 +08:00
|
|
|
return -1;
|
2000-11-15 05:36:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// catch failure caused by the DataSet operations
|
|
|
|
catch( DataSetIException error )
|
|
|
|
{
|
|
|
|
error.printError();
|
2003-06-26 01:25:11 +08:00
|
|
|
return -1;
|
2000-11-15 05:36:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// catch failure caused by the DataSpace operations
|
|
|
|
catch( DataSpaceIException error )
|
|
|
|
{
|
|
|
|
error.printError();
|
2003-06-26 01:25:11 +08:00
|
|
|
return -1;
|
2000-11-15 05:36:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// catch failure caused by the DataSpace operations
|
|
|
|
catch( DataTypeIException error )
|
|
|
|
{
|
|
|
|
error.printError();
|
2003-06-26 01:25:11 +08:00
|
|
|
return -1;
|
2000-11-15 05:36:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // successfully terminated
|
2005-08-14 04:53:35 +08:00
|
|
|
}
|
2000-11-15 05:36:14 +08:00
|
|
|
|