EleFits  5.0.0
A modern C++ API on top of CFITSIO
Classes | Functions
HDU selectors and iterators

Detailed Description

Iterate over HDUs with selected categories and/or states.

Iterate over all HDUs of a MefFile

An iterator class and begin/end functions are defined to enable looping over all the HDUs of a MefFile. Usage is straightforward:

MefFile f(filename, FileMode::Edit);
// ...
for (const auto& hdu : f) {
processHdu(hdu); // Do something with each HDU
}
Multi-Extension FITS file reader-writer.
Definition: MefFile.h:72
@ Edit
Open an existing file with write permission.

HDU categories and filters

HDUs can be selected using one of two dedicated classes: HduCategory or HduFilter.

An HduCategory object positions flags like: Primary or extension HDU, image or binary table HDU... HDU categories can be composed with binary operators & (and), | (or) and ~ (not). For example, to get the list of image extensions with integer values:

const auto category = HduCategory::IntImage & HduCategory::Ext;
static const HduCategory IntImage
Integer-valued image HDU.
Definition: HduCategory.h:204
static const HduCategory Ext
Extension.
Definition: HduCategory.h:208

An HduFilter is a list of acceptable HDU categories and unacceptable HDU categories. They are buit with operators + (accept) and - (reject). For example, to accept the Primary HDU and all the real-valued image HDUs, excluding the HDUs which were just created:

static const HduCategory Primary
Primary image HDU.
Definition: HduCategory.h:202
static const HduCategory Created
HDU was created.
Definition: HduCategory.h:230

It is possible to check if an HDU is of given category or matches a given filter with method Hdu::matches, as follows:

for (const auto& hdu : f) {
if (hdu.matches(HduCategory::Primary)) {
processPrimary(hdu);
} else if (hdu.matches(HduCategory::Image)) {
processImageExt(hdu.as<ImageHdu>());
} else if (hdu.matches(HduCategory::Bintable)) {
processBintableExt(hdu.as<BintableHdu>());
}
}
static const HduCategory Bintable
Binary table HDU (necessarily an extension)
Definition: HduCategory.h:210
static const HduCategory Image
Image HDU.
Definition: HduCategory.h:201

Iterate over selected HDUs of a MefFile

Similarly to the range loop presented at the beginning of this page, it is possible to loop over a subset of HDUs. This is done by creating the adequate iterator using method MefFile::filter():

for (const auto& hdu : f.filter<ImageHdu>(HduCategory::Image & HduCategory::Created)) {
processNewImage(hdu);
}
Image HDU reader-writer.
Definition: ImageHdu.h:44

The template parameter specifies the class to be returned: Hdu, ImageHdu, or BintableHdu. It is used to constrain the given filter: for ImageHdu (resp. BintableHdu), HduCategory::Image (resp. HduCategory::Bintable) is added to the filter. Therefore, f.filter<ImageHdu>() is strictly equivalent to f.filter<ImageHdu>(HduCategory::Image), and the above example can be rewritten more straightforwardly:

for (const auto& hdu : f.filter<ImageHdu>(HduCategory::Created)) { // Don't repeat Image
processNewImage(hdu);
}
Warning
Even when using filters, the whole file is scanned. It is therefore very inefficient to write several loops when one would be enough. For example, instead of:
for (const auto& hdu : f.filter<ImageHdu>()) { // Scans all HDUs
processImage(hdu);
}
for (const auto& hdu : f.filter<BintableHdu>()) { // Scans all HDUs again
processBintable(hdu);
}
write:
for (const auto& hdu : f) {
if (hdu.matches(HduCategory::Image)) {
processImage(hdu.as<ImageHdu>());
} else {
processBintable(hdu.as<BintableHdu>());
}
}
See also
Optimization and good practices

Classes

class  HduSelector< THdu >
 Helper class to provide filtered iterators. More...
 
class  HduIterator< THdu >
 Iterator for MefFile's HDUs. More...
 
class  HduCategory
 An extensible HDU categorization for filtering and iteration. More...
 
class  HduFilter
 HDU filter built from HDU categories. More...
 
class  KeywordCategory
 Keyword categories and related tools. More...
 

Functions

virtual HduCategory readCategory () const
 Read the category of the HDU. More...
 
bool matches (HduFilter filter) const
 Check whether the HDU matches a given filter. More...
 

Element access

template<typename THdu = Hdu>
HduSelector< THdu > filter (const HduFilter &categories=HduCategory::Any)
 Get an iterable object which represents a filtered set of HDUs. More...