35 #include "omega_recipe.h"
36 #include "omega_science.h"
38 #include "omega_background.h"
39 #include "omega_cosmic.h"
40 #include "omega_photometry.h"
41 #include "omega_satellites.h"
42 #include "omega_wcscor.h"
43 #include "omega_utils.h"
86 static int omega_standard_star_create(cpl_plugin *) ;
87 static int omega_standard_star_exec(cpl_plugin *) ;
88 static int omega_standard_star_destroy(cpl_plugin *) ;
89 static int omega_standard_star(cpl_frameset *,cpl_parameterlist *);
94 omega_fits *
omega_std_process(cpl_frameset *stdlist,cpl_parameterlist *pars,
int ext);
95 static int omega_std_retrieve_input_param(
const cpl_parameterlist *parlist);
96 static int omega_std_load_calib(
int ext);
97 static void omega_std_init(
void);
98 static void omega_std_tidy(
int level);
128 const cpl_frame *bpmfr;
129 const cpl_frame *extfr;
130 const cpl_frame *illumfr;
131 const cpl_frame *mbiasfr;
132 const cpl_frame *mflatfr;
133 const cpl_frame *monitfr;
134 const cpl_frame *reffr;
135 const cpl_frame *stdfr;
136 const cpl_frame *usnoa2;
137 const cpl_frame *colfr;
140 cpl_frameset *stdlist;
161 #define RECIPE "omega_standard_star"
175 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
176 cpl_plugin * plugin = &recipe->interface ;
178 cpl_plugin_init(plugin,
180 OMEGA_BINARY_VERSION,
181 CPL_PLUGIN_TYPE_RECIPE,
182 "omega_standard_star",
183 "OMEGA - This recipe is used to reduce standard star observations.",
184 "The recipe assumes that the data is for a single CCD. \n"
185 "The following operations are performed: \n\n"
186 " 1. Trim and overscan correct; \n"
187 " 2. Subtract the bias and divide by the flat field; \n"
188 " 3. Correct for fringing, illumination and the background, if requested; \n"
189 " 4. Extract the zero point from the data and calculate the extinction \n"
190 " value and save both in the header and in one table. \n\n"
191 " ------------------------------------------------------------------------ \n\n"
192 " Mandatory inputs : \n\n"
193 " : a raw standard star frame \n"
194 " : a master bias frame (Calfile 541) \n"
195 " : a master flat field (Calfile 546) \n\n"
196 " : a master USNOA2 table \n"
197 " : a standard stars catalog table \n\n"
198 " Optional inputs : \n\n"
199 " : fringe map (Calfile 545) \n"
200 " : bad pixels map (Calfile 522+535) \n"
201 " : illumination correction frame (Calfile 548) \n\n"
202 " The recipe will save the extinction and zero point values in one table.",
206 omega_standard_star_create,
207 omega_standard_star_exec,
208 omega_standard_star_destroy) ;
210 cpl_pluginlist_append(list, plugin) ;
225 static int omega_standard_star_create(cpl_plugin * plugin)
232 if (cpl_error_get_code() != CPL_ERROR_NONE) {
233 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
234 cpl_func, __LINE__, cpl_error_get_where());
235 return (
int)cpl_error_get_code();
238 if (plugin == NULL) {
239 cpl_msg_error(cpl_func,
"Null plugin");
240 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
244 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
245 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
246 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
250 recipe = (cpl_recipe *)plugin;
253 recipe->parameters = cpl_parameterlist_new() ;
254 if (recipe->parameters == NULL) {
255 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
256 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
263 p = cpl_parameter_new_value(
"omega.omega_standard_star.ExtensionNumber",
265 "FITS extension number to load (1 to 32). (-1 == all)",
266 "omega_standard_star",
269 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ext") ;
270 cpl_parameterlist_append(recipe->parameters, p) ;
272 p = cpl_parameter_new_range(
"omega.omega_standard_star.OverscanMethod",
274 "Overscan Correction Method",
277 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"oc-meth") ;
278 cpl_parameterlist_append(recipe->parameters, p) ;
280 p = cpl_parameter_new_value(
"omega.omega_standard_star.PAF",
282 "Boolean value to create PAF files. 1(TRUE), 0(FALSE)",
283 "omega_standard_star",
286 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"paf") ;
287 cpl_parameterlist_append(recipe->parameters, p) ;
291 p = cpl_parameter_new_value(
"omega.omega_standard_star.LowThreSatuPixel",
293 "Low threshold for calculating the saturated pixels map.",
294 "omega_standard_star",
297 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"lt-satu") ;
298 cpl_parameterlist_append(recipe->parameters, p) ;
300 p = cpl_parameter_new_value(
"omega.omega_standard_star.HighThreSatuPixel",
302 "High threshold for calculating the saturated pixels map.",
303 "omega_standard_star",
306 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"ht-satu") ;
307 cpl_parameterlist_append(recipe->parameters, p) ;
310 p = cpl_parameter_new_value(
"omega.omega_standard_star.DetectionThreSatellite",
312 "Minimum SNR for pixels to contribute to Hough map.",
316 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"det-sate") ;
317 cpl_parameterlist_append(recipe->parameters, p) ;
319 p = cpl_parameter_new_value(
"omega.omega_standard_star.HoughThreshold",
321 "Threshold for satellite tracks in Hough image.",
325 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"hough-thre") ;
326 cpl_parameterlist_append(recipe->parameters, p) ;
330 p = cpl_parameter_new_value(
"omega.omega_standard_star.SigmaFringeScaling",
332 "Sigma threshold in image data for scaling estimate (fringes only)",
333 "omega_standard_star",
336 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sig-fr-sc") ;
337 cpl_parameterlist_append(recipe->parameters, p) ;
339 p = cpl_parameter_new_value(
"omega.omega_standard_star.LowThreFringe",
341 "Lower bound of fringes to include in scaling (fringes only)",
342 "omega_standard_star",
345 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"lt-fr") ;
346 cpl_parameterlist_append(recipe->parameters, p) ;
348 p = cpl_parameter_new_value(
"omega.omega_standard_star.HighThreFringe",
350 "Higher bound of fringes to include in scaling (fringes only)",
351 "omega_standard_star",
354 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"ht-fr") ;
355 cpl_parameterlist_append(recipe->parameters, p) ;
359 p = cpl_parameter_new_value(
"omega.omega_standard_star.BackRestore",
361 "Boolean value to restore background after illumination correction. 1(TRUE), 0(FALSE)",
362 "omega_standard_star",
365 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"backg-rest") ;
366 cpl_parameterlist_append(recipe->parameters, p) ;
368 p = cpl_parameter_new_value(
"omega.omega_standard_star.BackThreshold",
370 "Detection threshold for background in image (Sextractor DETECT_THRESH)",
371 "omega_standard_star",
374 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"backthre") ;
375 cpl_parameterlist_append(recipe->parameters, p) ;
382 path = cpl_sprintf(
"%s", OMEGA_BIN_PATH);
383 p = cpl_parameter_new_value(
"omega.omega_standard_star.BinPath",
385 "Path to any external executable program.",
389 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"bin-path");
390 cpl_parameterlist_append(recipe->parameters, p);
394 path = cpl_sprintf(
"%s/omega.sex", OMEGA_CONFIG_PATH);
395 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexConfig",
397 "Path to Sextractor config file.",
401 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sex-config");
402 cpl_parameterlist_append(recipe->parameters, p);
406 path = cpl_sprintf(
"%s/omega.conv", OMEGA_CONFIG_PATH);
407 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexConv",
409 "Path to Sextractor convolution mask file.",
413 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sex-conv");
414 cpl_parameterlist_append(recipe->parameters, p);
418 path = cpl_sprintf(
"%s/omega.param", OMEGA_CONFIG_PATH);
419 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexParam",
421 "Path to Sextractor parameters file.",
425 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sex-param");
426 cpl_parameterlist_append(recipe->parameters, p);
429 path = cpl_sprintf(
"%s/omega.nnw", OMEGA_CONFIG_PATH);
430 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexNnw",
432 "Path to Sextractor neural network config file.",
436 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sex-nnw");
437 cpl_parameterlist_append(recipe->parameters, p);
440 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexDetThreshold",
442 "Detection threshold for Sextractor sources.",
446 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sex-dthre");
447 cpl_parameterlist_append(recipe->parameters, p);
451 path = cpl_sprintf(
"%s/omega.cosmic.sex", OMEGA_CONFIG_PATH);
452 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexCosmic",
454 "Path to Sextractor cosmic detection mode config file.",
458 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sex-cosmic");
459 cpl_parameterlist_append(recipe->parameters, p);
462 path = cpl_sprintf(
"%s/omega.cosmic.param", OMEGA_CONFIG_PATH);
463 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexCosmicParam",
465 "Path to Sextractor cosmic parameters file.",
469 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sex-cosmic-param");
470 cpl_parameterlist_append(recipe->parameters, p);
473 path = cpl_sprintf(
"%s/cosmic.ret", OMEGA_CONFIG_PATH);
474 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexCosmicFilt",
476 "Path to Sextractor filter mask for cosmic detection mode.",
480 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sex-cosfilt");
481 cpl_parameterlist_append(recipe->parameters, p);
484 p = cpl_parameter_new_value(
"omega.omega_standard_star.SexCosmicDet",
486 "Detection threshold for detecting cosmic rays in Sextractor",
490 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"sex-cosmic-det") ;
491 cpl_parameterlist_append(recipe->parameters, p) ;
495 p = cpl_parameter_new_value(
"omega.omega_standard_star.AstromDetThre",
497 "Sextractor threshold to detect stars for astrometric correction.",
501 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"astrom-thre") ;
502 cpl_parameterlist_append(recipe->parameters, p) ;
504 p = cpl_parameter_new_enum(
"omega.omega_standard_star.PlateSolution",
506 "Number of constant plate fits to do for the astrometry solution.",
510 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"platesol");
511 cpl_parameterlist_append(recipe->parameters, p);
513 p = cpl_parameter_new_value(
"omega.omega_standard_star.NumIterations",
515 "Number of iterations for the plate solution fitting.",
519 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"niter");
520 cpl_parameterlist_append(recipe->parameters, p);
522 p = cpl_parameter_new_range(
"omega.omega_standard_star.SextractorFlagLimit",
524 "Sextractor FLAG upper limit to preselect good sources for the "
525 "astrometric correction computation.",
"omega.Astrom",
527 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"SextractorFlagLimit") ;
528 cpl_parameterlist_append(recipe->parameters, p) ;
530 p = cpl_parameter_new_range(
"omega.omega_standard_star.SextractorClassStar",
532 "Sextractor CLASS_STAR lower limit to preselect starlike objects "
533 "for the astrometric correction computation.",
"omega.Astrom",
535 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"SextractorClassStar") ;
536 cpl_parameterlist_append(recipe->parameters, p) ;
538 p = cpl_parameter_new_value(
"omega.omega_standard_star.MaxWcsShift",
540 "Maximum allowed x and y pixel shift of the image when refining "
545 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"MaxWcsShift");
546 cpl_parameterlist_append(recipe->parameters, p);
551 p = cpl_parameter_new_value(
"omega.omega_standard_star.PhotomDetThre",
553 "Sextractor threshold to detect stars for photometric correction.",
557 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"photom-thre") ;
558 cpl_parameterlist_append(recipe->parameters, p) ;
560 p = cpl_parameter_new_value(
"omega.omega_standard_star.MatchPhotomRadius",
562 "Radius within which to match photometric stds and stars",
566 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"radius") ;
567 cpl_parameterlist_append(recipe->parameters, p) ;
569 p = cpl_parameter_new_value(
"omega.omega_standard_star.PhotomAperture",
571 "Aperture in pixels for photometric measurements in Sextractor",
575 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"aperture") ;
576 cpl_parameterlist_append(recipe->parameters, p) ;
579 p = cpl_parameter_new_value(
"omega.omega_standard_star.Nstars",
581 "Number of stars in image to use in "
586 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"nstars");
587 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
588 cpl_parameterlist_append(recipe->parameters, p);
591 p = cpl_parameter_new_value(
"omega.omega_standard_star.Npattern",
593 "Number of catalogue sources to use in "
598 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"npattern");
599 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
600 cpl_parameterlist_append(recipe->parameters, p);
603 p = cpl_parameter_new_value(
"omega.omega_standard_star.Tolerance",
605 "Max relative difference of angles and scales from their "
606 "median value for match acceptance (tolerance)",
610 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"tol");
611 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
612 cpl_parameterlist_append(recipe->parameters, p);
614 p = cpl_parameter_new_value(
"omega.omega_standard_star.SearchPatternRadius",
616 "Search radius for full PPM (pixels)",
619 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"pradius");
620 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
621 cpl_parameterlist_append(recipe->parameters, p);
623 p = cpl_parameter_new_value(
"omega.omega_standard_star.MaxPatternRadius",
625 "Maximum search radius for full PPM (pixels)",
628 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"maxpradius");
629 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
630 cpl_parameterlist_append(recipe->parameters, p);
643 static int omega_standard_star_exec(cpl_plugin * plugin)
649 if (cpl_error_get_code() != CPL_ERROR_NONE) {
650 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
651 cpl_func, __LINE__, cpl_error_get_where());
652 return (
int)cpl_error_get_code();
655 if (plugin == NULL) {
656 cpl_msg_error(cpl_func,
"Null plugin");
657 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
661 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
662 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
663 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
667 recipe = (cpl_recipe *)plugin;
670 if (recipe->parameters == NULL) {
671 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
672 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
674 if (recipe->frames == NULL) {
675 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
676 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
680 recipe_status = omega_standard_star(recipe->frames, recipe->parameters);
683 if (cpl_dfs_update_product_header(recipe->frames)) {
684 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
687 return recipe_status;
698 static int omega_standard_star_destroy(cpl_plugin * plugin)
702 if (plugin == NULL) {
703 cpl_msg_error(cpl_func,
"Null plugin");
704 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
708 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
709 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
710 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
714 recipe = (cpl_recipe *)plugin;
716 cpl_parameterlist_delete(recipe->parameters) ;
732 static int omega_standard_star(cpl_frameset *
set, cpl_parameterlist *pars)
734 int j,jst,jfn,isfirst;
738 double exptime = 0.0;
744 const char *stdname =
"omega_std.fits";
745 const char * maskname= NULL;
746 omega_fits * stdfits_mask=NULL;
747 cpl_image *image_simple_mask=NULL;
748 cpl_frame *product_frame = NULL;
749 cpl_frame *product_frame_mask=NULL ;
750 cpl_frame *pframe = NULL;
751 cpl_frame *zpframe = NULL;
752 cpl_image *image = NULL;
753 cpl_propertylist *plist,*xlist,*qclist,*alist;
754 cpl_errorstate prestate = cpl_errorstate_get();
759 cpl_msg_error (cpl_func,
"Parameters list not found");
763 if (cpl_frameset_is_empty(
set) == 1) {
764 cpl_msg_error (cpl_func,
"Frameset not found");
769 if(omega_std_retrieve_input_param(pars) != 0){
770 cpl_msg_error(cpl_func,
"Cannot retrieve input parameters. %s", cpl_error_get_message());
775 if (oc_dfs_set_groups(
set)) {
776 cpl_msg_error(cpl_func,
"Cannot identify RAW and CALIB frames") ;
786 cpl_msg_error(cpl_func,
"Cannot labelise the input frameset");
793 cpl_msg_error(cpl_func,
"Cannot find standard stars in input frameset");
797 nraw = cpl_frameset_count_tags (ps.stdlist, STD_RAW);
798 cpl_msg_info (cpl_func,
"There are %d %s frames in frame set",nraw, STD_RAW);
802 ps.mbiasfr = cpl_frameset_find_const(
set, OMEGA_CALIB_BIAS);
803 if (ps.mbiasfr == NULL) {
804 cpl_msg_error(cpl_func,
"Cannot find %s frame in frame set", OMEGA_CALIB_BIAS);
808 cpl_msg_info(cpl_func,
"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(ps.mbiasfr));
811 ps.mflatfr = cpl_frameset_find_const(
set, OMEGA_CALIB_FLAT);
812 if(ps.mflatfr == NULL) {
813 cpl_msg_error(cpl_func,
"Cannot find %s frame in frame set", OMEGA_CALIB_FLAT);
817 cpl_msg_info(cpl_func,
"Using %s %s", OMEGA_CALIB_FLAT, cpl_frame_get_filename(ps.mflatfr));
821 ps.reffr = cpl_frameset_find (
set, OMEGA_CALIB_REFSTAR);
822 if (ps.reffr == NULL) {
823 cpl_msg_error(cpl_func,
"Cannot find %s frame in frame set",OMEGA_CALIB_REFSTAR);
827 cpl_msg_info(cpl_func,
"Using %s %s", OMEGA_CALIB_REFSTAR, cpl_frame_get_filename(ps.reffr));
831 ps.monitfr = cpl_frameset_find(
set, OMEGA_CALIB_MONIT);
832 if(ps.monitfr == NULL) {
833 cpl_msg_error(cpl_func,
"Cannot find %s in frame set",OMEGA_CALIB_MONIT );
839 ps.extfr = cpl_frameset_find (
set,OMEGA_CALIB_EXTCUR);
840 if(ps.extfr == NULL) {
841 cpl_msg_error(cpl_func,
"Cannot find %s in frame set", OMEGA_CALIB_EXTCUR);
847 ps.usnoa2 = cpl_frameset_find (
set, OMEGA_USNOA2);
848 if (ps.usnoa2 == NULL) {
849 cpl_msg_error(cpl_func,
"Cannot find %s frame in frame set",OMEGA_USNOA2);
854 cpl_msg_info(cpl_func,
"Using %s %s", OMEGA_USNOA2, cpl_frame_get_filename(ps.usnoa2));
858 ps.bpmfr = cpl_frameset_find (
set,OMEGA_CALIB_BPM);
859 if(ps.bpmfr == NULL) {
860 cpl_msg_error(cpl_func,
"Cannot find %s in frame set", OMEGA_CALIB_BPM);
864 cpl_msg_info(cpl_func,
"Using %s %s",OMEGA_CALIB_BPM,cpl_frame_get_filename(ps.bpmfr));
867 ps.illumfr = cpl_frameset_find(
set, OMEGA_CALIB_ILLUM);
868 if(ps.illumfr != NULL)
869 cpl_msg_info(cpl_func,
"Using %s %s",OMEGA_CALIB_ILLUM,cpl_frame_get_filename(ps.illumfr));
872 ps.colfr = cpl_frameset_find(
set,OMEGA_CALIB_COLTERMS);
874 cpl_msg_info(cpl_func,
"Using %s %s", OMEGA_CALIB_COLTERMS, cpl_frame_get_filename(ps.colfr));
877 ps.stdfr = cpl_frameset_get_first_const(ps.stdlist);
881 if(omega_std_config.extnum == 0){
882 cpl_msg_error(cpl_func,
"Unsupported extension request, %d",omega_std_config.extnum);
888 for (j = jst; j <= jfn; j++) {
889 isfirst = (j == jst);
890 cpl_msg_indent_more();
891 cpl_msg_info(cpl_func,
".....Working on extension %d.....",j);
892 cpl_msg_indent_less();
894 omega_std_config.nmatches = 0;
895 omega_std_config.nstars = 0;
896 omega_std_config.mratio = 0.0;
897 omega_std_config.ZP = 0.0;
898 omega_std_config.ZPerr = 0.0;
899 omega_std_config.Extinction = 0.0;
900 omega_std_config.Exterr = 0.0;
901 omega_std_config.Seeing = 0.0;
902 omega_std_config.Ellipticity = 0.0;
905 if(ps.mbiasfr != NULL){
907 if(oscan1 != omega_std_config.oc) {
908 cpl_msg_warning (cpl_func,
"Overscan correction mode for Master Bias (oc = %d) differs from "
909 "the one used here (oc = %d)", oscan1, omega_std_config.oc);
914 if(omega_std_load_calib(j) != 0){
915 cpl_msg_error(cpl_func,
"Cannot load calibration frame(s)");
922 cpl_msg_error(cpl_func,
"Cannot reduce image extension");
932 ps.sname = cpl_sprintf(
"%s_%s.fits", INSTRUME,STD_PROCATG);
934 maskname=cpl_sprintf(
"%s_%s.fits", INSTRUME,
"REDUCED_STD_MASK");
935 product_frame_mask =
omega_product_frame(maskname,
"REDUCED_STD_MASK", CPL_FRAME_TYPE_IMAGE);
938 cpl_msg_info(cpl_func,
"Saving STD image");
939 if((
omega_save_fits(ps.stdfits,
set,pars,NULL,CPL_BPP_IEEE_FLOAT,ps.sname,RECIPE,
940 product_frame,NULL,isfirst)) == -1){
941 cpl_msg_error(cpl_func,
"Cannot save product");
951 image_simple_mask=cpl_image_new_from_mask(cpl_image_get_bpm(stdfits_mask->image));
952 cpl_image_delete(stdfits_mask->image); stdfits_mask->image=NULL;
953 stdfits_mask->image=image_simple_mask;
955 cpl_msg_info(cpl_func,
"Saving STD MASK image");
956 if((
omega_save_fits(stdfits_mask,
set,pars,NULL,CPL_BPP_8_UNSIGNED,maskname,RECIPE,
957 product_frame_mask,NULL,isfirst)) == -1){
958 cpl_msg_error(cpl_func,
"Cannot save product");
959 freefits(stdfits_mask);
964 freefits(stdfits_mask);
972 alist = cpl_propertylist_new();
973 cpl_propertylist_append_string(alist,
"EXTNAME",
974 cpl_propertylist_get_string(xlist,
"EXTNAME"));
975 cpl_propertylist_set_comment(alist,
"EXTNAME",
"Extension name");
978 if(cpl_propertylist_has(xlist,
"ESO QC STD PPM FAILURE")){
979 cpl_propertylist_append_int(alist,
"ESO QC STD PPM FAILURE",
980 cpl_propertylist_get_int(xlist,
"ESO QC STD PPM FAILURE"));
981 cpl_propertylist_set_comment(alist,
"ESO QC STD PPM FAILURE",
"PPM failure if ZERO");
987 cpl_propertylist_update_int(alist,
"ESO DRS OVERSCAN METHOD", omega_std_config.oc );
988 cpl_propertylist_set_comment(alist,
"ESO DRS OVERSCAN METHOD",
"overscan correction method");
997 cpl_image_divide_scalar(image, exptime);
999 cpl_propertylist_save(plist, stdname, CPL_IO_CREATE);
1000 cpl_image_save(image,stdname,CPL_BPP_IEEE_DOUBLE,xlist,CPL_IO_EXTEND);
1003 freefits(ps.stdfits);
1008 pars, &ps.extratbl)) == NULL){
1009 cpl_msg_warning(cpl_func,
"Cannot create photometric table");
1010 cpl_msg_warning(cpl_func,
"Creating an empty photometric table "
1011 "for this extension");
1012 ps.photbl = cpl_table_new(0);
1019 ps.pname = cpl_sprintf(
"%s_%s.fits", INSTRUME,PHOT_PROCATG);
1022 cpl_msg_info(cpl_func,
"Saving Photometric table");
1024 pframe,NULL,isfirst) == -1){
1025 cpl_msg_error(cpl_func,
"Cannot save photometric table");
1027 cpl_free(maskname); maskname=NULL;
1032 prestate = cpl_errorstate_get();
1034 if((ps.zptbl =
omega_zeropoints(ps.photbl, ps.extratbl, ps.monitfr, ps.extfr,
1035 ps.colfr)) == NULL ){
1036 cpl_msg_warning(cpl_func,
"Cannot create ZP table");
1037 cpl_msg_warning(cpl_func,
"Creating an empty ZP table for this "
1039 ps.zptbl = cpl_table_new(0);
1044 cpl_errorstate_set(prestate);
1046 freetable(ps.extratbl);
1047 freetable(ps.photbl);
1050 omega_std_config.ZP = cpl_table_get_double(ps.zptbl,
"ZEROPOINT", 0, NULL);
1051 omega_std_config.ZPerr = cpl_table_get_double(ps.zptbl,
"ZEROPOINT_ERR", 0, NULL);
1052 omega_std_config.Extinction = cpl_table_get_double(ps.zptbl,
"EXTINCTION", 0, NULL);
1053 omega_std_config.Exterr = cpl_table_get_double(ps.zptbl,
"EXTINCTION_ERR", 0, NULL);
1054 omega_std_config.nstars = cpl_table_get_int(ps.zptbl,
"NSTARS", 0, NULL);
1055 omega_std_config.Seeing = cpl_table_get_double(ps.zptbl,
"SEEING", 0, NULL);
1056 omega_std_config.Ellipticity = cpl_table_get_double(ps.zptbl,
"ELLIPTICITY", 0, NULL);
1058 qclist = cpl_propertylist_new();
1059 cpl_propertylist_update_double(qclist,
"ESO QC ZEROPOINT", omega_std_config.ZP) ;
1060 cpl_propertylist_set_comment(qclist,
"ESO QC ZEROPOINT",
"zp with flux in ADU/sec");
1061 cpl_propertylist_update_double(qclist,
"ESO QC ZEROPOINT ERR", omega_std_config.ZPerr) ;
1062 cpl_propertylist_set_comment(qclist,
"ESO QC ZEROPOINT ERR",
"zp error");
1064 cpl_propertylist_update_double(qclist,
"ESO QC ZEROPOINT_ELECTRON", omega_std_config.ZP+(2.5*log10(gain))) ;
1065 cpl_propertylist_set_comment(qclist,
"ESO QC ZEROPOINT_ELECTRON",
"zp with flux in e-/sec");
1066 cpl_propertylist_update_double(qclist,
"ESO QC ZEROPOINT_ELECTRON ERR", omega_std_config.ZPerr) ;
1067 cpl_propertylist_set_comment(qclist,
"ESO QC ZEROPOINT_ELECTRON ERR",
"zp error");
1069 cpl_propertylist_update_double(qclist,
"ESO QC EXTINCTION", omega_std_config.Extinction) ;
1070 cpl_propertylist_set_comment(qclist,
"ESO QC EXTINCTION",
"calculated extinction");
1071 cpl_propertylist_update_double(qclist,
"ESO QC EXTINCTION ERR", omega_std_config.Exterr) ;
1072 cpl_propertylist_set_comment(qclist,
"ESO QC EXTINCTION ERR",
"calculated extinction error");
1073 cpl_propertylist_update_int(qclist,
"ESO QC STD MATCHES", omega_std_config.nmatches);
1074 cpl_propertylist_set_comment(qclist,
"ESO QC STD MATCHES",
"number matches between USNOA2 cat and stars");
1075 cpl_propertylist_update_double(qclist,
"ESO QC STD RATIO MATCHES", omega_std_config.mratio);
1076 cpl_propertylist_set_comment(qclist,
"ESO QC STD RATIO MATCHES",
"ratio USNOA2 matches/detected stars");
1077 cpl_propertylist_update_double(qclist,
"ESO QC STD SEEING", omega_std_config.Seeing);
1078 cpl_propertylist_set_comment(qclist,
"ESO QC STD SEEING",
"calculated seeing of standard field");
1079 cpl_propertylist_update_double(qclist,
"ESO QC STD ELLIPTICITY", omega_std_config.Ellipticity);
1080 cpl_propertylist_set_comment(qclist,
"ESO QC STD ELLIPTICITY",
"calculated ellipticity of standard field");
1081 cpl_propertylist_update_int(qclist,
"ESO QC STD NUMBER STARS", omega_std_config.nstars);
1082 cpl_propertylist_set_comment(qclist,
"ESO QC STD NUMBER STARS",
"number of std stars used for ZP");
1086 ps.zname = cpl_sprintf(
"%s_%s.fits", INSTRUME,ZP_PROCATG);
1089 cpl_msg_info(cpl_func,
"Saving ZP table");
1091 zpframe,NULL,isfirst)) == -1){
1092 cpl_msg_error(cpl_func,
"Cannot save photometric table");
1095 cpl_free(maskname); maskname=NULL;
1100 freetable(ps.zptbl);
1106 cpl_free(maskname); maskname=NULL;
1129 const char *smoothed =
"omega_smoothed_std.fits";
1130 const char *smoothed_weight =
"omega_smoothed_std_weight.fits";
1131 double xshift=0.,yshift=0.;
1132 cpl_image *trim=NULL, *mask_img=NULL, *image=NULL, *combmap=NULL;
1133 cpl_image *back = NULL;
1134 cpl_image *reduced = NULL;
1135 cpl_image *sumwght = NULL;
1136 cpl_mask *satu_map=NULL,*bpm_map=NULL;
1137 cpl_mask *satellite= NULL;
1138 cpl_mask *cosmic=NULL;
1139 cpl_propertylist *plist=NULL, *astrolist=NULL, *wcslist=NULL;
1140 cpl_frame *frame=NULL;
1141 omega_fits *stdfits = NULL;
1142 omega_fits **sfits = NULL;
1145 if(cpl_image_get_mean(ps.mflat) < 0.1){
1146 cpl_msg_error(cpl_func,
"Master Flat image is NULL");
1150 if(cpl_image_get_min(ps.mflat) <= DBL_EPSILON){
1151 cpl_msg_warning(cpl_func,
"Some pixel values of the Master Flat are "
1152 "smaller or equal to %g.", DBL_EPSILON);
1153 cpl_msg_warning(cpl_func,
"Thresholding image to a minimum of %g ",
1155 cpl_image_threshold(ps.mflat, DBL_EPSILON, DBL_MAX,
1156 DBL_EPSILON, DBL_MAX);
1160 nraw = cpl_frameset_get_size(stdlist);
1163 for(i = 0; i < nraw; i++){
1166 cpl_msg_error(cpl_func,
"Unable to trim image %d",i);
1167 freelfits(sfits,nraw);
1176 cpl_msg_error(cpl_func,
"Error in flat correction");
1178 freelfits(sfits,nraw);
1184 bpm_map = cpl_mask_threshold_image_create(ps.bpm, 0.5, 1.5) ;
1185 if(satu_map != NULL){
1187 cpl_mask_or(bpm_map, satu_map);
1190 mask_img = cpl_image_new_from_mask(bpm_map);
1191 cpl_image_save(mask_img,
"bpmsatu.fits",CPL_BPP_16_SIGNED,NULL,CPL_IO_CREATE);
1193 cpl_image_reject_from_mask(trim, bpm_map);
1200 cpl_msg_debug(cpl_func,
"Unable to create background image");
1203 image = cpl_image_subtract_create(trim, back);
1207 if(omega_std_config.bckg == 1){
1209 trim = cpl_image_duplicate(image);
1216 satellite =
detsat(trim, pars);
1221 freemask(satellite);
1223 freeimage(mask_img);
1225 if((ps.illum != NULL) && (combmap != NULL))
1226 cpl_image_divide(combmap, ps.illum);
1229 cpl_image_multiply(trim, combmap);
1232 reduced = cpl_image_duplicate(trim);
1233 sumwght = cpl_image_duplicate(combmap);
1236 cpl_image_add(reduced, trim);
1237 cpl_image_add(sumwght, combmap);
1246 freelfits(sfits,nraw);
1249 if(cpl_image_divide(reduced, sumwght) != CPL_ERROR_NONE){
1250 cpl_msg_error(cpl_func,
"Cannot create weighted average of images. %s", cpl_error_get_message());
1257 frame = cpl_frameset_get_first(stdlist);
1258 plist = cpl_propertylist_load(cpl_frame_get_filename(frame),0);
1259 astrolist = cpl_propertylist_load(cpl_frame_get_filename(frame),ext);
1267 cpl_propertylist_save(plist, smoothed, CPL_IO_CREATE);
1268 cpl_image_save(image, smoothed, BITPIX, astrolist, CPL_IO_EXTEND);
1271 cpl_propertylist_save(plist, smoothed_weight, CPL_IO_CREATE);
1272 cpl_image_save(sumwght, smoothed_weight, BITPIX, astrolist, CPL_IO_EXTEND);
1279 &omega_std_config.nmatches, &omega_std_config.mratio);
1284 if(wcslist == NULL || fabs(xshift)> omega_std_config.MaxWcsShift ||
1285 fabs(yshift)>omega_std_config.MaxWcsShift){
1286 cpl_msg_warning(cpl_func,
"Unable to refine WCS for image");
1287 cpl_msg_warning(cpl_func,
"Taking original WCS coordinates");
1288 wcslist = cpl_propertylist_duplicate(astrolist);
1289 cpl_propertylist_update_int (wcslist,
"ESO QC STD PPM FAILURE", 0);
1290 cpl_propertylist_set_comment(wcslist,
"ESO QC STD PPM FAILURE",
"PPM failure if ZERO");
1295 cpl_propertylist_update_int (wcslist,
"ESO QC STD PPM FAILURE", 1);
1296 cpl_propertylist_set_comment(wcslist,
"ESO QC STD PPM FAILURE",
"PPM failure if ZERO");
1299 cpl_propertylist_update_double (astrolist,
"ESO QC DELTAX SHIFT", xshift);
1300 cpl_propertylist_set_comment(astrolist,
"ESO QC DELTAX SHIFT",
"WCS shift in x");
1301 cpl_propertylist_update_double (astrolist,
"ESO QC DELTAY SHIFT", yshift);
1302 cpl_propertylist_set_comment(astrolist,
"ESO QC DELTAY SHIFT",
"WCS shift in y");
1306 cpl_msg_debug(cpl_func,
"Cannot append WCS header to extension header");
1313 freeplist(astrolist);
1320 int omega_std_retrieve_input_param(
const cpl_parameterlist *pars)
1323 const cpl_parameter *par = NULL;
1324 cpl_errorstate prestate = cpl_errorstate_get();
1326 par = cpl_parameterlist_find_const(pars,
"omega.omega_standard_star.ExtensionNumber") ;
1327 omega_std_config.extnum = cpl_parameter_get_int(par) ;
1328 par = cpl_parameterlist_find_const(pars,
"omega.omega_standard_star.OverscanMethod") ;
1329 omega_std_config.oc = cpl_parameter_get_int(par) ;
1330 par = cpl_parameterlist_find_const(pars,
"omega.omega_standard_star.BackRestore");
1331 omega_std_config.bckg = cpl_parameter_get_bool(par) ;
1332 par = cpl_parameterlist_find_const(pars,
"omega.omega_standard_star.PAF");
1333 omega_std_config.paf = cpl_parameter_get_bool(par) ;
1334 par = cpl_parameterlist_find_const(pars,
"omega.omega_standard_star.MaxWcsShift") ;
1335 omega_std_config.MaxWcsShift = cpl_parameter_get_double(par) ;
1337 if(!cpl_errorstate_is_equal(prestate)){
1338 cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
1345 static int omega_std_load_calib(
int ext)
1350 ps.mbias = cpl_image_load(cpl_frame_get_filename(ps.mbiasfr), CPL_TYPE_FLOAT, 0, ext);
1351 if(ps.mbias == NULL){
1352 cpl_msg_error(cpl_func,
"Cannot load Master Bias. %s", cpl_error_get_message());
1356 ps.mflat = cpl_image_load(cpl_frame_get_filename(ps.mflatfr),CPL_TYPE_FLOAT,0,ext);
1357 if (ps.mflat == NULL){
1358 cpl_msg_error(cpl_func,
"Cannot load Master Flat. %s",cpl_error_get_message());
1363 ps.bpm = cpl_image_load(cpl_frame_get_filename(ps.bpmfr), CPL_TYPE_INT, 0, ext);
1365 cpl_msg_error(cpl_func,
"Cannot load BPM. %s", cpl_error_get_message());
1370 if(ps.illumfr != NULL)
1371 ps.illum = cpl_image_load(cpl_frame_get_filename(ps.illumfr),CPL_TYPE_FLOAT,0,ext);
1377 static void omega_std_init(
void) {
1401 static void omega_std_tidy(
int level) {
1402 freeimage(ps.mbias);
1404 freeimage(ps.illum);
1405 freeimage(ps.mflat);
1409 freeframeset(ps.stdlist);
1410 freefits(ps.stdfits);
1411 freetable(ps.extratbl);
1412 freetable(ps.zptbl);
1413 freetable(ps.photbl);
1414 freespace(ps.labels);
1415 freespace(ps.sname);
1416 freespace(ps.pname);
1417 freespace(ps.zname);