36 #include "naco_recipe.h"
42 #define RECIPE_STRING "naco_img_dark"
48 static cpl_error_code naco_img_dark_reduce(cpl_propertylist *,
49 const irplib_framelist *,
50 cpl_image **, cpl_mask **,
51 cpl_mask **, cpl_mask **);
53 static cpl_error_code naco_img_dark_qc(cpl_propertylist *,
55 const irplib_framelist *);
57 static cpl_error_code naco_img_dark_save(cpl_frameset *,
58 const cpl_parameterlist *,
59 const cpl_propertylist *,
60 const cpl_propertylist *,
61 const cpl_image *,
const cpl_mask *,
62 const cpl_mask *,
const cpl_mask *,
63 int,
const irplib_framelist *);
65 static char * naco_img_dark_make_tag(
const cpl_frame*,
66 const cpl_propertylist *,
int);
68 NACO_RECIPE_DEFINE(naco_img_dark,
76 "naco_img_dark -- NACO imaging dark recipe.\n"
77 "The files listed in the Set Of Frames (sof-file) "
79 "NACO-raw-file.fits " NACO_IMG_DARK_RAW
"\n");
96 } naco_img_dark_config;
117 static int naco_img_dark(cpl_frameset * framelist,
118 const cpl_parameterlist * parlist)
120 irplib_framelist* allframes = NULL;
121 irplib_framelist* rawframes = NULL;
122 const char ** taglist = NULL;
123 const char * rej_bord;
124 irplib_framelist* f_one = NULL;
125 cpl_imagelist * i_one = NULL;
126 cpl_image * avg = NULL;
127 cpl_mask * hot = NULL;
128 cpl_mask * cold = NULL;
129 cpl_mask * dev = NULL;
130 cpl_propertylist * qclist = cpl_propertylist_new();
131 cpl_propertylist * paflist = cpl_propertylist_new();
140 skip_if (sscanf(rej_bord,
"%d %d %d %d",
141 &naco_img_dark_config.rej_left,
142 &naco_img_dark_config.rej_right,
143 &naco_img_dark_config.rej_bottom,
144 &naco_img_dark_config.rej_top) != 4);
146 naco_img_dark_config.hot_thresh =
148 naco_img_dark_config.dev_thresh =
150 naco_img_dark_config.cold_thresh =
152 naco_img_dark_config.hsize =
154 naco_img_dark_config.nsamples =
163 skip_if(allframes == NULL);
166 skip_if(rawframes == NULL);
170 IRPLIB_PFITS_REGEXP_RECAL
"|"
171 NACO_PFITS_REGEXP_DARK
"|"
172 NACO_PFITS_REGEXP_DARK_PAF
176 skip_if(taglist == NULL);
178 cpl_msg_info(cpl_func,
"Identified %d setting(s) in %d frames",
182 for (i=0 ; i < nsets ; i++) {
190 cpl_msg_info(cpl_func,
"Reducing frame set %d of %d (size=%d) with "
191 "setting: %s", i+1, nsets,
194 skip_if (f_one == NULL);
198 cpl_msg_warning(cpl_func,
"Setting %d skipped (Need at least 2 "
205 skip_if(naco_img_dark_reduce(qclist, f_one, &avg, &hot, &cold, &dev));
207 skip_if(naco_img_dark_qc(qclist, paflist, f_one));
210 skip_if (naco_img_dark_save(framelist, parlist, qclist, paflist,
211 avg, hot, cold, dev, i+1, f_one));
215 cpl_image_delete(avg);
216 cpl_mask_delete(hot);
217 cpl_mask_delete(cold);
218 cpl_mask_delete(dev);
220 cpl_propertylist_empty(qclist);
221 cpl_propertylist_empty(paflist);
229 skip_if (nb_good == 0);
233 cpl_imagelist_delete(i_one);
235 cpl_image_delete(avg);
236 cpl_mask_delete(hot);
237 cpl_mask_delete(cold);
238 cpl_mask_delete(dev);
242 cpl_propertylist_delete(qclist);
243 cpl_propertylist_delete(paflist);
245 return cpl_error_get_code();
262 static cpl_error_code naco_img_dark_reduce(cpl_propertylist * qclist,
263 const irplib_framelist * f_one,
264 cpl_image ** pavg, cpl_mask ** phot,
265 cpl_mask ** pcold, cpl_mask ** pdev)
268 cpl_image * dark = NULL;
269 cpl_image * diff = NULL;
270 char * ron_key = NULL;
282 skip_if (f_one == NULL);
286 skip_if (nfiles < 2);
289 CPL_TYPE_INT, CPL_TRUE, 0.0));
292 CPL_TYPE_INT, CPL_TRUE, 0.0));
294 for (i=0 ; i < nfiles ; i++) {
296 const char * name = cpl_frame_get_filename(frame);
298 cpl_image_delete(diff);
300 irplib_check(dark = cpl_image_load(name, CPL_TYPE_FLOAT, 0, 0),
301 "Could not load FITS-image from %s", name);
304 const int nx = cpl_image_get_size_x(dark);
305 const int ny = cpl_image_get_size_y(dark);
307 zone[0] = naco_img_dark_config.rej_left+1;
308 zone[1] = nx - naco_img_dark_config.rej_right;
309 zone[2] = naco_img_dark_config.rej_bottom+1;
310 zone[3] = ny - naco_img_dark_config.rej_top;
312 *pavg = cpl_image_duplicate(dark);
313 skip_if(*pavg == NULL);
315 const cpl_propertylist * plist
318 const char ron_format[] =
"ESO QC RON%d";
323 irplib_ensure(ndit > 0, CPL_ERROR_ILLEGAL_INPUT,
324 NACO_PFITS_INT_NDIT
" must be positive, not %d",
327 skip_if(cpl_image_subtract(diff, dark));
330 irplib_check(cpl_flux_get_noise_window(diff, zone,
331 naco_img_dark_config.hsize,
332 naco_img_dark_config.nsamples,
334 "Cannot compute the RON for difference between images "
335 "%d and %d", i, i+1);
338 ron = rms * sqrt(ndit/2.0);
342 ron_key = cpl_sprintf(ron_format, i);
344 bug_if(ron_key == NULL);
346 skip_if(cpl_propertylist_append_double(qclist, ron_key, ron));
349 skip_if(cpl_image_add(*pavg, dark));
354 cpl_image_delete(dark);
357 mean = cpl_image_get_mean(diff);
361 lower = mean - rms * naco_img_dark_config.dev_thresh;
362 upper = mean + rms * naco_img_dark_config.dev_thresh;
363 cpl_mask_delete(*pdev);
364 irplib_check(*pdev = cpl_mask_threshold_image_create(diff, lower, upper),
365 "Cannot compute the deviant pixel map");
366 cpl_image_delete(diff);
369 skip_if (cpl_mask_not(*pdev));
370 ndevpix = cpl_mask_count(*pdev);
374 skip_if(cpl_image_divide_scalar(*pavg, (
double)nfiles));
377 dark_med = cpl_image_get_median_window(*pavg, zone[0], zone[2], zone[1],
380 irplib_check (cpl_flux_get_noise_window(*pavg, zone,
381 naco_img_dark_config.hsize,
382 naco_img_dark_config.nsamples,
384 "Cannot compute the RON of the master dark");
386 lower = dark_med - rms * naco_img_dark_config.cold_thresh;
387 upper = dark_med + rms * naco_img_dark_config.hot_thresh;
390 cpl_mask_delete(*pcold);
391 irplib_check(*pcold = cpl_mask_threshold_image_create(*pavg, -FLT_MAX,
393 "Cannot compute the cold pixel map");
394 coldpix_nb = cpl_mask_count(*pcold);
398 cpl_mask_delete(*phot);
399 irplib_check(*phot = cpl_mask_threshold_image_create(*pavg, upper, DBL_MAX),
400 "Cannot compute the hot pixel map");
401 hotpix_nb = cpl_mask_count(*phot);
406 skip_if(cpl_propertylist_append_double(qclist,
"ESO QC DARKMED", dark_med));
407 skip_if(cpl_propertylist_append_int(qclist,
"ESO QC NBCOLPIX", coldpix_nb));
408 skip_if(cpl_propertylist_append_int(qclist,
"ESO QC NBHOTPIX", hotpix_nb));
409 skip_if(cpl_propertylist_append_int(qclist,
"ESO QC NBDEVPIX", ndevpix));
413 cpl_image_delete(dark);
414 cpl_image_delete(diff);
417 return cpl_error_get_code();
430 static cpl_error_code naco_img_dark_qc(cpl_propertylist * qclist,
431 cpl_propertylist * paflist,
432 const irplib_framelist * rawframes)
435 const cpl_propertylist * reflist
437 const char pafcopy[] =
"^(" NACO_PFITS_REGEXP_DARK_PAF
")$";
442 bug_if (cpl_propertylist_copy_property_regexp(paflist, reflist, pafcopy,
444 bug_if (cpl_propertylist_append(paflist, qclist));
446 bug_if (cpl_propertylist_copy_property_regexp(qclist, reflist,
"^("
447 IRPLIB_PFITS_REGEXP_RECAL
454 return cpl_error_get_code();
473 static cpl_error_code naco_img_dark_save(cpl_frameset * set_tot,
474 const cpl_parameterlist * parlist,
475 const cpl_propertylist * qclist,
476 const cpl_propertylist * paflist,
477 const cpl_image * avg,
478 const cpl_mask * hot,
479 const cpl_mask * cold,
480 const cpl_mask * dev,
482 const irplib_framelist * f_one)
485 cpl_image * image = NULL;
486 char * filename = NULL;
492 filename = cpl_sprintf(RECIPE_STRING
"_set%02d_avg" CPL_DFS_FITS,
495 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
496 NACO_IMG_DARK_AVG, qclist, NULL, naco_pipe_id,
500 image = cpl_image_new_from_mask(hot);
504 filename = cpl_sprintf(RECIPE_STRING
"_set%02d_hotpix" CPL_DFS_FITS,
507 CPL_BPP_8_UNSIGNED, RECIPE_STRING, NACO_IMG_DARK_HOT,
508 qclist, NULL, naco_pipe_id, filename));
511 cpl_image_delete(image);
512 image = cpl_image_new_from_mask(cold);
516 filename = cpl_sprintf(RECIPE_STRING
"_set%02d_coldpix" CPL_DFS_FITS,
519 CPL_BPP_8_UNSIGNED, RECIPE_STRING,
520 NACO_IMG_DARK_COLD, qclist, NULL, naco_pipe_id,
524 cpl_image_delete(image);
525 image = cpl_image_new_from_mask(dev);
529 filename = cpl_sprintf(RECIPE_STRING
"_set%02d_devpix" CPL_DFS_FITS,
532 CPL_BPP_8_UNSIGNED, RECIPE_STRING, NACO_IMG_DARK_DEV,
533 qclist, NULL, naco_pipe_id, filename));
534 cpl_image_delete(image);
541 bug_if(cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
545 filename = cpl_sprintf(RECIPE_STRING
"_set%02d" CPL_DFS_PAF, set_nb);
546 skip_if (cpl_dfs_save_paf(
"NACO", RECIPE_STRING, paflist, filename));
548 bug_if(paflist == NULL);
553 cpl_image_delete(image);
554 cpl_frameset_delete(set_one);
557 return cpl_error_get_code();
572 static char * naco_img_dark_make_tag(
const cpl_frame*
self,
573 const cpl_propertylist * plist,
int dummy)
583 skip_if (cpl_error_get_code());
585 skip_if(
self == NULL);
586 skip_if(plist == NULL);
591 skip_if(cpl_error_get_code());
595 skip_if(cpl_error_get_code());
600 skip_if(cpl_error_get_code());
604 skip_if(cpl_error_get_code());
606 tag = cpl_sprintf(
"%s:%s:%d:%.5f", name, mode, irom,
612 if (cpl_error_get_code()) {
cpl_frameset * irplib_frameset_cast(const irplib_framelist *self)
Create a CPL frameset from an irplib_framelist.
cpl_error_code irplib_framelist_set_tag_all(irplib_framelist *self, const char *tag)
Set the tag of all frames in the list.
int naco_pfits_get_rom(const cpl_propertylist *self)
find out the read out mode
int naco_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
cpl_error_code irplib_pfits_set_airmass(cpl_propertylist *self, const irplib_framelist *rawframes)
Update/Set the AIRMASS property.
int naco_pfits_get_ndit(const cpl_propertylist *self)
find out the NDIT keyword
int naco_parameterlist_get_int(const cpl_parameterlist *self, const char *recipe, naco_parameter bitmask)
Retrieve the value of a NACO integer parameter.
const char * naco_pfits_get_opti7_name(const cpl_propertylist *self)
find out the OPTI7.NAME key
const cpl_propertylist * irplib_framelist_get_propertylist_const(const irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
void irplib_framelist_empty(irplib_framelist *self)
Erase all frames from a framelist.
cpl_error_code irplib_dfs_save_image(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_image *image, cpl_type_bpp bpp, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an image as a DFS-compliant pipeline product.
cpl_error_code irplib_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
double naco_parameterlist_get_double(const cpl_parameterlist *self, const char *recipe, naco_parameter bitmask)
Retrieve the value of a NACO parameter of type double.
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
const cpl_frame * irplib_framelist_get_const(const irplib_framelist *self, int pos)
Get the specified frame from the framelist.
const char ** naco_framelist_set_tag(irplib_framelist *self, char *(*pftag)(const cpl_frame *, const cpl_propertylist *, int), int *pntags)
Retag a framelist according to the given tagging function.
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
cpl_error_code irplib_framelist_contains(const irplib_framelist *self, const char *key, cpl_type type, cpl_boolean is_equal, double fp_tol)
Verify that a property is present for all frames.
const char * naco_pfits_get_mode(const cpl_propertylist *self)
find out the mode name
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
const char * naco_parameterlist_get_string(const cpl_parameterlist *self, const char *recipe, naco_parameter bitmask)
Retrieve the value of a NACO string parameter.
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.
double naco_pfits_get_exptime(const cpl_propertylist *self)
find out the exposure time