OpenImpala
Image-based parallel transport property solver
Loading...
Searching...
No Matches
OpenImpala::RawReader Class Reference

Reads and provides access to data from raw binary files where metadata (dimensions, data type, endianness) is provided externally. More...

#include <RawReader.H>

Public Types

using ByteType = unsigned char
 Type alias for the underlying byte storage.

Public Member Functions

amrex::Box box () const
 Gets the index space (bounding box) corresponding to the dataset dimensions.
int depth () const
 Gets the depth (Z dimension) of the loaded dataset.
RawDataType getDataType () const
 Gets the data type and endianness specified when the file was read.
double getValue (int i, int j, int k) const
 Retrieves the numerical value at a specific voxel, converting it to double.
int height () const
 Gets the height (Y dimension) of the loaded dataset.
bool isRead () const
 Checks if data has been successfully read and the reader is ready.
RawReader & operator= (const RawReader &)=delete
RawReader & operator= (RawReader &&)=default
 RawReader ()
 Default constructor. Creates an empty reader instance.
 RawReader (const RawReader &)=delete
 RawReader (const std::string &filename, int width, int height, int depth, RawDataType data_type)
 Constructs a RawReader and immediately attempts to read the specified file.
 RawReader (RawReader &&)=default
bool readFile (const std::string &filename, int width, int height, int depth, RawDataType data_type)
 Reads or re-reads data from the specified raw binary file.
void threshold (double threshold_value, amrex::iMultiFab &mf) const
 Fills an AMReX iMultiFab based on thresholding the raw data (outputting 1 or 0).
void threshold (double threshold_value, int value_if_true, int value_if_false, amrex::iMultiFab &mf) const
 Fills an AMReX iMultiFab based on thresholding the raw data.
int width () const
 Gets the width (X dimension) of the loaded dataset.
virtual ~RawReader ()=default
 Virtual default destructor.

Detailed Description

Reads and provides access to data from raw binary files where metadata (dimensions, data type, endianness) is provided externally.

This class handles reading a flat binary file containing voxel data. It assumes a simple, contiguous layout corresponding to standard C/Fortran XYZ array ordering (where the Z index varies slowest). The caller is required to supply the correct dimensions (width, height, depth) and the primitive data type (including endianness for multi-byte types) present in the file via the constructor or the readFile method.

The entire file content is read into an internal byte buffer (m_raw_bytes). Voxel values are interpreted and reconstructed from this byte buffer on demand (e.g., when getValue or threshold is called), handling potential byte swapping between the file's endianness and the host machine's endianness if they differ.

Member Typedef Documentation

◆ ByteType

using OpenImpala::RawReader::ByteType = unsigned char

Type alias for the underlying byte storage.

Using unsigned char for byte representation. std::byte could be used if C++17 is guaranteed and preferred.

Constructor & Destructor Documentation

◆ RawReader() [1/4]

OpenImpala::RawReader::RawReader ( )

Default constructor. Creates an empty reader instance.

The reader will be in an invalid state (isRead() returns false) until readFile is successfully called.

◆ RawReader() [2/4]

OpenImpala::RawReader::RawReader ( const std::string & filename,
int width,
int height,
int depth,
RawDataType data_type )
explicit

Constructs a RawReader and immediately attempts to read the specified file.

Reads the raw binary file specified by filename, interpreting the data according to the provided width, height, depth, and data_type. Throws a std::runtime_error if any part of the reading process fails (e.g., file cannot be opened, file size doesn't match expected dimensions and data type, or a read error occurs).

Parameters
filenamePath to the input .raw binary file.
widthWidth (X dimension) of the dataset in voxels. Must be > 0.
heightHeight (Y dimension) of the dataset in voxels. Must be > 0.
depthDepth (Z dimension) of the dataset in voxels. Must be > 0.
data_typeThe data type and endianness of the voxels stored in the file. Must not be RawDataType::UNKNOWN.
Exceptions
std::runtime_errorIf reading or validation fails.

◆ ~RawReader()

virtual OpenImpala::RawReader::~RawReader ( )
virtualdefault

Virtual default destructor.

Ensures proper cleanup if RawReader is used as a base class.

◆ RawReader() [3/4]

OpenImpala::RawReader::RawReader ( const RawReader & )
delete

◆ RawReader() [4/4]

OpenImpala::RawReader::RawReader ( RawReader && )
default

Member Function Documentation

◆ box()

amrex::Box OpenImpala::RawReader::box ( ) const

Gets the index space (bounding box) corresponding to the dataset dimensions.

The box covers the 0-based index range [0, 0, 0] to [width-1, height-1, depth-1].

Returns
An amrex::Box representing the dataset's index space. Returns an empty (invalid) box if data has not been successfully read.

◆ depth()

int OpenImpala::RawReader::depth ( ) const

Gets the depth (Z dimension) of the loaded dataset.

Returns
Depth in voxels, or 0 if data has not been successfully read.

◆ getDataType()

RawDataType OpenImpala::RawReader::getDataType ( ) const

Gets the data type and endianness specified when the file was read.

Returns
The RawDataType enum value.

◆ getValue()

double OpenImpala::RawReader::getValue ( int i,
int j,
int k ) const

Retrieves the numerical value at a specific voxel, converting it to double.

Accesses the raw bytes corresponding to the voxel at logical coordinates (i, j, k), reconstructs the original numerical value based on the m_data_type (performing byte swapping if the file's endianness differs from the host machine's), and then casts the result to double. Assumes standard XYZ layout where the k index (depth) varies slowest.

Parameters
iX-index (must be 0 <= i < width).
jY-index (must be 0 <= j < height).
kZ-index (must be 0 <= k < depth).
Returns
The voxel's numerical value converted to double.
Exceptions
std::out_of_rangeIf the provided indices (i, j, k) are outside the valid dataset dimensions [0..width-1, 0..height-1, 0..depth-1].
std::runtime_errorIf the reader is not in a valid state (isRead() is false), if the stored m_data_type is UNKNOWN, or if an internal error occurs during value reconstruction.

◆ height()

int OpenImpala::RawReader::height ( ) const

Gets the height (Y dimension) of the loaded dataset.

Returns
Height in voxels, or 0 if data has not been successfully read.

◆ isRead()

bool OpenImpala::RawReader::isRead ( ) const

Checks if data has been successfully read and the reader is ready.

Returns
true if the constructor or readFile succeeded, false otherwise.

◆ operator=() [1/2]

RawReader & OpenImpala::RawReader::operator= ( const RawReader & )
delete

◆ operator=() [2/2]

RawReader & OpenImpala::RawReader::operator= ( RawReader && )
default

◆ readFile()

bool OpenImpala::RawReader::readFile ( const std::string & filename,
int width,
int height,
int depth,
RawDataType data_type )

Reads or re-reads data from the specified raw binary file.

Clears any previously loaded data. Opens the specified file, verifies its size against the expected size based on dimensions and data type, and reads the entire content into the internal byte buffer. Prints detailed error messages using amrex::Print or amrex::Warning upon failure.

Parameters
filenamePath to the input .raw binary file.
widthWidth (X dimension) of the dataset in voxels. Must be > 0.
heightHeight (Y dimension) of the dataset in voxels. Must be > 0.
depthDepth (Z dimension) of the dataset in voxels. Must be > 0.
data_typeThe data type and endianness of the voxels stored in the file. Must not be RawDataType::UNKNOWN.
Returns
true if the file was read and validated successfully, false otherwise.

◆ threshold() [1/2]

void OpenImpala::RawReader::threshold ( double threshold_value,
amrex::iMultiFab & mf ) const

Fills an AMReX iMultiFab based on thresholding the raw data (outputting 1 or 0).

This is a convenience overload that calls the main threshold method with value_if_true = 1 and value_if_false = 0. It effectively creates a binary mask in the iMultiFab where cells corresponding to raw data values greater than threshold_value are set to 1, and all others are set to 0.

Parameters
threshold_valueThe value to compare against. The comparison is strictly greater than (>).
[out]mfThe amrex::iMultiFab to be filled. It must be defined (allocated) and is expected to have exactly one component (nComp() == 1).
Exceptions
std::runtime_errorIf the reader is not in a valid state (isRead() is false) or if mf.nComp() != 1.

◆ threshold() [2/2]

void OpenImpala::RawReader::threshold ( double threshold_value,
int value_if_true,
int value_if_false,
amrex::iMultiFab & mf ) const

Fills an AMReX iMultiFab based on thresholding the raw data.

Iterates through the cells defined by the valid boxes of the input mf. For each cell (i, j, k) that falls within the bounds of the loaded raw dataset [0..width-1, 0..height-1, 0..depth-1], this function retrieves the corresponding voxel value using getValue(i, j, k). It then compares this value against threshold_value.

  • If getValue(i, j, k) > threshold_value, the cell in mf is set to value_if_true.
  • Otherwise (including if the cell is outside the raw data bounds), the cell in mf is set to value_if_false.
Parameters
threshold_valueThe value to compare against. The comparison is strictly greater than (>).
value_if_trueThe integer value to write to mf for cells exceeding the threshold.
value_if_falseThe integer value to write to mf for cells not exceeding the threshold or outside the bounds of the raw data.
[out]mfThe amrex::iMultiFab to be filled. It must be defined (allocated) and is expected to have exactly one component (nComp() == 1). The operation is performed in parallel using OpenMP if available.
Exceptions
std::runtime_errorIf the reader is not in a valid state (isRead() is false) or if mf.nComp() != 1.

◆ width()

int OpenImpala::RawReader::width ( ) const

Gets the width (X dimension) of the loaded dataset.

Returns
Width in voxels, or 0 if data has not been successfully read.

The documentation for this class was generated from the following files: