Alexandria  2.14.1
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{};
50  std::ifstream sfile(file);
51 
52  // Check file exists
53  if (!sfile) {
54  throw Elements::Exception() << "File not found : " << file;
55  }
56 
57  // Read first HDU
58  std::unique_ptr<CCfits::FITS> fits { new CCfits::FITS(file, CCfits::RWmode::Read)};
59  CCfits::ExtHDU& table_hdu = fits->extension(1);
60 
61  table_hdu.readAllKeys();
62  auto keyword_map = table_hdu.keyWord();
63  auto iter=keyword_map.find(m_name_keyword);
64  if (iter != keyword_map.end()) {
65  iter->second->value(dataset_name);
66  }
67  else {
68  // Dataset name is the filename without extension and path
69  std::string str {};
70  str = removeAllBeforeLastSlash(file);
71  dataset_name = removeExtension(str);
72  }
73  return (dataset_name);
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  std::unique_ptr<CCfits::FITS> fits { new 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
96  for (auto row : table) {
97  vector_pair.push_back(std::make_pair( boost::get<double>(row[0]), boost::get<double>(row[1]) ));
98  }
99  dataset_ptr = std::unique_ptr<XYDataset> { new XYDataset(vector_pair) };
100  }
101  catch (CCfits::FitsException& fits_except){
102  throw Elements::Exception() << "FitsException catched! File: " << file;
103  } // Eof try-catch
104  } // Eof if
105 
106  return(dataset_ptr);
107 }
108 
109 //
110 // Check that the FITS file is a dataset file(with at least one HDU table)
111 //
113  bool is_a_dataset_file = true;
114  try {
115  std::unique_ptr<CCfits::FITS> fits { new CCfits::FITS(file, CCfits::RWmode::Read)};
116  const CCfits::ExtHDU& table_hdu = fits->extension(1);
117  ELEMENTS_UNUSED auto& temp = dynamic_cast<const CCfits::Table&>(table_hdu);
118  }
119  catch (CCfits::FitsException& fits_except){
120  is_a_dataset_file = false;
121  }
122  return is_a_dataset_file;
123 }
124 
125 } // XYDataset namespace
126 } // end of namespace Euclid
127 
128 
129 
std::string removeAllBeforeLastSlash(const std::string &input_str)
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:112
STL class.
T push_back(T... args)
T make_pair(T... args)
STL class.
Table read(long rows=-1)
Reads next rows as a table.
Definition: TableReader.h:92
std::string removeExtension(const std::string &input_str)
This module provides an interface for accessing two dimensional datasets (pairs of (X,...
Definition: XYDataset.h:59
TableReader implementation for reading FITS tables.
Definition: FitsReader.h:75
#define ELEMENTS_UNUSED
std::string getName(const std::string &file) override
Get the dataset name of a FITS file.
Definition: FitsParser.cpp:47
STL class.
std::unique_ptr< XYDataset > getDataset(const std::string &file) override
Get a XYDataset object reading data from an FITS file.
Definition: FitsParser.cpp:79