36 #include "omega_recipe.h"
37 #include "omega_wcscor.h"
66 static int omega_nightsky_flat_create(cpl_plugin *) ;
67 static int omega_nightsky_flat_exec(cpl_plugin *) ;
68 static int omega_nightsky_flat_destroy(cpl_plugin *) ;
69 static int omega_nightsky_flat(cpl_frameset *, cpl_parameterlist *) ;
71 cpl_image *bpm, cpl_parameterlist *pars,
int ext);
73 static int omega_nsky_load_calib(
int ext);
74 static void omega_nsky_init(
void);
75 static void omega_nsky_tidy(
int level);
90 const cpl_frame *mbiasfr;
91 const cpl_frame *mflatfr;
92 const cpl_frame *bpmfr;
104 #define RECIPE "omega_nightsky_flat"
118 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
119 cpl_plugin * plugin = &recipe->interface ;
121 cpl_plugin_init(plugin,
123 OMEGA_BINARY_VERSION,
124 CPL_PLUGIN_TYPE_RECIPE,
125 "omega_nightsky_flat",
126 "OMEGA - Create Master Night Sky Flat.",
127 "This recipe is used to create a Master Night Sky Flat or also called \n"
128 "a Blank Sky Flat. The recipe always takes as input a list of at least 3 \n"
129 "science frames, a master bias and a master flat. In addition to these, \n"
130 "an optional Bad Pixels Map may be provided.",
134 omega_nightsky_flat_create,
135 omega_nightsky_flat_exec,
136 omega_nightsky_flat_destroy) ;
138 cpl_pluginlist_append(list, plugin) ;
153 static int omega_nightsky_flat_create(cpl_plugin * plugin)
159 if (cpl_error_get_code() != CPL_ERROR_NONE) {
160 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
161 cpl_func, __LINE__, cpl_error_get_where());
162 return (
int)cpl_error_get_code();
165 if (plugin == NULL) {
166 cpl_msg_error(cpl_func,
"Null plugin");
167 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
171 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
172 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
173 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
177 recipe = (cpl_recipe *)plugin;
180 recipe->parameters = cpl_parameterlist_new() ;
181 if (recipe->parameters == NULL) {
182 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
183 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
187 p = cpl_parameter_new_value(
"omega.omega_nightsky_flat.ExtensionNumber",
189 "FITS extension number to load (1 to 32). (-1 == all)",
190 "omega_nightsky_flat",
193 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ext") ;
194 cpl_parameterlist_append(recipe->parameters, p) ;
196 p = cpl_parameter_new_range(
"omega.omega_nightsky_flat.OverscanMethod",
198 "Overscan Correction Method",
199 "omega_nightsky_flat",
201 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,
"oc-meth") ;
202 cpl_parameterlist_append(recipe->parameters, p) ;
215 static int omega_nightsky_flat_exec(cpl_plugin * plugin)
221 if (cpl_error_get_code() != CPL_ERROR_NONE) {
222 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
223 cpl_func, __LINE__, cpl_error_get_where());
224 return (
int)cpl_error_get_code();
227 if (plugin == NULL) {
228 cpl_msg_error(cpl_func,
"Null plugin");
229 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
233 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
234 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
235 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
239 recipe = (cpl_recipe *)plugin;
242 if (recipe->parameters == NULL) {
243 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
244 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
246 if (recipe->frames == NULL) {
247 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
248 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
252 recipe_status = omega_nightsky_flat(recipe->frames, recipe->parameters);
255 if (cpl_dfs_update_product_header(recipe->frames)) {
256 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
259 return recipe_status;
269 static int omega_nightsky_flat_destroy(cpl_plugin * plugin)
273 if (plugin == NULL) {
274 cpl_msg_error(cpl_func,
"Null plugin");
275 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
279 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
280 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
281 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
285 recipe = (cpl_recipe *)plugin;
287 cpl_parameterlist_delete(recipe->parameters) ;
300 static int omega_nightsky_flat(cpl_frameset *
set, cpl_parameterlist *pars)
303 int i,j,jst,jfn,isfirst;
306 char *outfile = NULL;
308 cpl_frame *sciframe, *product_frame;
311 cpl_imagelist *ilist;
312 cpl_propertylist *qclist;
318 cpl_msg_error (cpl_func,
"Parameters list not found");
322 if (cpl_frameset_is_empty(
set) == 1) {
323 cpl_msg_error (cpl_func,
"Frameset not found");
328 par = cpl_parameterlist_find(pars,
"omega.omega_nightsky_flat.ExtensionNumber") ;
329 omega_nsky_config.extnum = cpl_parameter_get_int(par) ;
332 if (oc_dfs_set_groups(
set)) {
333 cpl_msg_error(cpl_func,
"Cannot identify RAW and CALIB frames") ;
343 cpl_msg_error(cpl_func,
"Cannot labelise the input frameset");
348 NSKY_RAW)) == NULL) {
349 cpl_msg_error(cpl_func,
"Cannot find night sky frames in input frameset");
356 nraw = cpl_frameset_count_tags(ps.sciset, NSKY_RAW);
358 cpl_msg_error (cpl_func,
"Need at least 3 (%s) frames to run "
359 "this recipe", NSKY_RAW);
364 cpl_msg_info (cpl_func,
"There are %d %s frames in frame set",nraw, NSKY_RAW);
368 ps.mbiasfr = cpl_frameset_find_const(
set, OMEGA_CALIB_BIAS);
369 if (ps.mbiasfr == NULL) {
370 cpl_msg_error(cpl_func,
"Cannot find %s frame in frame set", OMEGA_CALIB_BIAS);
373 cpl_msg_info(cpl_func,
"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(ps.mbiasfr));
376 ps.mflatfr = cpl_frameset_find_const(
set, OMEGA_CALIB_FLAT);
377 if(ps.mflatfr == NULL) {
378 cpl_msg_error(cpl_func,
"Cannot find %s frame in frame set", OMEGA_CALIB_FLAT);
382 cpl_msg_info(cpl_func,
"Using %s %s", OMEGA_CALIB_FLAT, cpl_frame_get_filename(ps.mflatfr));
385 ps.bpmfr = cpl_frameset_find (
set,OMEGA_CALIB_BPM);
387 cpl_msg_info(cpl_func,
"Using %s %s",OMEGA_CALIB_BPM,cpl_frame_get_filename(ps.bpmfr));
390 sciframe = cpl_frameset_get_first(ps.sciset);
394 if(omega_nsky_config.extnum == 0){
395 cpl_msg_error(cpl_func,
"Unsupported extension request, %d",omega_nsky_config.extnum);
400 for (j = jst; j <= jfn; j++) {
401 isfirst = (j == jst);
402 cpl_msg_indent_more();
403 cpl_msg_info(cpl_func,
".....Working on extension %d.....",j);
404 cpl_msg_indent_less();
407 if(omega_nsky_load_calib(j) != 0){
408 cpl_msg_error(cpl_func,
"Cannot load calibration frame(s)");
414 ilist = cpl_imagelist_new();
416 sciframe = cpl_frameset_get_first(ps.sciset);
417 for(i = 0; i < nraw; i++) {
424 cpl_msg_error(cpl_func,
"Cannot reduce image %d of %d",i,nraw);
431 cpl_imagelist_set(ilist, image,i);
432 sciframe = cpl_frameset_get_next(ps.sciset);
433 freefits(ps.scifits);
441 outfile = cpl_sprintf(
"%s_%s.fits", INSTRUME,NSKY_PROCATG);
445 stats = cpl_stats_new_from_image(ps.nsky, CPL_STATS_ALL);
446 qclist = cpl_propertylist_new();
447 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC NSKYMIN", cpl_stats_get_min(stats));
448 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC NSKYMAX", cpl_stats_get_max(stats));
449 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC NSKYMEAN", cpl_stats_get_mean(stats));
450 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC NSKYDEV", cpl_stats_get_stdev(stats));
451 cpl_propertylist_append_double(qclist,
"HIERARCH ESO QC NSKYMED", cpl_stats_get_median(stats));
454 cpl_msg_info(cpl_func,
"Saving Night Sky Flat");
456 if(
omega_save_image(ps.nsky,
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",NSKY_PROCATG);
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_nightsky_flat.OverscanMethod") ;
502 omega_nsky_config.oc = cpl_parameter_get_int(p);
506 if(oc != omega_nsky_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_nsky_config.oc);
513 cpl_msg_error(cpl_func,
"Unable to trim image");
526 cpl_msg_error(cpl_func,
"Error in flat correction");
538 static int omega_nsky_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_nsky_init(
void)
585 static void omega_nsky_tidy(
int level)
594 freeframeset(ps.sciset);
595 freespace(ps.labels);
596 freefits(ps.scifits);