47 #include "irplib_ksigma_clip.h"
48 #include "irplib_hist.h"
49 #include "irplib_utils.h"
53 #define pdist(x1,y1,x2,y2) (((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)))
55 #define cpl_drand() ((double)rand()/(double)RAND_MAX)
70 #define HIST_FACT 2.354820045
96 const char * ron_method;
97 const char * dsnu_method;
101 } detmon_dark_config;
113 detmon_rm_bpixs(cpl_image **,
122 static cpl_bivector *
123 irplib_bivector_gen_rect_poisson(
const int *r,
127 static cpl_error_code
128 detmon_dark_save(
const cpl_parameterlist *,
142 const cpl_frameset *);
146 static cpl_error_code
147 detmon_retrieve_dark_params(
const char *,
149 const cpl_parameterlist *);
166 detmon_fill_parlist(cpl_parameterlist * parlist,
167 const char *recipe_name,
168 const char *pipeline_name,
176 int pars_counter = 0;
178 group_name = cpl_sprintf(
"%s.%s", pipeline_name, recipe_name);
179 assert(group_name != NULL);
181 #define insert_par(PARNAME, PARDESC, PARVALUE, PARTYPE) \
183 char * par_name = cpl_sprintf("%s.%s", group_name, PARNAME); \
185 assert(par_name != NULL); \
186 p = cpl_parameter_new_value(par_name, PARTYPE, \
187 PARDESC, group_name, PARVALUE); \
188 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, PARNAME); \
189 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); \
190 cpl_parameterlist_append(parlist, p); \
191 cpl_free(par_name); \
197 while(pars_counter < npars) {
198 char *name = va_arg(ap,
char *);
199 char *desc = va_arg(ap,
char *);
200 char *type = va_arg(ap,
char *);
202 if(!strcmp(type,
"CPL_TYPE_INT")) {
203 int v1 = va_arg(ap,
int);
205 insert_par(name, desc, v1, CPL_TYPE_INT);
206 }
else if(!strcmp(type,
"CPL_TYPE_BOOL")) {
207 char *v2 = va_arg(ap,
char *);
209 if(!strcmp(v2,
"CPL_FALSE"))
210 insert_par(name, desc, CPL_FALSE, CPL_TYPE_BOOL);
211 if(!strcmp(v2,
"CPL_TRUE"))
212 insert_par(name, desc, CPL_TRUE, CPL_TYPE_BOOL);
213 }
else if(!strcmp(type,
"CPL_TYPE_STRING")) {
214 char *v2 = va_arg(ap,
char *);
216 insert_par(name, desc, v2, CPL_TYPE_STRING);
217 }
else if(!strcmp(type,
"CPL_TYPE_DOUBLE")) {
218 double v3 = va_arg(ap,
double);
219 insert_par(name, desc, v3, CPL_TYPE_DOUBLE);
227 cpl_free(group_name);
245 detmon_retrieve_par_int(
const char *parn,
246 const char *pipeline_name,
247 const char *recipe_name,
248 const cpl_parameterlist * parlist)
254 par_name = cpl_sprintf(
"%s.%s.%s", pipeline_name, recipe_name, parn);
255 assert(par_name != NULL);
256 par = cpl_parameterlist_find((cpl_parameterlist *) parlist, par_name);
257 value = cpl_parameter_get_int(par);
275 detmon_retrieve_par_double(
const char *parn,
276 const char *pipeline_name,
277 const char *recipe_name,
278 const cpl_parameterlist * parlist)
284 par_name = cpl_sprintf(
"%s.%s.%s", pipeline_name, recipe_name, parn);
285 assert(par_name != NULL);
286 par = cpl_parameterlist_find((cpl_parameterlist *) parlist, par_name);
287 value = cpl_parameter_get_double(par);
305 irplib_pfits_get_exptime(
const cpl_propertylist * plist)
309 exptime = cpl_propertylist_get_double(plist,
"EXPTIME");
325 detmon_fill_prolist(
const char * procatg,
326 const char * protype,
327 const char * protech,
328 cpl_boolean proscience)
330 cpl_propertylist * prolist = cpl_propertylist_new();
332 cpl_propertylist_append_string(prolist, CPL_DFS_PRO_CATG, procatg);
333 cpl_propertylist_append_bool(prolist, CPL_DFS_PRO_SCIENCE, proscience);
335 cpl_propertylist_append_string(prolist, CPL_DFS_PRO_TYPE, protype);
338 cpl_propertylist_append_string(prolist, CPL_DFS_PRO_TECH, protech);
359 detmon_rm_bpixs(cpl_image ** image,
360 const double kappa,
int nffts,
int nsamples)
364 float *data = cpl_image_get_data_float(*image);
366 for(i = 0; i < nffts; i++) {
367 for(j = 0; j < nsamples; j++) {
368 float neighbours = 0;
378 neighbours += *(data + (i - 1) * nsamples + j);
382 neighbours += *(data + (i + 1) * nsamples + j);
386 neighbours += *(data + i * nsamples + (j - 1));
389 if(j < nsamples - 1) {
390 neighbours += *(data + i * nsamples + (j + 1));
393 average = neighbours / nneighs;
395 if(*(data + i * nsamples + j) < average * (-1 * kappa) ||
396 *(data + i * nsamples + j) > average * (kappa)) {
398 *(data + i * nsamples + j) = average;
402 if(*(data + i * nsamples + j) > average * (-1 * kappa) ||
403 *(data + i * nsamples + j) < average * (kappa)) {
405 *(data + i * nsamples + j) = average;
413 return cpl_error_get_code();
428 static cpl_bivector *
429 irplib_bivector_gen_rect_poisson(
const int *r,
const int np,
const int homog)
435 double cand_x, cand_y;
438 int xmin, xmax, ymin, ymax;
441 const int homogc = 0 < homog && homog < np ? homog : np;
446 cpl_ensure(r, CPL_ERROR_NULL_INPUT, NULL);
447 cpl_ensure(np > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
449 list = cpl_bivector_new(np);
450 cpl_ensure(list, CPL_ERROR_NULL_INPUT, NULL);
451 px = cpl_bivector_get_x_data(list);
452 py = cpl_bivector_get_y_data(list);
460 CPL_MATH_SQRT1_2 * ((xmax - xmin) * (ymax - ymin) / (double) (homogc + 1));
466 while(gnp < homogc) {
468 cand_x = cpl_drand() * (xmax - xmin) + xmin;
469 cand_y = cpl_drand() * (ymax - ymin) + ymin;
473 for(i = 0; i < gnp; i++) {
474 if(pdist(cand_x, cand_y, px[i], py[i]) < min_dist) {
493 cand_x = cpl_drand() * (xmax - xmin) + xmin;
494 cand_y = cpl_drand() * (ymax - ymin) + ymin;
498 for(i = 0; i < homogc; i++) {
501 px[start_ndx + i], py[start_ndx + i]) < min_dist) {
520 cand_x = cpl_drand() * (xmax - xmin) + xmin;
521 cand_y = cpl_drand() * (ymax - ymin) + ymin;
525 for(i = 0; i < homogc; i++) {
528 px[start_ndx + i], py[start_ndx + i]) < min_dist) {
557 static cpl_error_code
558 detmon_retrieve_dark_params(
const char *pipeline_name,
559 const char *recipe_name,
560 const cpl_parameterlist * parlist)
566 par_name = cpl_sprintf(
"%s.%s.ron.method", pipeline_name, recipe_name);
567 assert(par_name != NULL);
568 par = cpl_parameterlist_find((cpl_parameterlist *) parlist, par_name);
569 detmon_dark_config.ron_method = cpl_parameter_get_string(par);
573 par_name = cpl_sprintf(
"%s.%s.dsnu.method", pipeline_name, recipe_name);
574 assert(par_name != NULL);
575 par = cpl_parameterlist_find((cpl_parameterlist *) parlist, par_name);
576 detmon_dark_config.dsnu_method = cpl_parameter_get_string(par);
580 par_name = cpl_sprintf(
"%s.%s.opt_nir", pipeline_name, recipe_name);
581 assert(par_name != NULL);
582 par = cpl_parameterlist_find((cpl_parameterlist *) parlist, par_name);
583 detmon_dark_config.opt_nir = cpl_parameter_get_bool(par);
587 detmon_dark_config.exts =
588 detmon_retrieve_par_int(
"exts", pipeline_name, recipe_name,
591 if(cpl_error_get_code()) {
592 cpl_msg_error(cpl_func,
"Failed to retrieve the input parameters");
593 cpl_ensure_code(0, CPL_ERROR_DATA_NOT_FOUND);
597 return CPL_ERROR_NONE;
624 static cpl_error_code
625 detmon_dark_save(
const cpl_parameterlist * parlist,
626 cpl_frameset * frameset,
627 const char *recipe_name,
628 const char *pipeline_name,
629 const char *procatg_master,
630 const char *procatg_tbl,
631 const char *procatg_dsnu,
633 cpl_imagelist ** masters,
634 cpl_table ** dsnu_table,
635 cpl_imagelist ** dsnu,
636 cpl_propertylist ** qclist,
639 const cpl_frameset * usedframes)
642 cpl_frame *ref_frame;
643 cpl_propertylist *plist;
646 cpl_propertylist *paflist;
647 cpl_error_code error;
654 nb_images = cpl_imagelist_get_size(masters[0]);
655 cpl_ensure_code(nb_images > 0, CPL_ERROR_DATA_NOT_FOUND);
658 for(i = 0; i < nb_images; i++) {
662 cpl_sprintf(
"%s_master_dit_%d.fits", recipe_name, i+1);
663 assert(name_o != NULL);
666 cpl_sprintf(
"%s_master_dit_%d_set%02d.fits",
667 recipe_name, i, which_set);
668 assert(name_o != NULL);
673 if(detmon_dark_config.exts >= 0) {
674 cpl_propertylist * pro_master = cpl_propertylist_new();
676 cpl_propertylist_append_string(pro_master,
677 CPL_DFS_PRO_CATG, procatg_master);
679 cpl_propertylist_append(pro_master, qclist[0]);
681 if(cpl_dfs_save_image
682 (frameset, NULL, parlist, usedframes, NULL,
683 cpl_imagelist_get(*masters, i), CPL_BPP_IEEE_FLOAT,
684 recipe_name, pro_master, NULL, package,
686 cpl_msg_error(cpl_func,
"Cannot save the product: %s",
689 cpl_ensure_code(0, CPL_ERROR_FILE_NOT_CREATED);
693 cpl_propertylist_delete(pro_master);
695 cpl_propertylist * pro_master = cpl_propertylist_new();
697 cpl_propertylist_append_string(pro_master,
698 CPL_DFS_PRO_CATG, procatg_master);
700 cpl_propertylist_append(pro_master, qclist[0]);
702 if(cpl_dfs_save_image(frameset, NULL, parlist, usedframes, NULL,
703 NULL, CPL_BPP_IEEE_FLOAT, recipe_name,
706 cpl_msg_error(cpl_func,
"Cannot save the product: %s",
709 cpl_ensure_code(0, CPL_ERROR_FILE_NOT_CREATED);
712 cpl_propertylist_delete(pro_master);
713 for(j = 0; j < detmon_dark_config.nb_extensions; j++) {
715 cpl_image_save(cpl_imagelist_get(masters[j], i),
716 name_o, CPL_BPP_IEEE_FLOAT, qclist[j],
718 cpl_ensure_code(!error, error);
724 if (detmon_dark_config.opt_nir == OPT) {
725 cpl_propertylist * pro_tbl = cpl_propertylist_new();
727 cpl_propertylist_append_string(pro_tbl,
728 CPL_DFS_PRO_CATG, procatg_tbl);
730 cpl_propertylist_append(pro_tbl, qclist[0]);
737 name_o = cpl_sprintf(
"%s_dsnu_table.fits", recipe_name);
738 assert(name_o != NULL);
741 cpl_sprintf(
"%s_dsnu_table_set%02d.fits", recipe_name,
743 assert(name_o != NULL);
746 if(cpl_dfs_save_table(frameset, NULL, parlist, usedframes, NULL,
747 dsnu_table[0], NULL, recipe_name, pro_tbl, NULL,
749 cpl_msg_error(cpl_func,
"Cannot save the product: %s", name_o);
751 cpl_ensure_code(0, CPL_ERROR_FILE_NOT_CREATED);
754 cpl_propertylist_delete(pro_tbl);
756 if(detmon_dark_config.exts < 0) {
758 for(i = 1; i < detmon_dark_config.nb_extensions; i++) {
760 cpl_table_save(dsnu_table[i], NULL, qclist[i], name_o,
762 cpl_ensure_code(!error, error);
773 for(i = 0; i < nb_images; i++) {
777 cpl_sprintf(
"%s_dsnu_map_dit_%d.fits", recipe_name, i+1);
778 assert(name_o != NULL);
781 cpl_sprintf(
"%s_dsnu_map_dit_%d_set%02d.fits",
782 recipe_name, i, which_set);
783 assert(name_o != NULL);
788 if(detmon_dark_config.exts >= 0) {
789 cpl_propertylist * pro_dsnu = cpl_propertylist_new();
791 cpl_propertylist_append_string(pro_dsnu,
792 CPL_DFS_PRO_CATG, procatg_dsnu);
794 cpl_propertylist_append(pro_dsnu, qclist[0]);
796 if(cpl_dfs_save_image
797 (frameset, NULL, parlist, usedframes, NULL,
798 cpl_imagelist_get(*dsnu, i), CPL_BPP_IEEE_FLOAT,
799 recipe_name, pro_dsnu, NULL, package,
801 cpl_msg_error(cpl_func,
"Cannot save the product: %s",
804 cpl_ensure_code(0, CPL_ERROR_FILE_NOT_CREATED);
808 cpl_propertylist_delete(pro_dsnu);
810 cpl_propertylist * pro_dsnu = cpl_propertylist_new();
812 cpl_propertylist_append_string(pro_dsnu,
813 CPL_DFS_PRO_CATG, procatg_dsnu);
815 cpl_propertylist_append(pro_dsnu, qclist[0]);
817 if(cpl_dfs_save_image(frameset, NULL, parlist, usedframes,
819 CPL_BPP_IEEE_FLOAT, recipe_name,
822 cpl_msg_error(cpl_func,
"Cannot save the product: %s",
825 cpl_ensure_code(0, CPL_ERROR_FILE_NOT_CREATED);
828 cpl_propertylist_delete(pro_dsnu);
829 for(j = 0; j < detmon_dark_config.nb_extensions; j++) {
831 cpl_image_save(cpl_imagelist_get(dsnu[j], i),
832 name_o, CPL_BPP_IEEE_FLOAT, qclist[j],
834 cpl_ensure_code(!error, error);
849 ref_frame = cpl_frameset_get_first(frameset);
850 if((plist = cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
852 cpl_msg_error(cpl_func,
"getting header from reference frame");
853 cpl_ensure_code(0, cpl_error_get_code());
857 paflist = cpl_propertylist_new();
858 cpl_propertylist_copy_property_regexp(paflist, plist,
859 "^(ARCFILE|MJD-OBS|ESO TPL ID|"
860 "DATE-OBS|ESO DET DIT|ESO DET NDIT|"
862 "ESO DET MODE NAME)$", 0);
864 for(i = 0; i < detmon_dark_config.nb_extensions; i++) {
865 cpl_propertylist * c_paflist = cpl_propertylist_duplicate(paflist);
866 error = cpl_propertylist_append(c_paflist, qclist[i]);
867 cpl_ensure_code(!error, error);
870 if(detmon_dark_config.exts >= 0) {
872 name_o = cpl_sprintf(
"%s.paf", recipe_name);
873 assert(name_o != NULL);
875 name_o = cpl_sprintf(
"%s_set%02d.paf", recipe_name, which_set);
876 assert(name_o != NULL);
880 name_o = cpl_sprintf(
"%s_ext%02d.paf", recipe_name, i+1);
881 assert(name_o != NULL);
883 name_o = cpl_sprintf(
"%s_set%02d_ext%02d.paf", recipe_name, which_set, i+1);
884 assert(name_o != NULL);
888 if(cpl_dfs_save_paf(pipeline_name, recipe_name, c_paflist, name_o)) {
889 cpl_msg_error(cpl_func,
"Cannot save the product: %s", name_o);
891 cpl_propertylist_delete(paflist);
892 cpl_propertylist_delete(plist);
894 cpl_ensure_code(0, CPL_ERROR_FILE_NOT_CREATED);
896 cpl_propertylist_delete(c_paflist);
900 cpl_propertylist_delete(plist);
901 cpl_propertylist_delete(paflist);
903 return cpl_error_get_code();
907 detmon_print_rec_status(
void) {
908 if(cpl_error_get_code() != CPL_ERROR_NONE) {
909 cpl_msg_error(cpl_func,
"%s",(
const char*) cpl_error_get_message());
910 cpl_msg_error(cpl_func,
"%s",(
const char*) cpl_error_get_where());