SourceXtractorPlusPlus  0.19
SourceXtractor++, the next generation SExtractor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExternalFlagConfig.cpp
Go to the documentation of this file.
1 
23 #include <boost/filesystem.hpp>
24 #include <boost/algorithm/string.hpp>
25 
26 #include <boost/regex.hpp>
27 using boost::regex;
28 using boost::regex_match;
29 using boost::smatch;
30 
32 
34 
36 
37 namespace po = boost::program_options;
38 namespace fs = boost::filesystem;
40 
41 namespace SourceXtractor {
42 
43 namespace {
44 
45 const std::string FLAG_IMAGE {"flag-image"};
46 const std::string FLAG_TYPE {"flag-type"};
47 
54 };
55 
56 }
57 
59  return {{"External flag options", {
60  {poh::wildcard(FLAG_IMAGE).c_str(), po::value<std::string>(),
61  "The FITS file containing the external flag, several images can be provided, replace * by any identifier (ex. use numbers)"},
62  {poh::wildcard(FLAG_TYPE).c_str(), po::value<std::string>(),
63  "The combination type of the external flag (OR, AND, MIN, MAX, MOST)"}
64  }}};
65 }
66 
67 
68 void ExternalFlagConfig::preInitialize(const UserValues& args) {
69  for (auto& name : poh::findWildcardNames({FLAG_IMAGE, FLAG_TYPE}, args)) {
70 
71  // Check that the user gave both the filename and the type
72  if (args.count(poh::wildcard(FLAG_IMAGE, name)) == 0) {
73  throw Elements::Exception() << "Missing option " << poh::wildcard(FLAG_IMAGE, name);
74  }
75 
76  std::string type;
77  if (args.count(poh::wildcard(FLAG_TYPE, name)) == 0) {
78  type = "OR";
79  } else {
80  type = boost::to_upper_copy(args.at(poh::wildcard(FLAG_TYPE, name)).as<std::string>());
81  }
82 
83  // Check that the type is a valid option
84  if (available_types.count(type) == 0) {
85  throw Elements::Exception() << "Invalid option " << poh::wildcard(FLAG_TYPE, name)
86  << " : " << type;
87  }
88  }
89 }
90 
91 void ExternalFlagConfig::initialize(const UserValues& args) {
92  for (auto& name : poh::findWildcardNames({FLAG_IMAGE, FLAG_TYPE}, args)) {
93 
94  auto& filename = args.at(poh::wildcard(FLAG_IMAGE, name)).as<std::string>();
96  boost::regex hdu_regex(".*\\[[0-9]*\\]$");
97 
98  for (int i=0;; i++) {
99  std::shared_ptr<FitsImageSource> fits_image_source;
100  if (boost::regex_match(filename, hdu_regex)) {
101  if (i==0) {
102  fits_image_source = std::make_shared<FitsImageSource>(filename, 0, ImageTile::LongLongImage);
103  } else {
104  break;
105  }
106  } else {
107  try {
108  fits_image_source = std::make_shared<FitsImageSource>(filename, i+1, ImageTile::LongLongImage);
109  } catch (...) {
110  if (i==0) {
111  // Skip past primary HDU if it doesn't have an image
112  continue;
113  } else {
114  if (flag_images.size() == 0) {
115  throw;
116  }
117  break;
118  }
119  }
120  }
121 
122  flag_images.emplace_back(BufferedImage<std::int64_t>::create(fits_image_source));
123  }
124 
125  std::string type_str;
126  if (args.count(poh::wildcard(FLAG_TYPE, name)) == 0) {
127  type_str = "OR";
128  } else {
129  type_str = boost::to_upper_copy(args.at(poh::wildcard(FLAG_TYPE, name)).as<std::string>());
130  }
131  Type type = available_types.at(type_str);
132 
133  m_flag_info_list.emplace_back(name, FlagInfo{std::move(flag_images), type});
134  }
135 }
136 
137 auto ExternalFlagConfig::getFlagInfoList() const -> const std::vector<std::pair<std::string, FlagInfo>>& {
138  return m_flag_info_list;
139 }
140 
141 } // SourceXtractor namespace
142 
143 
144 
static std::string wildcard(const std::string &name, const std::string &instance="*")
std::map< std::string, OptionDescriptionList > getProgramOptions() override
STL class.
STL class.
std::vector< std::pair< std::string, FlagInfo > > m_flag_info_list
string filename
Definition: conf.py:65
void initialize(const UserValues &args) override
void preInitialize(const UserValues &args) override
T move(T...args)
static std::set< std::string > findWildcardNames(const std::vector< std::string > &option_name_list, const std::map< std::string, boost::program_options::variable_value > &options)
STL class.
T c_str(T...args)
const std::vector< std::pair< std::string, FlagInfo > > & getFlagInfoList() const