Alexandria  2.19
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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, const std::vector<std::pair<std::string, bool>> convert_from_mag)
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  , m_convert_from_mag(convert_from_mag){
50 
51  std::unique_ptr<size_t> flux_column_index_ptr;
52  std::unique_ptr<size_t> error_column_index_ptr;
53 
54  for (auto filter_name_pair : filter_name_mapping) {
55  flux_column_index_ptr = column_info_ptr->find(filter_name_pair.second.first);
56  error_column_index_ptr = column_info_ptr->find(filter_name_pair.second.second);
57 
58  if (flux_column_index_ptr == nullptr) {
59  throw Elements::Exception() << "Column info does not have the flux column " << filter_name_pair.second.first;
60  }
61  if (error_column_index_ptr == nullptr) {
62  throw Elements::Exception() << "Column info does not have the flux error column " << filter_name_pair.second.second;
63  }
64  m_table_index_vector.push_back(std::make_pair(*(flux_column_index_ptr), *(error_column_index_ptr)));
65  }
66 
67  // create and filled the shared pointer to the filter name vector
68  m_filter_name_vector_ptr = std::make_shared<std::vector<std::string>>();
69  for (auto a_filter_name_map : filter_name_mapping) {
70  m_filter_name_vector_ptr->push_back(a_filter_name_map.first);
71  }
72 
73  // default the m_convert_from_mag
74  if (m_convert_from_mag.size() != m_n_map.size()) {
76  for (auto m_n_pair : m_n_map) {
77  m_convert_from_mag.push_back(std::make_pair(m_n_pair.first, false));
78  }
79  }
80 }
81 
83  // @todo Auto-generated destructor stub
84 }
85 
86 
87 
88 std::pair<double, double> PhotometryAttributeFromRow::convertFromMag(const double mag, const double mag_err) const{
89 
92  } else {
93  // check if the error is a flag
94  bool is_flag = Elements::isEqual(mag_err, m_n_upper_limit_flag);
95 
96  //compute the flux and the error
97  double flux = 3.631e9*std::pow(10, -0.4 * mag);
98  // with this formula the sign of mag_err is forwarded to the flux_err
99  double flux_err = is_flag ? m_n_upper_limit_flag : 0.4* flux * mag_err * std::log(10);
100 
101  return std::make_pair(flux, flux_err);
102  }
103 }
104 
106 
107  std::vector<FluxErrorPair> photometry_vector{};
108 
109  auto n_threshod_iter = m_n_map.begin();
110  auto convert_from_mag_iter = m_convert_from_mag.begin();
111  for (auto& filter_index_pair : m_table_index_vector) {
112  Euclid::Table::Row::cell_type flux_cell = row[filter_index_pair.first];
113  Euclid::Table::Row::cell_type error_cell = row[filter_index_pair.second];
114 
115  double flux = boost::apply_visitor(Table::CastVisitor<double>{}, flux_cell);
116  double error = boost::apply_visitor(Table::CastVisitor<double>{}, error_cell);
117 
118  if (convert_from_mag_iter->second) {
119  auto converted = convertFromMag(flux, error);
120  flux = converted.first;
121  error = converted.second;
122  }
123 
124  bool missing_data = false;
125  bool upper_limit = false;
126  if (std::isinf(flux)) {
127  throw SourceCatalog::PhotometryParsingException("Infinite flux encountered when parsing the Photometry", flux, error);
128  }
131  missing_data = Elements::isEqual(flux, m_missing_photometry_flag) || std::isnan(flux);
132  if (missing_data) {
133  error = 0;
134  } else {
135  if (m_upper_limit_enabled) {
137  if (error == 0.) {
139  "Zero error encountered when parsing the Photometry with 'missing data' and 'upper limit' enabled", flux, error);
140  }
141  if (error < 0) {
144  error = flux / n_threshod_iter->second;
145  }
146  upper_limit = true;
147  if (flux <= 0) {
149  "Negative or Zero flux encountered when parsing the Photometry in the context of an 'upper limit'", flux, error);
150  }
151 
152  error = std::abs(error);
153  }
154  } else {
156  if (error <= 0) {
158  "Negative or Zero error encountered when parsing the Photometry with 'missing data' enabled "
159  "and 'upper limit' disabled",
160  flux, error);
161  }
162  }
163  }
164  } else {
166  if (std::isnan(flux)) {
168  "NAN flux encountered when parsing the Photometry with 'missing data' disabled", flux, error);
169  }
170 
171  if (m_upper_limit_enabled) {
173  if (error == 0.) {
175  "Zero error encountered when parsing the Photometry with 'missing data' disabled and 'upper limit' enabled", flux,
176  error);
177  }
178  if (error < 0) {
180  upper_limit = true;
182  error = flux / n_threshod_iter->second;
183  }
184  if (flux <= 0) {
186  "Negative or Zero flux encountered when parsing the Photometry in the context of an 'upper limit'", flux, error);
187  }
188  error = std::abs(error);
189  }
190  } else {
192  if (error <= 0) {
194  "Negative or Zero error encountered when parsing the Photometry with 'missing data' "
195  "and 'upper limit' disabled",
196  flux, error);
197  }
198  }
199  }
200 
201  photometry_vector.push_back(FluxErrorPair{flux, error, missing_data, upper_limit});
202  ++n_threshod_iter;
203  ++convert_from_mag_iter;
204  } // Eof for
205 
206  std::unique_ptr<Attribute> photometry_ptr{new Photometry{m_filter_name_vector_ptr, photometry_vector}};
207 
208  return photometry_ptr;
209 }
210 
211 } // namespace SourceCatalog
212 } // end of namespace Euclid
T log(T...args)
STL class.
std::pair< double, double > convertFromMag(const double mag, const double mag_err) const
T push_back(T...args)
std::vector< std::pair< size_t, size_t > > m_table_index_vector
T make_pair(T...args)
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, const std::vector< std::pair< std::string, bool >> convert_from_mag={})
Create a PhotometryAttributeFromRow object.
Represents one row of a Table.
Definition: Row.h:64
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
T size(T...args)
STL class.
STL class.
T begin(T...args)
std::shared_ptr< std::vector< std::string > > m_filter_name_vector_ptr
T pow(T...args)
std::unique_ptr< Attribute > createAttribute(const Euclid::Table::Row &row) override
Create a photometricAttribute from a Table row.
bool isEqual(const RawType &left, const RawType &right)
T isnan(T...args)
std::vector< std::pair< std::string, bool > > m_convert_from_mag
T isinf(T...args)
std::vector< std::pair< std::string, float > > m_n_map