SourceXtractorPlusPlus  0.15
Please provide a description of the project.
AssocModeConfig.cpp
Go to the documentation of this file.
1 
19 
20 #include <map>
21 #include <boost/algorithm/string.hpp>
22 
23 #include <CCfits/CCfits>
24 
25 #include "Table/AsciiReader.h"
26 #include "Table/FitsReader.h"
27 
30 
32 
33 using namespace Euclid::Configuration;
34 namespace po = boost::program_options;
35 
36 namespace SourceXtractor {
37 
38 static const std::string ASSOC_CATALOG { "assoc-catalog" };
39 static const std::string ASSOC_MODE { "assoc-mode" };
40 static const std::string ASSOC_RADIUS { "assoc-radius" };
41 static const std::string ASSOC_FILTER { "assoc-filter" };
42 static const std::string ASSOC_COPY { "assoc-copy" };
43 static const std::string ASSOC_COLUMNS { "assoc-columns" };
44 
45 namespace {
46 
48  std::make_pair("", AssocModeConfig::AssocMode::UNKNOWN),
49  std::make_pair("FIRST", AssocModeConfig::AssocMode::FIRST),
50  std::make_pair("NEAREST", AssocModeConfig::AssocMode::NEAREST),
52  std::make_pair("MAG_MEAN", AssocModeConfig::AssocMode::MAG_MEAN),
53  std::make_pair("SUM", AssocModeConfig::AssocMode::SUM),
54  std::make_pair("MAG_SUM", AssocModeConfig::AssocMode::MAG_SUM),
55  std::make_pair("MIN", AssocModeConfig::AssocMode::MIN),
56  std::make_pair("MAX", AssocModeConfig::AssocMode::MAX)
57 };
58 
60  std::make_pair("ALL", AssocModeConfig::AssocFilter::ALL),
61  std::make_pair("MATCHED", AssocModeConfig::AssocFilter::MATCHED),
62  std::make_pair("UNMATCHED", AssocModeConfig::AssocFilter::UNMATCHED)
63 };
64 
65 std::vector<int> parseColumnList(const std::string& arg) {
66  if (arg.size() > 0) {
67  try {
69  boost::split(parts, arg, boost::is_any_of(","));
70 
71  std::vector<int> column_list;
72  for (auto& part : parts) {
73  column_list.emplace_back(boost::lexical_cast<int>(part));
74  }
75 
76  return column_list;
77  } catch(...) {
78  throw Elements::Exception() << "Can't parse column list";
79  }
80  } else {
81  return {};
82  }
83 }
84 
85 }
86 
87 AssocModeConfig::AssocModeConfig(long manager_id) :
88  Configuration(manager_id) {
89  declareDependency<PartitionStepConfig>();
91 }
92 
94  return { {"Assoc config", {
95  {ASSOC_CATALOG.c_str(), po::value<std::string>(),
96  "Assoc catalog file"},
97  {ASSOC_COLUMNS.c_str(), po::value<std::string>()->default_value("2,3,4"),
98  "Assoc columns"},
99  {ASSOC_MODE.c_str(), po::value<std::string>()->default_value("NEAREST"),
100  "Assoc mode"},
101  {ASSOC_RADIUS.c_str(), po::value<double>()->default_value(2.0),
102  "Assoc radius"},
103  {ASSOC_FILTER.c_str(), po::value<std::string>()->default_value("ALL"),
104  "Assoc catalog filter setting: ALL, MATCHED, UNMATCHED"},
105  {ASSOC_COPY.c_str(), po::value<std::string>()->default_value(""),
106  "List of assoc catalog columns to copy on match"},
107  }}};
108 }
109 
111 
112  auto filter = boost::to_upper_copy(args.at(ASSOC_FILTER).as<std::string>());
113  if (assoc_filter_table.find(filter) != assoc_filter_table.end()) {
114  auto assoc_filter = assoc_filter_table.at(filter);
115  if (assoc_filter == AssocFilter::MATCHED) {
116  getDependency<PartitionStepConfig>().addPartitionStepCreator(
117  [](std::shared_ptr<SourceFactory> source_factory) {
118  return std::make_shared<AssocModePartitionStep>(true);
119  }
120  );
121  } else if (assoc_filter == AssocFilter::UNMATCHED) {
122  getDependency<PartitionStepConfig>().addPartitionStepCreator(
123  [](std::shared_ptr<SourceFactory> source_factory) {
124  return std::make_shared<AssocModePartitionStep>(false);
125  }
126  );
127  }
128  } else {
129  throw Elements::Exception() << "Invalid assoc filter: " << filter;
130  }
131 
132  m_assoc_radius = args.at(ASSOC_RADIUS).as<double>();
133 
134  auto columns = parseColumnList(args.at(ASSOC_COLUMNS).as<std::string>());
135  if (columns.size() < 2) {
136  throw Elements::Exception() << "At least 2 columns must be specified for x,y coordinates in the assoc catalog";
137  }
138  if (columns.size() > 3) {
139  throw Elements::Exception() << "Maximum 3 columns for x, y and weight must be specified in the assoc catalog";
140  }
141 
142  auto copy_columns = parseColumnList(args.at(ASSOC_COPY).as<std::string>());
143 
144  if (args.find(ASSOC_MODE) != args.end()) {
145  auto assoc_mode = boost::to_upper_copy(args.at(ASSOC_MODE).as<std::string>());
146  if (assoc_mode_table.find(assoc_mode) != assoc_mode_table.end()) {
147  m_assoc_mode = assoc_mode_table.at(assoc_mode);
148  } else {
149  throw Elements::Exception() << "Invalid association mode: " << assoc_mode;
150  }
151  }
152 
153  if (args.find(ASSOC_CATALOG) != args.end()) {
154  try {
155  auto filename = args.at(ASSOC_CATALOG).as<std::string>();
156 
158  try {
159  reader = std::make_shared<Euclid::Table::FitsReader>(filename);
160  } catch(...) {
161  // If FITS not successful try reading as ascii
162  reader = std::make_shared<Euclid::Table::AsciiReader>(filename);
163  }
164  auto table = reader->read();
165  readTable(table, columns, copy_columns);
166 
167  } catch(...) {
168  throw Elements::Exception() << "Can't open/read assoc catalog";
169  }
170  }
171 }
172 
174  const Euclid::Table::Table& table, const std::vector<int>& columns, const std::vector<int>& copy_columns) {
175  for (auto& row : table) {
176  // our internal pixel coordinates are zero-based
177  auto coord =
178  ImageCoordinate { boost::get<double>(row[columns.at(0)]) - 1.0, boost::get<double>(row[columns.at(1)]) - 1.0 };
179  m_catalog.emplace_back(CatalogEntry { coord, 1.0, {} });
180  if (columns.size() == 3 && columns.at(2) >= 0) {
181  m_catalog.back().weight = boost::get<double>(row[columns.at(2)]);
182  }
183  for (auto column : copy_columns) {
184  if (row[column].type() == typeid(int)) {
185  m_catalog.back().assoc_columns.emplace_back(boost::get<int>(row[column]));
186  } else if (row[column].type() == typeid(double)) {
187  m_catalog.back().assoc_columns.emplace_back(boost::get<double>(row[column]));
188  } else {
189  throw Elements::Exception() << "Wrong type in assoc column";
190  }
191  }
192  }
193 }
194 
195 
196 }
197 
std::string
STL class.
std::shared_ptr< SourceFactory >
split
ELEMENTS_API auto split(Args &&... args) -> decltype(splitPath(std::forward< Args >(args)...))
conf.filename
string filename
Definition: conf.py:63
SourceXtractor::AssocModeConfig::AssocModeConfig
AssocModeConfig(long manager_id)
Definition: AssocModeConfig.cpp:87
std::vector< int >
std::map::find
T find(T... args)
std::string::size
T size(T... args)
SourceXtractor::AssocModeConfig::initialize
void initialize(const UserValues &args) override
Definition: AssocModeConfig.cpp:110
SourceXtractor::AssocModeConfig::m_assoc_mode
AssocMode m_assoc_mode
Definition: AssocModeConfig.h:83
AssocModePartitionStep.h
MEAN
#define MEAN
Definition: BackgroundDefine.h:46
Euclid::Configuration
AssocModeConfig.h
SourceXtractor::ASSOC_FILTER
static const std::string ASSOC_FILTER
Definition: AssocModeConfig.cpp:41
SourceXtractor::AssocModeConfig::AssocFilter::UNMATCHED
@ UNMATCHED
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::MultiThresholdPartitionConfig
Definition: MultiThresholdPartitionConfig.h:32
std::map::at
T at(T... args)
SourceXtractor::ASSOC_MODE
static const std::string ASSOC_MODE
Definition: AssocModeConfig.cpp:39
std::string::c_str
T c_str(T... args)
MultiThresholdPartitionConfig.h
FitsReader.h
Elements::Exception
SourceXtractor::AssocModeConfig::m_assoc_radius
double m_assoc_radius
Definition: AssocModeConfig.h:84
SourceXtractor::ASSOC_COLUMNS
static const std::string ASSOC_COLUMNS
Definition: AssocModeConfig.cpp:43
std::map
STL class.
SourceXtractor::ImageCoordinate
Definition: CoordinateSystem.h:42
AsciiReader.h
SourceXtractor::AssocModeConfig::getProgramOptions
std::map< std::string, OptionDescriptionList > getProgramOptions() override
Definition: AssocModeConfig.cpp:93
std::vector::emplace_back
T emplace_back(T... args)
SourceXtractor::AssocModeConfig::readTable
void readTable(const Euclid::Table::Table &table, const std::vector< int > &columns, const std::vector< int > &copy_columns)
Definition: AssocModeConfig.cpp:173
SourceXtractor::AssocModeConfig::m_catalog
std::vector< CatalogEntry > m_catalog
Definition: AssocModeConfig.h:86
PartitionStepConfig.h
SourceXtractor::AssocModeConfig::AssocFilter::MATCHED
@ MATCHED
Euclid::Table::Table
Euclid::Configuration::Configuration
std::make_pair
T make_pair(T... args)
std::map::end
T end(T... args)
SourceXtractor::ASSOC_COPY
static const std::string ASSOC_COPY
Definition: AssocModeConfig.cpp:42
SourceXtractor::ASSOC_CATALOG
static const std::string ASSOC_CATALOG
Definition: AssocModeConfig.cpp:38
SourceXtractor::ASSOC_RADIUS
static const std::string ASSOC_RADIUS
Definition: AssocModeConfig.cpp:40
Euclid::Configuration::ConfigManager::getInstance
static ConfigManager & getInstance(long id)
Euclid::Configuration::ConfigManager::registerDependency
void registerDependency()
SourceXtractor::AssocModeConfig::CatalogEntry
Definition: AssocModeConfig.h:53