Alexandria  2.18
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 using boost::regex;
30 using boost::regex_match;
31 using boost::smatch;
32 #include <boost/algorithm/string.hpp>
33 
36 #include "ElementsKernel/Logging.h"
37 
38 namespace po = boost::program_options;
39 namespace fs = boost::filesystem;
40 
41 namespace Euclid {
42 namespace Configuration {
43 
44 static Elements::Logging logger = Elements::Logging::getLogger("PhotometricBandMappingConfig");
45 
46 static const std::string FILTER_MAPPING_FILE{"filter-mapping-file"};
47 static const std::string EXCLUDE_FILTER{"exclude-filter"};
48 
50 
52  return {{"Input catalog options",
53  {{FILTER_MAPPING_FILE.c_str(), po::value<std::string>()->default_value("filter_mapping.txt"),
54  "The file containing the photometry mapping of the catalog columns"},
55  {EXCLUDE_FILTER.c_str(), po::value<std::vector<std::string>>()->default_value(std::vector<std::string>{}, ""),
56  "A list of filters to ignore"}}}};
57 }
58 
59 static fs::path getMappingFileFromOptions(const Configuration::UserValues& args, const fs::path& base_dir) {
60  fs::path mapping_file{args.at(FILTER_MAPPING_FILE).as<std::string>()};
61  if (mapping_file.is_relative()) {
62  mapping_file = base_dir / mapping_file;
63  }
64  if (!fs::exists(mapping_file)) {
65  throw Elements::Exception() << "Photometry mapping file " << mapping_file << " does not exist";
66  }
67  if (fs::is_directory(mapping_file)) {
68  throw Elements::Exception() << "Photometry mapping file " << mapping_file << " is not a file";
69  }
70  return mapping_file;
71 }
72 
74 parseFile(fs::path filename) {
75  PhotometricBandMappingConfig::MappingMap filter_name_mapping{};
77  std::ifstream in{filename.string()};
78  std::string line;
79  regex expr{"\\s*([^\\s#]+)\\s+([^\\s#]+)\\s+([^\\s#]+)(\\s+[^\\s#]+\\s*$)?"};
80  while (std::getline(in, line)) {
81  boost::trim(line);
82  if (line[0] == '#') {
83  continue;
84  }
85  smatch match_res;
86  if (!regex_match(line, match_res, expr)) {
87  logger.error() << "Syntax error in " << filename << ": " << line;
88  throw Elements::Exception() << "Syntax error in " << filename << ": " << line;
89  }
90  filter_name_mapping.emplace_back(match_res.str(1), std::make_pair(match_res.str(2), match_res.str(3)));
91 
92  if (match_res.str(4) == "") {
93  threshold_mapping.emplace_back(match_res.str(1), 3.0);
94  } else {
95  float n = std::stof(match_res.str(4));
96  threshold_mapping.emplace_back(match_res.str(1), n);
97  }
98  }
99  return std::make_pair(filter_name_mapping, threshold_mapping);
100 }
101 
103 
104  // Parse the file with the mapping
105  auto filename = getMappingFileFromOptions(args, m_base_dir);
106  auto parsed = parseFile(filename);
107  auto all_filter_name_mapping = parsed.first;
108  auto all_threshold_mapping = parsed.second;
109 
110  // Remove the filters which are marked to exclude
111  auto exclude_vector = args.at(EXCLUDE_FILTER).as<std::vector<std::string>>();
112  std::set<std::string> exclude_filters{exclude_vector.begin(), exclude_vector.end()};
113 
114  for (auto& pair : all_threshold_mapping) {
115  if (exclude_filters.count(pair.first) <= 0) {
117  }
118  }
119 
120  for (auto& pair : all_filter_name_mapping) {
121  if (exclude_filters.count(pair.first) > 0) {
122  exclude_filters.erase(pair.first);
123  } else {
124  m_mapping_map.push_back(pair);
125  }
126  }
127 
128  if (!exclude_filters.empty()) {
129  std::stringstream wrong_filters{};
130  for (auto& f : exclude_filters) {
131  wrong_filters << f << " ";
132  }
133  throw Elements::Exception() << "Wrong " << EXCLUDE_FILTER << " option value(s) : " << wrong_filters.str();
134  }
135 }
136 
137 void PhotometricBandMappingConfig::setBaseDir(const boost::filesystem::path& base_dir) {
139  throw Elements::Exception() << "setBaseDir() call to initialized PhotometricBandMappingConfig";
140  }
141  m_base_dir = base_dir;
142 }
143 
146  throw Elements::Exception() << "getPhotometricBandMapping() call to uninitialized "
147  << "PhotometricBandMappingConfig";
148  }
149  return m_mapping_map;
150 }
151 
154  throw Elements::Exception() << "getUpperLimitThresholdMapping() call to uninitialized "
155  << "PhotometricBandMappingConfig";
156  }
157  return m_threshold_map;
158 }
159 
160 } // namespace Configuration
161 } // namespace Euclid
Superclass of all configuration classes.
Definition: Configuration.h:45
T stof(T...args)
T getline(T...args)
void setBaseDir(const boost::filesystem::path &base_dir)
Sets the directory used when resolving relative paths.
static const std::string FILTER_MAPPING_FILE
State & getCurrentState()
Returns the current state of the configuration.
STL class.
void initialize(const UserValues &args) override
It initializes the photometric bands list.
STL class.
T at(T...args)
T push_back(T...args)
static const std::string EXCLUDE_FILTER
PhotometricBandMappingConfig(long manager_id)
Constructs a new PhotometricBandMappingConfig object.
static fs::path getMappingFileFromOptions(const Configuration::UserValues &args, const fs::path &base_dir)
static Elements::Logging logger
static std::pair< PhotometricBandMappingConfig::MappingMap, PhotometricBandMappingConfig::UpperLimitThresholdMap > parseFile(fs::path filename)
T make_pair(T...args)
std::map< std::string, OptionDescriptionList > getProgramOptions() override
Returns the program options defined by the PhotometryCatalogConfig.
const UpperLimitThresholdMap & getUpperLimitThresholdMapping()
Returns the mapping of threshold used in the upper limit computation which will be red from the catal...
const MappingMap & getPhotometricBandMapping()
Returns the list of the photometric band mapping which will be red from the catalog.
STL class.
void error(const std::string &logMessage)
T begin(T...args)
T c_str(T...args)
static Logging getLogger(const std::string &name="")
The initialize() method has been called.
STL class.