Alexandria  2.27.0
SDC-CH common library for the Euclid project
FitsParser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2022 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 
26 #include "XYDataset/FitsParser.h"
28 #include "ElementsKernel/Unused.h"
29 #include "StringFunctions.h"
30 #include "Table/FitsReader.h"
31 #include <CCfits/CCfits>
32 #include <fstream>
33 #include <iostream>
34 
35 namespace Euclid {
36 namespace XYDataset {
37 
38 //
39 // Get dataset name from a FITS file
40 //
42 
43  std::string dataset_name = getParameter(file, m_name_keyword);
44  if (dataset_name.empty()) {
45  // Dataset name is the filename without extension and path
46  std::string str{};
47  str = removeAllBeforeLastSlash(file);
48  dataset_name = removeExtension(str);
49  }
50  return dataset_name;
51 }
52 
54  std::string value{};
55  std::ifstream sfile(file);
56 
57  // Check file exists
58  if (!sfile) {
59  throw Elements::Exception() << "File not found : " << file;
60  }
61 
62  // Read first HDU
63  auto fits = make_unique<CCfits::FITS>(file, CCfits::RWmode::Read);
64  CCfits::ExtHDU& table_hdu = fits->extension(1);
65 
66  table_hdu.readAllKeys();
67  auto keyword_map = table_hdu.keyWord();
68  auto iter = keyword_map.find(key_word);
69  if (iter != keyword_map.end()) {
70  iter->second->value(value);
71  }
72 
73  return value;
74 }
75 
76 //
77 // Get dataset from a FITS file
78 //
80 
81  std::unique_ptr<XYDataset> dataset_ptr{};
82  std::ifstream sfile(file);
83 
84  CCfits::FITS::setVerboseMode(true);
85 
86  // Check file exists
87  if (sfile) {
88  auto fits = make_unique<CCfits::FITS>(file, CCfits::RWmode::Read);
89  try {
90  const CCfits::ExtHDU& table_hdu = fits->extension(1);
91  // Read first HDU
92  auto table = Table::FitsReader{table_hdu}.read();
93 
94  // Put the Table data into vector pair
95  std::vector<std::pair<double, double>> vector_pair(table.size());
96  std::transform(table.begin(), table.end(), vector_pair.begin(), [](const Table::Row& row) {
97  return std::make_pair(boost::get<double>(row[0]), boost::get<double>(row[1]));
98  });
99  dataset_ptr = make_unique<XYDataset>(std::move(vector_pair));
100  } catch (const CCfits::FitsException&) {
101  throw Elements::Exception() << "FitsException catched! File: " << file;
102  } // Eof try-catch
103  } // Eof if
104 
105  return dataset_ptr;
106 }
107 
108 //
109 // Check that the FITS file is a dataset file(with at least one HDU table)
110 //
112  bool is_a_dataset_file = true;
113  try {
114  auto fits = make_unique<CCfits::FITS>(file, CCfits::RWmode::Read);
115  const CCfits::ExtHDU& table_hdu = fits->extension(1);
116  ELEMENTS_UNUSED auto& temp = dynamic_cast<const CCfits::Table&>(table_hdu);
117  } catch (const CCfits::FitsException&) {
118  is_a_dataset_file = false;
119  }
120  return is_a_dataset_file;
121 }
122 
123 } // namespace XYDataset
124 } // end of namespace Euclid
T begin(T... args)
TableReader implementation for reading FITS tables.
Definition: FitsReader.h:75
Represents one row of a Table.
Definition: Row.h:57
Table read(long rows=-1)
Reads next rows as a table.
Definition: TableReader.h:91
std::unique_ptr< XYDataset > getDataset(const std::string &file) override
Get a XYDataset object reading data from an FITS file.
Definition: FitsParser.cpp:79
std::string getName(const std::string &file) override
Get the dataset name of a FITS file.
Definition: FitsParser.cpp:41
bool isDatasetFile(const std::string &file) override
Check that the FITS file is a dataset file(with at least one HDU table)
Definition: FitsParser.cpp:111
std::string getParameter(const std::string &file, const std::string &key_word) override
Get the parameter identified by a given key_word value from a file.
Definition: FitsParser.cpp:53
T empty(T... args)
#define ELEMENTS_UNUSED
T move(T... args)
std::string removeExtension(const std::string &input_str)
std::string removeAllBeforeLastSlash(const std::string &input_str)
T transform(T... args)