Alexandria  2.18
Please provide a description of the project.
PhotometryAttributeFromRow.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 
27 #include "ElementsKernel/Real.h"
28 #include <cmath>
29 #include <typeindex>
30 
31 #include "../../SourceCatalog/PhotometryParsingException.h"
33 #include "ElementsKernel/Real.h"
34 #include "Table/CastVisitor.h"
35 
36 namespace Euclid {
37 namespace SourceCatalog {
38 
42  const bool missing_photometry_enabled, const double missing_photometry_flag, const bool upper_limit_enabled,
43  const std::vector<std::pair<std::string, float>> n_map, const double n_upper_limit_flag)
44  : m_missing_photometry_enabled(missing_photometry_enabled)
45  , m_missing_photometry_flag(missing_photometry_flag)
46  , m_upper_limit_enabled(upper_limit_enabled)
47  , m_n_map(n_map)
48  , m_n_upper_limit_flag(n_upper_limit_flag) {
49 
50  std::unique_ptr<size_t> flux_column_index_ptr;
51  std::unique_ptr<size_t> error_column_index_ptr;
52 
53  for (auto filter_name_pair : filter_name_mapping) {
54  flux_column_index_ptr = column_info_ptr->find(filter_name_pair.second.first);
55  error_column_index_ptr = column_info_ptr->find(filter_name_pair.second.second);
56 
57  if (flux_column_index_ptr == nullptr) {
58  throw Elements::Exception() << "Column info does not have the flux column " << filter_name_pair.second.first;
59  }
60  if (error_column_index_ptr == nullptr) {
61  throw Elements::Exception() << "Column info does not have the flux error column " << filter_name_pair.second.second;
62  }
63  m_table_index_vector.push_back(std::make_pair(*(flux_column_index_ptr), *(error_column_index_ptr)));
64  }
65 
66  // create and filled the shared pointer to the filter name vector
67  m_filter_name_vector_ptr = std::make_shared<std::vector<std::string>>();
68  for (auto a_filter_name_map : filter_name_mapping) {
69  m_filter_name_vector_ptr->push_back(a_filter_name_map.first);
70  }
71 }
72 
74  // @todo Auto-generated destructor stub
75 }
76 
78 
79  std::vector<FluxErrorPair> photometry_vector{};
80 
81  auto n_threshod_iter = m_n_map.begin();
82  for (auto& filter_index_pair : m_table_index_vector) {
83  Euclid::Table::Row::cell_type flux_cell = row[filter_index_pair.first];
84  Euclid::Table::Row::cell_type error_cell = row[filter_index_pair.second];
85 
86  double flux = boost::apply_visitor(Table::CastVisitor<double>{}, flux_cell);
87  double error = boost::apply_visitor(Table::CastVisitor<double>{}, error_cell);
88 
89  bool missing_data = false;
90  bool upper_limit = false;
91  if (std::isinf(flux)) {
92  throw SourceCatalog::PhotometryParsingException("Infinite flux encountered when parsing the Photometry", flux, error);
93  }
96  missing_data = Elements::isEqual(flux, m_missing_photometry_flag) || std::isnan(flux);
97  if (missing_data) {
98  error = 0;
99  } else {
100  if (m_upper_limit_enabled) {
102  if (error == 0.) {
104  "Zero error encountered when parsing the Photometry with 'missing data' and 'upper limit' enabled", flux, error);
105  }
106  if (error < 0) {
108  if (error == m_n_upper_limit_flag) {
109  error = flux / n_threshod_iter->second;
110  }
111  upper_limit = true;
112  if (flux <= 0) {
114  "Negative or Zero flux encountered when parsing the Photometry in the context of an 'upper limit'", flux, error);
115  }
116 
117  error = std::abs(error);
118  }
119  } else {
121  if (error <= 0) {
123  "Negative or Zero error encountered when parsing the Photometry with 'missing data' enabled "
124  "and 'upper limit' disabled",
125  flux, error);
126  }
127  }
128  }
129  } else {
131  if (std::isnan(flux)) {
133  "NAN flux encountered when parsing the Photometry with 'missing data' disabled", flux, error);
134  }
135 
136  if (m_upper_limit_enabled) {
138  if (error == 0.) {
140  "Zero error encountered when parsing the Photometry with 'missing data' disabled and 'upper limit' enabled", flux,
141  error);
142  }
143  if (error < 0) {
145  upper_limit = true;
146  if (error == m_n_upper_limit_flag) {
147  error = flux / n_threshod_iter->second;
148  }
149  if (flux <= 0) {
151  "Negative or Zero flux encountered when parsing the Photometry in the context of an 'upper limit'", flux, error);
152  }
153  error = std::abs(error);
154  }
155  } else {
157  if (error <= 0) {
159  "Negative or Zero error encountered when parsing the Photometry with 'missing data' "
160  "and 'upper limit' disabled",
161  flux, error);
162  }
163  }
164  }
165 
166  photometry_vector.push_back(FluxErrorPair{flux, error, missing_data, upper_limit});
167  ++n_threshod_iter;
168  } // Eof for
169 
170  std::unique_ptr<Attribute> photometry_ptr{new Photometry{m_filter_name_vector_ptr, photometry_vector}};
171 
172  return photometry_ptr;
173 }
174 
175 } // namespace SourceCatalog
176 } // end of namespace Euclid
Euclid::SourceCatalog::PhotometryAttributeFromRow::m_n_map
std::vector< std::pair< std::string, float > > m_n_map
Definition: PhotometryAttributeFromRow.h:118
Euclid::SourceCatalog::PhotometryAttributeFromRow::m_upper_limit_enabled
bool m_upper_limit_enabled
Definition: PhotometryAttributeFromRow.h:116
std::string
STL class.
std::shared_ptr< Euclid::Table::ColumnInfo >
Euclid::Table::Row::cell_type
boost::variant< bool, int32_t, int64_t, float, double, std::string, std::vector< bool >, std::vector< int32_t >, std::vector< int64_t >, std::vector< float >, std::vector< double >, NdArray::NdArray< int32_t >, NdArray::NdArray< int64_t >, NdArray::NdArray< float >, NdArray::NdArray< double > > cell_type
The possible cell types.
Definition: Row.h:71
std::pair
Euclid::SourceCatalog::PhotometryAttributeFromRow::m_missing_photometry_enabled
bool m_missing_photometry_enabled
Definition: PhotometryAttributeFromRow.h:109
std::vector
STL class.
Euclid::SourceCatalog::FluxErrorPair
Definition: Photometry.h:41
PhotometryAttributeFromRow.h
Elements::isEqual
bool isEqual(const RawType &left, const RawType &right)
Euclid::SourceCatalog::PhotometryAttributeFromRow::PhotometryAttributeFromRow
PhotometryAttributeFromRow(std::shared_ptr< Euclid::Table::ColumnInfo > column_info_ptr, const std::vector< std::pair< std::string, std::pair< std::string, std::string >>> &filter_name_mapping, const bool missing_photometry_enabled, const double missing_photometry_flag, const bool upper_limit_enabled, const std::vector< std::pair< std::string, float >> n_map, const double n_upper_limit_flag)
Create a PhotometryAttributeFromRow object.
Definition: PhotometryAttributeFromRow.cpp:39
Euclid::SourceCatalog::PhotometryParsingException
Definition: PhotometryParsingException.h:34
CastVisitor.h
Euclid::SourceCatalog::PhotometryAttributeFromRow::m_n_upper_limit_flag
double m_n_upper_limit_flag
Definition: PhotometryAttributeFromRow.h:120
Euclid::SourceCatalog::Photometry
Definition: Photometry.h:66
std::vector::push_back
T push_back(T... args)
std::isnan
T isnan(T... args)
std::isinf
T isinf(T... args)
Exception.h
Euclid::SourceCatalog::PhotometryAttributeFromRow::m_table_index_vector
std::vector< std::pair< size_t, size_t > > m_table_index_vector
Definition: PhotometryAttributeFromRow.h:102
Elements::Exception
std::vector::begin
T begin(T... args)
Euclid::SourceCatalog::PhotometryAttributeFromRow::m_missing_photometry_flag
double m_missing_photometry_flag
Definition: PhotometryAttributeFromRow.h:114
Euclid::SourceCatalog::PhotometryAttributeFromRow::m_filter_name_vector_ptr
std::shared_ptr< std::vector< std::string > > m_filter_name_vector_ptr
Definition: PhotometryAttributeFromRow.h:107
Euclid::SourceCatalog::PhotometryAttributeFromRow::~PhotometryAttributeFromRow
virtual ~PhotometryAttributeFromRow()
Definition: PhotometryAttributeFromRow.cpp:73
Euclid::SourceCatalog::PhotometryAttributeFromRow::createAttribute
std::unique_ptr< Attribute > createAttribute(const Euclid::Table::Row &row) override
Create a photometricAttribute from a Table row.
Definition: PhotometryAttributeFromRow.cpp:77
std::make_pair
T make_pair(T... args)
Euclid::Table::Row
Represents one row of a Table.
Definition: Row.h:64
Euclid::Table::CastVisitor< double >
Definition: CastVisitor.h:68
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
Real.h
std::unique_ptr
STL class.
Euclid
Definition: InstOrRefHolder.h:29