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