Alexandria  2.19
Please provide a description of the project.
PhotometricBandMappingConfig.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 
25 #include <algorithm>
26 #include <boost/regex.hpp>
27 #include <fstream>
28 #include <sstream>
29 #include <tuple>
30 using boost::regex;
31 using boost::regex_match;
32 using boost::smatch;
33 #include <boost/algorithm/string.hpp>
34 
37 #include "ElementsKernel/Logging.h"
38 
39 namespace po = boost::program_options;
40 namespace fs = boost::filesystem;
41 
42 namespace Euclid {
43 namespace Configuration {
44 
45 static Elements::Logging logger = Elements::Logging::getLogger("PhotometricBandMappingConfig");
46 
47 static const std::string FILTER_MAPPING_FILE{"filter-mapping-file"};
48 static const std::string EXCLUDE_FILTER{"exclude-filter"};
49 
51 
53  return {{"Input catalog options",
54  {{FILTER_MAPPING_FILE.c_str(), po::value<std::string>()->default_value("filter_mapping.txt"),
55  "The file containing the photometry mapping of the catalog columns"},
56  {EXCLUDE_FILTER.c_str(), po::value<std::vector<std::string>>()->default_value(std::vector<std::string>{}, ""),
57  "A list of filters to ignore"}}}};
58 }
59 
60 static fs::path getMappingFileFromOptions(const Configuration::UserValues& args, const fs::path& base_dir) {
61  fs::path mapping_file{args.at(FILTER_MAPPING_FILE).as<std::string>()};
62  if (mapping_file.is_relative()) {
63  mapping_file = base_dir / mapping_file;
64  }
65  if (!fs::exists(mapping_file)) {
66  throw Elements::Exception() << "Photometry mapping file " << mapping_file << " does not exist";
67  }
68  if (fs::is_directory(mapping_file)) {
69  throw Elements::Exception() << "Photometry mapping file " << mapping_file << " is not a file";
70  }
71  return mapping_file;
72 }
73 
75 parseFile(fs::path filename) {
76  PhotometricBandMappingConfig::MappingMap filter_name_mapping{};
78  PhotometricBandMappingConfig::ConvertFromMagMap convert_from_mag_mapping{};
79  std::ifstream in{filename.string()};
80  std::string line;
81  regex expr{"\\s*([^\\s#]+)\\s+([^\\s#]+)\\s+([^\\s#]+)(\\s+[^\\s#]+)?(\\s+[^\\s#]+\\s*$)?"};
82  while (std::getline(in, line)) {
83  boost::trim(line);
84  if (line[0] == '#') {
85  continue;
86  }
87  smatch match_res;
88  if (!regex_match(line, match_res, expr)) {
89  logger.error() << "Syntax error in " << filename << ": " << line;
90  throw Elements::Exception() << "Syntax error in " << filename << ": " << line;
91  }
92  filter_name_mapping.emplace_back(match_res.str(1), std::make_pair(match_res.str(2), match_res.str(3)));
93 
94 
95  if (match_res.size() < 5 || match_res.str(4) == "") {
96  threshold_mapping.emplace_back(match_res.str(1), 3.0);
97  } else {
98  float n = std::stof(match_res.str(4));
99  threshold_mapping.emplace_back(match_res.str(1), n);
100  }
101 
102 
103  if (match_res.size() < 6 || match_res.str(5) == "") {
104  convert_from_mag_mapping.emplace_back(match_res.str(1), false);
105  } else {
106  bool f = std::stoi(match_res.str(5));
107  convert_from_mag_mapping.emplace_back(match_res.str(1), f);
108  }
109 
110  }
111  return std::make_tuple(filter_name_mapping, threshold_mapping, convert_from_mag_mapping);
112 }
113 
115 
116  // Parse the file with the mapping
117  auto filename = getMappingFileFromOptions(args, m_base_dir);
118  auto parsed = parseFile(filename);
119  auto all_filter_name_mapping = std::get<0>(parsed);
120  auto all_threshold_mapping = std::get<1>(parsed);
121  auto all_convert_mapping = std::get<2>(parsed);
122 
123  // Remove the filters which are marked to exclude
124  auto exclude_vector = args.at(EXCLUDE_FILTER).as<std::vector<std::string>>();
125  std::set<std::string> exclude_filters{exclude_vector.begin(), exclude_vector.end()};
126 
127  for (auto& pair : all_threshold_mapping) {
128  if (exclude_filters.count(pair.first) == 0) {
130  }
131  }
132 
133  for (auto& pair : all_convert_mapping) {
134  if (exclude_filters.count(pair.first) == 0) {
136  }
137  }
138 
139  for (auto& pair : all_filter_name_mapping) {
140  if (exclude_filters.count(pair.first) > 0) {
141  exclude_filters.erase(pair.first);
142  } else {
143  m_mapping_map.push_back(pair);
144  }
145  }
146 
147 
148 
149  if (!exclude_filters.empty()) {
150  std::stringstream wrong_filters{};
151  for (auto& f : exclude_filters) {
152  wrong_filters << f << " ";
153  }
154  throw Elements::Exception() << "Wrong " << EXCLUDE_FILTER << " option value(s) : " << wrong_filters.str();
155  }
156 }
157 
158 void PhotometricBandMappingConfig::setBaseDir(const boost::filesystem::path& base_dir) {
160  throw Elements::Exception() << "setBaseDir() call to initialized PhotometricBandMappingConfig";
161  }
162  m_base_dir = base_dir;
163 }
164 
167  throw Elements::Exception() << "getPhotometricBandMapping() call to uninitialized "
168  << "PhotometricBandMappingConfig";
169  }
170  return m_mapping_map;
171 }
172 
175  throw Elements::Exception() << "getUpperLimitThresholdMapping() call to uninitialized "
176  << "PhotometricBandMappingConfig";
177  }
178  return m_threshold_map;
179 }
180 
183  throw Elements::Exception() << "getConvertFromMagMapping() call to uninitialized "
184  << "PhotometricBandMappingConfig";
185  }
186  return m_convert_from_mag_map;
187 }
188 
189 } // namespace Configuration
190 } // namespace Euclid
std::make_tuple
T make_tuple(T... args)
std::string
STL class.
Euclid::Configuration::PhotometricBandMappingConfig::PhotometricBandMappingConfig
PhotometricBandMappingConfig(long manager_id)
Constructs a new PhotometricBandMappingConfig object.
Definition: PhotometricBandMappingConfig.cpp:50
Euclid::Configuration::PhotometricBandMappingConfig::m_mapping_map
MappingMap m_mapping_map
Definition: PhotometricBandMappingConfig.h:145
Euclid::Configuration::Configuration::getCurrentState
State & getCurrentState()
Returns the current state of the configuration.
Definition: Configuration.cpp:43
Elements::Logging
Euclid::Configuration::PhotometricBandMappingConfig::m_threshold_map
UpperLimitThresholdMap m_threshold_map
Definition: PhotometricBandMappingConfig.h:146
std::vector< std::string >
Euclid::Configuration::getMappingFileFromOptions
static fs::path getMappingFileFromOptions(const Configuration::UserValues &args, const fs::path &base_dir)
Definition: PhotometricBandMappingConfig.cpp:60
Euclid::Configuration::logger
static Elements::Logging logger
Definition: CatalogConfig.cpp:39
std::stringstream
STL class.
std::tuple
std::vector::push_back
T push_back(T... args)
std::stoi
T stoi(T... args)
std::map::at
T at(T... args)
Euclid::Configuration::PhotometricBandMappingConfig::getProgramOptions
std::map< std::string, OptionDescriptionList > getProgramOptions() override
Returns the program options defined by the PhotometryCatalogConfig.
Definition: PhotometricBandMappingConfig.cpp:52
Exception.h
Euclid::Configuration::PhotometricBandMappingConfig::getConvertFromMagMapping
const ConvertFromMagMap & getConvertFromMagMapping()
Returns the mapping of the flag indicating if the photometry has to be computed from a MAG_AB.
Definition: PhotometricBandMappingConfig.cpp:181
std::string::c_str
T c_str(T... args)
Euclid::Configuration::EXCLUDE_FILTER
static const std::string EXCLUDE_FILTER
Definition: PhotometricBandMappingConfig.cpp:48
Elements::Exception
Euclid::Configuration::PhotometricBandMappingConfig::initialize
void initialize(const UserValues &args) override
It initializes the photometric bands list.
Definition: PhotometricBandMappingConfig.cpp:114
std::map
STL class.
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
Euclid::Configuration::PhotometricBandMappingConfig::setBaseDir
void setBaseDir(const boost::filesystem::path &base_dir)
Sets the directory used when resolving relative paths.
Definition: PhotometricBandMappingConfig.cpp:158
std::stof
T stof(T... args)
Euclid::Configuration::PhotometricBandMappingConfig::getUpperLimitThresholdMapping
const UpperLimitThresholdMap & getUpperLimitThresholdMapping()
Returns the mapping of threshold used in the upper limit computation which will be red from the catal...
Definition: PhotometricBandMappingConfig.cpp:173
std::set::begin
T begin(T... args)
std::getline
T getline(T... args)
Euclid::Configuration::FILTER_MAPPING_FILE
static const std::string FILTER_MAPPING_FILE
Definition: PhotometricBandMappingConfig.cpp:47
Euclid::Configuration::PhotometricBandMappingConfig::m_base_dir
boost::filesystem::path m_base_dir
Definition: PhotometricBandMappingConfig.h:144
Euclid::Configuration::PhotometricBandMappingConfig::m_convert_from_mag_map
ConvertFromMagMap m_convert_from_mag_map
Definition: PhotometricBandMappingConfig.h:147
Elements::Logging::error
void error(const std::string &logMessage)
Euclid::Configuration::Configuration
Superclass of all configuration classes.
Definition: Configuration.h:45
Logging.h
std::make_pair
T make_pair(T... args)
Euclid::Configuration::parseFile
static std::tuple< PhotometricBandMappingConfig::MappingMap, PhotometricBandMappingConfig::UpperLimitThresholdMap, PhotometricBandMappingConfig::ConvertFromMagMap > parseFile(fs::path filename)
Definition: PhotometricBandMappingConfig.cpp:75
Euclid
Definition: InstOrRefHolder.h:29
Euclid::Configuration::PhotometricBandMappingConfig::getPhotometricBandMapping
const MappingMap & getPhotometricBandMapping()
Returns the list of the photometric band mapping which will be red from the catalog.
Definition: PhotometricBandMappingConfig.cpp:165
Euclid::Configuration::Configuration::State::INITIALIZED
@ INITIALIZED
The initialize() method has been called.
std::set
STL class.
PhotometricBandMappingConfig.h
std::ifstream
STL class.