Alexandria  2.16
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FitsWriter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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 <CCfits/CCfits>
27 #include "Table/FitsWriter.h"
28 #include "FitsWriterHelper.h"
29 
30 namespace Euclid {
31 namespace Table {
32 
33 
34 FitsWriter::FitsWriter(const std::string& filename, bool override_flag)
35  : m_filename(filename), m_override_file(override_flag) {
36 }
37 
39 }
40 
42  if (m_initialized) {
43  throw Elements::Exception() << "Changing the format after writing "
44  << "has started is not allowed";
45  }
46  m_format = format;
47  return *this;
48 }
49 
51  if (m_initialized) {
52  throw Elements::Exception() << "Changing the HDU name after writing "
53  << "has started is not allowed";
54  }
55  m_hdu_name = name;
56  return *this;
57 }
58 
59 void FitsWriter::addComment(const std::string& message) {
60  if (m_initialized) {
61  throw Elements::Exception() << "Adding comments after writing "
62  << "has started is not allowed";
63  }
64  m_comments.push_back(message);
65 }
66 
67 void FitsWriter::init(const Table& table) {
68 
70  if (m_fits != nullptr) {
71  fits = m_fits;
72  } else {
73  // CCfits overrides the file if the name starts with !, otherwise it opens it
74  std::string filename = (m_override_file ? "!" : "") + m_filename;
75  fits = std::make_shared<CCfits::FITS>(filename, CCfits::RWmode::Write);
76  }
77 
78  // Create the column info arrays to feed the CCfits based on the ColumnInfo object
79  auto& info = *table.getColumnInfo();
80  std::vector<std::string> column_name_list {};
81  std::vector<std::string> column_unit_list {};
82  for (size_t column_index=0; column_index<info.size(); ++column_index) {
83  column_name_list.push_back(info.getDescription(column_index).name);
84  column_unit_list.push_back(info.getDescription(column_index).unit);
85  }
86  std::vector<std::string> column_format_list = (m_format == Format::BINARY)
87  ? getBinaryFormatList(table)
88  : getAsciiFormatList(table);
89 
90  CCfits::HduType hdu_type = (m_format == Format::BINARY)
91  ? CCfits::HduType::BinaryTbl
92  : CCfits::HduType::AsciiTbl;
93 
94  auto number_of_hdus_before = fits->extension().size();
95  CCfits::Table* table_hdu = fits->addTable(m_hdu_name, 0, column_name_list,
96  column_format_list, column_unit_list, hdu_type);
97  bool new_hdu = number_of_hdus_before != fits->extension().size();
98  m_hdu_index = table_hdu->index();
99  m_current_line = table_hdu->rows() + 1;
100 
101  if (new_hdu) {
102  // Write the customized description header keywords, and also dimensions for multidimensional arrays
103  for (size_t column_index=0; column_index<info.size(); ++column_index) {
104  auto& desc = info.getDescription(column_index).description;
105  table_hdu->addKey("TDESC" + std::to_string(column_index+1), desc, "");
106 
107  auto shape_str = getTDIM(table, column_index);
108  if (!shape_str.empty()) {
109  table_hdu->addKey(CCfits::Column::TDIM() + std::to_string(column_index+1), shape_str, "");
110  }
111  }
112 
113  for (auto& c : m_comments) {
114  table_hdu->writeComment(c);
115  }
116  }
117 
118  m_initialized = true;
119 }
120 
121 void FitsWriter::append(const Table& table) {
123  if (m_fits != nullptr) {
124  fits = m_fits;
125  } else {
126  fits = std::make_shared<CCfits::FITS>(m_filename, CCfits::RWmode::Write);
127  }
128  auto& table_hdu = fits->extension(m_hdu_index);
129 
130  auto& info = *table.getColumnInfo();
131  for (size_t column_index=0; column_index<info.size(); ++column_index) {
132  populateColumn(table, column_index, table_hdu, m_current_line);
133  }
134  m_current_line += table.size();
135 }
136 
137 } // Table namespace
138 } // Euclid namespace
139 
140 
141 
FitsWriter & setFormat(Format format)
Set the FITS table format.
Definition: FitsWriter.cpp:41
FITS binary table HDU format.
FitsWriter(const std::string &filename, bool override_flag=false)
Creates a FitsWriter that writes to a specific file.
Definition: FitsWriter.cpp:34
T to_string(T...args)
void populateColumn(const Table &table, size_t column_index, CCfits::ExtHDU &table_hdu, long first_row)
std::shared_ptr< CCfits::FITS > m_fits
Definition: FitsWriter.h:196
TableWriter implementation for writing tables in FITS format.
Definition: FitsWriter.h:76
STL class.
void init(const Table &table) override
Definition: FitsWriter.cpp:67
T push_back(T...args)
Format
The format of the HDUs a FitsWriter creates.
Definition: FitsWriter.h:81
std::vector< std::string > m_comments
Definition: FitsWriter.h:201
FitsWriter & setHduName(const std::string &name)
Set the HDU name where the table is written.
Definition: FitsWriter.cpp:50
void addComment(const std::string &message) override
Adds a comment to the stream.
Definition: FitsWriter.cpp:59
Represents a table.
Definition: Table.h:49
void append(const Table &table) override
Definition: FitsWriter.cpp:121
std::string getTDIM(const Table &table, size_t column_index)
std::vector< std::string > getAsciiFormatList(const Table &table)
Returns a vector with strings representing the FITS ASCII table formats for the given table...
std::vector< std::string > getBinaryFormatList(const Table &table)
Returns a vector with strings representing the FITS binary table formats for the given table...