Alexandria  2.16
Please provide a description of the project.
PhotometricBandMappingConfig.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 
25 #include <fstream>
26 #include <algorithm>
27 #include <sstream>
28 #include <boost/regex.hpp>
29 using boost::regex;
30 using boost::regex_match;
31 using boost::smatch;
32 #include <boost/algorithm/string.hpp>
33 
35 #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  : Configuration(manager_id) { }
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 
62  const fs::path& base_dir) {
63  fs::path mapping_file {args.at(FILTER_MAPPING_FILE).as<std::string>()};
64  if (mapping_file.is_relative()) {
65  mapping_file = base_dir / mapping_file;
66  }
67  if (!fs::exists(mapping_file)) {
68  throw Elements::Exception() << "Photometry mapping file " << mapping_file << " does not exist";
69  }
70  if (fs::is_directory(mapping_file)) {
71  throw Elements::Exception() << "Photometry mapping file " << mapping_file << " is not a file";
72  }
73  return mapping_file;
74 }
75 
77  PhotometricBandMappingConfig::MappingMap filter_name_mapping {};
79  std::ifstream in {filename.string()};
80  std::string line;
81  regex expr {"\\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  if (match_res.str(4)==""){
95  threshold_mapping.emplace_back(match_res.str(1), 3.0);
96  } else {
97  float n=std::stof(match_res.str(4));
98  threshold_mapping.emplace_back(match_res.str(1), n);
99  }
100  }
101  return std::make_pair(filter_name_mapping,threshold_mapping);
102 }
103 
105 
106  // Parse the file with the mapping
107  auto filename = getMappingFileFromOptions(args, m_base_dir);
108  auto parsed = parseFile(filename);
109  auto all_filter_name_mapping = parsed.first;
110  auto all_threshold_mapping = parsed.second;
111 
112  // Remove the filters which are marked to exclude
113  auto exclude_vector = args.at(EXCLUDE_FILTER).as<std::vector<std::string>>();
114  std::set<std::string> exclude_filters {exclude_vector.begin(), exclude_vector.end()};
115 
116  for (auto& pair : all_threshold_mapping) {
117  if (exclude_filters.count(pair.first) <= 0) {
119  }
120  }
121 
122  for (auto& pair : all_filter_name_mapping) {
123  if (exclude_filters.count(pair.first) > 0) {
124  exclude_filters.erase(pair.first);
125  } else {
126  m_mapping_map.push_back(pair);
127  }
128  }
129 
130 
131  if (!exclude_filters.empty()) {
132  std::stringstream wrong_filters {};
133  for (auto& f : exclude_filters) {
134  wrong_filters << f << " ";
135  }
136  throw Elements::Exception() << "Wrong " << EXCLUDE_FILTER << " option value(s) : "
137  << wrong_filters.str();
138  }
139 
140 }
141 
142 void PhotometricBandMappingConfig::setBaseDir(const boost::filesystem::path& base_dir) {
144  throw Elements::Exception() << "setBaseDir() call to initialized PhotometricBandMappingConfig";
145  }
146  m_base_dir = base_dir;
147 }
148 
151  throw Elements::Exception() << "getPhotometricBandMapping() call to uninitialized "
152  << "PhotometricBandMappingConfig";
153  }
154  return m_mapping_map;
155 }
156 
157 
160  throw Elements::Exception() << "getUpperLimitThresholdMapping() call to uninitialized "
161  << "PhotometricBandMappingConfig";
162  }
163  return m_threshold_map;
164 }
165 
166 } // Configuration namespace
167 } // Euclid namespace
168 
169 
170 
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.