Alexandria  2.18
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
T at(T... args)
T c_str(T... args)
Superclass of all configuration classes.
Definition: Configuration.h:45
void preInitialize(const UserValues &args) override
Checks that all the options are valid. See the exceptions thrown for a detailed list of the checks.
void initialize(const UserValues &args) override
Adds the SpectroscopicRedshiftAttributeFromRow handler to the CatalogCnofig.
std::map< std::string, OptionDescriptionList > getProgramOptions() override
Returns the program options defined by the SpecZCatalogConfig.
SpecZCatalogConfig(long manager_id)
Constructs a new SpecZCatalogConfig object.
Implementation of the AttributeFromRow for a SpectroscopicRedshift attribute. This class implements t...
Provides information about the columns of a Table.
Definition: ColumnInfo.h:52
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
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
std::size_t size() const
Returns the number of columns represented by this ColumnInfo.
Definition: ColumnInfo.cpp:57
T end(T... args)
T find(T... args)
static const std::string SPECZ_COLUMN_NAME
static const std::string SPECZ_ERR_COLUMN_NAME
static std::string getFluxColumnFromOptions(const Configuration::UserValues &args, const Table::ColumnInfo &column_info)
static std::string getErrColumnFromOptions(const Configuration::UserValues &args, const Table::ColumnInfo &column_info)
static const std::string SPECZ_COLUMN_INDEX
static const std::string SPECZ_ERR_COLUMN_INDEX
T reset(T... args)