Alexandria  2.22.0
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)
34  : m_filename(filename), m_override_file(override_flag) {}
35 
37 
39  if (m_initialized) {
40  throw Elements::Exception() << "Changing the format after writing "
41  << "has started is not allowed";
42  }
43  m_format = format;
44  return *this;
45 }
46 
48  if (m_initialized) {
49  throw Elements::Exception() << "Changing the HDU name after writing "
50  << "has started is not allowed";
51  }
52  m_hdu_name = name;
53  return *this;
54 }
55 
56 void FitsWriter::addComment(const std::string& message) {
57  if (m_initialized) {
58  throw Elements::Exception() << "Adding comments after writing "
59  << "has started is not allowed";
60  }
61  m_comments.push_back(message);
62 }
63 
64 void FitsWriter::init(const Table& table) {
65 
67  if (m_fits != nullptr) {
68  fits = m_fits;
69  } else {
70  // CCfits overrides the file if the name starts with !, otherwise it opens it
71  std::string filename = (m_override_file ? "!" : "") + m_filename;
72  fits = std::make_shared<CCfits::FITS>(filename, CCfits::RWmode::Write);
73  }
74 
75  // Create the column info arrays to feed the CCfits based on the ColumnInfo object
76  auto& info = *table.getColumnInfo();
77  std::vector<std::string> column_name_list{};
78  std::vector<std::string> column_unit_list{};
79  for (size_t column_index = 0; column_index < info.size(); ++column_index) {
80  column_name_list.push_back(info.getDescription(column_index).name);
81  column_unit_list.push_back(info.getDescription(column_index).unit);
82  }
83  std::vector<std::string> column_format_list =
85 
86  CCfits::HduType hdu_type = (m_format == Format::BINARY) ? CCfits::HduType::BinaryTbl : CCfits::HduType::AsciiTbl;
87 
88  auto extension_map = fits->extension();
89  auto extension_i = extension_map.find(m_hdu_name);
90  bool new_hdu = (extension_i == extension_map.end() || extension_i->second->version() != 1);
91 
92  CCfits::Table* table_hdu;
93  if (!new_hdu) {
94  table_hdu = dynamic_cast<CCfits::Table*>(extension_i->second);
95  assert(table_hdu != nullptr);
96  } else {
97  table_hdu = fits->addTable(m_hdu_name, 0, column_name_list, column_format_list, column_unit_list, hdu_type);
98 
99  // Write the customized description header keywords, and also dimensions for multidimensional arrays
100  for (size_t column_index = 0; column_index < info.size(); ++column_index) {
101  auto& desc = info.getDescription(column_index).description;
102  table_hdu->addKey("TDESC" + std::to_string(column_index + 1), desc, "");
103 
104  auto shape_str = getTDIM(table, column_index);
105  if (!shape_str.empty()) {
106  table_hdu->addKey(CCfits::Column::TDIM() + std::to_string(column_index + 1), shape_str, "");
107  }
108  }
109 
110  for (auto& c : m_comments) {
111  table_hdu->writeComment(c);
112  }
113  }
114 
115  m_hdu_index = table_hdu->index();
116  m_current_line = table_hdu->rows() + 1;
117  m_initialized = true;
118 }
119 
120 void FitsWriter::append(const Table& table) {
122  if (m_fits != nullptr) {
123  fits = m_fits;
124  } else {
125  fits = std::make_shared<CCfits::FITS>(m_filename, CCfits::RWmode::Write);
126  }
127  auto& table_hdu = fits->extension(m_hdu_index);
128 
129  auto& info = *table.getColumnInfo();
130  for (size_t column_index = 0; column_index < info.size(); ++column_index) {
131  populateColumn(table, column_index, table_hdu, m_current_line);
132  }
133  m_current_line += table.size();
134 }
135 
136 } // namespace Table
137 } // 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:38
void addComment(const std::string &message) override
Adds a comment to the stream.
Definition: FitsWriter.cpp:56
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:120
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:64
FitsWriter & setHduName(const std::string &name)
Set the HDU name where the table is written.
Definition: FitsWriter.cpp:47
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)