40 #include "hawki_dfs.h"
41 #include "hawki_load.h"
42 #include "hawki_save.h"
43 #include "hawki_pfits.h"
44 #include "hawki_image_stats.h"
45 #include "hawki_utils.h"
55 int cpl_plugin_get_info(cpl_pluginlist * list);
57 static int hawki_step_stats_create(cpl_plugin *) ;
58 static int hawki_step_stats_exec(cpl_plugin *) ;
59 static int hawki_step_stats_destroy(cpl_plugin *) ;
60 static int hawki_step_stats(cpl_parameterlist *, cpl_frameset *) ;
62 static int hawki_step_stats_frameset_stats
63 (cpl_table ** target_stats,
64 cpl_propertylist ** stats_stats,
65 cpl_frameset * target_frames);
67 static int hawki_step_stats_save
68 (cpl_table ** target_stats,
69 cpl_parameterlist * recipe_parlist,
70 cpl_frameset * recipe_frameset,
71 cpl_frameset * used_frameset,
72 cpl_propertylist ** stats_stats,
74 const char * protype);
80 static char hawki_step_stats_description[] =
81 "hawki_step_stats -- hawki statistics utility (mean, stdev, ...).\n"
82 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
83 "raw-jitter.fits "HAWKI_IMG_JITTER_RAW
" or\n"
84 "bkg.fits "HAWKI_CALPRO_BKGIMAGE
" or\n"
85 "raw-flat.fits "HAWKI_CAL_FLAT_RAW
" or\n"
86 "raw-dark.fits "HAWKI_CAL_DARK_RAW
" or\n"
87 "raw-zpoint.fits "HAWKI_CAL_ZPOINT_RAW
" \n"
88 "The recipe creates as an output:\n"
89 "hawki_step_stats.fits ("HAWKI_CALPRO_JITTER_STATS
"): Statistics of raw jitter images, or\n"
90 "hawki_step_stats.fits ("HAWKI_CALPRO_JITTER_BKG_STATS
"): Statistics of background images, or\n"
91 "hawki_step_stats.fits ("HAWKI_CALPRO_FLAT_STATS
"): Statistics of raw flats, or\n"
92 "hawki_step_stats.fits ("HAWKI_CALPRO_DARK_STATS
"): Statistics of raw darks, or\n"
93 "hawki_step_stats.fits ("HAWKI_CALPRO_ZPOINT_STATS
"): Statistics of raw standard star images.\n"
95 "esorex exits with an error code of 0 if the recipe completes successfully\n"
112 int cpl_plugin_get_info(cpl_pluginlist * list)
114 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
115 cpl_plugin * plugin = &recipe->interface ;
117 cpl_plugin_init(plugin,
119 HAWKI_BINARY_VERSION,
120 CPL_PLUGIN_TYPE_RECIPE,
122 "Standard statistics utility",
123 hawki_step_stats_description,
124 "Cesar Enrique Garcia Dabo",
127 hawki_step_stats_create,
128 hawki_step_stats_exec,
129 hawki_step_stats_destroy) ;
131 cpl_pluginlist_append(list, plugin) ;
146 static int hawki_step_stats_create(cpl_plugin * plugin)
148 cpl_recipe * recipe ;
152 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
153 recipe = (cpl_recipe *)plugin ;
157 recipe->parameters = cpl_parameterlist_new() ;
158 if (recipe->parameters == NULL)
175 static int hawki_step_stats_exec(cpl_plugin * plugin)
177 cpl_recipe * recipe ;
180 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
181 recipe = (cpl_recipe *)plugin ;
187 return hawki_step_stats(recipe->parameters, recipe->frames) ;
197 static int hawki_step_stats_destroy(cpl_plugin * plugin)
199 cpl_recipe * recipe ;
202 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
203 recipe = (cpl_recipe *)plugin ;
206 cpl_parameterlist_delete(recipe->parameters) ;
218 static int hawki_step_stats(
219 cpl_parameterlist * parlist,
220 cpl_frameset * framelist)
222 cpl_frameset * frames ;
223 cpl_table ** target_stats;
224 cpl_propertylist ** stats_stats;
232 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
237 cpl_msg_info(__func__,
"Identifying input frames");
239 snprintf(calpro, 1024, HAWKI_CALPRO_JITTER_STATS);
240 snprintf(protype, 1024, HAWKI_PROTYPE_JITTER_STATS);
244 snprintf(calpro, 1024, HAWKI_CALPRO_JITTER_BKG_STATS);
245 snprintf(protype, 1024, HAWKI_PROTYPE_JITTER_BKG_STATS);
250 snprintf(calpro, 1024, HAWKI_CALPRO_DARK_STATS);
251 snprintf(protype, 1024, HAWKI_PROTYPE_DARK_STATS);
256 snprintf(calpro, 1024, HAWKI_CALPRO_FLAT_STATS);
257 snprintf(protype, 1024, HAWKI_PROTYPE_FLAT_STATS);
262 snprintf(calpro, 1024, HAWKI_CALPRO_ZPOINT_STATS);
263 snprintf(protype, 1024, HAWKI_PROTYPE_ZPOINT_STATS);
267 cpl_msg_error(__func__,
"Tag of input frames not supported");
268 cpl_msg_error(__func__,
"Supported: %s %s %s %s %s",
269 HAWKI_IMG_JITTER_RAW, HAWKI_CALPRO_BKGIMAGE,
270 HAWKI_CAL_DARK_RAW, HAWKI_CAL_FLAT_RAW, HAWKI_CAL_ZPOINT_RAW);
275 target_stats = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_table *));
276 stats_stats = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist *));
277 for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
279 target_stats[idet] = cpl_table_new(cpl_frameset_get_size(frames));
280 stats_stats[idet] = cpl_propertylist_new();
285 hawki_step_stats_frameset_stats(target_stats, stats_stats, frames);
288 if(hawki_step_stats_save
289 (target_stats, parlist, framelist, frames, stats_stats, calpro, protype) !=0)
290 cpl_msg_warning(__func__,
"Some data could not be saved. "
291 "Check permisions or disk space\n");
294 cpl_frameset_delete(frames);
295 for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
297 cpl_table_delete(target_stats[idet]);
298 cpl_propertylist_delete(stats_stats[idet]);
300 cpl_free(target_stats);
301 cpl_free(stats_stats);
304 if (cpl_error_get_code())
306 cpl_msg_error(__func__,
307 "HAWK-I pipeline could not recover from previous errors");
324 static int hawki_step_stats_frameset_stats
325 (cpl_table ** target_stats,
326 cpl_propertylist ** stats_stats,
327 cpl_frameset * target_frames)
333 nframes = cpl_frameset_get_size(target_frames);
334 cpl_msg_info(__func__,
"Looping the target frames: %d frames", nframes);
335 cpl_msg_indent_more();
336 for( iframe = 0 ; iframe < nframes ; ++iframe)
339 cpl_frame * this_target_frame;
342 cpl_msg_info(__func__,
"Computing stats for frame: %d", iframe +1);
343 this_target_frame = cpl_frameset_get_frame(target_frames, iframe);
345 (target_stats, this_target_frame, iframe);
347 cpl_msg_indent_less();
353 hawki_image_stats_print(target_stats);
369 static int hawki_step_stats_save
370 (cpl_table ** target_stats,
371 cpl_parameterlist * recipe_parlist,
372 cpl_frameset * recipe_frameset,
373 cpl_frameset * used_frameset,
374 cpl_propertylist ** stats_stats,
376 const char * protype)
378 const cpl_frame * reference_frame;
379 cpl_propertylist * referencelist;
380 cpl_propertylist ** extlists;
383 const char * recipe_name =
"hawki_step_stats";
384 cpl_errorstate error_prevstate = cpl_errorstate_get();
388 reference_frame = cpl_frameset_get_first_const(used_frameset);
391 cpl_msg_info(__func__,
"Creating the keywords list") ;
392 referencelist = cpl_propertylist_load_regexp
393 (cpl_frame_get_filename(reference_frame), 0,HAWKI_HEADER_EXT_FORWARD,0);
395 cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist*));
396 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
400 (cpl_frame_get_filename(reference_frame), idet+1);
403 extlists[idet] = cpl_propertylist_load_regexp(
404 cpl_frame_get_filename(reference_frame), ext_nb,
405 HAWKI_HEADER_EXT_FORWARD, 0);
408 cpl_propertylist_append(extlists[idet],stats_stats[idet]);
415 (
const cpl_table **)target_stats,
419 (
const cpl_propertylist*)referencelist,
420 (
const cpl_propertylist**)extlists,
421 "hawki_step_stats.fits");
424 cpl_propertylist_delete(referencelist) ;
425 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
427 cpl_propertylist_delete(extlists[idet]) ;
431 if(!cpl_errorstate_is_equal(error_prevstate))
433 cpl_errorstate_set(CPL_ERROR_NONE);