Alexandria  2.14.1
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 {};
78  std::ifstream in {filename.string()};
79  std::string line;
80  regex expr {"\\s*([^\\s#]+)\\s+([^\\s#]+)\\s+([^\\s#]+)\\s*(#.*)?"};
81  while (std::getline(in, line)) {
82  boost::trim(line);
83  if (line[0] == '#') {
84  continue;
85  }
86  smatch match_res;
87  if (!regex_match(line, match_res, expr)) {
88  logger.error() << "Syntax error in " << filename << ": " << line;
89  throw Elements::Exception() << "Syntax error in " << filename << ": " << line;
90  }
91  filter_name_mapping.emplace_back(match_res.str(1), std::make_pair(match_res.str(2), match_res.str(3)));
92  }
93  return filter_name_mapping;
94 }
95 
97 
98  // Parse the file with the mapping
99  auto filename = getMappingFileFromOptions(args, m_base_dir);
100  auto all_filter_name_mapping = parseFile(filename);
101 
102  // Remove the filters which are marked to exclude
103  auto exclude_vector = args.at(EXCLUDE_FILTER).as<std::vector<std::string>>();
104  std::set<std::string> exclude_filters {exclude_vector.begin(), exclude_vector.end()};
105  for (auto& pair : all_filter_name_mapping) {
106  if (exclude_filters.count(pair.first) > 0) {
107  exclude_filters.erase(pair.first);
108  } else {
109  m_mapping_map.push_back(pair);
110  }
111  }
112 
113  if (!exclude_filters.empty()) {
114  std::stringstream wrong_filters {};
115  for (auto& f : exclude_filters) {
116  wrong_filters << f << " ";
117  }
118  throw Elements::Exception() << "Wrong " << EXCLUDE_FILTER << " option value(s) : "
119  << wrong_filters.str();
120  }
121 
122 }
123 
124 void PhotometricBandMappingConfig::setBaseDir(const boost::filesystem::path& base_dir) {
126  throw Elements::Exception() << "setBaseDir() call to initialized PhotometricBandMappingConfig";
127  }
128  m_base_dir = base_dir;
129 }
130 
133  throw Elements::Exception() << "getPhotometricBandMapping() call to uninitialized "
134  << "PhotometricBandMappingConfig";
135  }
136  return m_mapping_map;
137 }
138 
139 } // Configuration namespace
140 } // Euclid namespace
141 
142 
143 
Superclass of all configuration classes.
Definition: Configuration.h:45
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
T make_pair(T... args)
std::map< std::string, OptionDescriptionList > getProgramOptions() override
Returns the program options defined by the PhotometryCatalogConfig.
static PhotometricBandMappingConfig::MappingMap parseFile(fs::path filename)
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.