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