24 #include <boost/algorithm/string.hpp>
25 #include <boost/timer/timer.hpp>
45 namespace po = boost::program_options;
46 namespace timer = boost::timer;
47 using namespace Euclid;
48 using namespace SourceXtractor;
70 {
"simple", Algorithm::SIMPLE},
79 auto i = args.
find(var);
82 auto input = boost::filesystem::path(m_detection_config.getDetectionImagePath());
83 auto basename = input.leaf();
84 while (!basename.extension().empty())
85 basename = basename.stem();
87 auto output_area = boost::filesystem::path(args.
at(
"output-area").as<
std::string>());
88 auto output = output_area / boost::filesystem::path(basename.native() +
"_" + algo +
"_" + default_suffix);
89 output.replace_extension(
"fits");
90 return output.native();
95 switch (m_algorithm) {
96 case Algorithm::SIMPLE:
97 return Euclid::make_unique<SimpleBackgroundAnalyzer>();
99 return Euclid::make_unique<SEBackgroundLevelAnalyzer>(m_cell_size, m_smooth, m_weight_config.getWeightType());
110 po::options_description options;
116 options.add(config_manager.closeRegistration());
117 options.add_options()
118 (
"output", po::value<std::string>(),
"Output image for the background")
119 (
"output-variance", po::value<std::string>(),
"Output image for the variance")
120 (
"output-area", po::value<std::string>()->default_value(boost::filesystem::temp_directory_path().native()),
"Output area")
121 (
"algorithm", po::value<std::string>()->required(),
"Algorithm to use: Simple, ng")
122 (
"cell-size", po::value<std::string>()->default_value(
"64"),
"Cell size for the histogram")
123 (
"smooth-size", po::value<std::string>()->default_value(
"3"),
"Box size for the median filtering")
124 (
"no-write", po::bool_switch(),
"Do not write the image (skip interpolation)")
125 (
"tile-size", po::value<int>()->default_value(512),
"Tile size")
126 (
"tile-memory", po::value<int>()->default_value(2048),
"Tile memory limit");
133 config_manager.initialize(args);
138 m_output_bg = getOutputPath(args,
"output",
"background");
139 m_output_var = getOutputPath(args,
"output-variance",
"variance");
142 boost::to_lower(algorithm_str);
143 if (s_algo_map.count(algorithm_str) > 0)
144 m_algorithm = s_algo_map[algorithm_str];
148 m_cell_size = stringToVector<int>(args.
at(
"cell-size").as<
std::string>());
149 m_smooth = stringToVector<int>(args.
at(
"smooth-size").as<
std::string>());
151 auto tile_size = args.
at(
"tile-size").as<
int>();
152 auto tile_memory = args.
at(
"tile-memory").as<
int>();
153 TileManager::getInstance()->setOptions(tile_size, tile_size, tile_memory);
159 logger.
info() <<
"Input image: " << m_detection_config.getDetectionImagePath();
160 logger.
info() <<
"Destination background image: " << m_output_bg;
161 logger.
info() <<
"Destination variance image: " << m_output_var;
163 auto bg_analyzer = getBackgroundAnalyzer();
164 assert (bg_analyzer !=
nullptr);
166 auto image = m_detection_config.getDetectionImage();
167 auto weight_image = m_weight_config.getWeightImage();
168 auto threshold = m_weight_config.getWeightThreshold();
174 logger.
info() <<
"Weight image " << weight_image->getRepr();
178 timer::cpu_timer timer;
179 auto bg_model = bg_analyzer->analyzeBackground(image, weight_image, mask, threshold);
181 if (!args.
at(
"no-write").as<
bool>()) {
183 FitsWriter::writeFile(*bg_model.getLevelMap(), m_output_bg, m_detection_config.getCoordinateSystem());
185 FitsWriter::writeFile(*bg_model.getVarianceMap(), m_output_var, m_detection_config.getCoordinateSystem());
193 TileManager::getInstance()->saveAllTiles();
std::unique_ptr< BackgroundAnalyzer > getBackgroundAnalyzer()
WeightImageConfig m_weight_config
DetectionImageConfig m_detection_config
static ConfigManager & getInstance(long id)
long getUniqueManagerId() noexcept
void info(const std::string &logMessage)
static Elements::Logging logger
void configure(const std::map< std::string, po::variable_value > &args)
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
std::string getOutputPath(const std::map< std::string, po::variable_value > &args, const std::string &var, const std::string &default_suffix)
po::options_description defineSpecificProgramOptions() override
std::vector< int > m_smooth
static long config_manager_id
Elements::ExitCode mainMethod(std::map< std::string, po::variable_value > &args) override
static Logging getLogger(const std::string &name="")