32#include "hdrl_catalogue.h"
33#include "hdrl_image.h"
34#include "hdrl_types.h"
35#include "hdrl_utils.h"
78 cpl_boolean obj_deblending;
79 double obj_core_radius;
80 cpl_boolean bkg_estimate;
82 hdrl_catalogue_options resulttype;
83 double bkg_smooth_fwhm;
85 double det_saturation;
86} hdrl_catalogue_parameter;
89static hdrl_parameter_typeobj hdrl_catalogue_parameter_type = {
90 HDRL_PARAMETER_CATALOGUE,
91 (hdrl_alloc *)&cpl_malloc,
92 (hdrl_free *)&cpl_free,
94 sizeof(hdrl_catalogue_parameter),
97static cpl_error_code hdrl_cleanup_qclist(cpl_propertylist * qclist);
108hdrl_catalogue_parameter_verify(
const hdrl_parameter * param)
110 cpl_error_ensure(param != NULL, CPL_ERROR_NULL_INPUT,
111 return CPL_ERROR_NULL_INPUT,
"NULL Input Parameters");
113 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
114 "Expected catalogue parameter") ;
116 const hdrl_catalogue_parameter * param_loc = (
const hdrl_catalogue_parameter *)param ;
118 cpl_error_ensure(param_loc->obj_min_pixels > 0, CPL_ERROR_ILLEGAL_INPUT,
119 return CPL_ERROR_ILLEGAL_INPUT,
"obj.min-pixels > 0");
121 cpl_error_ensure(param_loc->obj_threshold > 0., CPL_ERROR_ILLEGAL_INPUT,
122 return CPL_ERROR_ILLEGAL_INPUT,
"obj_threshold > 0.");
124 cpl_error_ensure(param_loc->obj_core_radius > 0., CPL_ERROR_ILLEGAL_INPUT,
125 return CPL_ERROR_ILLEGAL_INPUT,
"obj_core_radius > 0.");
127 if (param_loc->bkg_estimate) {
128 cpl_error_ensure(param_loc->bkg_mesh_size > 2, CPL_ERROR_ILLEGAL_INPUT,
129 return CPL_ERROR_ILLEGAL_INPUT,
"bkg_mesh_size > 2");
131 cpl_error_ensure(param_loc->bkg_smooth_fwhm >= 0., CPL_ERROR_ILLEGAL_INPUT,
132 return CPL_ERROR_ILLEGAL_INPUT,
"bkg_mesh_size >= 0.");
135 cpl_error_ensure(param_loc->det_eff_gain > 0., CPL_ERROR_ILLEGAL_INPUT,
136 return CPL_ERROR_ILLEGAL_INPUT,
"det_eff_gain > 0.");
138 cpl_error_ensure(param_loc->det_saturation > 0. ||
139 param_loc->det_saturation == HDRL_SATURATION_INIT,
140 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
141 "det_saturation > 0");
143 return CPL_ERROR_NONE ;
175 double obj_threshold, cpl_boolean obj_deblending,
176 double obj_core_radius,
177 cpl_boolean bkg_estimate,
int bkg_mesh_size,
178 double bkg_smooth_fwhm,
double det_eff_gain,
179 double det_saturation,
180 hdrl_catalogue_options resulttype)
182 hdrl_catalogue_parameter * p = (hdrl_catalogue_parameter *)
183 hdrl_parameter_new(&hdrl_catalogue_parameter_type);
185 p->obj_min_pixels = obj_min_pixels;
186 p->obj_threshold = obj_threshold;
187 p->obj_deblending = obj_deblending;
188 p->obj_core_radius = obj_core_radius;
189 p->bkg_estimate = bkg_estimate;
190 p->bkg_mesh_size = bkg_mesh_size;
191 p->resulttype = resulttype;
192 p->bkg_smooth_fwhm = bkg_smooth_fwhm;
193 p->det_eff_gain = det_eff_gain;
194 p->det_saturation = det_saturation;
197 p->resulttype &= ~(HDRL_CATALOGUE_BKG);
200 if (hdrl_catalogue_parameter_verify((hdrl_parameter *)p)) {
204 return (hdrl_parameter *)p;
217 return hdrl_parameter_check_type(self, &hdrl_catalogue_parameter_type);
231 hdrl_catalogue_options opt)
233 cpl_ensure_code(par, CPL_ERROR_NULL_INPUT);
234 cpl_error_code err = hdrl_catalogue_parameter_verify(par);
235 if (err != CPL_ERROR_NONE) {
238 hdrl_catalogue_parameter * par_ = (hdrl_catalogue_parameter*)par;
239 par_->resulttype = opt;
240 if (!par_->bkg_estimate) {
241 par_->resulttype &= ~(HDRL_CATALOGUE_BKG);
243 return hdrl_catalogue_parameter_verify(par);
270 const char *base_context,
272 hdrl_parameter *defaults)
275 cpl_ensure(prefix && base_context && defaults,
276 CPL_ERROR_NULL_INPUT, NULL);
279 CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
281 hdrl_catalogue_parameter *par = (hdrl_catalogue_parameter*)defaults;
283 cpl_parameterlist *parlist = cpl_parameterlist_new();
285 hdrl_setup_vparameter(parlist, prefix,
".",
"obj.",
"min-pixels",
286 base_context,
"Minimum pixel area for each detected "
287 "object.", CPL_TYPE_INT, par->obj_min_pixels);
289 hdrl_setup_vparameter(parlist, prefix,
".",
"obj.",
"threshold",
291 "Detection threshold in sigma above sky.",
292 CPL_TYPE_DOUBLE, par->obj_threshold);
294 hdrl_setup_vparameter(parlist, prefix,
".",
"obj.",
"deblending",
295 base_context,
"Use deblending?.",
296 CPL_TYPE_BOOL, par->obj_deblending);
298 hdrl_setup_vparameter(parlist, prefix,
".",
"obj.",
"core-radius",
299 base_context,
"Value of Rcore in pixels.",
300 CPL_TYPE_DOUBLE, par->obj_core_radius);
302 hdrl_setup_vparameter(parlist, prefix,
".",
"bkg.",
"estimate",
303 base_context,
"Estimate background from input, if "
304 "false it is assumed input is already background "
305 "corrected with median 0",
306 CPL_TYPE_BOOL, par->bkg_estimate);
308 hdrl_setup_vparameter(parlist, prefix,
".",
"bkg.",
"mesh-size",
309 base_context,
"Background smoothing box size.",
310 CPL_TYPE_INT, par->bkg_mesh_size);
314 hdrl_setup_vparameter(parlist, prefix,
".",
"bkg.",
"smooth-gauss-fwhm",
315 base_context,
"The FWHM of the Gaussian kernel used "
316 "in convolution for object detection.",
317 CPL_TYPE_DOUBLE, par->bkg_smooth_fwhm);
319 hdrl_setup_vparameter(parlist, prefix,
".",
"det.",
"effective-gain",
320 base_context,
"Detector gain value to rescale "
321 "convert intensity to electrons",
322 CPL_TYPE_DOUBLE, par->det_eff_gain);
324 hdrl_setup_vparameter(parlist, prefix,
".",
"det.",
"saturation",
325 base_context,
"Detector saturation value",
326 CPL_TYPE_DOUBLE, par->det_saturation);
328 if (cpl_error_get_code()) {
329 cpl_parameterlist_delete(parlist);
359 const cpl_parameterlist * parlist,
362 cpl_ensure(prefix && parlist, CPL_ERROR_NULL_INPUT, NULL);
364 const cpl_parameter * par;
365 int obj_min_pixels, bkg_mesh_size;
366 cpl_boolean obj_deblending, bkg_estimate;
367 double obj_threshold, obj_core_radius, bkg_smooth_fwhm, det_eff_gain,
372 par = cpl_parameterlist_find_const(parlist, name);
373 obj_min_pixels = cpl_parameter_get_int(par);
377 par = cpl_parameterlist_find_const(parlist, name);
378 obj_threshold = cpl_parameter_get_double(par);
382 par = cpl_parameterlist_find_const(parlist, name);
383 obj_deblending = cpl_parameter_get_bool(par);
387 par=cpl_parameterlist_find_const(parlist, name);
388 obj_core_radius = cpl_parameter_get_double(par);
392 par = cpl_parameterlist_find_const(parlist, name);
393 bkg_estimate = cpl_parameter_get_bool(par);
397 par = cpl_parameterlist_find_const(parlist, name);
398 bkg_mesh_size = cpl_parameter_get_int(par);
402 par = cpl_parameterlist_find_const(parlist, name);
403 bkg_smooth_fwhm = cpl_parameter_get_double(par);
407 par = cpl_parameterlist_find_const(parlist, name);
408 det_eff_gain = cpl_parameter_get_double(par);
412 par = cpl_parameterlist_find_const(parlist, name);
413 det_saturation = cpl_parameter_get_double(par);
416 if (cpl_error_get_code()) {
417 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
418 "Error while parsing parameterlist with prefix %s", prefix);
422 obj_deblending, obj_core_radius,
424 bkg_mesh_size, bkg_smooth_fwhm,
425 det_eff_gain, det_saturation,
441 if (result == NULL) {
444 cpl_table_delete(result->catalogue);
445 cpl_image_delete(result->background);
446 cpl_image_delete(result->segmentation_map);
447 cpl_propertylist_delete(result->qclist);
461hdrl_catalogue_result *
463 const cpl_wcs * wcs, hdrl_parameter * param_)
465 hdrl_casu_fits *inf,*inconf = NULL;
466 hdrl_casu_result * intres = NULL;
467 cpl_ensure(image_, CPL_ERROR_NULL_INPUT, NULL);
468 if (hdrl_catalogue_parameter_verify((hdrl_parameter *)param_)) {
472 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
474 hdrl_catalogue_result * res = NULL;
475 const hdrl_catalogue_parameter * param = (hdrl_catalogue_parameter *)param_;
476 cpl_image * image = (cpl_image *)image_;
477 if (cpl_image_get_type(image) != CPL_TYPE_DOUBLE) {
478 image = cpl_image_cast(image, CPL_TYPE_DOUBLE);
483 cpl_image * conf_ = (cpl_image*)confidence_map;
488 if (confidence_map) {
489 if (cpl_image_get_min(confidence_map) < 0) {
490 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
491 "confidence_map must only contain "
497 if (cpl_image_get_bpm_const(image) != NULL) {
498 if (confidence_map) {
499 conf_ = cpl_image_cast(confidence_map, CPL_TYPE_DOUBLE);
502 conf_ = cpl_image_new(cpl_image_get_size_x(image),
503 cpl_image_get_size_y(image), CPL_TYPE_DOUBLE);
504 cpl_image_add_scalar(conf_, 100);
506 cpl_image_reject_from_mask(conf_, cpl_image_get_bpm_const(image));
507 cpl_image_fill_rejected(conf_, 0);
508 cpl_image_accept_all(conf_);
510 else if (confidence_map &&
511 cpl_image_get_type(confidence_map) != CPL_TYPE_DOUBLE) {
512 conf_ = cpl_image_cast(confidence_map, CPL_TYPE_DOUBLE);
517 res = cpl_calloc(
sizeof(hdrl_catalogue_result), 1);
518 intres = cpl_calloc(
sizeof(hdrl_casu_result), 1);
520 param->obj_min_pixels, param->obj_threshold,
521 param->obj_deblending, param->obj_core_radius,
523 param->bkg_mesh_size, (
int)param->resulttype,
524 param->bkg_smooth_fwhm, param->det_eff_gain,
525 param->det_saturation, intres);
527 if (intres->catalogue) {
530 hdrl_cleanup_qclist(res->qclist);
532 res->segmentation_map = intres->segmentation_map;
533 res->background = intres->background;
537 if (image_ != image) {
538 cpl_image_delete(image);
540 if (inconf && inconf->image == confidence_map) {
541 inconf->image = NULL;
560static cpl_error_code hdrl_cleanup_qclist(cpl_propertylist * qclist){
562 cpl_propertylist * qclocal = cpl_propertylist_duplicate(qclist);
563 cpl_propertylist_empty(qclist);
565 if (cpl_propertylist_has(qclocal,
"APCOR1"))
566 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR1");
567 if (cpl_propertylist_has(qclocal,
"APCOR2"))
568 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR2");
569 if (cpl_propertylist_has(qclocal,
"APCOR3"))
570 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR3");
571 if (cpl_propertylist_has(qclocal,
"APCOR4"))
572 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR4");
573 if (cpl_propertylist_has(qclocal,
"APCOR5"))
574 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR5");
575 if (cpl_propertylist_has(qclocal,
"APCOR6"))
576 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR6");
577 if (cpl_propertylist_has(qclocal,
"APCOR7"))
578 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR7");
579 if (cpl_propertylist_has(qclocal,
"APCORPK"))
580 cpl_propertylist_copy_property(qclist, qclocal,
"APCORPK");
581 if (cpl_propertylist_has(qclocal,
"SYMBOL1"))
582 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL1");
583 if (cpl_propertylist_has(qclocal,
"SYMBOL2"))
584 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL2");
585 if (cpl_propertylist_has(qclocal,
"SYMBOL3"))
586 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL3");
587 if (cpl_propertylist_has(qclocal,
"SYMBOL4"))
588 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL4");
589 if (cpl_propertylist_has(qclocal,
"SYMBOL5"))
590 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL5");
591 if (cpl_propertylist_has(qclocal,
"SYMBOL6"))
592 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL6");
593 if (cpl_propertylist_has(qclocal,
"SYMBOL7"))
594 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL7");
595 if (cpl_propertylist_has(qclocal,
"SYMBOL8"))
596 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL8");
597 if (cpl_propertylist_has(qclocal,
"SYMBOL9"))
598 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL9");
644 cpl_propertylist_delete(qclocal);
646 return cpl_error_get_code();
hdrl_casu_fits * hdrl_casu_fits_wrap(cpl_image *im)
Wrap an image in a hdrl_casu_fits wrapper.
cpl_propertylist * hdrl_casu_tfits_get_ehu(hdrl_casu_tfits *p)
Get the propertylist for the extension header for a given hdrl_casu_tfits image.
void hdrl_casu_tfits_delete(hdrl_casu_tfits *p)
Free all the workspace associated with a hdrl_casu_fits object.
void hdrl_casu_fits_delete(hdrl_casu_fits *p)
Free all the workspace associated with a hdrl_casu_fits object.
cpl_table * hdrl_casu_tfits_get_table(hdrl_casu_tfits *p)
Get the CPL table from the hdrl_casu_tfits object.
cpl_error_code hdrl_casu_catalogue(hdrl_casu_fits *infile, hdrl_casu_fits *conf, const cpl_wcs *wcs, cpl_size ipix, double threshold, cpl_size icrowd, double rcore, cpl_size bkg_subtr, cpl_size nbsize, hdrl_catalogue_options cattype, double filtfwhm, double gainloc, double saturation, hdrl_casu_result *res)
Generate object catalogues from input images.
hdrl_catalogue_result * hdrl_catalogue_compute(const cpl_image *image_, const cpl_image *confidence_map, const cpl_wcs *wcs, hdrl_parameter *param_)
build object catalog
void hdrl_catalogue_result_delete(hdrl_catalogue_result *result)
delete hdrl parameter result object
hdrl_parameter * hdrl_catalogue_parameter_create(int obj_min_pixels, double obj_threshold, cpl_boolean obj_deblending, double obj_core_radius, cpl_boolean bkg_estimate, int bkg_mesh_size, double bkg_smooth_fwhm, double det_eff_gain, double det_saturation, hdrl_catalogue_options resulttype)
Creates catalogue Parameters object.
cpl_boolean hdrl_catalogue_parameter_check(const hdrl_parameter *self)
Check that the parameter is a catalogue parameter.
hdrl_parameter * hdrl_catalogue_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse parameter list to create input parameters for the catalogue.
cpl_parameterlist * hdrl_catalogue_parameter_create_parlist(const char *base_context, const char *prefix, hdrl_parameter *defaults)
Create parameter list for the catalogue computation.
cpl_error_code hdrl_catalogue_parameter_set_option(hdrl_parameter *par, hdrl_catalogue_options opt)
set result option of catalogue parameter
char * hdrl_join_string(const char *sep_, int n,...)
join strings together