HAWKI Pipeline Reference Manual  1.8.12
hawki_tec_filtchk.c
1 /* $Id: hawki_tec_filtchk.c,v 1.16 2013/03/11 11:02:59 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: 2013/03/11 11:02:59 $
24  * $Revision: 1.16 $
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_tec_filtchk_create(cpl_plugin *) ;
58 static int hawki_tec_filtchk_exec(cpl_plugin *) ;
59 static int hawki_tec_filtchk_destroy(cpl_plugin *) ;
60 static int hawki_tec_filtchk(cpl_parameterlist *, cpl_frameset *) ;
61 
62 static int hawki_tec_filtchk_frameset_stats
63 (cpl_table ** target_stats,
64  cpl_propertylist ** stats_stats,
65  cpl_frameset * target_frames);
66 
67 static int hawki_tec_filtchk_save
68 (cpl_table ** target_stats,
69  cpl_parameterlist * recipe_parlist,
70  cpl_frameset * recipe_frameset,
71  cpl_propertylist ** stats_stats,
72  const char * calpro,
73  const char * protype);
74 
75 /*-----------------------------------------------------------------------------
76  Static variables
77  -----------------------------------------------------------------------------*/
78 
79 static char hawki_tec_filtchk_description[] =
80 "hawki_tec_filtchk -- Check pairs of flats taken with different filters.\n"
81 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
82 "raw-file.fits "HAWKI_TEC_FLAT_RAW"\n";
83 
84 /*-----------------------------------------------------------------------------
85  Functions code
86  -----------------------------------------------------------------------------*/
87 
88 /*----------------------------------------------------------------------------*/
96 /*----------------------------------------------------------------------------*/
97 int cpl_plugin_get_info(cpl_pluginlist * list)
98 {
99  cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
100  cpl_plugin * plugin = &recipe->interface ;
101 
102  cpl_plugin_init(plugin,
103  CPL_PLUGIN_API,
104  HAWKI_BINARY_VERSION,
105  CPL_PLUGIN_TYPE_RECIPE,
106  "hawki_tec_filtchk",
107  "Filter checking recipe",
108  hawki_tec_filtchk_description,
109  "Cesar Enrique Garcia Dabo",
110  PACKAGE_BUGREPORT,
112  hawki_tec_filtchk_create,
113  hawki_tec_filtchk_exec,
114  hawki_tec_filtchk_destroy) ;
115 
116  cpl_pluginlist_append(list, plugin) ;
117 
118  return 0;
119 }
120 
121 /*----------------------------------------------------------------------------*/
130 /*----------------------------------------------------------------------------*/
131 static int hawki_tec_filtchk_create(cpl_plugin * plugin)
132 {
133  cpl_recipe * recipe ;
134  /* cpl_parameter * p ; */
135 
136  /* Get the recipe out of the plugin */
137  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
138  recipe = (cpl_recipe *)plugin ;
139  else return -1 ;
140 
141  /* Create the parameters list in the cpl_recipe object */
142  recipe->parameters = cpl_parameterlist_new() ;
143  if (recipe->parameters == NULL)
144  return 1;
145 
146  /* Fill the parameters list */
147  /* None.. */
148 
149  /* Return */
150  return 0;
151 }
152 
153 /*----------------------------------------------------------------------------*/
159 /*----------------------------------------------------------------------------*/
160 static int hawki_tec_filtchk_exec(cpl_plugin * plugin)
161 {
162  cpl_recipe * recipe ;
163 
164  /* Get the recipe out of the plugin */
165  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
166  recipe = (cpl_recipe *)plugin ;
167  else return -1 ;
168 
169  /* Issue a banner */
171 
172  return hawki_tec_filtchk(recipe->parameters, recipe->frames) ;
173 }
174 
175 /*----------------------------------------------------------------------------*/
181 /*----------------------------------------------------------------------------*/
182 static int hawki_tec_filtchk_destroy(cpl_plugin * plugin)
183 {
184  cpl_recipe * recipe ;
185 
186  /* Get the recipe out of the plugin */
187  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
188  recipe = (cpl_recipe *)plugin ;
189  else return -1 ;
190 
191  cpl_parameterlist_delete(recipe->parameters) ;
192  return 0 ;
193 }
194 
195 /*----------------------------------------------------------------------------*/
202 /*----------------------------------------------------------------------------*/
203 static int hawki_tec_filtchk(
204  cpl_parameterlist * parlist,
205  cpl_frameset * framelist)
206 {
207  cpl_frameset * frames ;
208  cpl_table ** target_stats;
209  cpl_propertylist ** stats_stats;
210  int idet;
211  char calpro[1024];
212  char protype[1024];
213 
214  /* Identify the RAW and CALIB frames in the input frameset */
215  if (hawki_dfs_set_groups(framelist))
216  {
217  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
218  return -1;
219  }
220 
221  /* Retrieve raw frames */
222  cpl_msg_info(__func__, "Identifying input frames");
223  frames = hawki_extract_frameset(framelist, HAWKI_TEC_FLAT_RAW) ;
224  snprintf(calpro, 1024, HAWKI_CALPRO_FILTERPOSCHECK_STATS);
225  snprintf(protype, 1024, HAWKI_PROTYPE_FILTERPOSCHECK_STATS);
226  if (frames == NULL)
227  {
228  cpl_msg_error(__func__,"Input files should be tagged %s",
229  HAWKI_TEC_FLAT_RAW);
230  return -1;
231  }
232 
233  /* Create the statistics table */
234  target_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_table *));
235  stats_stats = cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist *));
236  for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
237  {
238  target_stats[idet] = cpl_table_new(cpl_frameset_get_size(frames));
239  stats_stats[idet] = cpl_propertylist_new();
240  }
241  hawki_image_stats_initialize(target_stats);
242 
243  /* Compute actually the statistics */
244  hawki_tec_filtchk_frameset_stats(target_stats, stats_stats, frames);
245 
246  /* Saving the table product */
247  hawki_tec_filtchk_save
248  (target_stats, parlist, framelist, stats_stats, calpro, protype);
249 
250  /* Free and return */
251  cpl_frameset_delete(frames);
252  for( idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
253  {
254  cpl_table_delete(target_stats[idet]);
255  cpl_propertylist_delete(stats_stats[idet]);
256  }
257  cpl_free(target_stats);
258  cpl_free(stats_stats);
259 
260  /* Return */
261  if (cpl_error_get_code())
262  {
263  cpl_msg_error(__func__,
264  "HAWK-I pipeline could not recover from previous errors");
265  return -1 ;
266  }
267  else return 0 ;
268 }
269 
270 /*----------------------------------------------------------------------------*/
280 /*----------------------------------------------------------------------------*/
281 static int hawki_tec_filtchk_frameset_stats
282 (cpl_table ** target_stats,
283  cpl_propertylist ** stats_stats,
284  cpl_frameset * target_frames)
285 {
286  int iframe;
287  int nframes;
288 
289  /* Loop on the number of frames */
290  nframes = cpl_frameset_get_size(target_frames);
291  cpl_msg_info(__func__, "Looping the target frames: %d frames", nframes);
292  cpl_msg_indent_more();
293  for( iframe = 0 ; iframe < nframes ; ++iframe)
294  {
295  /* Local storage variables */
296  cpl_frame * this_target_frame;
297 
298  /* Computing statistics for this frame */
299  cpl_msg_info(__func__, "Computing stats for frame: %d", iframe +1);
300  this_target_frame = cpl_frameset_get_frame(target_frames, iframe);
302  (target_stats, this_target_frame, iframe);
303  }
304  cpl_msg_indent_less();
305 
306  /* Compute stats of the stats */
307  hawki_image_stats_stats(target_stats, stats_stats);
308 
309  /* Print info about the statistics */
310  hawki_image_stats_print(target_stats);
311 
312  return 0;
313 }
314 
315 /*----------------------------------------------------------------------------*/
325 /*----------------------------------------------------------------------------*/
326 static int hawki_tec_filtchk_save
327 (cpl_table ** target_stats,
328  cpl_parameterlist * recipe_parlist,
329  cpl_frameset * recipe_frameset,
330  cpl_propertylist ** stats_stats,
331  const char * calpro,
332  const char * protype)
333 {
334  const cpl_frame * reference_frame;
335  cpl_propertylist * referencelist;
336  cpl_propertylist ** extlists;
337  int idet;
338  int ext_nb;
339  const char * recipe_name = "hawki_tec_filtchk";
340 
341  /* Get the reference frame (the first one) */
342  reference_frame = cpl_frameset_get_first_const(recipe_frameset);
343 
344  /* Create the prop lists */
345  cpl_msg_info(__func__, "Creating the keywords list") ;
346  referencelist = cpl_propertylist_load_regexp
347  (cpl_frame_get_filename(reference_frame), 0,HAWKI_HEADER_EXT_FORWARD,0);
348  extlists =
349  cpl_malloc(HAWKI_NB_DETECTORS * sizeof(cpl_propertylist*));
350  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
351  {
352  /* Get the extension number */
354  (cpl_frame_get_filename(reference_frame), idet+1);
355 
356  /* Propagate the keywords from input frame extensions */
357  extlists[idet] = cpl_propertylist_load_regexp(
358  cpl_frame_get_filename(reference_frame), ext_nb,
359  HAWKI_HEADER_EXT_FORWARD, 0);
360 
361  /* Add the stats of the stats */
362  cpl_propertylist_append(extlists[idet],stats_stats[idet]);
363  }
364 
365  /* Write the table with the statistics */
366  hawki_tables_save(recipe_frameset,
367  recipe_parlist,
368  recipe_frameset,
369  (const cpl_table **)target_stats,
370  recipe_name,
371  calpro,
372  protype,
373  (const cpl_propertylist*)referencelist,
374  (const cpl_propertylist**)extlists,
375  "hawki_tec_filtchk_stats.fits");
376 
377  /* Free and return */
378  cpl_propertylist_delete(referencelist) ;
379  for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
380  {
381  cpl_propertylist_delete(extlists[idet]) ;
382  }
383  cpl_free(extlists) ;
384  return 0;
385 }