 |
C++ |
Using the C++ Interface
To use the C++ interface, you must include the
headers IO.hh and IEEEIO.hh. If you only require
the IEEEIO interface, you link only with libieeeio.a using
-L$IEEE_DIRECTORY/lib -lieeeio
.
If you
also want HDF support you must also include the header
HDFIO.hh and link with libhdfio.a and
the usual complement of HDF libraries using
-L$HDF_DIRECTORY/lib -L$IEEE_DIRECTORY/lib -lhdfio
-lieeeio -lmfhdf -ldf -ljpeg -lz
.
C++ Methods
Opening
To open an IEEEIO file, you simply create a new IEEEIO object.
The constructor handles the opening.
C++ Prototype
IEEEIO::IEEEIO(char *filename,IObase::AccessMode accessmode);
- filename:
- The name of the IEEEIO data file to
open. The typical extension for these files is .raw
- accessmode:
- The accessmode for the file. This is
one of 3 different access modes
- IObase::Read:
Opens a file in read-only mode.
- IObase::Create:
Opens a file in write-only mode. If the file does not exist, it will be created. If it does exist, it will be truncated.
- IObase::Append:
Opens a file in read/write mode. The file pointer is automatically placed at the end of the file for appending, but random-access read operations are allowed as well.
Since this is inherited from the IO base class, you can assign
the newly created object to a pointer to the base class and
deal with it generically.
IO *writer = new IEEEIO("datafileout.raw",IObase::Create);
IO *reader = new IEEEIO("datafilein.raw",IObase::Read);
You can test if the file was opened successfully using the IObase::isValid() method.
if(!reader->isValid())
puts("The file you specified does not exist or is not in a readable format");
Other IO systems can be inherited from the IO base class. So
for instance, to read HDF files you simply link with the the HDFIO
and HDF libraries and open using;
IO *writer = new HDFIO("datafileout.raw",IObase::Create);
IO *reader = new HDFIO("datafilein.raw",IObase::Read);
After the files are opened, you can use the methods in the
baseclass IO to do all of your reading and writing in a completely
generic manner regardless of the implementation of the underlying
IO system so long as the underlying IO system implements the
methods of the base class. There are plans, for example, to have
a SocketIO system that allows the data to be written out to a TCP
socket instead of to a file for real-time simulation-visualization
systems. There are also plans to use this interface to drive an
existing Parallel IO system.
To close the file, you simply delete the object.
delete writer;
delete reader;
Since virtual destructors are used, you can delete them as
objects of the base IO class (no casts required).
Writing
To write data you simply use the method write().
C++ Prototype
IEEEIO::write(IObase::DataType numbertype,int rank,int *dimensions,void *data);
- numbertype:
- The type of the data being stored
(as defined in DataType.html).
It can be one of
- IObase::Float32
32-bit single-precision IEEE float
- IObase::Float64
64-bit double-precision IEEE float
- IObase::Int8
byte
- IObase::Int16
16-bit short integer
- IObase::Int32
32-bit standard integer
- IObase::Int64
64-bit long integer. (note:
this is not availible on the Intel/Windows
platform)
- IObase::uInt8
unsigned character
- IObase::uInt16
unsigned 16-bit short integer
- IObase::uInt32
unsigned 32-bit standard integer
- IObase::uInt64
unsigned 64-bit long integer. (note: this is not availible on the Intel/Windows platform)
- rank
- Number of dimensions of the dataset
- dimensions:
- An array of rank integers that give the dimensions of the dataset
- data:
- Your data array.
So to write a sample array of data.
float myarray[40][50][60]; // our bogus data array
int rank=3;
int dims[3]={60,50,40}; // notice these are reversed.
// this is because ieeeio assumes f77 order for data
// and c/c++ use exactly the opposite ordering for data in memory.
IO *writer=new IEEEIO("datafile.raw",IObase::Create); // create a outfile
writer->write(IObase::Float32,rank,dims,myarray); // write a dataset
. . . . you can write as many datasets as you want
delete writer; // then close the file
Reading Data
Reading is a two step process. First you get information on
the size and type of the data you intend to read. This allows
you to allocate an array of the proper size and type for the
reading. Then you actually read the data into a pre-allocated
array. The methods for this are readInfo() and read().
C++ Prototype
readInfo(IObase::DataType &numbertype,int &rank,int
*dims,int maxdims=3);
- numbertype:
- The type of the data being stored
(datatype definition).
It can be one of
- IObase::Float32
32-bit single-precision IEEE float
- IObase::Float64
64-bit double-precision IEEE float
- IObase::Int8
byte
- IObase::Int16
16-bit short integer
- IObase::Int32
32-bit standard integer
- IObase::Int64
64-bit long integer. (note:
this is not availible on the Intel/Windows
platform)
- IObase::uInt8
unsigned character
- IObase::uInt16
unsigned 16-bit short integer
- IObase::uInt32
unsigned 32-bit standard integer
- IObase::uInt64
unsigned 64-bit long integer. (note: this is not availible on the Intel/Windows platform)
- rank
- Number of dimensions of the dataset
- dimensions:
- An array of rank integers that give the dimensions
of the dataset
- maxdims:
- The maximum size of the dimensions array you given it.
This prevents array overruns if the dataset has more
dimensions than you were anticipating. The default is 3 but
it can be any arbitrary positive integer.
This retrieves information about the datatype, rank,
and dimensions of the dataset to be retrieved.
By default the maximum size of the dimensions array is 3,
but you can set it to be larger.
C++ Prototype
IEEEIO::read(void *data);
This actually reads the dataset into the preallocated array data.
Another useful utility function is IObase::sizeOf() which returns the number of bytes in a give IObase:: datatype in manner analogous to the standard C sizeof() operator.
So for instance, to read a simple dataset, you would do
int rank;
IObase::DataType numbertype;
int dims[3];
float *data; // assumes float data
IO *infile = new IEEEIO("dataset.raw",IObase::Read);
infile->readInfo(numbertype,rank,dims);
// OK, we are assuming a 3D IObase::Float32 array,
// but you can be more sophisticated...
data = new char[IObase::nBytes(numbertype,rank,dims)];
// You can also use
// data = new float[IObase::nElements(rank,dims)];
infile->read(data); // read in the data
Since multiple datasets can be stored in a file, you can
retrieve them in the order they were written (there is a seek() function that allows random access as well). The method readInfo() implies reading the next dataset stored in the file. The method nDatasets() tells how many datasets are in a file. So typically if you want to read all datasets in a file in order, you would use code similar to;
IO *infile = new IEEEIO("dataset.raw",IObase::Read);
for(int i=0;i<infile->nDatasets();i++){
.....lots of code....
infile->readInfo(numbertype,rank,dims); // increments to next dataset
.....more code....
}
Random Access to Datasets
(Seeking)
You can select specific datasets in a file using the seek()
method.
C++ Prototype
IEEEIO::seek(int index)
- index
- The index of the dataset you want to read from. This can
be any number from 0 to (number_of_datasets - 1).
Writing Attributes
Attributes allow you to attach extra information to each
dataset stored in the file. Each attribute has a name and an
array of data (of any of the standard IObase:: types) stored with
it. These attributes can be retrieved by name or by integer
index in the order in which they were stored. A typical
attribute would typically be parameters that describe the grid
or the data like, "origin" which would be the 3-vector of floats
which locates of the origin of a grid in 3-space. The method
used to write these attributes is writeAttribute();
C++ Prototype
IEEEIO::writeAttribute(char *name,IObase::DataType numbertype,int length,void *data)
- name:
- Name of the attribute (like "origin" or "coordinates")
- numbertype:
- The type of the data array associated with the attribute
(datatype definition)
- length:
- The number of elements in the data array.
- data:
- The attribute data.
So to write an attribute named origin along with a 3-vector float for the coordinates of the origin, you would use;
float origin[3]={5.1,0.3,0.5};
// and assuming the file is already open for writing
// the following attribute will be attached to the last
// written dataset. (you must have write data before adding attribs)
writer->writeAttribute("origin",IObase::Float32,3,origin);
Reading Attributes
The attributes can be retrieved in the order they were written
or they can be retrieved by their name. To retrieve the
attributes in order, you would utilize the nAttributes()
method to determine how many attributes are attached,
readAttributeInfo() to get the size and type of the
attribute, and readAttribute() to read the attribute
data.
C++ Prototype
int IEEEIO::nAttributes()
- returnvalue:
- Number of attributes in the file
C++ Prototype
IEEEIO::readAttributeInfo(int index,char *name,IObase::DataType &numbertype,int &length,int maxnamelength=128);
- index:
- The index of the attribute which can be 0 to (nattributes-1)
- name:
- A buffer in which the name of the attribute will be placed.
- numbertype:
- The type of the attribute data
(datatype definition)
- length:
- The number of elements in the attribute data.
- maxnamelength:
- The maximum size of a name that can be stored in the name buffer. The default maximum is 128, but can be set to any size.
C++ Prototype
IEEEIO::readAttribute(int index,void *data);
- index:
- The index of the attribute data to read
- data:
- The array into which the attribute data is copied.
So for example, to read the attributes in order, you can use
for(int i=0;i<infile->nAttributes();i++){
char name[128];
int length;
IObase::DataType datatype;
...
infile->readAttributeInfo(i,name,datatype,length);
... // allocate some data for storage
infile->readAttribute(i,data);
}
The attributes can also be retrieve by name. In fact,
the is the most likely way you will use the attibutes interface.
The readAttributeInfo() method is overloaded to allow retrieval
by name as well. It returns the index of the attribute if one
is found with a matching name: it returns -1 if one is not
found.
C++ Prototype
int IEEEIO::readAttributeInfo(char *name,IObase::DataType &numbertype,int &length);
- returnvalue:
- The index of the attribute if found or -1 if no attribute with matching name is found.
- name:
- The name of the attribute to find.
- numbertype:
- Returns the numbertype of the stored attribute data
(datatype definition)
- length:
- he length of the stored attribute data.
So a typical use of this interface would be to find an attribute named "origin" and retrieve its data if it exists.
int index = infile->readAttributeInfo("origin",datatype,length);
if(index>=0) // the attribute exists
infile->readAttribute(index,data);
else
puts("The attribute origin could not be found");
Writing Annotations
An annotation is a text string which can be used to
describe a dataset. To write an annotation, you use the
writeAnnotation() method.
C++ Prototype
IEEEIO::writeAnnotation(char *annotationtext)
- annotationtext:
- A null terminated string of the annotation text
The annotation will be attached to the last written dataset.
You can store more than one annotation per dataset and the
annotations can be of arbitrary length.
Reading Annotations
The annotations are stored in the order they are written. The
method nAnnotations() is used to find out how many
attributes are attached to a dataset. The method
readAnnotationInfo() is used to find the length of the
annotation and readAnnotatin() reads the actual
annotation text.
C++ Prototype
int nAnnotations();
- returnvalue:
- Number of annotations attached to current dataset.
C++ Prototype
readAnnotationInfo(int index,int &length)
- index:
- Index of the annotations which can be 0 to (nannotations-1)
- length:
- Length in characters of the annotation. This includes the null-terminating character.
Writing and Reading in Chunks
For distributed-memory programming paradigms like HPF, MPI, or
PVM, it is often not unfeasible to write data to disk in a
single operation. For this reason, a chunking interface
is provided which allows you to write data in blocks to the
disk.
To begin a chunk writing operation, you must first reserve a
data chunk in the file. This is accomplished using reserveChunk()
C++ Prototype
IObase::reserveChunk(IObase::DataType datatype,int rank,int *dims);
Once space has been allocated in the datafile, you can write
blocks of data specified by their dimensions and origin using
writeChunk()
C++ Prototype
int IObase::IOwriteChunk(int *dims,int *origin,void *data);
Likewise, it is possible to read chunks from the disk as
well. No special procedure is required to select a record to
read in chunks. Simply use readInfo()
to get the dimensions and
type of the dataset and then use readChunk() in place
of read() in order to read-in the data.
C++ Prototype
int IObase::readChunk(int *dims,int *origin,void *data);
John Shalf
Last modified: Thu Feb 4 21:53:13 CST 1999