Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Protected Member Functions | Private Attributes | List of all members
Euclid::Table::FitsWriter Class Reference

TableWriter implementation for writing tables in FITS format. More...

#include <FitsWriter.h>

Inheritance diagram for Euclid::Table::FitsWriter:
Inheritance graph
[legend]
Collaboration diagram for Euclid::Table::FitsWriter:
Collaboration graph
[legend]

Classes

struct  Header
 

Public Types

enum class  Format { ASCII , BINARY }
 The format of the HDUs a FitsWriter creates. More...
 

Public Member Functions

 FitsWriter (const std::string &filename, bool override_flag=false)
 Creates a FitsWriter that writes to a specific file.
 
 FitsWriter (std::shared_ptr< CCfits::FITS > fits)
 Creates a FitsWriter that writes to a specific CCfits::FITS object.
 
 FitsWriter (FitsWriter &&)=default
 
FitsWriteroperator= (FitsWriter &&)=default
 
 FitsWriter (const FitsWriter &)=delete
 
FitsWriteroperator= (const FitsWriter &)=delete
 
virtual ~FitsWriter ()=default
 Destructor.
 
FitsWritersetFormat (Format format)
 Set the FITS table format.
 
FitsWritersetHduName (const std::string &name)
 Set the HDU name where the table is written.
 
void addComment (const std::string &message) override
 Adds a comment to the stream.
 
template<typename T >
void setHeader (const std::string &key, T &&value, const std::string &comment="")
 Sets a header key/value pair.
 
- Public Member Functions inherited from Euclid::Table::TableWriter
 TableWriter ()=default
 
 TableWriter (TableWriter &&)=default
 
TableWriteroperator= (TableWriter &&)=default
 
 TableWriter (const TableWriter &)=delete
 
TableWriteroperator= (const TableWriter &)=delete
 
virtual ~TableWriter ()=default
 
void addData (const Table &table)
 Appends the contents of the given table to the output.
 

Protected Member Functions

void init (const Table &table) override
 
void append (const Table &table) override
 

Private Attributes

std::string m_filename
 
std::shared_ptr< CCfits::FITS > m_fits = nullptr
 
bool m_initialized = false
 
bool m_override_file = true
 
Format m_format = Format::BINARY
 
std::string m_hdu_name
 
std::vector< std::stringm_comments
 
std::vector< Headerm_headers
 
int m_hdu_index = -1
 
long m_current_line = 0
 

Detailed Description

TableWriter implementation for writing tables in FITS format.

This class allows for both creating new FITS tables or extending existing ones, based on the constructor used and the method setHduName(). For more information see the documentation of the constructors and this method.

The FITS table HDUs can be both in ASCII and binary format. The conventions are the following:

ASCII format:

Binary format:

Note that, at the moment, only fixed length vector columns are supported and that there is no support for vector columns for ASCII FITS tables.

The TUNITn fits keywords are populated using the unit of the of the ColumnDescriptions of the Table. The descriptions of the columns are set as the values of the (non standard) keywords TDESCn.

Definition at line 76 of file FitsWriter.h.

Member Enumeration Documentation

◆ Format

The format of the HDUs a FitsWriter creates.

Enumerator
ASCII 

FITS ASCII table HDU format.

BINARY 

FITS binary table HDU format.

Definition at line 80 of file FitsWriter.h.

Constructor & Destructor Documentation

◆ FitsWriter() [1/4]

Euclid::Table::FitsWriter::FitsWriter ( const std::string filename,
bool  override_flag = false 
)
explicit

Creates a FitsWriter that writes to a specific file.

If the override_flag is set to true, any pre-existing file will be deleted. If this flag is set to false and there is already an HDU with the name set by the setHduName() method, the table of this HDU will be appended. Otherwise a new table HDU will be added to the file.

Note that when this constructor is used the FITS file will be re-opened each time the addData() method is called. This takes away from the user the responsibility of managing the lifetime of the CCfits::FITS objects, but it might cause performance issues (especially with files with hundreds of HDUs). If this is the case, consider to use the other constructor of this class.

Parameters
filenameThe path of the file to store the FITS table
override_flagWhen true, any existing file will be overridden

Definition at line 35 of file FitsWriter.cpp.

◆ FitsWriter() [2/4]

Euclid::Table::FitsWriter::FitsWriter ( std::shared_ptr< CCfits::FITS >  fits)
explicit

Creates a FitsWriter that writes to a specific CCfits::FITS object.

If the given obejct already contain an HDU with the name set by the setHduName() method, the table of this HDU will be appended. Otherwisea new table HDU will be added to the CCfits::FITS object.

This constructor is not handling the opening/closing of the given file, so is should be used when there are performance issues with the other constructor. As the usage of this constructor delegates the management of the lifetime of the CCfits::FITS object to the user, it is recommended to be used only when performance issues have been identified.

Parameters
fitsA pointer to the CCfits::FITS object to write the table

Definition at line 38 of file FitsWriter.cpp.

◆ FitsWriter() [3/4]

Euclid::Table::FitsWriter::FitsWriter ( FitsWriter &&  )
default

◆ FitsWriter() [4/4]

Euclid::Table::FitsWriter::FitsWriter ( const FitsWriter )
delete

◆ ~FitsWriter()

virtual Euclid::Table::FitsWriter::~FitsWriter ( )
virtualdefault

Destructor.

Member Function Documentation

◆ addComment()

void Euclid::Table::FitsWriter::addComment ( const std::string message)
overridevirtual

Adds a comment to the stream.

This method can only be called before any data have been written. The comments are written to the FITS during the first call of addData(). Note that if an existing HDU is being appended (see setHduName() method) all comments are ignored.

Parameters
messageThe message to add
Exceptions
Elements::ExceptionIf data have already been written

Implements Euclid::Table::TableWriter.

Definition at line 58 of file FitsWriter.cpp.

References m_comments, m_initialized, and std::vector< T >::push_back().

Here is the call graph for this function:

◆ append()

void Euclid::Table::FitsWriter::append ( const Table table)
overrideprotectedvirtual

Writes to the FITS file the contents of the table, following the rules explained at the class documentation

Implements Euclid::Table::TableWriter.

Definition at line 126 of file FitsWriter.cpp.

References m_current_line, m_filename, m_fits, m_hdu_index, and Euclid::Table::populateColumn().

Here is the call graph for this function:

◆ init()

void Euclid::Table::FitsWriter::init ( const Table table)
overrideprotectedvirtual

Creates the FITS file if it needs to be created, the table HDU if the name already exist and writes the comments.

Implements Euclid::Table::TableWriter.

Definition at line 66 of file FitsWriter.cpp.

References BINARY, Euclid::Table::getAsciiFormatList(), Euclid::Table::getBinaryFormatList(), Euclid::Table::getTDIM(), m_comments, m_current_line, m_filename, m_fits, m_format, m_hdu_index, m_hdu_name, m_headers, m_initialized, m_override_file, std::vector< T >::push_back(), and std::to_string().

Here is the call graph for this function:

◆ operator=() [1/2]

FitsWriter & Euclid::Table::FitsWriter::operator= ( const FitsWriter )
delete

◆ operator=() [2/2]

FitsWriter & Euclid::Table::FitsWriter::operator= ( FitsWriter &&  )
default

◆ setFormat()

FitsWriter & Euclid::Table::FitsWriter::setFormat ( Format  format)

Set the FITS table format.

It can be set either to ASCII or binary (default). It returns a reference to the FitsWriter so it can be chained with other calls in the same line.

Parameters
formatOne of FitsWriter::Format::ASCII, FitsWriter::Format::BINARY
Returns
A reference to the FitsWriter instance
Exceptions
Elements::Exceptionif writing of data has already started

Definition at line 40 of file FitsWriter.cpp.

References m_format, and m_initialized.

◆ setHduName()

FitsWriter & Euclid::Table::FitsWriter::setHduName ( const std::string name)

Set the HDU name where the table is written.

This method has to be called before any data are written and can be used to change the name of the table HDU. If there is already existing a table HDU with this name, any calls to the addData() method will append data to it. Otherwise a new HDU will be added in the FITS file. The default name is the empty string.

Parameters
nameThe name of the HDU to write the table in
Returns
A reference to the FitsWriter instance

Definition at line 49 of file FitsWriter.cpp.

References m_hdu_name, and m_initialized.

◆ setHeader()

template<typename T >
void Euclid::Table::FitsWriter::setHeader ( const std::string key,
T &&  value,
const std::string comment = "" 
)
inline

Sets a header key/value pair.

Template Parameters
TValue type
Parameters
keyHeader name
valueHeader value
commentHeader comment

Definition at line 194 of file FitsWriter.h.

References m_headers.

Member Data Documentation

◆ m_comments

std::vector<std::string> Euclid::Table::FitsWriter::m_comments
private

Definition at line 219 of file FitsWriter.h.

Referenced by addComment(), and init().

◆ m_current_line

long Euclid::Table::FitsWriter::m_current_line = 0
private

Definition at line 222 of file FitsWriter.h.

Referenced by append(), and init().

◆ m_filename

std::string Euclid::Table::FitsWriter::m_filename
private

Definition at line 213 of file FitsWriter.h.

Referenced by append(), and init().

◆ m_fits

std::shared_ptr<CCfits::FITS> Euclid::Table::FitsWriter::m_fits = nullptr
private

Definition at line 214 of file FitsWriter.h.

Referenced by append(), and init().

◆ m_format

Format Euclid::Table::FitsWriter::m_format = Format::BINARY
private

Definition at line 217 of file FitsWriter.h.

Referenced by init(), and setFormat().

◆ m_hdu_index

int Euclid::Table::FitsWriter::m_hdu_index = -1
private

Definition at line 221 of file FitsWriter.h.

Referenced by append(), and init().

◆ m_hdu_name

std::string Euclid::Table::FitsWriter::m_hdu_name
private

Definition at line 218 of file FitsWriter.h.

Referenced by init(), and setHduName().

◆ m_headers

std::vector<Header> Euclid::Table::FitsWriter::m_headers
private

Definition at line 220 of file FitsWriter.h.

Referenced by init(), and setHeader().

◆ m_initialized

bool Euclid::Table::FitsWriter::m_initialized = false
private

Definition at line 215 of file FitsWriter.h.

Referenced by addComment(), init(), setFormat(), and setHduName().

◆ m_override_file

bool Euclid::Table::FitsWriter::m_override_file = true
private

Definition at line 216 of file FitsWriter.h.

Referenced by init().


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