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,
98 cpl_boolean include_experimental_qc);
99static hdrl_catalogue_result * hdrl_catalogue_compute_internal(
100 const cpl_image * image_,
const cpl_image * confidence_map,
101 const cpl_wcs * wcs, hdrl_parameter * param_,
102 cpl_boolean include_experimental_qc);
113hdrl_catalogue_parameter_verify(
const hdrl_parameter * param)
115 cpl_error_ensure(param != NULL, CPL_ERROR_NULL_INPUT,
116 return CPL_ERROR_NULL_INPUT,
"NULL Input Parameters");
118 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
119 "Expected catalogue parameter") ;
121 const hdrl_catalogue_parameter * param_loc = (
const hdrl_catalogue_parameter *)param ;
123 cpl_error_ensure(param_loc->obj_min_pixels > 0, CPL_ERROR_ILLEGAL_INPUT,
124 return CPL_ERROR_ILLEGAL_INPUT,
"obj.min-pixels > 0");
126 cpl_error_ensure(param_loc->obj_threshold > 0., CPL_ERROR_ILLEGAL_INPUT,
127 return CPL_ERROR_ILLEGAL_INPUT,
"obj_threshold > 0.");
129 cpl_error_ensure(param_loc->obj_core_radius > 0., CPL_ERROR_ILLEGAL_INPUT,
130 return CPL_ERROR_ILLEGAL_INPUT,
"obj_core_radius > 0.");
132 if (param_loc->bkg_estimate) {
133 cpl_error_ensure(param_loc->bkg_mesh_size > 2, CPL_ERROR_ILLEGAL_INPUT,
134 return CPL_ERROR_ILLEGAL_INPUT,
"bkg_mesh_size > 2");
136 cpl_error_ensure(param_loc->bkg_smooth_fwhm >= 0., CPL_ERROR_ILLEGAL_INPUT,
137 return CPL_ERROR_ILLEGAL_INPUT,
"bkg_mesh_size >= 0.");
140 cpl_error_ensure(param_loc->det_eff_gain > 0., CPL_ERROR_ILLEGAL_INPUT,
141 return CPL_ERROR_ILLEGAL_INPUT,
"det_eff_gain > 0.");
143 cpl_error_ensure(param_loc->det_saturation > 0. ||
144 param_loc->det_saturation == HDRL_SATURATION_INIT,
145 CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
146 "det_saturation > 0");
148 return CPL_ERROR_NONE ;
180 double obj_threshold, cpl_boolean obj_deblending,
181 double obj_core_radius,
182 cpl_boolean bkg_estimate,
int bkg_mesh_size,
183 double bkg_smooth_fwhm,
double det_eff_gain,
184 double det_saturation,
185 hdrl_catalogue_options resulttype)
187 hdrl_catalogue_parameter * p = (hdrl_catalogue_parameter *)
188 hdrl_parameter_new(&hdrl_catalogue_parameter_type);
190 p->obj_min_pixels = obj_min_pixels;
191 p->obj_threshold = obj_threshold;
192 p->obj_deblending = obj_deblending;
193 p->obj_core_radius = obj_core_radius;
194 p->bkg_estimate = bkg_estimate;
195 p->bkg_mesh_size = bkg_mesh_size;
196 p->resulttype = resulttype;
197 p->bkg_smooth_fwhm = bkg_smooth_fwhm;
198 p->det_eff_gain = det_eff_gain;
199 p->det_saturation = det_saturation;
202 p->resulttype &= ~(HDRL_CATALOGUE_BKG);
205 if (hdrl_catalogue_parameter_verify((hdrl_parameter *)p)) {
209 return (hdrl_parameter *)p;
222 return hdrl_parameter_check_type(self, &hdrl_catalogue_parameter_type);
236 hdrl_catalogue_options opt)
238 cpl_ensure_code(par, CPL_ERROR_NULL_INPUT);
239 cpl_error_code err = hdrl_catalogue_parameter_verify(par);
240 if (err != CPL_ERROR_NONE) {
243 hdrl_catalogue_parameter * par_ = (hdrl_catalogue_parameter*)par;
244 par_->resulttype = opt;
245 if (!par_->bkg_estimate) {
246 par_->resulttype &= ~(HDRL_CATALOGUE_BKG);
248 return hdrl_catalogue_parameter_verify(par);
275 const char *base_context,
277 hdrl_parameter *defaults)
280 cpl_ensure(prefix && base_context && defaults,
281 CPL_ERROR_NULL_INPUT, NULL);
284 CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
286 hdrl_catalogue_parameter *par = (hdrl_catalogue_parameter*)defaults;
288 cpl_parameterlist *parlist = cpl_parameterlist_new();
290 hdrl_setup_vparameter(parlist, prefix,
".",
"obj.",
"min-pixels",
291 base_context,
"Minimum pixel area for each detected "
292 "object.", CPL_TYPE_INT, par->obj_min_pixels);
294 hdrl_setup_vparameter(parlist, prefix,
".",
"obj.",
"threshold",
296 "Detection threshold in sigma above sky.",
297 CPL_TYPE_DOUBLE, par->obj_threshold);
299 hdrl_setup_vparameter(parlist, prefix,
".",
"obj.",
"deblending",
300 base_context,
"Use deblending?.",
301 CPL_TYPE_BOOL, par->obj_deblending);
303 hdrl_setup_vparameter(parlist, prefix,
".",
"obj.",
"core-radius",
304 base_context,
"Value of Rcore in pixels.",
305 CPL_TYPE_DOUBLE, par->obj_core_radius);
307 hdrl_setup_vparameter(parlist, prefix,
".",
"bkg.",
"estimate",
308 base_context,
"Estimate background from input, if "
309 "false it is assumed input is already background "
310 "corrected with median 0",
311 CPL_TYPE_BOOL, par->bkg_estimate);
313 hdrl_setup_vparameter(parlist, prefix,
".",
"bkg.",
"mesh-size",
314 base_context,
"Background smoothing box size.",
315 CPL_TYPE_INT, par->bkg_mesh_size);
319 hdrl_setup_vparameter(parlist, prefix,
".",
"bkg.",
"smooth-gauss-fwhm",
320 base_context,
"The FWHM of the Gaussian kernel used "
321 "in convolution for object detection.",
322 CPL_TYPE_DOUBLE, par->bkg_smooth_fwhm);
324 hdrl_setup_vparameter(parlist, prefix,
".",
"det.",
"effective-gain",
325 base_context,
"Detector gain value to rescale "
326 "convert intensity to electrons",
327 CPL_TYPE_DOUBLE, par->det_eff_gain);
329 hdrl_setup_vparameter(parlist, prefix,
".",
"det.",
"saturation",
330 base_context,
"Detector saturation value",
331 CPL_TYPE_DOUBLE, par->det_saturation);
333 if (cpl_error_get_code()) {
334 cpl_parameterlist_delete(parlist);
364 const cpl_parameterlist * parlist,
367 cpl_ensure(prefix && parlist, CPL_ERROR_NULL_INPUT, NULL);
369 const cpl_parameter * par;
370 int obj_min_pixels, bkg_mesh_size;
371 cpl_boolean obj_deblending, bkg_estimate;
372 double obj_threshold, obj_core_radius, bkg_smooth_fwhm, det_eff_gain,
377 par = cpl_parameterlist_find_const(parlist, name);
378 obj_min_pixels = cpl_parameter_get_int(par);
382 par = cpl_parameterlist_find_const(parlist, name);
383 obj_threshold = cpl_parameter_get_double(par);
387 par = cpl_parameterlist_find_const(parlist, name);
388 obj_deblending = cpl_parameter_get_bool(par);
392 par=cpl_parameterlist_find_const(parlist, name);
393 obj_core_radius = cpl_parameter_get_double(par);
397 par = cpl_parameterlist_find_const(parlist, name);
398 bkg_estimate = cpl_parameter_get_bool(par);
402 par = cpl_parameterlist_find_const(parlist, name);
403 bkg_mesh_size = cpl_parameter_get_int(par);
407 par = cpl_parameterlist_find_const(parlist, name);
408 bkg_smooth_fwhm = cpl_parameter_get_double(par);
412 par = cpl_parameterlist_find_const(parlist, name);
413 det_eff_gain = cpl_parameter_get_double(par);
417 par = cpl_parameterlist_find_const(parlist, name);
418 det_saturation = cpl_parameter_get_double(par);
421 if (cpl_error_get_code()) {
422 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
423 "Error while parsing parameterlist with prefix %s", prefix);
427 obj_deblending, obj_core_radius,
429 bkg_mesh_size, bkg_smooth_fwhm,
430 det_eff_gain, det_saturation,
444 if (result == NULL) {
447 cpl_table_delete(result->catalogue);
448 cpl_image_delete(result->background);
449 cpl_image_delete(result->segmentation_map);
450 cpl_propertylist_delete(result->qclist);
464hdrl_catalogue_result *
466 const cpl_wcs * wcs, hdrl_parameter * param_)
468 return hdrl_catalogue_compute_internal(image_, confidence_map, wcs, param_,
487hdrl_catalogue_result *
489 const cpl_image * confidence_map,
490 const cpl_wcs * wcs, hdrl_parameter * param_)
492 return hdrl_catalogue_compute_internal(image_, confidence_map, wcs, param_,
496static hdrl_catalogue_result *
497hdrl_catalogue_compute_internal(
const cpl_image * image_,
498 const cpl_image * confidence_map,
499 const cpl_wcs * wcs, hdrl_parameter * param_,
500 cpl_boolean include_experimental_qc)
502 hdrl_casu_fits *inf,*inconf = NULL;
503 hdrl_casu_result * intres = NULL;
504 cpl_ensure(image_, CPL_ERROR_NULL_INPUT, NULL);
505 if (hdrl_catalogue_parameter_verify((hdrl_parameter *)param_)) {
509 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
511 hdrl_catalogue_result * res = NULL;
512 const hdrl_catalogue_parameter * param = (hdrl_catalogue_parameter *)param_;
513 cpl_image * image = (cpl_image *)image_;
514 if (cpl_image_get_type(image) != CPL_TYPE_DOUBLE) {
515 image = cpl_image_cast(image, CPL_TYPE_DOUBLE);
520 cpl_image * conf_ = (cpl_image*)confidence_map;
525 if (confidence_map) {
526 if (cpl_image_get_min(confidence_map) < 0) {
527 cpl_error_set_message(cpl_func, CPL_ERROR_INCOMPATIBLE_INPUT,
528 "confidence_map must only contain "
534 if (cpl_image_get_bpm_const(image) != NULL) {
535 if (confidence_map) {
536 conf_ = cpl_image_cast(confidence_map, CPL_TYPE_DOUBLE);
539 conf_ = cpl_image_new(cpl_image_get_size_x(image),
540 cpl_image_get_size_y(image), CPL_TYPE_DOUBLE);
541 cpl_image_add_scalar(conf_, 100);
543 cpl_image_reject_from_mask(conf_, cpl_image_get_bpm_const(image));
544 cpl_image_fill_rejected(conf_, 0);
545 cpl_image_accept_all(conf_);
547 else if (confidence_map &&
548 cpl_image_get_type(confidence_map) != CPL_TYPE_DOUBLE) {
549 conf_ = cpl_image_cast(confidence_map, CPL_TYPE_DOUBLE);
554 res = cpl_calloc(1,
sizeof(hdrl_catalogue_result));
555 intres = cpl_calloc(1,
sizeof(hdrl_casu_result));
557 param->obj_min_pixels, param->obj_threshold,
558 param->obj_deblending, param->obj_core_radius,
560 param->bkg_mesh_size, (
int)param->resulttype,
561 param->bkg_smooth_fwhm, param->det_eff_gain,
562 param->det_saturation, intres);
564 if (intres->catalogue) {
567 hdrl_cleanup_qclist(res->qclist, include_experimental_qc);
569 res->segmentation_map = intres->segmentation_map;
570 res->background = intres->background;
574 if (image_ != image) {
575 cpl_image_delete(image);
577 if (inconf && inconf->image == confidence_map) {
578 inconf->image = NULL;
597static cpl_error_code hdrl_cleanup_qclist(cpl_propertylist * qclist,
598 cpl_boolean include_experimental_qc){
600 cpl_propertylist * qclocal = cpl_propertylist_duplicate(qclist);
601 cpl_propertylist_empty(qclist);
603 if (cpl_propertylist_has(qclocal,
"APCOR1"))
604 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR1");
605 if (cpl_propertylist_has(qclocal,
"APCOR2"))
606 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR2");
607 if (cpl_propertylist_has(qclocal,
"APCOR3"))
608 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR3");
609 if (cpl_propertylist_has(qclocal,
"APCOR4"))
610 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR4");
611 if (cpl_propertylist_has(qclocal,
"APCOR5"))
612 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR5");
613 if (cpl_propertylist_has(qclocal,
"APCOR6"))
614 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR6");
615 if (cpl_propertylist_has(qclocal,
"APCOR7"))
616 cpl_propertylist_copy_property(qclist, qclocal,
"APCOR7");
617 if (cpl_propertylist_has(qclocal,
"APCORPK"))
618 cpl_propertylist_copy_property(qclist, qclocal,
"APCORPK");
619 if (cpl_propertylist_has(qclocal,
"SYMBOL1"))
620 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL1");
621 if (cpl_propertylist_has(qclocal,
"SYMBOL2"))
622 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL2");
623 if (cpl_propertylist_has(qclocal,
"SYMBOL3"))
624 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL3");
625 if (cpl_propertylist_has(qclocal,
"SYMBOL4"))
626 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL4");
627 if (cpl_propertylist_has(qclocal,
"SYMBOL5"))
628 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL5");
629 if (cpl_propertylist_has(qclocal,
"SYMBOL6"))
630 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL6");
631 if (cpl_propertylist_has(qclocal,
"SYMBOL7"))
632 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL7");
633 if (cpl_propertylist_has(qclocal,
"SYMBOL8"))
634 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL8");
635 if (cpl_propertylist_has(qclocal,
"SYMBOL9"))
636 cpl_propertylist_copy_property(qclist, qclocal,
"SYMBOL9");
639 if (include_experimental_qc) {
640 if (cpl_propertylist_has(qclocal,
"ESO DRS CLASSIFD"))
641 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS CLASSIFD");
642 if (cpl_propertylist_has(qclocal,
"ESO DRS CROWDED"))
643 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS CROWDED");
644 if (cpl_propertylist_has(qclocal,
"ESO DRS FILTFWHM"))
645 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS FILTFWHM");
646 if (cpl_propertylist_has(qclocal,
"ESO DRS MINPIX"))
647 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS MINPIX");
648 if (cpl_propertylist_has(qclocal,
"ESO DRS NXOUT"))
649 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS NXOUT");
650 if (cpl_propertylist_has(qclocal,
"ESO DRS NYOUT"))
651 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS NYOUT");
652 if (cpl_propertylist_has(qclocal,
"ESO DRS RCORE"))
653 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS RCORE");
654 if (cpl_propertylist_has(qclocal,
"ESO DRS SEEING"))
655 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS SEEING");
656 if (cpl_propertylist_has(qclocal,
"ESO DRS THRESHOL"))
657 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS THRESHOL");
658 if (cpl_propertylist_has(qclocal,
"ESO DRS XCOL"))
659 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS XCOL");
660 if (cpl_propertylist_has(qclocal,
"ESO DRS YCOL"))
661 cpl_propertylist_copy_property(qclist, qclocal,
"ESO DRS YCOL");
662 if (cpl_propertylist_has(qclocal,
"ESO QC APERTURE_CORR"))
663 cpl_propertylist_copy_property(qclist, qclocal,
"ESO QC APERTURE_CORR");
664 if (cpl_propertylist_has(qclocal,
"ESO QC ELLIPTICITY"))
665 cpl_propertylist_copy_property(qclist, qclocal,
"ESO QC ELLIPTICITY");
666 if (cpl_propertylist_has(qclocal,
"ESO QC IMAGE_SIZE"))
667 cpl_propertylist_copy_property(qclist, qclocal,
"ESO QC IMAGE_SIZE");
668 if (cpl_propertylist_has(qclocal,
"ESO QC MEAN_SKY"))
669 cpl_propertylist_copy_property(qclist, qclocal,
"ESO QC MEAN_SKY");
670 if (cpl_propertylist_has(qclocal,
"ESO QC NOISE_OBJ"))
671 cpl_propertylist_copy_property(qclist, qclocal,
"ESO QC NOISE_OBJ");
672 if (cpl_propertylist_has(qclocal,
"ESO QC POSANG"))
673 cpl_propertylist_copy_property(qclist, qclocal,
"ESO QC POSANG");
674 if (cpl_propertylist_has(qclocal,
"ESO QC SATURATION"))
675 cpl_propertylist_copy_property(qclist, qclocal,
"ESO QC SATURATION");
676 if (cpl_propertylist_has(qclocal,
"ESO QC SKY_NOISE"))
677 cpl_propertylist_copy_property(qclist, qclocal,
"ESO QC SKY_NOISE");
678 if (cpl_propertylist_has(qclocal,
"HISTORY"))
679 cpl_propertylist_copy_property(qclist, qclocal,
"HISTORY");
682 cpl_propertylist_delete(qclocal);
684 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
hdrl_catalogue_result * hdrl_catalogue_compute_experimental(const cpl_image *image_, const cpl_image *confidence_map, const cpl_wcs *wcs, hdrl_parameter *param_)
build object catalog (experimental QC mode)
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