36 #include "naco_recipe.h"
44 #define RECIPE_STRING "naco_spc_lampflat"
46 #ifndef NACO_SPC_MEDIAN_XSIZE
47 #define NACO_SPC_MEDIAN_XSIZE 200
50 #ifndef NACO_SPC_MEDIAN_YSIZE
51 #define NACO_SPC_MEDIAN_YSIZE NACO_SPC_MEDIAN_XSIZE
58 static cpl_image * naco_spc_lampflat_reduce(cpl_propertylist *,
59 const irplib_framelist *,
60 const cpl_parameterlist *);
62 static cpl_error_code naco_spc_lampflat_qc(cpl_propertylist *,
64 const irplib_framelist *);
66 static cpl_error_code naco_spc_lampflat_save(cpl_frameset *,
67 const cpl_parameterlist *,
68 const cpl_propertylist *,
69 const cpl_propertylist *,
71 int,
const irplib_framelist *);
73 NACO_RECIPE_DEFINE(naco_spc_lampflat,
74 NACO_PARAM_REJBORD | NACO_PARAM_HOT_LIM | NACO_PARAM_COLD_LIM,
75 "Spectrocopic flat recipe using a lamp",
76 RECIPE_STRING
" -- NACO spectrocopy flat-field creation from "
78 "The files listed in the Set Of Frames (sof-file) "
80 "NACO-raw-file.fits " NACO_SPC_LAMPFLAT_RAW
"\n"
82 NACO_SPC_MAN_MODESPLIT
"\n\n"
83 "Furthermore, the input set of frames must have values of "
85 NACO_PFITS_INT_LAMP2
" that of zero for off-frames and "
86 "non-zero for on-frames.\n");
107 static int naco_spc_lampflat(cpl_frameset * framelist,
108 const cpl_parameterlist * parlist)
110 cpl_errorstate cleanstate = cpl_errorstate_get();
111 irplib_framelist * allframes = NULL;
112 irplib_framelist * rawframes = NULL;
113 irplib_framelist * f_one = NULL;
114 const char ** taglist = NULL;
115 cpl_image * lamp_flat = NULL;
116 cpl_propertylist * qclist = cpl_propertylist_new();
117 cpl_propertylist * paflist = cpl_propertylist_new();
126 allframes = irplib_framelist_cast(framelist);
127 skip_if(allframes == NULL);
129 rawframes = irplib_framelist_extract(allframes, NACO_SPC_LAMPFLAT_RAW);
130 skip_if(rawframes == NULL);
131 irplib_framelist_empty(allframes);
133 skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
"^("
134 NACO_PFITS_REGEXP_SPCFLAT
"|"
135 NACO_PFITS_REGEXP_SPCFLAT_PAF
139 skip_if(taglist == NULL);
141 cpl_msg_info(cpl_func,
"Identified %d setting(s) in %d frames",
142 nsets, irplib_framelist_get_size(rawframes));
145 for (i=0 ; i < nsets ; i++) {
148 cpl_msg_info(cpl_func,
"Reducing data set %d of %d", i+1, nsets);
151 f_one = irplib_framelist_extract(rawframes, taglist[i]);
154 skip_if(irplib_framelist_set_tag_all(f_one, NACO_SPC_LAMPFLAT_RAW));
156 cpl_msg_info(cpl_func,
"Reducing frame set %d of %d (size=%d) with "
157 "setting: %s", i+1, nsets,
158 irplib_framelist_get_size(f_one), taglist[i]);
160 skip_if (f_one == NULL);
162 lamp_flat = naco_spc_lampflat_reduce(qclist, f_one, parlist);
165 if (lamp_flat == NULL) {
167 irplib_error_recover(cleanstate,
"Could not compute the flat for "
170 skip_if(naco_spc_lampflat_qc(qclist, paflist, f_one));
172 bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
173 NACO_CALIB_SPCFLAT));
174 skip_if(naco_spc_lampflat_save(framelist, parlist, qclist, paflist,
175 lamp_flat, i+1, f_one));
176 cpl_image_delete(lamp_flat);
180 cpl_propertylist_empty(qclist);
181 cpl_propertylist_empty(paflist);
182 irplib_framelist_delete(f_one);
186 irplib_ensure(nb_good > 0, CPL_ERROR_DATA_NOT_FOUND,
187 "None of the %d sets could be reduced", nsets);
192 cpl_image_delete(lamp_flat);
193 irplib_framelist_delete(f_one);
194 irplib_framelist_delete(allframes);
195 irplib_framelist_delete(rawframes);
196 cpl_propertylist_delete(qclist);
197 cpl_propertylist_delete(paflist);
199 return cpl_error_get_code();
211 static cpl_image * naco_spc_lampflat_reduce(cpl_propertylist * qclist,
212 const irplib_framelist * framelist,
213 const cpl_parameterlist * parlist)
215 cpl_image *
self = NULL;
216 cpl_imagelist * difflist = cpl_imagelist_new();
217 cpl_mask * bpm = NULL;
218 cpl_vector * medians = NULL;
219 const double hot_thresh
222 const double cold_thresh = 1.0 /
224 NACO_PARAM_COLD_LIM);
225 const char * rej_bord
228 cpl_propertylist * lampkeys = cpl_propertylist_new();
229 int rej_left, rej_right, rej_bottom, rej_top;
235 bug_if (qclist == NULL);
236 bug_if (framelist == NULL);
237 bug_if (parlist == NULL);
239 skip_if(cold_thresh <= 0.0);
240 skip_if(cold_thresh >= 1.0);
241 skip_if(hot_thresh <= 1.0);
243 skip_if_ne(sscanf(rej_bord,
"%d %d %d %d",
244 &rej_left, &rej_right, &rej_bottom, &rej_top), 4,
245 "number(s) in string parameter (%s): \"%s\"",
246 CPL_XSTRINGIFY(NACO_PARAM_REJBORD), rej_bord);
249 bug_if(cpl_propertylist_append_int(lampkeys, NACO_PFITS_INT_LAMP2, 1));
250 bug_if(cpl_propertylist_append_int(lampkeys, NACO_PFITS_BOOL_LAMP1, 0));
254 ndiff = cpl_imagelist_get_size(difflist);
256 medians = cpl_vector_new(ndiff);
260 for (idiff = 0, nflat = 0; idiff < ndiff; idiff++) {
261 cpl_image * diff = cpl_imagelist_get(difflist, nflat);
262 const int nx = cpl_image_get_size_x(diff);
263 const int ny = cpl_image_get_size_y(diff);
267 = cpl_image_get_mean_window(diff,
268 rej_left, nx - rej_right,
269 rej_bottom, ny - rej_top);
274 cpl_msg_warning(cpl_func,
"Ignoring difference image %d with an "
275 "invalid mean: %g", 1+idiff, mean);
276 cpl_image_delete(cpl_imagelist_unset(difflist, nflat));
281 median = cpl_image_get_median_window(diff,
282 (nx-NACO_SPC_MEDIAN_XSIZE)/2,
283 (ny-NACO_SPC_MEDIAN_YSIZE)/2,
284 (nx+NACO_SPC_MEDIAN_XSIZE)/2,
285 (ny+NACO_SPC_MEDIAN_YSIZE)/2);
289 bug_if(cpl_image_divide_scalar(diff, mean));
292 bpm = cpl_mask_threshold_image_create(diff, cold_thresh, hot_thresh);
295 if (cpl_mask_is_empty(bpm)) {
296 cpl_msg_warning(cpl_func,
"Ignoring difference image %d with no "
297 "good pixels", 1+idiff);
298 cpl_mask_delete(bpm);
300 cpl_image_delete(cpl_imagelist_unset(difflist, nflat));
304 bug_if(cpl_mask_not(bpm));
306 cpl_msg_info(cpl_func,
"Difference image %d has %d bad pixels", 1+idiff,
307 (
int)cpl_mask_count(bpm));
310 bug_if(cpl_mask_or(cpl_image_get_bpm(diff), bpm));
312 cpl_mask_delete(bpm);
315 bug_if (cpl_vector_set(medians, nflat, median));
320 bug_if(nflat != cpl_imagelist_get_size(difflist));
322 error_if(nflat == 0, CPL_ERROR_DATA_NOT_FOUND,
323 "The %d difference images are all invalid", ndiff);
325 self = cpl_imagelist_collapse_create(difflist);
328 nbad = cpl_image_count_rejected(
self);
332 cpl_msg_info(cpl_func,
"Setting %d bad pixels in master flat to 1.0",
334 bug_if(cpl_image_fill_rejected(
self, 1.0));
337 bug_if(cpl_vector_set_size(medians, nflat));
339 bug_if(cpl_propertylist_append_double(qclist,
"ESO QC SPECFLAT NCOUNTS",
340 cpl_vector_get_mean(medians)));
341 bug_if(cpl_propertylist_append_double(qclist,
"ESO QC SPECFLAT STDEV",
342 cpl_vector_get_stdev(medians)));
346 if (cpl_error_get_code()) {
347 cpl_image_delete(
self);
351 cpl_propertylist_delete(lampkeys);
352 cpl_imagelist_delete(difflist);
353 cpl_mask_delete(bpm);
354 cpl_vector_delete(medians);
369 static cpl_error_code naco_spc_lampflat_qc(cpl_propertylist * qclist,
370 cpl_propertylist * paflist,
371 const irplib_framelist * rawframes)
374 const cpl_propertylist * reflist
375 = irplib_framelist_get_propertylist_const(rawframes, 0);
376 const char pafcopy[] =
"^(" NACO_PFITS_REGEXP_SPCFLAT_PAF
")$";
383 skip_if (cpl_propertylist_copy_property_regexp(paflist, reflist, pafcopy,
385 skip_if (cpl_propertylist_append(paflist, qclist));
387 bug_if (cpl_propertylist_copy_property_regexp(qclist, reflist,
"^("
388 IRPLIB_PFITS_REGEXP_RECAL_LAMP
393 return cpl_error_get_code();
409 static cpl_error_code naco_spc_lampflat_save(cpl_frameset * set_tot,
410 const cpl_parameterlist * parlist,
411 const cpl_propertylist * qclist,
412 const cpl_propertylist * paflist,
413 const cpl_image * flat,
415 const irplib_framelist * rawframes)
417 cpl_frameset * proframes = irplib_frameset_cast(rawframes);
418 char * filename = NULL;
426 filename = cpl_sprintf(RECIPE_STRING
"_set%02d" CPL_DFS_FITS, set_nb);
427 skip_if (irplib_dfs_save_image(set_tot, parlist, proframes, flat,
428 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
429 NACO_CALIB_SPCFLAT, qclist, NULL,
430 naco_pipe_id, filename));
434 filename = cpl_sprintf(RECIPE_STRING
"_set%02d" CPL_DFS_PAF, set_nb);
435 skip_if (cpl_dfs_save_paf(
"NACO", RECIPE_STRING, paflist, filename));
437 bug_if(paflist == NULL);
443 cpl_frameset_delete(proframes);
445 return cpl_error_get_code();