36 #include "omega_recipe.h"
37 #include "omega_wcscor.h"
67 static int omega_fringes_flat_create(cpl_plugin *) ;
68 static int omega_fringes_flat_exec(cpl_plugin *) ;
69 static int omega_fringes_flat_destroy(cpl_plugin *) ;
70 static int omega_fringes_flat(cpl_frameset *, cpl_parameterlist *) ;
72 cpl_image *bpm, cpl_parameterlist *pars,
int ext);
74 static int omega_fringes_load_calib(
int ext);
75 static void omega_fringes_init(
void);
76 static void omega_fringes_tidy(
int level);
85 }omega_fringes_config;
91 const cpl_frame *mbiasfr;
92 const cpl_frame *mflatfr;
93 const cpl_frame *bpmfr;
105 #define RECIPE "omega_fringes_flat"
119 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
120 cpl_plugin * plugin = &recipe->interface ;
122 cpl_plugin_init(plugin,
124 OMEGA_BINARY_VERSION,
125 CPL_PLUGIN_TYPE_RECIPE,
126 "omega_fringes_flat",
127 "OMEGA - Create Fringes Flat.",
128 "This recipe is used to create a Fringes Flat if the filter \n"
129 "requires it. The recipe always takes as input a list of at least 3 \n"
130 "science frames, a master bias and a master flat. In addition to these, \n"
131 "an optional Bad Pixels Map may be provided.",
135 omega_fringes_flat_create,
136 omega_fringes_flat_exec,
137 omega_fringes_flat_destroy) ;
139 cpl_pluginlist_append(list, plugin) ;
154 static int omega_fringes_flat_create(cpl_plugin * plugin)
160 if (cpl_error_get_code() != CPL_ERROR_NONE) {
161 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
162 cpl_func, __LINE__, cpl_error_get_where());
163 return (
int)cpl_error_get_code();
166 if (plugin == NULL) {
167 cpl_msg_error(cpl_func,
"Null plugin");
168 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
172 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
173 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
174 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
178 recipe = (cpl_recipe *)plugin;
181 recipe->parameters = cpl_parameterlist_new() ;
182 if (recipe->parameters == NULL) {
183 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
184 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
188 p = cpl_parameter_new_value(
"omega.omega_fringes_flat.ExtensionNumber",
190 "FITS extension number to load (1 to 32). (-1 == all)",
191 "omega_fringes_flat",
194 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ext") ;
195 cpl_parameterlist_append(recipe->parameters, p) ;
197 p = cpl_parameter_new_range(
"omega.omega_fringes_flat.OverscanMethod",
199 "Overscan Correction Method",
200 "omega_fringes_flat",
202 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"oc-meth") ;
203 cpl_parameterlist_append(recipe->parameters, p) ;
216 static int omega_fringes_flat_exec(cpl_plugin * plugin)
222 if (cpl_error_get_code() != CPL_ERROR_NONE) {
223 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
224 cpl_func, __LINE__, cpl_error_get_where());
225 return (
int)cpl_error_get_code();
228 if (plugin == NULL) {
229 cpl_msg_error(cpl_func,
"Null plugin");
230 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
234 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
235 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
236 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
240 recipe = (cpl_recipe *)plugin;
243 if (recipe->parameters == NULL) {
244 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
245 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
247 if (recipe->frames == NULL) {
248 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
249 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
253 recipe_status = omega_fringes_flat(recipe->frames, recipe->parameters);
256 if (cpl_dfs_update_product_header(recipe->frames)) {
257 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
260 return recipe_status;
270 static int omega_fringes_flat_destroy(cpl_plugin * plugin)
274 if (plugin == NULL) {
275 cpl_msg_error(cpl_func,
"Null plugin");
276 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
280 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
281 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
282 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
286 recipe = (cpl_recipe *)plugin;
288 cpl_parameterlist_delete(recipe->parameters) ;
301 static int omega_fringes_flat(cpl_frameset *
set, cpl_parameterlist *pars)
304 int i,j,jst,jfn,isfirst;
307 char *outfile = NULL;
309 cpl_frame *sciframe, *product_frame;
312 cpl_imagelist *ilist;
313 cpl_propertylist *qclist;
319 cpl_msg_error (cpl_func,
"Parameters list not found");
323 if (cpl_frameset_is_empty(
set) == 1) {
324 cpl_msg_error (cpl_func,
"Frameset not found");
329 par = cpl_parameterlist_find(pars,
"omega.omega_fringes_flat.ExtensionNumber") ;
330 omega_fringes_config.extnum = cpl_parameter_get_int(par) ;
333 if (oc_dfs_set_groups(
set)) {
334 cpl_msg_error(cpl_func,
"Cannot identify RAW and CALIB frames") ;
339 omega_fringes_init();
344 cpl_msg_error(cpl_func,
"Cannot labelise the input frameset");
345 omega_fringes_tidy(0);
349 FRINGES_RAW)) == NULL) {
350 cpl_msg_error(cpl_func,
"Cannot find fringes frames in input frameset");
351 omega_fringes_tidy(0);
357 nraw = cpl_frameset_count_tags(ps.sciset, FRINGES_RAW);
359 cpl_msg_error (cpl_func,
"Need at least 3 (%s) frames to run "
360 "this recipe", FRINGES_RAW);
361 omega_fringes_tidy(0);
365 cpl_msg_info (cpl_func,
"There are %d %s frames in frame set",nraw, FRINGES_RAW);
369 ps.mbiasfr = cpl_frameset_find_const(
set, OMEGA_CALIB_BIAS);
370 if (ps.mbiasfr == NULL) {
371 cpl_msg_error(cpl_func,
"Cannot find %s frame in frame set", OMEGA_CALIB_BIAS);
372 omega_fringes_tidy(0);
374 cpl_msg_info(cpl_func,
"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(ps.mbiasfr));
377 ps.mflatfr = cpl_frameset_find_const(
set, OMEGA_CALIB_FLAT);
378 if(ps.mflatfr == NULL) {
379 cpl_msg_error(cpl_func,
"Cannot find %s frame in frame set", OMEGA_CALIB_FLAT);
380 omega_fringes_tidy(0);
383 cpl_msg_info(cpl_func,
"Using %s %s", OMEGA_CALIB_FLAT, cpl_frame_get_filename(ps.mflatfr));
386 ps.bpmfr = cpl_frameset_find (
set,OMEGA_CALIB_BPM);
388 cpl_msg_info(cpl_func,
"Using %s %s",OMEGA_CALIB_BPM,cpl_frame_get_filename(ps.bpmfr));
391 sciframe = cpl_frameset_get_first(ps.sciset);
395 if(omega_fringes_config.extnum == 0){
396 cpl_msg_error(cpl_func,
"Unsupported extension request, %d",omega_fringes_config.extnum);
397 omega_fringes_tidy(0);
401 for (j = jst; j <= jfn; j++) {
402 isfirst = (j == jst);
403 cpl_msg_indent_more();
404 cpl_msg_info(cpl_func,
".....Working on extension %d.....",j);
405 cpl_msg_indent_less();
408 if(omega_fringes_load_calib(j) != 0){
409 cpl_msg_error(cpl_func,
"Cannot load calibration frame(s)");
411 omega_fringes_tidy(0);
415 ilist = cpl_imagelist_new();
417 sciframe = cpl_frameset_get_first(ps.sciset);
418 for(i = 0; i < nraw; i++) {
425 cpl_msg_error(cpl_func,
"Cannot reduce image %d of %d",i,nraw);
428 omega_fringes_tidy(0);
432 cpl_imagelist_set(ilist, image,i);
433 sciframe = cpl_frameset_get_next(ps.sciset);
434 freefits(ps.scifits);
442 outfile = cpl_sprintf(
"%s_%s.fits", INSTRUME,FRINGES_PROCATG);
446 stats = cpl_stats_new_from_image(ps.fringes, CPL_STATS_ALL);
447 qclist = cpl_propertylist_new();
448 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESMIN", cpl_stats_get_min(stats));
449 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESMAX", cpl_stats_get_max(stats));
450 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESMEAN", cpl_stats_get_mean(stats));
451 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESDEV", cpl_stats_get_stdev(stats));
452 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESMED", cpl_stats_get_median(stats));
455 cpl_msg_info(cpl_func,
"Saving Fringes Flat");
457 if(
omega_save_image(ps.fringes,
set,pars,NULL,qclist,CPL_BPP_IEEE_FLOAT,outfile,
458 RECIPE,product_frame,NULL,isfirst) == -1){
459 cpl_msg_error(cpl_func,
"Cannot save product %s",FRINGES_PROCATG);
462 omega_fringes_tidy(0);
467 omega_fringes_tidy(1);
471 omega_fringes_tidy(0);
489 cpl_image *bpm, cpl_parameterlist *pars,
int ext)
495 cpl_propertylist *xlist;
497 if((scifits == NULL) || (mbias == NULL) || (mflat == NULL) ||
502 p = cpl_parameterlist_find(pars,
"omega.omega_fringes_flat.OverscanMethod") ;
503 omega_fringes_config.oc = cpl_parameter_get_int(p);
507 if(oc != omega_fringes_config.oc) {
508 cpl_msg_warning (cpl_func,
"Overscan correction mode for Master Bias (oc = %d) differs from "
509 "the one used here (oc = %d)", oc, omega_fringes_config.oc);
514 cpl_msg_error(cpl_func,
"Unable to trim image");
527 cpl_msg_error(cpl_func,
"Error in flat correction");
538 static int omega_fringes_load_calib(
int ext)
543 ps.mbias = cpl_image_load(cpl_frame_get_filename(ps.mbiasfr), CPL_TYPE_FLOAT, 0, ext);
544 if(ps.mbias == NULL){
545 cpl_msg_error(cpl_func,
"Cannot load Master Bias. %s", cpl_error_get_message());
549 ps.mflat = cpl_image_load(cpl_frame_get_filename(ps.mflatfr),CPL_TYPE_FLOAT,0,ext);
550 if (ps.mflat == NULL){
551 cpl_msg_error(cpl_func,
"Cannot load Master Flat. %s",cpl_error_get_message());
557 if(ps.bpmfr != NULL){
558 ps.bpm = cpl_image_load(cpl_frame_get_filename(ps.bpmfr), CPL_TYPE_INT, 0, ext);
560 cpl_msg_warning(cpl_func,
"Cannot load BPM. %s", cpl_error_get_message());
568 static void omega_fringes_init(
void)
585 static void omega_fringes_tidy(
int level)
590 freeimage(ps.fringes);
594 freeframeset(ps.sciset);
595 freespace(ps.labels);
596 freefits(ps.scifits);