GRAVI Pipeline Reference Manual  0.9.6
gravity_biasmask.c
1 /* $Id: gravity_biasmask.c,v 1.29 2009/02/10 09:16:12 llundin Exp $
2  *
3  * This file is part of the GRAVI 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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 /*-----------------------------------------------------------------------------
26  Includes
27  -----------------------------------------------------------------------------*/
28 
29 #include <cpl.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 
34 #include "gravi_data.h"
35 #include "gravi_pfits.h"
36 #include "gravi_dfs.h"
37 
38 #include "gravi_utils.h"
39 
40 #include "gravi_calib.h"
41 
42 
43 /*-----------------------------------------------------------------------------
44  Private function prototypes
45  -----------------------------------------------------------------------------*/
46 
47 static int gravity_biasmask_create(cpl_plugin *);
48 static int gravity_biasmask_exec(cpl_plugin *);
49 static int gravity_biasmask_destroy(cpl_plugin *);
50 static int gravity_biasmask(cpl_frameset *, const cpl_parameterlist *);
51 
52 /*-----------------------------------------------------------------------------
53  Static variables
54  -----------------------------------------------------------------------------*/
55 
56 static char gravity_biasmask_short[] = GRAVI_UNOFFERED"Determine which pixels can be used to measure the bias of SC detector.";
57 static char gravity_biasmask_description[] = GRAVI_UNOFFERED"The recipe creates a binary mask (BIASPIX) indentifying which pixels of the SC detector are not illuminated, and thus could be used as bias-pixels in further processing. The idea would be to input such a mask, as static calibration, in all reductions. However this is not yet implemented, nor demonstrated as necessary.\n"
58  GRAVI_RECIPE_INPUT"\n"
59  GRAVI_DARK_RAW" : raw dark, all shutters closed (DPR.TYPE=DARK)\n"
60  GRAVI_FLAT_RAW" x4 : raw flats, one sutter open (DPR.TYPE=FLAT)\n"
61  GRAVI_RECIPE_OUTPUT"\n"
62  GRAVI_BIASMASK_MAP" : biaspixel mask calibration \n"
63  "\n";
64 
65 /*-----------------------------------------------------------------------------
66  Function code
67  -----------------------------------------------------------------------------*/
68 
69 /*----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------*/
80 int cpl_plugin_get_info(cpl_pluginlist * list)
81 {
82  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
83  cpl_plugin * plugin = &recipe->interface;
84 
85  if (cpl_plugin_init(plugin,
86  CPL_PLUGIN_API,
87  GRAVI_BINARY_VERSION,
88  CPL_PLUGIN_TYPE_RECIPE,
89  "gravity_biasmask",
90  gravity_biasmask_short,
91  gravity_biasmask_description,
92  "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
93  PACKAGE_BUGREPORT,
94  gravi_get_license(),
95  gravity_biasmask_create,
96  gravity_biasmask_exec,
97  gravity_biasmask_destroy)) {
98  cpl_msg_error(cpl_func, "Plugin initialization failed");
99  (void)cpl_error_set_where(cpl_func);
100  return 1;
101  }
102 
103  if (cpl_pluginlist_append(list, plugin)) {
104  cpl_msg_error(cpl_func, "Error adding plugin to list");
105  (void)cpl_error_set_where(cpl_func);
106  return 1;
107  }
108 
109  return 0;
110 }
111 
112 /*----------------------------------------------------------------------------*/
120 /*----------------------------------------------------------------------------*/
121 static int gravity_biasmask_create(cpl_plugin * plugin)
122 {
123  cpl_recipe * recipe;
124  // cpl_parameter * p;
125 
126  /* Do not create the recipe if an error code is already set */
127  if (cpl_error_get_code() != CPL_ERROR_NONE) {
128  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
129  cpl_func, __LINE__, cpl_error_get_where());
130  return (int)cpl_error_get_code();
131  }
132 
133  if (plugin == NULL) {
134  cpl_msg_error(cpl_func, "Null plugin");
135  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
136  }
137 
138  /* Verify plugin type */
139  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
140  cpl_msg_error(cpl_func, "Plugin is not a recipe");
141  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
142  }
143 
144  /* Get the recipe */
145  recipe = (cpl_recipe *)plugin;
146 
147  /* Create the parameters list in the cpl_recipe object */
148  recipe->parameters = cpl_parameterlist_new();
149  if (recipe->parameters == NULL) {
150  cpl_msg_error(cpl_func, "Parameter list allocation failed");
151  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
152  }
153 
154  /* Fill the parameters list */
155 
156  /* Use static names (output_procatg.fits) */
157  gravi_parameter_add_static_name (recipe->parameters);
158 
159  return 0;
160 }
161 
162 /*----------------------------------------------------------------------------*/
168 /*----------------------------------------------------------------------------*/
169 static int gravity_biasmask_exec(cpl_plugin * plugin)
170 {
171 
172  cpl_recipe * recipe;
173  int recipe_status;
174  cpl_errorstate initial_errorstate = cpl_errorstate_get();
175 
176  /* Return immediately if an error code is already set */
177  if (cpl_error_get_code() != CPL_ERROR_NONE) {
178  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
179  cpl_func, __LINE__, cpl_error_get_where());
180  return (int)cpl_error_get_code();
181  }
182 
183  if (plugin == NULL) {
184  cpl_msg_error(cpl_func, "Null plugin");
185  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
186  }
187 
188  /* Verify plugin type */
189  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
190  cpl_msg_error(cpl_func, "Plugin is not a recipe");
191  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
192  }
193 
194  /* Get the recipe */
195  recipe = (cpl_recipe *)plugin;
196 
197  /* Verify parameter and frame lists */
198  if (recipe->parameters == NULL) {
199  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
200  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
201  }
202  if (recipe->frames == NULL) {
203  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
204  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
205  }
206 
207  /* Invoke the recipe */
208  recipe_status = gravity_biasmask(recipe->frames, recipe->parameters);
209 
210  /* Ensure DFS-compliance of the products */
211  if (cpl_dfs_update_product_header(recipe->frames)) {
212  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
213  }
214 
215  if (!cpl_errorstate_is_equal(initial_errorstate)) {
216  /* Dump the error history since recipe execution start.
217  At this point the recipe cannot recover from the error */
218  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
219  }
220 
221  return recipe_status;
222 }
223 
224 /*----------------------------------------------------------------------------*/
230 /*----------------------------------------------------------------------------*/
231 static int gravity_biasmask_destroy(cpl_plugin * plugin)
232 {
233  cpl_recipe * recipe;
234 
235  if (plugin == NULL) {
236  cpl_msg_error(cpl_func, "Null plugin");
237  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
238  }
239 
240  /* Verify plugin type */
241  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
242  cpl_msg_error(cpl_func, "Plugin is not a recipe");
243  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
244  }
245 
246  /* Get the recipe */
247  recipe = (cpl_recipe *)plugin;
248 
249  cpl_parameterlist_delete(recipe->parameters);
250 
251  return 0;
252 }
253 
254 /*----------------------------------------------------------------------------*/
262 /*----------------------------------------------------------------------------*/
263 static int gravity_biasmask(cpl_frameset * frameset,
264  const cpl_parameterlist * parlist)
265 {
266  cpl_frameset * dark_frameset=NULL, * flat_frameset=NULL, * used_frameset=NULL;
267 
268  cpl_frame * frame=NULL;
269 
270  gravi_data * data = NULL, * dark_map=NULL;
271  gravi_data ** raw_data=NULL, * biasmask_map=NULL;
272 
273  int nb_frame_gain = 0;
274 
275  /* Message */
276  gravity_print_banner ();
277  cpl_msg_set_time_on();
278  cpl_msg_set_component_on();
279  gravi_msg_function_start(1);
280 
281  /* Get the input frameset */
282  cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE, cpl_error_get_code()) ;
283 
284  /* Init the used frameset */
285  used_frameset = cpl_frameset_new ();
286 
287  /* Extract DARK frameset */
288  dark_frameset = gravi_frameset_extract_dark_data (frameset);
289 
290  /* Extract FLAT frameset */
291  flat_frameset = gravi_frameset_extract_flat_data (frameset);
292 
293  /* To use this recipe the frameset must contain the p2vm, wave and
294  * gain calibration file. */
295  if ( cpl_frameset_is_empty (dark_frameset) ||
296  cpl_frameset_get_size (dark_frameset) != 1 ||
297  cpl_frameset_is_empty (flat_frameset) ||
298  cpl_frameset_get_size (flat_frameset) != 4 ) {
299  cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
300  "Need 1 DARK_RAW and 4 FLAT_RAW");
301  goto cleanup;
302  }
303 
304 
305  /*
306  * (1) Identify and extract the dark file
307  */
308 
309  cpl_msg_info (cpl_func, " ***** Compute DARK map ***** ");
310 
311  /* Load this DARK_RAW */
312  frame = cpl_frameset_get_position (dark_frameset, 0);
313  data = gravi_data_load_rawframe (frame, used_frameset);
314  gravi_data_detector_cleanup (data, parlist);
315 
316  /* Compute the dark */
317  dark_map = gravi_compute_dark (data);
318  FREE (gravi_data_delete, data);
319 
320  CPLCHECK_CLEAN ("Cannot compute the DARK map");
321 
322  /*
323  * (2) Load the FLAT files
324  */
325 
326  cpl_msg_info (cpl_func, " ***** Load FLATs ***** ");
327 
328  /* Identify the flat files */
329  nb_frame_gain = cpl_frameset_get_size (flat_frameset);
330  raw_data = cpl_calloc (nb_frame_gain, sizeof(gravi_data *));
331 
332  /* Build the list of FLAT files and output file name */
333  for (int i = 0; i < nb_frame_gain; i++) {
334  frame = cpl_frameset_get_position (flat_frameset, i);
335  raw_data[i] = gravi_data_load_rawframe (frame, used_frameset);
336  gravi_data_detector_cleanup (raw_data[i], parlist);
337  }
338 
339  /*
340  * (2) Compute the BIASMASK from the DARK and FLATs
341  */
342 
343  cpl_msg_info (cpl_func, " ***** Compute BIAS_MASK map ***** ");
344  biasmask_map = gravi_compute_biasmask (dark_map, raw_data,
345  nb_frame_gain, parlist);
346 
347  CPLCHECK_CLEAN("Cannot compute the BIAS_MASK");
348 
349  /* Free the list of files */
350  FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
351 
352  /* Save the BAD */
353  frame = cpl_frameset_get_position (dark_frameset, 0);
354  gravi_data_save_new (biasmask_map, frameset, NULL, parlist,
355  NULL, frame, "gravity_biasmask",
356  NULL, GRAVI_BIASMASK_MAP);
357 
358  CPLCHECK_CLEAN ("Could not save the BAD pixel map");
359 
360  /* Deallocation of all variables */
361  cleanup:
362  cpl_msg_info (cpl_func,"Cleanup memory");
363 
364  FREE (cpl_frameset_delete, dark_frameset);
365  FREE (gravi_data_delete, dark_map);
366  FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
367  FREE (gravi_data_delete, biasmask_map);
368  FREE (cpl_frameset_delete, flat_frameset);
369  FREE (cpl_frameset_delete, used_frameset);
370  FREE (gravi_data_delete, data);
371 
372  /* FIXME: check a *change* of cpl_state instead */
373  CPLCHECK_INT ("Could not cleanup memory");
374 
375  gravi_msg_function_exit(1);
376  return (int)cpl_error_get_code();
377 }
378 
379