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_position(ps.sciset, 0) ;
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 for(i = 0; i < nraw; i++) {
418 sciframe = cpl_frameset_get_position(ps.sciset, 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 freefits(ps.scifits);
441 outfile = cpl_sprintf(
"%s_%s.fits", INSTRUME,FRINGES_PROCATG);
445 stats = cpl_stats_new_from_image(ps.fringes, CPL_STATS_ALL);
446 qclist = cpl_propertylist_new();
447 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESMIN", cpl_stats_get_min(stats));
448 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESMAX", cpl_stats_get_max(stats));
449 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESMEAN", cpl_stats_get_mean(stats));
450 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESDEV", cpl_stats_get_stdev(stats));
451 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC FRINGESMED", cpl_stats_get_median(stats));
454 cpl_msg_info(cpl_func,
"Saving Fringes Flat");
456 if(
omega_save_image(ps.fringes,set,pars,NULL,qclist,CPL_BPP_IEEE_FLOAT,outfile,
457 RECIPE,product_frame,NULL,isfirst) == -1){
458 cpl_msg_error(cpl_func,
"Cannot save product %s",FRINGES_PROCATG);
461 omega_fringes_tidy(0);
466 omega_fringes_tidy(1);
470 omega_fringes_tidy(0);
488 cpl_image *bpm, cpl_parameterlist *pars,
int ext)
494 cpl_propertylist *xlist;
496 if((scifits == NULL) || (mbias == NULL) || (mflat == NULL) ||
501 p = cpl_parameterlist_find(pars,
"omega.omega_fringes_flat.OverscanMethod") ;
502 omega_fringes_config.oc = cpl_parameter_get_int(p);
506 if(oc != omega_fringes_config.oc) {
507 cpl_msg_warning (cpl_func,
"Overscan correction mode for Master Bias (oc = %d) differs from "
508 "the one used here (oc = %d)", oc, omega_fringes_config.oc);
513 cpl_msg_error(cpl_func,
"Unable to trim image");
526 cpl_msg_error(cpl_func,
"Error in flat correction");
537 static int omega_fringes_load_calib(
int ext)
542 ps.mbias = cpl_image_load(cpl_frame_get_filename(ps.mbiasfr), CPL_TYPE_FLOAT, 0, ext);
543 if(ps.mbias == NULL){
544 cpl_msg_error(cpl_func,
"Cannot load Master Bias. %s", cpl_error_get_message());
548 ps.mflat = cpl_image_load(cpl_frame_get_filename(ps.mflatfr),CPL_TYPE_FLOAT,0,ext);
549 if (ps.mflat == NULL){
550 cpl_msg_error(cpl_func,
"Cannot load Master Flat. %s",cpl_error_get_message());
556 if(ps.bpmfr != NULL){
557 ps.bpm = cpl_image_load(cpl_frame_get_filename(ps.bpmfr), CPL_TYPE_INT, 0, ext);
559 cpl_msg_warning(cpl_func,
"Cannot load BPM. %s", cpl_error_get_message());
567 static void omega_fringes_init(
void)
584 static void omega_fringes_tidy(
int level)
589 freeimage(ps.fringes);
593 freeframeset(ps.sciset);
594 freespace(ps.labels);
595 freefits(ps.scifits);
int omega_pfits_get_overscan(const cpl_frame *frame, int xn)
Get the DRS keyword of the overscan method used to reduce the image.
void omega_extensions(const cpl_frame *frame, int inexten, int *out1, int *out2)
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
int omega_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
int omega_biascor(cpl_image *image, const cpl_image *bias)
Subtract bias from an image.
int omega_flatcor(cpl_image *image, const cpl_image *flat, const cpl_image *nsky)
Divide image by flat field.
omega_fits * omega_fits_load(const cpl_frame *inframe, cpl_type type, int extnum)
cpl_image * omega_trim_oscan_correct(omega_fits *ofits, int oscan)
This method loads a raw image, trims it and (optionally) performs an overscan correction, using the statistics of the pre and overscan regions. This can be done with a single value for the whole image or on a per-row basis.
const char * omega_get_license(void)
Get the pipeline copyright and license.
cpl_frame * omega_fits_get_frame(omega_fits *p)
cpl_frame * omega_product_frame(const char *filename, const char *tag, cpl_frame_type type)
Setup a frame to save a product.
cpl_propertylist * omega_fits_get_ehu(omega_fits *p)
cpl_frameset * omega_frameset_subgroup(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
cpl_image * omega_fringes_flat_process(omega_fits *scifits, cpl_image *bias, cpl_image *flat, cpl_image *bpm, cpl_parameterlist *pars, int ext)
Reduce a science image.
int omega_save_image(const cpl_image *img, cpl_frameset *set, const cpl_parameterlist *pars, cpl_propertylist *alist, const cpl_propertylist *qclist, cpl_type_bpp bpp, const char *name, const char *recipe, cpl_frame *frame, const cpl_frame *inherit, int isfirst)
Save an image as a DFS compliant product.
int omega_shift_refpix(const cpl_frame *frame, int xn, cpl_propertylist *xlist)
Shift reference pixel of a trimmed image based on the trimming regions.
cpl_image * omega_fringes_create(cpl_imagelist *ilist, cpl_image *bpm, int n)
Create a fringe image from a list of science frames.