SourceXtractorPlusPlus 0.21
SourceXtractor++, the next generation SExtractor
Loading...
Searching...
No Matches
DetectionImageConfig.cpp
Go to the documentation of this file.
1
22#include "Configuration/ConfigManager.h"
23
24#include <boost/regex.hpp>
25using boost::regex;
26using boost::regex_match;
27using boost::smatch;
28
32
34
36
37using namespace Euclid::Configuration;
38namespace po = boost::program_options;
39
40namespace SourceXtractor {
41
42static const std::string DETECTION_IMAGE { "detection-image" };
43static const std::string DETECTION_IMAGE_GAIN { "detection-image-gain" };
44static const std::string DETECTION_IMAGE_FLUX_SCALE {"detection-image-flux-scale"};
45static const std::string DETECTION_IMAGE_SATURATION { "detection-image-saturation" };
46static const std::string DETECTION_IMAGE_INTERPOLATION { "detection-image-interpolation" };
47static const std::string DETECTION_IMAGE_INTERPOLATION_GAP { "detection-image-interpolation-gap" };
48
51
53 return { {"Detection image", {
54 {DETECTION_IMAGE.c_str(), po::value<std::string>(),
55 "Path to a fits format image to be used as detection image."},
56 {DETECTION_IMAGE_GAIN.c_str(), po::value<double>(),
57 "Detection image gain in e-/ADU (0 = infinite gain)"},
58 {DETECTION_IMAGE_FLUX_SCALE.c_str(), po::value<double>(),
59 "Detection image flux scale"},
60 {DETECTION_IMAGE_SATURATION.c_str(), po::value<double>(),
61 "Detection image saturation level (0 = no saturation)"},
62 {DETECTION_IMAGE_INTERPOLATION.c_str(), po::value<bool>()->default_value(true),
63 "Interpolate bad pixels in detection image"},
64 {DETECTION_IMAGE_INTERPOLATION_GAP.c_str(), po::value<int>()->default_value(5),
65 "Maximum number if pixels to interpolate over"}
66 }}};
67}
68
70 // Normally we would define this one as required, but then --list-output-properties would be
71 // unusable unless we also specify --detection-image, which is not very intuitive.
72 // For this reason, we check for its existence here
73
74 if (args.find(DETECTION_IMAGE) == args.end()) {
75 // Running without a detection image
76 return;
77 }
78
80
81 boost::regex hdu_regex(".*\\[[0-9]*\\]$");
82
83 for (int i=0;; i++) {
85
87 if (boost::regex_match(m_detection_image_path, hdu_regex)) {
88 if (i==0) {
90 } else {
91 break;
92 }
93 } else {
94 try {
96 } catch (...) {
97 if (i==0) {
98 // Skip past primary HDU if it doesn't have an image
99 continue;
100 } else {
101 if (m_extensions.size() == 0) {
102 throw;
103 }
104 break;
105 }
106 }
107 }
108
109 extension.m_image_source = fits_image_source;
111 extension.m_coordinate_system = std::make_shared<WCS>(*fits_image_source);
112
114 auto img_metadata = fits_image_source->getMetadata();
115
116 if (img_metadata.count("GAIN")){
117 // read the keyword GAIN from the metadata
118 if (double* double_gain = boost::get<double>(&img_metadata.at("GAIN").m_value)){
120 } else if (int64_t *int64_gain = boost::get<int64_t>(&img_metadata.at("GAIN").m_value)){
122 }
123 else {
124 throw Elements::Exception() << "Keyword GAIN must be either float or int!";
125 }
126 }
127
128 if (img_metadata.count("SATURATE")){
129 // read the keyword SATURATE from the metadata
130 if (double* double_saturate = boost::get<double>(&img_metadata.at("SATURATE").m_value)){
132 } else if (int64_t *int64_saturate = boost::get<int64_t>(&img_metadata.at("SATURATE").m_value)){
134 }
135 else {
136 throw Elements::Exception() << "Keyword SATURATE must be either float or int!";
137 }
138 }
139
140 if (args.find(DETECTION_IMAGE_FLUX_SCALE) != args.end()) {
141 extension.m_flux_scale = args.find(DETECTION_IMAGE_FLUX_SCALE)->second.as<double>();
142 }
143 else if (img_metadata.count("FLXSCALE")) {
144 // read the keyword FLXSCALE from the metadata
145 if (double* f_scale = boost::get<double>(&img_metadata.at("FLXSCALE").m_value)){
146 extension.m_flux_scale = *f_scale;
147 } else if (int64_t *int64_f_scale = boost::get<int64_t>(&img_metadata.at("FLXSCALE").m_value)){
148 extension.m_flux_scale = (double) *int64_f_scale;
149 }
150 else {
151 throw Elements::Exception() << "Keyword FLXSCALE must be either float or int!";
152 }
153 }
154
155 if (args.find(DETECTION_IMAGE_GAIN) != args.end()) {
156 extension.m_gain = args.find(DETECTION_IMAGE_GAIN)->second.as<double>();
157 }
158 else {
160 }
161
162 if (args.find(DETECTION_IMAGE_SATURATION) != args.end()) {
163 extension.m_saturation = args.find(DETECTION_IMAGE_SATURATION)->second.as<double>();
164 }
165 else {
166 extension.m_saturation = detection_image_saturate;
167 }
168
169 extension.m_interpolation_gap = args.find(DETECTION_IMAGE_INTERPOLATION)->second.as<bool>() ?
170 std::max(0, args.find(DETECTION_IMAGE_INTERPOLATION_GAP)->second.as<int>()) : 0;
171
172 // Adapt image and parameters to take flux_scale into consideration
173 if (extension.m_flux_scale != 1.0) {
174 extension.m_detection_image =
176 extension.m_gain /= extension.m_flux_scale;
177 extension.m_saturation *= extension.m_flux_scale;
178 }
179
180 m_extensions.emplace_back(std::move(extension));
181 }
182}
183
187
189 if (getCurrentState() < State::INITIALIZED) {
190 throw Elements::Exception() << "getDetectionImage() call on not initialized DetectionImageConfig";
191 }
192 return m_extensions.at(index).m_detection_image;
193}
194
196 if (getCurrentState() < State::INITIALIZED) {
197 throw Elements::Exception() << "getCoordinateSystem() call on not initialized DetectionImageConfig";
198 }
199 return m_extensions.at(index).m_coordinate_system;
200}
201
202} // SourceXtractor namespace
203
204
205
static std::shared_ptr< BufferedImage< T > > create(std::shared_ptr< const ImageSource > source, std::shared_ptr< TileManager > tile_manager=TileManager::getInstance())
std::shared_ptr< DetectionImage > getDetectionImage(size_t index=0) const
std::shared_ptr< CoordinateSystem > getCoordinateSystem(size_t index=0) const
void initialize(const UserValues &args) override
std::vector< DetectionImageExtension > m_extensions
DetectionImageConfig(long manager_id)
Constructs a new DetectionImageConfig object.
std::map< std::string, Configuration::OptionDescriptionList > getProgramOptions() override
T max(T... args)
T move(T... args)
static const std::string DETECTION_IMAGE_GAIN
static const std::string DETECTION_IMAGE_INTERPOLATION_GAP
static const std::string DETECTION_IMAGE_INTERPOLATION
static const std::string DETECTION_IMAGE_FLUX_SCALE
static const std::string DETECTION_IMAGE
static const std::string DETECTION_IMAGE_SATURATION