Alexandria  2.18
Please provide a description of the project.
FitsWriter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #include "Table/FitsWriter.h"
27 #include "FitsWriterHelper.h"
28 #include <CCfits/CCfits>
29 
30 namespace Euclid {
31 namespace Table {
32 
33 FitsWriter::FitsWriter(const std::string& filename, bool override_flag) : m_filename(filename), m_override_file(override_flag) {}
34 
36 
38  if (m_initialized) {
39  throw Elements::Exception() << "Changing the format after writing "
40  << "has started is not allowed";
41  }
42  m_format = format;
43  return *this;
44 }
45 
47  if (m_initialized) {
48  throw Elements::Exception() << "Changing the HDU name after writing "
49  << "has started is not allowed";
50  }
51  m_hdu_name = name;
52  return *this;
53 }
54 
55 void FitsWriter::addComment(const std::string& message) {
56  if (m_initialized) {
57  throw Elements::Exception() << "Adding comments after writing "
58  << "has started is not allowed";
59  }
60  m_comments.push_back(message);
61 }
62 
63 void FitsWriter::init(const Table& table) {
64 
66  if (m_fits != nullptr) {
67  fits = m_fits;
68  } else {
69  // CCfits overrides the file if the name starts with !, otherwise it opens it
70  std::string filename = (m_override_file ? "!" : "") + m_filename;
71  fits = std::make_shared<CCfits::FITS>(filename, CCfits::RWmode::Write);
72  }
73 
74  // Create the column info arrays to feed the CCfits based on the ColumnInfo object
75  auto& info = *table.getColumnInfo();
76  std::vector<std::string> column_name_list{};
77  std::vector<std::string> column_unit_list{};
78  for (size_t column_index = 0; column_index < info.size(); ++column_index) {
79  column_name_list.push_back(info.getDescription(column_index).name);
80  column_unit_list.push_back(info.getDescription(column_index).unit);
81  }
82  std::vector<std::string> column_format_list =
84 
85  CCfits::HduType hdu_type = (m_format == Format::BINARY) ? CCfits::HduType::BinaryTbl : CCfits::HduType::AsciiTbl;
86 
87  auto extension_map = fits->extension();
88  auto extension_i = extension_map.find(m_hdu_name);
89  bool new_hdu = (extension_i == extension_map.end() || extension_i->second->version() != 1);
90 
91  CCfits::Table* table_hdu;
92  if (!new_hdu) {
93  table_hdu = dynamic_cast<CCfits::Table*>(extension_i->second);
94  } else {
95  table_hdu = fits->addTable(m_hdu_name, 0, column_name_list, column_format_list, column_unit_list, hdu_type);
96 
97  // Write the customized description header keywords, and also dimensions for multidimensional arrays
98  for (size_t column_index = 0; column_index < info.size(); ++column_index) {
99  auto& desc = info.getDescription(column_index).description;
100  table_hdu->addKey("TDESC" + std::to_string(column_index + 1), desc, "");
101 
102  auto shape_str = getTDIM(table, column_index);
103  if (!shape_str.empty()) {
104  table_hdu->addKey(CCfits::Column::TDIM() + std::to_string(column_index + 1), shape_str, "");
105  }
106  }
107 
108  for (auto& c : m_comments) {
109  table_hdu->writeComment(c);
110  }
111  }
112 
113  m_hdu_index = table_hdu->index();
114  m_current_line = table_hdu->rows() + 1;
115  m_initialized = true;
116 }
117 
118 void FitsWriter::append(const Table& table) {
120  if (m_fits != nullptr) {
121  fits = m_fits;
122  } else {
123  fits = std::make_shared<CCfits::FITS>(m_filename, CCfits::RWmode::Write);
124  }
125  auto& table_hdu = fits->extension(m_hdu_index);
126 
127  auto& info = *table.getColumnInfo();
128  for (size_t column_index = 0; column_index < info.size(); ++column_index) {
129  populateColumn(table, column_index, table_hdu, m_current_line);
130  }
131  m_current_line += table.size();
132 }
133 
134 } // namespace Table
135 } // namespace Euclid
TableWriter implementation for writing tables in FITS format.
Definition: FitsWriter.h:76
FitsWriter & setFormat(Format format)
Set the FITS table format.
Definition: FitsWriter.cpp:37
void addComment(const std::string &message) override
Adds a comment to the stream.
Definition: FitsWriter.cpp:55
Format
The format of the HDUs a FitsWriter creates.
Definition: FitsWriter.h:80
@ BINARY
FITS binary table HDU format.
void append(const Table &table) override
Definition: FitsWriter.cpp:118
FitsWriter(const std::string &filename, bool override_flag=false)
Creates a FitsWriter that writes to a specific file.
Definition: FitsWriter.cpp:33
std::vector< std::string > m_comments
Definition: FitsWriter.h:198
std::shared_ptr< CCfits::FITS > m_fits
Definition: FitsWriter.h:193
void init(const Table &table) override
Definition: FitsWriter.cpp:63
FitsWriter & setHduName(const std::string &name)
Set the HDU name where the table is written.
Definition: FitsWriter.cpp:46
Represents a table.
Definition: Table.h:49
std::vector< std::string > getAsciiFormatList(const Table &table)
Returns a vector with strings representing the FITS ASCII table formats for the given table.
void populateColumn(const Table &table, size_t column_index, CCfits::ExtHDU &table_hdu, long first_row)
std::string getTDIM(const Table &table, size_t column_index)
std::vector< std::string > getBinaryFormatList(const Table &table)
Returns a vector with strings representing the FITS binary table formats for the given table.
T push_back(T... args)
T to_string(T... args)