36 #include "omega_recipe.h"
66 static int omega_qcheck_create(cpl_plugin *) ;
67 static int omega_qcheck_exec(cpl_plugin *) ;
68 static int omega_qcheck_destroy(cpl_plugin *) ;
69 static int omega_qcheck_old(cpl_frameset *,cpl_parameterlist *) ;
70 static int omega_qcheck(cpl_frameset *,cpl_parameterlist *) ;
75 static void omega_qcheck_init(
void);
76 static void omega_qcheck_tidy(
void);
98 cpl_frameset *domelist;
99 omega_fits *domefits1;
100 cpl_propertylist *eh;
110 #define RECIPE "omega_qcheck"
123 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
124 cpl_plugin * plugin = &recipe->interface ;
126 cpl_plugin_init(plugin,
128 OMEGA_BINARY_VERSION,
129 CPL_PLUGIN_TYPE_RECIPE,
131 "OMEGA - Generates a quick check on the detector responsivity for each chip.",
132 "The recipe takes as input exactly one raw domeflat frame, and \n"
133 "a master bias. Optionally, an overscan correction mode can be set. \n"
134 "The default is to apply no overscan correction.",
140 omega_qcheck_destroy) ;
142 cpl_pluginlist_append(list, plugin) ;
157 static int omega_qcheck_create(cpl_plugin * plugin)
163 if (cpl_error_get_code() != CPL_ERROR_NONE) {
164 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
165 cpl_func, __LINE__, cpl_error_get_where());
166 return (
int)cpl_error_get_code();
169 if (plugin == NULL) {
170 cpl_msg_error(cpl_func,
"Null plugin");
171 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
175 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
176 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
177 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
181 recipe = (cpl_recipe *)plugin;
184 recipe->parameters = cpl_parameterlist_new() ;
187 p = cpl_parameter_new_value(
"omega.omega_qcheck.ExtensionNumber",
189 "FITS extension number to load (1 to 32). (-1 == all)",
193 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ext") ;
194 cpl_parameterlist_append(recipe->parameters, p) ;
197 p = cpl_parameter_new_range(
"omega.omega_qcheck.OverscanMethod",
199 "Overscan Correction Method (0 to 6):\n"
200 "0 = no overscan correction;\n"
201 "1 = use median of prescan in X;\n"
202 "2 = use median of overscan in X;\n"
203 "3 = use median on prescan in Y;\n"
204 "4 = use median of overscan in Y;\n"
205 "5 = per-row subtraction of the median of row in prescan regions of X;\n"
206 "6 = per-row subtraction of the median of row in overscan regions of X\n",
210 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"oc-meth") ;
211 cpl_parameterlist_append(recipe->parameters, p) ;
213 p = cpl_parameter_new_value(
"omega.omega_qcheck.PAF",
215 "Boolean value to create PAF files. 1(Yes), 0(No)",
219 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"paf") ;
220 cpl_parameterlist_append(recipe->parameters, p) ;
222 p = cpl_parameter_new_value(
"omega.omega_qcheck.rej_threshold",
224 "The rejection threshold for outlying pixels",
228 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"rej-thre") ;
229 cpl_parameterlist_append(recipe->parameters, p) ;
231 p = cpl_parameter_new_value(
"omega.omega_qcheck.NumberIter",
233 "The maximum number of iterations",
237 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"niter") ;
238 cpl_parameterlist_append(recipe->parameters, p) ;
252 static int omega_qcheck_exec(cpl_plugin * plugin)
258 if (cpl_error_get_code() != CPL_ERROR_NONE) {
259 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
260 cpl_func, __LINE__, cpl_error_get_where());
261 return (
int)cpl_error_get_code();
264 if (plugin == NULL) {
265 cpl_msg_error(cpl_func,
"Null plugin");
266 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
270 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
271 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
272 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
276 recipe = (cpl_recipe *)plugin;
279 if (recipe->parameters == NULL) {
280 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
281 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
283 if (recipe->frames == NULL) {
284 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
285 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
289 recipe_status = omega_qcheck(recipe->frames, recipe->parameters);
292 if (cpl_dfs_update_product_header(recipe->frames)) {
293 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
297 return recipe_status;
307 static int omega_qcheck_destroy(cpl_plugin * plugin)
309 cpl_recipe * recipe = (cpl_recipe *)plugin ;
311 if (plugin == NULL) {
312 cpl_msg_error(cpl_func,
"Null plugin");
313 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
317 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
318 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
319 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
323 recipe = (cpl_recipe *)plugin;
325 cpl_parameterlist_delete(recipe->parameters);
339 static int omega_qcheck_old(cpl_frameset *
set, cpl_parameterlist *pars)
342 int j,jst,jfn,live,oscan1,isfirst;
344 const char *_id =
"omega_qcheck";
345 char *outfile = NULL;
347 cpl_frame *mbias_frame;
348 const cpl_frame *frame1;
349 const cpl_frame *frame2;
350 cpl_frame *product_frame;
351 cpl_image *mbias_image;
352 cpl_image *trim_raw1;
353 cpl_image *trim_raw2;
355 cpl_propertylist *plist,*qclist;
361 cpl_msg_error (_id,
"Parameters list not found");
365 if (cpl_frameset_is_empty(
set) == 1) {
366 cpl_msg_error (_id,
"Frameset not found");
374 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.ExtensionNumber") ;
375 omega_qcheck_config.extnum = cpl_parameter_get_int(par) ;
377 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.OverscanMethod") ;
378 omega_qcheck_config.oc = cpl_parameter_get_int(par) ;
380 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.NumberIter") ;
381 omega_qcheck_config.niter = cpl_parameter_get_int(par) ;
383 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.rej_threshold") ;
384 omega_qcheck_config.rejt = cpl_parameter_get_double(par) ;
386 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.PAF") ;
387 omega_qcheck_config.paf = cpl_parameter_get_bool(par) ;
390 if (oc_dfs_set_groups(
set)) {
391 cpl_msg_error(_id,
"Cannot identify RAW and CALIB frames") ;
401 cpl_msg_error(_id,
"Cannot labelise the input frameset");
406 LIFETEST_RAW)) == NULL) {
407 cpl_msg_error(_id,
"Cannot find dome frames in input frameset");
411 if (cpl_frameset_get_size(ps.domelist) < 2) {
412 cpl_msg_error(_id,
"Need exactly 2 (%s) frames to run this recipe",LIFETEST_RAW);
418 frame1 = cpl_frameset_get_frame_const(ps.domelist,0);
419 frame2 = cpl_frameset_get_frame_const(ps.domelist,1);
420 cpl_msg_info (_id,
"Using %s frames: %s and %s",LIFETEST_RAW,cpl_frame_get_filename(frame1),
421 cpl_frame_get_filename(frame2));
423 mbias_frame = cpl_frameset_find(
set, OMEGA_CALIB_BIAS);
424 if (mbias_frame == NULL) {
425 cpl_msg_error (_id,
"No Master Bias is present in frame set");
430 cpl_msg_info(_id,
"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(mbias_frame));
434 if(omega_qcheck_config.extnum == 0){
435 cpl_msg_error(cpl_func,
"Unsupported extension request, %d",omega_qcheck_config.extnum);
442 omega_qcheck_config.extnum == 0 && jfn == 32)
445 for (j = jst; j <= jfn; j++) {
446 cpl_msg_info(_id,
"Beginning work on extension %d",j);
447 isfirst = (j == jst);
448 omega_qcheck_config.mean = 0.0;
449 omega_qcheck_config.median = 0.0;
450 omega_qcheck_config.stdev = 0.0;
454 plist = cpl_propertylist_load(cpl_frame_get_filename(frame1),j);
457 cpl_msg_warning(_id,
"First dome image detector not live");
464 plist = cpl_propertylist_load(cpl_frame_get_filename(frame2),j);
467 cpl_msg_warning(_id,
"Second dome image detector not live");
476 if(oscan1 != omega_qcheck_config.oc) {
477 cpl_msg_warning (_id,
"Overscan correction mode for Master Bias (oc = %d) differs from "
478 "the one used here (oc = %d)", oscan1, omega_qcheck_config.oc);
482 mbias_image = cpl_image_load(cpl_frame_get_filename(mbias_frame), CPL_TYPE_FLOAT, 0, j);
483 if (mbias_image == NULL) {
484 cpl_msg_error(_id,
"Cannot load MASTER BIAS");
489 cpl_msg_info (_id,
"Doing trim and overscan correction on images");
492 if(trim_raw1 == NULL){
493 cpl_msg_error(_id,
"Cannot trim first image");
494 freeimage(mbias_image);
499 cpl_image_subtract(trim_raw1, mbias_image);
502 if(trim_raw2 == NULL){
503 cpl_msg_error(_id,
"Cannot trim second image");
504 freeimage(mbias_image);
509 cpl_image_subtract(trim_raw2, mbias_image);
510 freeimage(mbias_image);
512 cpl_image_divide(trim_raw1, trim_raw2);
513 if(trim_raw1 == NULL){
514 cpl_msg_error(_id,
"Error in image division");
519 freeimage(trim_raw2);
522 ps.stats =
omega_iter_stat_opts(trim_raw1,NULL,omega_qcheck_config.rejt,omega_qcheck_config.niter);
523 if(ps.stats != NULL){
524 omega_qcheck_config.mean = cpl_stats_get_mean(ps.stats);
525 omega_qcheck_config.median = cpl_stats_get_median(ps.stats);
526 omega_qcheck_config.stdev = cpl_stats_get_stdev(ps.stats);
529 cpl_msg_warning(_id,
"Cannot calculate statistics iteratively");
533 freeimage(trim_raw1);
536 ps.result = cpl_table_new(1);
537 cpl_table_new_column(ps.result,
"MEAN", CPL_TYPE_DOUBLE);
538 cpl_table_new_column(ps.result,
"MEDIAN", CPL_TYPE_DOUBLE);
539 cpl_table_new_column(ps.result,
"STDEV", CPL_TYPE_DOUBLE);
540 cpl_table_set_double(ps.result,
"MEAN", 0, omega_qcheck_config.mean);
541 cpl_table_set_double(ps.result,
"MEDIAN", 0, omega_qcheck_config.median);
542 cpl_table_set_double(ps.result,
"STDEV", 0, omega_qcheck_config.stdev);
550 qclist = cpl_propertylist_new();
551 cpl_propertylist_append_double(qclist,
"ESO QC QUICK CHECK MEAN",
552 omega_qcheck_config.mean) ;
553 cpl_propertylist_set_comment(qclist,
"ESO QC QUICK CHECK MEAN",
"Mean of difference");
555 cpl_propertylist_append_double(qclist,
"ESO QC QUICK CHECK MEDIAN",
556 omega_qcheck_config.median) ;
557 cpl_propertylist_set_comment(qclist,
"ESO QC QUICK CHECK MEDIAN",
"Median of difference");
559 cpl_propertylist_append_double(qclist,
"ESO QC QUICK CHECK STDEV",
560 omega_qcheck_config.stdev) ;
561 cpl_propertylist_set_comment(qclist,
"ESO QC QUICK CHECK STDEV",
"Standard deviation of difference");
564 ps.proname = cpl_sprintf(
"%s_%s.fits", INSTRUME,LTEST_PROCATG);
568 if(
omega_save_table(ps.result,
set,pars,NULL,qclist,ps.proname,RECIPE,product_frame,
569 NULL,isfirst) == -1){
570 cpl_msg_error(cpl_func,
"Cannot save the product");
584 freetable(ps.result);
614 static int omega_qcheck(cpl_frameset *
set, cpl_parameterlist *pars)
617 int j,jst,jfn,oscan1,isfirst;
619 cpl_frame *mbias_frame;
620 const cpl_frame *frame1;
621 cpl_image *mbias_image;
622 cpl_image *trim_raw1;
623 cpl_frame *product_frame;
625 cpl_propertylist *qclist,*alist;
631 cpl_msg_error (cpl_func,
"Parameters list not found");
635 if (cpl_frameset_is_empty(
set) == 1) {
636 cpl_msg_error (cpl_func,
"Frameset not found");
644 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.ExtensionNumber") ;
645 omega_qcheck_config.extnum = cpl_parameter_get_int(par) ;
647 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.OverscanMethod") ;
648 omega_qcheck_config.oc = cpl_parameter_get_int(par) ;
650 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.NumberIter") ;
651 omega_qcheck_config.niter = cpl_parameter_get_int(par) ;
653 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.rej_threshold") ;
654 omega_qcheck_config.rejt = cpl_parameter_get_double(par) ;
656 par = cpl_parameterlist_find(pars,
"omega.omega_qcheck.PAF") ;
657 omega_qcheck_config.paf = cpl_parameter_get_bool(par) ;
660 if (oc_dfs_set_groups(
set)) {
661 cpl_msg_error(cpl_func,
"Cannot identify RAW and CALIB frames") ;
671 cpl_msg_error(cpl_func,
"Cannot labelise the input frameset");
676 LIFETEST_RAW)) == NULL) {
677 cpl_msg_error(cpl_func,
"Cannot find dome frame in input frameset");
681 if (cpl_frameset_get_size(ps.domelist) < 1) {
682 cpl_msg_error(cpl_func,
"Need exactly 1 (%s) frame to run this recipe",LIFETEST_RAW);
688 frame1 = cpl_frameset_get_frame_const(ps.domelist,0);
689 cpl_msg_info (cpl_func,
"Using %s frame: %s",LIFETEST_RAW,cpl_frame_get_filename(frame1));
691 mbias_frame = cpl_frameset_find(
set, OMEGA_CALIB_BIAS);
692 if (mbias_frame == NULL) {
693 cpl_msg_error (cpl_func,
"No Master Bias is present in frame set");
698 cpl_msg_info(cpl_func,
"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(mbias_frame));
702 if(omega_qcheck_config.extnum == 0){
703 cpl_msg_error(cpl_func,
"Unsupported extension request, %d",omega_qcheck_config.extnum);
708 for (j = jst; j <= jfn; j++) {
709 cpl_msg_info(cpl_func,
"Working on extension %d",j);
710 isfirst = (j == jst);
711 omega_qcheck_config.mean = 0.0;
712 omega_qcheck_config.median = 0.0;
713 omega_qcheck_config.stdev = 0.0;
731 if(oscan1 != omega_qcheck_config.oc) {
732 cpl_msg_warning (cpl_func,
"Overscan correction mode for Master Bias (oc = %d) differs from "
733 "the one used here (oc = %d)", oscan1, omega_qcheck_config.oc);
737 mbias_image = cpl_image_load(cpl_frame_get_filename(mbias_frame), CPL_TYPE_FLOAT, 0, j);
738 if (mbias_image == NULL) {
739 cpl_msg_error(cpl_func,
"Cannot load MASTER BIAS");
747 if(trim_raw1 == NULL){
748 cpl_msg_error(cpl_func,
"Cannot trim input image");
749 freeimage(mbias_image);
754 cpl_image_subtract(trim_raw1, mbias_image);
755 freeimage(mbias_image);
758 ps.stats =
omega_iter_stat_opts(trim_raw1,NULL,omega_qcheck_config.rejt,omega_qcheck_config.niter);
759 if(ps.stats != NULL){
760 omega_qcheck_config.mean = cpl_stats_get_mean(ps.stats);
761 omega_qcheck_config.median = cpl_stats_get_median(ps.stats);
762 omega_qcheck_config.stdev = cpl_stats_get_stdev(ps.stats);
765 cpl_msg_warning(cpl_func,
"Cannot calculate statistics iteratively");
769 freeimage(trim_raw1);
773 ps.result = cpl_table_new(1);
774 cpl_table_new_column(ps.result,
"MEAN", CPL_TYPE_DOUBLE);
775 cpl_table_new_column(ps.result,
"MEDIAN", CPL_TYPE_DOUBLE);
776 cpl_table_new_column(ps.result,
"STDEV", CPL_TYPE_DOUBLE);
777 cpl_table_set_double(ps.result,
"MEAN", 0, omega_qcheck_config.mean);
778 cpl_table_set_double(ps.result,
"MEDIAN", 0, omega_qcheck_config.median);
779 cpl_table_set_double(ps.result,
"STDEV", 0, omega_qcheck_config.stdev);
782 qclist = cpl_propertylist_new();
783 cpl_propertylist_append_double(qclist,
"ESO QC QUICK CHECK MEAN",
784 omega_qcheck_config.mean) ;
785 cpl_propertylist_set_comment(qclist,
"ESO QC QUICK CHECK MEAN",
"Mean of difference");
787 cpl_propertylist_append_double(qclist,
"ESO QC QUICK CHECK MEDIAN",
788 omega_qcheck_config.median) ;
789 cpl_propertylist_set_comment(qclist,
"ESO QC QUICK CHECK MEDIAN",
"Median of difference");
791 cpl_propertylist_append_double(qclist,
"ESO QC QUICK CHECK STDEV",
792 omega_qcheck_config.stdev) ;
793 cpl_propertylist_set_comment(qclist,
"ESO QC QUICK CHECK STDEV",
"Standard deviation of difference");
796 ps.proname = cpl_sprintf(
"%s_%s.fits", INSTRUME,LTEST_PROCATG);
801 alist=cpl_propertylist_load_regexp(cpl_frame_get_filename(frame1),j,
"EXTNAME",0);
802 if(
omega_save_table(ps.result,
set,pars,alist,qclist,ps.proname,RECIPE,product_frame,
803 NULL,isfirst) == -1){
804 cpl_msg_error(cpl_func,
"Cannot save the product");
805 cpl_propertylist_delete(alist);
810 cpl_propertylist_delete(alist);
819 freetable(ps.result);
821 freefits(ps.domefits1);
835 static void omega_qcheck_init(
void) {
846 static void omega_qcheck_tidy(
void) {
847 freespace(ps.labels);
848 freeframeset(ps.domelist);
849 freefits(ps.domefits1);
851 freespace(ps.proname);
852 freetable(ps.result);