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