Alexandria  2.19
Please provide a description of the project.
SpecZCatalogConfig.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 
29 
30 namespace po = boost::program_options;
31 
32 namespace Euclid {
33 namespace Configuration {
34 
35 static const std::string SPECZ_COLUMN_NAME{"spec-z-column-name"};
36 static const std::string SPECZ_COLUMN_INDEX{"spec-z-column-index"};
37 static const std::string SPECZ_ERR_COLUMN_NAME{"spec-z-err-column-name"};
38 static const std::string SPECZ_ERR_COLUMN_INDEX{"spec-z-err-column-index"};
39 
40 SpecZCatalogConfig::SpecZCatalogConfig(long manager_id) : Configuration(manager_id) {
41  declareDependency<CatalogConfig>();
42 }
43 
45  return {
46  {"Input catalog options",
47  {{SPECZ_COLUMN_NAME.c_str(), po::value<std::string>(), "The name of the column representing the spectroscopic redshift"},
48  {SPECZ_COLUMN_INDEX.c_str(), po::value<int>(), "The 1-based index of the column representing the spectroscopic redshift"},
49  {SPECZ_ERR_COLUMN_NAME.c_str(), po::value<std::string>(),
50  "The name of the column representing spectroscopic redshift error"},
51  {SPECZ_ERR_COLUMN_INDEX.c_str(), po::value<int>(),
52  "The 1-based index of the column representing the spectroscopic redshift error"}}}};
53 }
54 
56  if (args.find(SPECZ_COLUMN_NAME) != args.end() && args.find(SPECZ_COLUMN_INDEX) != args.end()) {
57  throw Elements::Exception() << "Options " << SPECZ_COLUMN_NAME << " and " << SPECZ_COLUMN_INDEX << " are mutually exclusive";
58  }
59  if (args.find(SPECZ_COLUMN_NAME) == args.end() && args.find(SPECZ_COLUMN_INDEX) == args.end()) {
60  throw Elements::Exception() << "One of the options " << SPECZ_COLUMN_NAME << " and " << SPECZ_COLUMN_INDEX << " must be given";
61  }
62  if (args.find(SPECZ_COLUMN_INDEX) != args.end() && args.at(SPECZ_COLUMN_INDEX).as<int>() < 1) {
63  throw Elements::Exception() << SPECZ_COLUMN_INDEX << " must be a one-based "
64  << "index but was " << args.at(SPECZ_COLUMN_INDEX).as<int>();
65  }
66  if (args.find(SPECZ_ERR_COLUMN_NAME) != args.end() && args.find(SPECZ_ERR_COLUMN_INDEX) != args.end()) {
67  throw Elements::Exception() << "Options " << SPECZ_ERR_COLUMN_NAME << " and " << SPECZ_ERR_COLUMN_INDEX
68  << " are mutually exclusive";
69  }
70  if (args.find(SPECZ_ERR_COLUMN_INDEX) != args.end() && args.at(SPECZ_ERR_COLUMN_INDEX).as<int>() < 1) {
71  throw Elements::Exception() << SPECZ_ERR_COLUMN_INDEX << " must be a one-based "
72  << "index but was " << args.at(SPECZ_ERR_COLUMN_INDEX).as<int>();
73  }
74 }
75 
77  if (args.find(SPECZ_COLUMN_NAME) != args.end()) {
78  std::string name = args.at(SPECZ_COLUMN_NAME).as<std::string>();
79  if (column_info.find(name) == nullptr) {
80  throw Elements::Exception() << "Input catalog file does not contain the "
81  << " SpecZ column with name " << name;
82  }
83  return name;
84  } else {
85  std::size_t index = args.at(SPECZ_COLUMN_INDEX).as<int>();
86  if (index > column_info.size()) {
87  throw Elements::Exception() << SPECZ_COLUMN_INDEX << " (" << index << ") is out of range (" << column_info.size() << ")";
88  }
89  return column_info.getDescription(index - 1).name;
90  }
91 }
92 
94  if (args.find(SPECZ_ERR_COLUMN_NAME) != args.end()) {
96  if (column_info.find(name) == nullptr) {
97  throw Elements::Exception() << "Input catalog file does not contain the "
98  << " SpecZ error column with name " << name;
99  }
100  return name;
101  } else {
102  std::size_t index = args.at(SPECZ_ERR_COLUMN_INDEX).as<int>();
103  if (index > column_info.size()) {
104  throw Elements::Exception() << SPECZ_ERR_COLUMN_INDEX << " (" << index << ") is out of range (" << column_info.size() << ")";
105  }
106  return column_info.getDescription(index - 1).name;
107  }
108 }
109 
111  auto column_info = getDependency<CatalogConfig>().getColumnInfo();
112 
113  std::string flux_column = getFluxColumnFromOptions(args, *column_info);
114 
116 
117  if (args.find(SPECZ_ERR_COLUMN_NAME) == args.end() && args.find(SPECZ_ERR_COLUMN_INDEX) == args.end()) {
118  handler_ptr.reset(new SourceCatalog::SpectroscopicRedshiftAttributeFromRow{column_info, flux_column});
119  } else {
120  std::string err_column = getErrColumnFromOptions(args, *column_info);
121  handler_ptr.reset(new SourceCatalog::SpectroscopicRedshiftAttributeFromRow{column_info, flux_column, err_column});
122  }
123 
124  getDependency<CatalogConfig>().addAttributeHandler(handler_ptr);
125 }
126 
127 } // namespace Configuration
128 } // namespace Euclid
std::string
STL class.
std::shared_ptr
STL class.
Euclid::Configuration::SpecZCatalogConfig::getProgramOptions
std::map< std::string, OptionDescriptionList > getProgramOptions() override
Returns the program options defined by the SpecZCatalogConfig.
Definition: SpecZCatalogConfig.cpp:44
Euclid::Configuration::SPECZ_ERR_COLUMN_NAME
static const std::string SPECZ_ERR_COLUMN_NAME
Definition: SpecZCatalogConfig.cpp:37
Euclid::Configuration::SPECZ_COLUMN_INDEX
static const std::string SPECZ_COLUMN_INDEX
Definition: SpecZCatalogConfig.cpp:36
SpectroscopicRedshiftAttributeFromRow.h
Euclid::Table::ColumnDescription::name
std::string name
Definition: ColumnDescription.h:80
Euclid::Table::ColumnInfo::size
std::size_t size() const
Returns the number of columns represented by this ColumnInfo.
Definition: ColumnInfo.cpp:57
std::map::find
T find(T... args)
Euclid::Configuration::SpecZCatalogConfig::SpecZCatalogConfig
SpecZCatalogConfig(long manager_id)
Constructs a new SpecZCatalogConfig object.
Definition: SpecZCatalogConfig.cpp:40
Euclid::Configuration::SpecZCatalogConfig::preInitialize
void preInitialize(const UserValues &args) override
Checks that all the options are valid. See the exceptions thrown for a detailed list of the checks.
Definition: SpecZCatalogConfig.cpp:55
Euclid::Configuration::SPECZ_COLUMN_NAME
static const std::string SPECZ_COLUMN_NAME
Definition: SpecZCatalogConfig.cpp:35
std::shared_ptr::reset
T reset(T... args)
Euclid::Configuration::SpecZCatalogConfig::initialize
void initialize(const UserValues &args) override
Adds the SpectroscopicRedshiftAttributeFromRow handler to the CatalogCnofig.
Definition: SpecZCatalogConfig.cpp:110
Euclid::Configuration::getErrColumnFromOptions
static std::string getErrColumnFromOptions(const Configuration::UserValues &args, const Table::ColumnInfo &column_info)
Definition: SpecZCatalogConfig.cpp:93
Euclid::Table::ColumnInfo::getDescription
const ColumnDescription & getDescription(std::size_t index) const
Returns the description of the column with the given index or throws an exception if the index is big...
Definition: ColumnInfo.cpp:61
Euclid::Table::ColumnInfo
Provides information about the columns of a Table.
Definition: ColumnInfo.h:52
std::map::at
T at(T... args)
Exception.h
std::string::c_str
T c_str(T... args)
Elements::Exception
std::map
STL class.
SpecZCatalogConfig.h
Euclid::Configuration::getFluxColumnFromOptions
static std::string getFluxColumnFromOptions(const Configuration::UserValues &args, const Table::ColumnInfo &column_info)
Definition: SpecZCatalogConfig.cpp:76
Euclid::Configuration::SPECZ_ERR_COLUMN_INDEX
static const std::string SPECZ_ERR_COLUMN_INDEX
Definition: SpecZCatalogConfig.cpp:38
std::size_t
Euclid::Configuration::Configuration
Superclass of all configuration classes.
Definition: Configuration.h:45
std::map::end
T end(T... args)
Euclid::SourceCatalog::SpectroscopicRedshiftAttributeFromRow
Implementation of the AttributeFromRow for a SpectroscopicRedshift attribute. This class implements t...
Definition: SpectroscopicRedshiftAttributeFromRow.h:52
Euclid::Table::ColumnInfo::find
std::unique_ptr< std::size_t > find(const std::string &name) const
Returns the index of a column, given the name of it, or nullptr if there is no column with this name.
Definition: ColumnInfo.cpp:76
Euclid
Definition: InstOrRefHolder.h:29
CatalogConfig.h