39 #include "irplib_utils.h"
41 #include "isaac_utils.h"
42 #include "isaac_pfits.h"
43 #include "isaac_dfs.h"
44 #include "isaac_fit.h"
50 static int isaac_img_detlin_create(cpl_plugin *);
51 static int isaac_img_detlin_exec(cpl_plugin *);
52 static int isaac_img_detlin_destroy(cpl_plugin *);
53 static int isaac_img_detlin(cpl_parameterlist *, cpl_frameset *);
55 static cpl_imagelist * isaac_img_detlin_load(cpl_frameset *, cpl_frameset *,
57 static int isaac_img_detlin_save(cpl_imagelist *, cpl_parameterlist *,
69 double lamp_stability;
70 } isaac_img_detlin_config;
72 static char isaac_img_detlin_description[] =
73 "isaac_img_detlin -- ISAAC imaging detector linearity recipe.\n"
74 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
75 "raw-file.fits "ISAAC_IMG_DETLIN_LAMP_RAW
" or\n"
76 "raw-file.fits "ISAAC_IMG_DETLIN_DARK_RAW
"\n";
91 int cpl_plugin_get_info(cpl_pluginlist * list)
93 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe));
94 cpl_plugin * plugin = &recipe->interface;
96 cpl_plugin_init(plugin,
99 CPL_PLUGIN_TYPE_RECIPE,
101 "Detector linearity recipe",
102 isaac_img_detlin_description,
106 isaac_img_detlin_create,
107 isaac_img_detlin_exec,
108 isaac_img_detlin_destroy);
110 cpl_pluginlist_append(list, plugin);
125 static int isaac_img_detlin_create(cpl_plugin * plugin)
131 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
132 recipe = (cpl_recipe *)plugin;
133 else return CPL_ERROR_UNSPECIFIED;
136 recipe->parameters = cpl_parameterlist_new();
140 p = cpl_parameter_new_value(
"isaac.isaac_img_detlin.force", CPL_TYPE_BOOL,
141 "flag to force th computation",
"isaac.isaac_img_detlin",
143 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"force");
144 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
145 cpl_parameterlist_append(recipe->parameters, p);
158 static int isaac_img_detlin_exec(cpl_plugin * plugin)
163 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
164 recipe = (cpl_recipe *)plugin;
165 else return CPL_ERROR_UNSPECIFIED;
167 return isaac_img_detlin(recipe->parameters, recipe->frames);
177 static int isaac_img_detlin_destroy(cpl_plugin * plugin)
182 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
183 recipe = (cpl_recipe *)plugin;
184 else return CPL_ERROR_UNSPECIFIED;
186 cpl_parameterlist_delete(recipe->parameters);
198 static int isaac_img_detlin(
199 cpl_parameterlist * parlist,
200 cpl_frameset * framelist)
203 cpl_frameset * darkframes;
204 cpl_frameset * lampframes;
205 cpl_imagelist * iset;
207 cpl_imagelist * fitres;
214 par = cpl_parameterlist_find(parlist,
"isaac.isaac_img_detlin.force");
215 isaac_img_detlin_config.force_flag = cpl_parameter_get_bool(par);
219 cpl_msg_error(cpl_func,
"Cannot identify RAW and CALIB frames");
220 return CPL_ERROR_UNSPECIFIED;
225 ISAAC_IMG_DETLIN_LAMP_RAW)) == NULL) {
226 cpl_msg_error(cpl_func,
"Cannot find any lamp frame in input");
227 return CPL_ERROR_UNSPECIFIED;
230 ISAAC_IMG_DETLIN_DARK_RAW)) == NULL) {
231 cpl_msg_error(cpl_func,
"Cannot find any dark frame in input");
232 cpl_frameset_delete(lampframes);
233 return CPL_ERROR_UNSPECIFIED;
237 cpl_msg_info(cpl_func,
"Load the data");
238 cpl_msg_indent_more();
239 if ((iset = isaac_img_detlin_load(lampframes, darkframes, &ditval))==NULL) {
240 cpl_msg_error(cpl_func,
"Cannot load the data");
241 cpl_frameset_delete(lampframes);
242 cpl_frameset_delete(darkframes);
243 cpl_msg_indent_less();
244 return CPL_ERROR_UNSPECIFIED;
246 cpl_frameset_delete(lampframes);
247 cpl_frameset_delete(darkframes);
248 cpl_msg_indent_less();
251 cpl_msg_info(cpl_func,
"Compute the linearity coefficients");
252 cpl_msg_indent_more();
254 cpl_msg_error(cpl_func,
"Cannot compute the linearity coefficients");
255 cpl_imagelist_delete(iset);
257 cpl_msg_indent_less();
258 return CPL_ERROR_UNSPECIFIED;
260 cpl_msg_indent_less();
262 cpl_imagelist_delete(iset);
265 cpl_msg_info(cpl_func,
"Save the products");
266 cpl_msg_indent_more();
267 if (isaac_img_detlin_save(fitres, parlist, framelist)==-1) {
268 cpl_msg_error(cpl_func,
"Cannot save the products");
269 cpl_imagelist_delete(fitres);
270 cpl_msg_indent_less();
271 return CPL_ERROR_UNSPECIFIED;
273 cpl_msg_indent_less();
276 cpl_imagelist_delete(fitres);
289 static cpl_imagelist * isaac_img_detlin_load(
290 cpl_frameset * lamps,
291 cpl_frameset * darks,
295 cpl_vector * selection;
297 cpl_propertylist * propertylist;
298 double dit_lamp, dit_dark;
300 cpl_imagelist * lamps_data;
301 cpl_imagelist * darks_data;
302 double * stab_levels;
307 if ((nb_lamps = cpl_frameset_get_size(lamps)) < 3)
return NULL;
308 if (cpl_frameset_get_size(darks) != nb_lamps)
return NULL;
311 cpl_msg_info(cpl_func,
"Checking DIT consistency");
312 selection = cpl_vector_new(nb_lamps);
313 *ditvals = cpl_malloc(nb_lamps *
sizeof(
double));
315 for (i=0; i<nb_lamps; i++) {
317 if (cpl_error_get_code()) {
318 cpl_vector_delete(selection);
323 frame = cpl_frameset_get_position(lamps, i);
324 propertylist=cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
326 cpl_propertylist_delete(propertylist);
327 if (cpl_error_get_code()) {
328 cpl_msg_error(cpl_func,
"Cannot get DIT");
329 cpl_vector_delete(selection);
334 frame = cpl_frameset_get_position(darks, i);
335 propertylist=cpl_propertylist_load(cpl_frame_get_filename(frame), 0);
337 cpl_propertylist_delete(propertylist);
338 if (cpl_error_get_code()) {
339 cpl_msg_error(cpl_func,
"Cannot get DIT");
340 cpl_vector_delete(selection);
345 if (fabs(dit_dark-dit_lamp) > 1e-3) {
346 cpl_msg_error(cpl_func,
"DIT not consistent between LAMP and DARK");
347 cpl_vector_delete(selection);
351 (*ditvals)[i] = dit_lamp;
354 cpl_vector_set(selection, i, -1.0);
357 if (fabs(dit_lamp - (*ditvals)[0]) < 1e-5) {
358 cpl_vector_set(selection, i, -1.0);
361 cpl_vector_set(selection, i, 1.0);
368 cpl_msg_error(cpl_func,
"Not enough frames for stability check");
369 cpl_vector_delete(selection);
375 cpl_msg_info(cpl_func,
"Compute the differences lamp - dark");
376 lamps_data = cpl_imagelist_load_frameset(lamps, CPL_TYPE_FLOAT, 1, 0);
377 darks_data = cpl_imagelist_load_frameset(darks, CPL_TYPE_FLOAT, 1, 0);
378 if (cpl_imagelist_subtract(lamps_data,darks_data) != CPL_ERROR_NONE) {
379 cpl_msg_error(cpl_func,
"Cannot subtract the 2 image lists");
380 cpl_vector_delete(selection);
382 if (lamps_data) cpl_imagelist_delete(lamps_data);
383 if (darks_data) cpl_imagelist_delete(darks_data);
386 cpl_imagelist_delete(darks_data);
389 cpl_msg_info(cpl_func,
"Check the lamp stability");
390 stab_levels = cpl_malloc(dit_stab *
sizeof(
double));
392 for (i=0; i<nb_lamps; i++) {
393 if (cpl_vector_get(selection, i) < 0) {
395 cpl_image_get_mean(cpl_imagelist_get(lamps_data, i));
401 isaac_img_detlin_config.lamp_stability = 0.0;
402 for (i=1; i<dit_stab; i++) {
403 if ((fabs(stab_levels[i]-stab_levels[0]) / stab_levels[0]) >
404 isaac_img_detlin_config.lamp_stability)
405 isaac_img_detlin_config.lamp_stability =
406 fabs(stab_levels[i]-stab_levels[0]) / stab_levels[0];
408 cpl_free(stab_levels);
411 if (isaac_img_detlin_config.lamp_stability > 0.01) {
412 if (isaac_img_detlin_config.force_flag == 1) {
413 cpl_msg_warning(cpl_func,
414 "level difference #%d too high - proceed anyway",i+1);
416 cpl_msg_error(cpl_func,
"level difference #%d too high", i+1);
417 cpl_vector_delete(selection);
419 cpl_imagelist_delete(lamps_data);
425 if (cpl_imagelist_erase(lamps_data, selection) != CPL_ERROR_NONE) {
426 cpl_msg_error(cpl_func,
"cannot discard stability frames");
427 cpl_vector_delete(selection);
429 cpl_imagelist_delete(lamps_data);
432 dit_purged = cpl_malloc(cpl_imagelist_get_size(lamps_data)*
sizeof(
double));
434 for (i=0; i<nb_lamps; i++) {
435 if (cpl_vector_get(selection, i) > 0) {
436 dit_purged[j] = (*ditvals)[i];
441 *ditvals = dit_purged;
444 cpl_vector_delete(selection);
457 static int isaac_img_detlin_save(
458 cpl_imagelist * fitres,
459 cpl_parameterlist * parlist,
462 const cpl_frame * ref_frame;
463 cpl_propertylist * plist;
464 cpl_propertylist * qclist;
465 cpl_propertylist * paflist;
466 double qc_meda, qc_medb, qc_medc, qc_medq;
469 qc_meda = cpl_image_get_median(cpl_imagelist_get(fitres, 0));
470 qc_medb = cpl_image_get_median(cpl_imagelist_get(fitres, 1));
471 qc_medc = cpl_image_get_median(cpl_imagelist_get(fitres, 2));
472 qc_medq = cpl_image_get_median(cpl_imagelist_get(fitres, 3));
475 qclist = cpl_propertylist_new();
476 cpl_propertylist_append_double(qclist,
"ESO QC DETLIN MEDA", qc_meda);
477 cpl_propertylist_append_double(qclist,
"ESO QC DETLIN MEDB", qc_medb);
478 cpl_propertylist_append_double(qclist,
"ESO QC DETLIN MEDC", qc_medc);
479 cpl_propertylist_append_double(qclist,
"ESO QC DETLIN MEDQ", qc_medq);
480 cpl_propertylist_append_double(qclist,
"ESO QC DETLIN LAMP",
481 isaac_img_detlin_config.lamp_stability );
484 irplib_dfs_save_image(set,
487 cpl_imagelist_get(fitres, 0),
493 PACKAGE
"/" PACKAGE_VERSION,
494 "isaac_img_detlin_A.fits");
497 irplib_dfs_save_image(set,
500 cpl_imagelist_get(fitres, 1),
506 PACKAGE
"/" PACKAGE_VERSION,
507 "isaac_img_detlin_B.fits");
510 irplib_dfs_save_image(set,
513 cpl_imagelist_get(fitres, 2),
519 PACKAGE
"/" PACKAGE_VERSION,
520 "isaac_img_detlin_C.fits");
523 irplib_dfs_save_image(set,
526 cpl_imagelist_get(fitres, 3),
532 PACKAGE
"/" PACKAGE_VERSION,
533 "isaac_img_detlin_Q.fits");
536 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
539 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
541 cpl_msg_error(cpl_func,
"getting header from reference frame");
542 cpl_propertylist_delete(qclist);
543 return CPL_ERROR_UNSPECIFIED;
547 paflist = cpl_propertylist_new();
548 cpl_propertylist_copy_property_regexp(paflist, plist,
549 "^(ARCFILE|MJD-OBS|ESO TPL ID|DATE-OBS|ESO DET DIT|"
550 "ESO DET NDIT|ESO DET NCORRS|ESO DET MODE NAME)$", 0);
551 cpl_propertylist_delete(plist);
554 cpl_propertylist_copy_property_regexp(paflist, qclist,
".", 0);
555 cpl_propertylist_delete(qclist);
558 cpl_propertylist_update_string(paflist, CPL_DFS_PRO_CATG,
562 cpl_dfs_save_paf(
"ISAAC",
565 "isaac_img_detlin_QC.paf");
566 cpl_propertylist_delete(paflist);
int isaac_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
cpl_imagelist * isaac_imagelist_fit_polynomial(const cpl_imagelist *ilist, const double *dit, int deg)
Compute the non linearity of the detector.
double isaac_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
cpl_frameset * isaac_extract_frameset(const cpl_frameset *self, const char *tag)
Extract the frames with the given tag from a frameset.
const char * isaac_get_license(void)
Get the pipeline copyright and license.