HAWKI Pipeline Reference Manual  1.8.12
hawki_step_stats.c
1 /* $Id: hawki_step_stats.c,v 1.13 2012/11/30 14:51:22 cgarcia Exp $
2  *
3  * This file is part of the HAWKI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2012/11/30 14:51:22 $
24  * $Revision: 1.13 $
25  * $Name: hawki-1_8_12 $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <string.h>
37 #include <math.h>
38 #include <cpl.h>
39 
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"
46 
47 
48 /*-----------------------------------------------------------------------------
49  Functions prototypes
50  -----------------------------------------------------------------------------*/
51 
52 #ifdef __cplusplus
53 extern "C"
54 #endif
55 int cpl_plugin_get_info(cpl_pluginlist * list);
56 
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 *) ;
61 
62 static int hawki_step_stats_frameset_stats
63 (cpl_table ** target_stats,
64  cpl_propertylist ** stats_stats,
65  cpl_frameset * target_frames);
66 
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,
73  const char * calpro,
74  const char * protype);
75 
76 /*-----------------------------------------------------------------------------
77  Static variables
78  -----------------------------------------------------------------------------*/
79 
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"
94 "Return code:\n"
95 "esorex exits with an error code of 0 if the recipe completes successfully\n"
96 "or 1 otherwise";
97 
98 
99 /*-----------------------------------------------------------------------------
100  Functions code
101  -----------------------------------------------------------------------------*/
102 
103 /*----------------------------------------------------------------------------*/
111 /*----------------------------------------------------------------------------*/
112 int cpl_plugin_get_info(cpl_pluginlist * list)
113 {
114  cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
115  cpl_plugin * plugin = &recipe->interface ;
116 
117  cpl_plugin_init(plugin,
118  CPL_PLUGIN_API,
119  HAWKI_BINARY_VERSION,
120  CPL_PLUGIN_TYPE_RECIPE,
121  "hawki_step_stats",
122  "Standard statistics utility",
123  hawki_step_stats_description,
124  "Cesar Enrique Garcia Dabo",
125  PACKAGE_BUGREPORT,
127  hawki_step_stats_create,
128  hawki_step_stats_exec,
129  hawki_step_stats_destroy) ;
130 
131  cpl_pluginlist_append(list, plugin) ;
132 
133  return 0;
134 }
135 
136 /*----------------------------------------------------------------------------*/
145 /*----------------------------------------------------------------------------*/
146 static int hawki_step_stats_create(cpl_plugin * plugin)
147 {
148  cpl_recipe * recipe ;
149  /* cpl_parameter * p ; */
150 
151  /* Get the recipe out of the plugin */
152  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
153  recipe = (cpl_recipe *)plugin ;
154  else return -1 ;
155 
156  /* Create the parameters list in the cpl_recipe object */
157  recipe->parameters = cpl_parameterlist_new() ;
158  if (recipe->parameters == NULL)
159  return 1;
160 
161  /* Fill the parameters list */
162  /* None.. */
163 
164  /* Return */
165  return 0;
166 }
167 
168 /*----------------------------------------------------------------------------*/
174 /*----------------------------------------------------------------------------*/
175 static int hawki_step_stats_exec(cpl_plugin * plugin)
176 {
177  cpl_recipe * recipe ;
178 
179  /* Get the recipe out of the plugin */
180  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
181  recipe = (cpl_recipe *)plugin ;
182  else return -1 ;
183 
184  /* Issue a banner */
186 
187  return hawki_step_stats(recipe->parameters, recipe->frames) ;
188 }
189 
190 /*----------------------------------------------------------------------------*/
196 /*----------------------------------------------------------------------------*/
197 static int hawki_step_stats_destroy(cpl_plugin * plugin)
198 {
199  cpl_recipe * recipe ;
200 
201  /* Get the recipe out of the plugin */
202  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
203  recipe = (cpl_recipe *)plugin ;
204  else return -1 ;
205 
206  cpl_parameterlist_delete(recipe->parameters) ;
207  return 0 ;
208 }
209 
210 /*----------------------------------------------------------------------------*/
217 /*----------------------------------------------------------------------------*/
218 static int hawki_step_stats(
219  cpl_parameterlist * parlist,
220  cpl_frameset * framelist)
221 {
222  cpl_frameset * frames ;
223  cpl_table ** target_stats;
224  cpl_propertylist ** stats_stats;
225  int idet;
226  char calpro[1024];
227  char protype[1024];
228 
229  /* Identify the RAW and CALIB frames in the input frameset */
230  if (hawki_dfs_set_groups(framelist))
231  {
232  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
233  return -1;
234  }
235 
236  /* Retrieve raw frames */
237  cpl_msg_info(__func__, "Identifying input frames");
238  frames = hawki_extract_frameset(framelist, HAWKI_IMG_JITTER_RAW) ;
239  snprintf(calpro, 1024, HAWKI_CALPRO_JITTER_STATS);
240  snprintf(protype, 1024, HAWKI_PROTYPE_JITTER_STATS);
241  if (frames == NULL)
242  {
243  frames = hawki_extract_frameset(framelist, HAWKI_CALPRO_BKGIMAGE);
244  snprintf(calpro, 1024, HAWKI_CALPRO_JITTER_BKG_STATS);
245  snprintf(protype, 1024, HAWKI_PROTYPE_JITTER_BKG_STATS);
246  }
247  if (frames == NULL)
248  {
249  frames = hawki_extract_frameset(framelist, HAWKI_CAL_DARK_RAW);
250  snprintf(calpro, 1024, HAWKI_CALPRO_DARK_STATS);
251  snprintf(protype, 1024, HAWKI_PROTYPE_DARK_STATS);
252  }
253  if (frames == NULL)
254  {
255  frames = hawki_extract_frameset(framelist, HAWKI_CAL_FLAT_RAW);
256  snprintf(calpro, 1024, HAWKI_CALPRO_FLAT_STATS);
257  snprintf(protype, 1024, HAWKI_PROTYPE_FLAT_STATS);
258  }
259  if (frames == NULL)
260  {
261  frames = hawki_extract_frameset(framelist, HAWKI_CAL_ZPOINT_RAW);
262  snprintf(calpro, 1024, HAWKI_CALPRO_ZPOINT_STATS);
263  snprintf(protype, 1024, HAWKI_PROTYPE_ZPOINT_STATS);
264  }
265  if (frames == NULL)
266  {
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);
271  return -1;
272  }
273 
274  /* Create the statistics table and the "stats of the stats"*/
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++)
278  {
279  target_stats[idet] = cpl_table_new(cpl_frameset_get_size(frames));
280  stats_stats[idet] = cpl_propertylist_new();
281  }
282  hawki_image_stats_initialize(target_stats);
283 
284  /* Compute actually the statistics */
285  hawki_step_stats_frameset_stats(target_stats, stats_stats, frames);
286 
287  /* Saving the table product */
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");
292 
293  /* Free and return */
294  cpl_frameset_delete(frames);
295  for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
296  {
297  cpl_table_delete(target_stats[idet]);
298  cpl_propertylist_delete(stats_stats[idet]);
299  }
300  cpl_free(target_stats);
301  cpl_free(stats_stats);
302 
303  /* Return */
304  if (cpl_error_get_code())
305  {
306  cpl_msg_error(__func__,
307  "HAWK-I pipeline could not recover from previous errors");
308  return -1 ;
309  }
310  else return 0 ;
311 }
312 
313 /*----------------------------------------------------------------------------*/
323 /*----------------------------------------------------------------------------*/
324 static int hawki_step_stats_frameset_stats
325 (cpl_table ** target_stats,
326  cpl_propertylist ** stats_stats,
327  cpl_frameset * target_frames)
328 {
329  int iframe;
330  int nframes;
331 
332  /* Loop on the number of 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)
337  {
338  /* Local storage variables */
339  cpl_frame * this_target_frame;
340 
341  /* Computing statistics for this 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);
346  }
347  cpl_msg_indent_less();
348 
349  /* Compute stats of the stats */
350  hawki_image_stats_stats(target_stats, stats_stats);
351 
352  /* Print info about the statistics */
353  hawki_image_stats_print(target_stats);
354 
355  return 0;
356 }
357 
358 /*----------------------------------------------------------------------------*/
368 /*----------------------------------------------------------------------------*/
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,
375  const char * calpro,
376  const char * protype)
377 {
378  const cpl_frame * reference_frame;
379  cpl_propertylist * referencelist;
380  cpl_propertylist ** extlists;
381  int idet;
382  int ext_nb;
383  const char * recipe_name = "hawki_step_stats";
384  cpl_errorstate error_prevstate = cpl_errorstate_get();
385 
386 
387  /* Get the reference frame (the first one) */
388  reference_frame = cpl_frameset_get_first_const(used_frameset);
389 
390  /* Create the prop lists */
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);
394  extlists =
395  cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
396  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
397  {
398  /* Get the extension number */
400  (cpl_frame_get_filename(reference_frame), idet+1);
401 
402  /* Propagate the keywords from input frame extensions */
403  extlists[idet] = cpl_propertylist_load_regexp(
404  cpl_frame_get_filename(reference_frame), ext_nb,
405  HAWKI_HEADER_EXT_FORWARD, 0);
406 
407  /* Add the stats of the stats */
408  cpl_propertylist_append(extlists[idet],stats_stats[idet]);
409  }
410 
411  /* Write the table with the statistics */
412  hawki_tables_save(recipe_frameset,
413  recipe_parlist,
414  used_frameset,
415  (const cpl_table **)target_stats,
416  recipe_name,
417  calpro,
418  protype,
419  (const cpl_propertylist*)referencelist,
420  (const cpl_propertylist**)extlists,
421  "hawki_step_stats.fits");
422 
423  /* Free and return */
424  cpl_propertylist_delete(referencelist) ;
425  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
426  {
427  cpl_propertylist_delete(extlists[idet]) ;
428  }
429  cpl_free(extlists) ;
430 
431  if(!cpl_errorstate_is_equal(error_prevstate))
432  {
433  cpl_errorstate_set(CPL_ERROR_NONE);
434  return -1;
435  }
436  return 0;
437 }