GRAVI Pipeline Reference Manual  1.2.3
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_FLOW"\n"
59  "* Load the input files\n"
60  "* Identify the mask\n"
61  "* Write product\n"
62  GRAVI_RECIPE_INPUT"\n"
63  GRAVI_DARK_RAW" : raw dark, all shutters closed (DPR.TYPE=DARK)\n"
64  GRAVI_FLAT_RAW" x4 : raw flats, one sutter open (DPR.TYPE=FLAT)\n"
65  GRAVI_RECIPE_OUTPUT"\n"
66  GRAVI_BIASMASK_MAP" : biaspixel mask calibration \n"
67  "\n";
68 
69 /*-----------------------------------------------------------------------------
70  Function code
71  -----------------------------------------------------------------------------*/
72 
73 /*----------------------------------------------------------------------------*/
83 /*----------------------------------------------------------------------------*/
84 int cpl_plugin_get_info(cpl_pluginlist * list)
85 {
86  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
87  cpl_plugin * plugin = &recipe->interface;
88 
89  if (cpl_plugin_init(plugin,
90  CPL_PLUGIN_API,
91  GRAVI_BINARY_VERSION,
92  CPL_PLUGIN_TYPE_RECIPE,
93  "gravity_biasmask",
94  gravity_biasmask_short,
95  gravity_biasmask_description,
96  "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
97  PACKAGE_BUGREPORT,
99  gravity_biasmask_create,
100  gravity_biasmask_exec,
101  gravity_biasmask_destroy)) {
102  cpl_msg_error(cpl_func, "Plugin initialization failed");
103  (void)cpl_error_set_where(cpl_func);
104  return 1;
105  }
106 
107  if (cpl_pluginlist_append(list, plugin)) {
108  cpl_msg_error(cpl_func, "Error adding plugin to list");
109  (void)cpl_error_set_where(cpl_func);
110  return 1;
111  }
112 
113  return 0;
114 }
115 
116 /*----------------------------------------------------------------------------*/
124 /*----------------------------------------------------------------------------*/
125 static int gravity_biasmask_create(cpl_plugin * plugin)
126 {
127  cpl_recipe * recipe;
128  // cpl_parameter * p;
129 
130  /* Do not create the recipe if an error code is already set */
131  if (cpl_error_get_code() != CPL_ERROR_NONE) {
132  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
133  cpl_func, __LINE__, cpl_error_get_where());
134  return (int)cpl_error_get_code();
135  }
136 
137  if (plugin == NULL) {
138  cpl_msg_error(cpl_func, "Null plugin");
139  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
140  }
141 
142  /* Verify plugin type */
143  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
144  cpl_msg_error(cpl_func, "Plugin is not a recipe");
145  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
146  }
147 
148  /* Get the recipe */
149  recipe = (cpl_recipe *)plugin;
150 
151  /* Create the parameters list in the cpl_recipe object */
152  recipe->parameters = cpl_parameterlist_new();
153  if (recipe->parameters == NULL) {
154  cpl_msg_error(cpl_func, "Parameter list allocation failed");
155  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
156  }
157 
158  /* Fill the parameters list */
159 
160  /* Use static names (output_procatg.fits) */
161  gravi_parameter_add_static_name (recipe->parameters);
162 
163  return 0;
164 }
165 
166 /*----------------------------------------------------------------------------*/
172 /*----------------------------------------------------------------------------*/
173 static int gravity_biasmask_exec(cpl_plugin * plugin)
174 {
175 
176  cpl_recipe * recipe;
177  int recipe_status;
178  cpl_errorstate initial_errorstate = cpl_errorstate_get();
179 
180  /* Return immediately if an error code is already set */
181  if (cpl_error_get_code() != CPL_ERROR_NONE) {
182  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
183  cpl_func, __LINE__, cpl_error_get_where());
184  return (int)cpl_error_get_code();
185  }
186 
187  if (plugin == NULL) {
188  cpl_msg_error(cpl_func, "Null plugin");
189  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
190  }
191 
192  /* Verify plugin type */
193  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
194  cpl_msg_error(cpl_func, "Plugin is not a recipe");
195  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
196  }
197 
198  /* Get the recipe */
199  recipe = (cpl_recipe *)plugin;
200 
201  /* Verify parameter and frame lists */
202  if (recipe->parameters == NULL) {
203  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
204  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
205  }
206  if (recipe->frames == NULL) {
207  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
208  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
209  }
210 
211  /* Invoke the recipe */
212  recipe_status = gravity_biasmask(recipe->frames, recipe->parameters);
213 
214  /* Ensure DFS-compliance of the products */
215  if (cpl_dfs_update_product_header(recipe->frames)) {
216  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
217  }
218 
219  if (!cpl_errorstate_is_equal(initial_errorstate)) {
220  /* Dump the error history since recipe execution start.
221  At this point the recipe cannot recover from the error */
222  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
223  }
224 
225  return recipe_status;
226 }
227 
228 /*----------------------------------------------------------------------------*/
234 /*----------------------------------------------------------------------------*/
235 static int gravity_biasmask_destroy(cpl_plugin * plugin)
236 {
237  cpl_recipe * recipe;
238 
239  if (plugin == NULL) {
240  cpl_msg_error(cpl_func, "Null plugin");
241  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
242  }
243 
244  /* Verify plugin type */
245  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
246  cpl_msg_error(cpl_func, "Plugin is not a recipe");
247  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
248  }
249 
250  /* Get the recipe */
251  recipe = (cpl_recipe *)plugin;
252 
253  cpl_parameterlist_delete(recipe->parameters);
254 
255  return 0;
256 }
257 
258 /*----------------------------------------------------------------------------*/
266 /*----------------------------------------------------------------------------*/
267 static int gravity_biasmask(cpl_frameset * frameset,
268  const cpl_parameterlist * parlist)
269 {
270  cpl_frameset * dark_frameset=NULL, * flat_frameset=NULL, * used_frameset=NULL;
271 
272  cpl_frame * frame=NULL;
273 
274  gravi_data * data = NULL, * dark_map=NULL;
275  gravi_data ** raw_data=NULL, * biasmask_map=NULL;
276 
277  int nb_frame_gain = 0;
278 
279  /* Message */
280  gravity_print_banner ();
281  cpl_msg_set_time_on();
282  cpl_msg_set_component_on();
283  gravi_msg_function_start(1);
284 
285  /* Get the input frameset */
286  cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE, cpl_error_get_code()) ;
287 
288  /* Init the used frameset */
289  used_frameset = cpl_frameset_new ();
290 
291  /* Extract DARK frameset */
292  dark_frameset = gravi_frameset_extract_dark_data (frameset);
293 
294  /* Extract FLAT frameset */
295  flat_frameset = gravi_frameset_extract_flat_data (frameset);
296 
297  /* To use this recipe the frameset must contain the p2vm, wave and
298  * gain calibration file. */
299  if ( cpl_frameset_is_empty (dark_frameset) ||
300  cpl_frameset_get_size (dark_frameset) != 1 ||
301  cpl_frameset_is_empty (flat_frameset) ||
302  cpl_frameset_get_size (flat_frameset) != 4 ) {
303  cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
304  "Need 1 DARK_RAW and 4 FLAT_RAW");
305  goto cleanup;
306  }
307 
308 
309  /*
310  * (1) Identify and extract the dark file
311  */
312 
313  cpl_msg_info (cpl_func, " ***** Compute DARK map ***** ");
314 
315  /* Load this DARK_RAW */
316  frame = cpl_frameset_get_position (dark_frameset, 0);
317  data = gravi_data_load_rawframe (frame, used_frameset);
318  gravi_data_detector_cleanup (data, parlist);
319 
320  /* Compute the dark */
321  dark_map = gravi_compute_dark (data);
322  FREE (gravi_data_delete, data);
323 
324  CPLCHECK_CLEAN ("Cannot compute the DARK map");
325 
326  /*
327  * (2) Load the FLAT files
328  */
329 
330  cpl_msg_info (cpl_func, " ***** Load FLATs ***** ");
331 
332  /* Identify the flat files */
333  nb_frame_gain = cpl_frameset_get_size (flat_frameset);
334  raw_data = cpl_calloc (nb_frame_gain, sizeof(gravi_data *));
335 
336  /* Build the list of FLAT files and output file name */
337  for (int i = 0; i < nb_frame_gain; i++) {
338  frame = cpl_frameset_get_position (flat_frameset, i);
339  raw_data[i] = gravi_data_load_rawframe (frame, used_frameset);
340  gravi_data_detector_cleanup (raw_data[i], parlist);
341  }
342 
343  /*
344  * (2) Compute the BIASMASK from the DARK and FLATs
345  */
346 
347  cpl_msg_info (cpl_func, " ***** Compute BIAS_MASK map ***** ");
348  biasmask_map = gravi_compute_biasmask (dark_map, raw_data,
349  nb_frame_gain, parlist);
350 
351  CPLCHECK_CLEAN("Cannot compute the BIAS_MASK");
352 
353  /* Free the list of files */
354  FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
355 
356  /* Save the BAD */
357  frame = cpl_frameset_get_position (dark_frameset, 0);
358  gravi_data_save_new (biasmask_map, frameset, NULL, NULL, parlist,
359  NULL, frame, "gravity_biasmask",
360  NULL, GRAVI_BIASMASK_MAP);
361 
362  CPLCHECK_CLEAN ("Could not save the BAD pixel map");
363 
364  /* Deallocation of all variables */
365  cleanup:
366  cpl_msg_info (cpl_func,"Cleanup memory");
367 
368  FREE (cpl_frameset_delete, dark_frameset);
369  FREE (gravi_data_delete, dark_map);
370  FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
371  FREE (gravi_data_delete, biasmask_map);
372  FREE (cpl_frameset_delete, flat_frameset);
373  FREE (cpl_frameset_delete, used_frameset);
374  FREE (gravi_data_delete, data);
375 
376  /* FIXME: check a *change* of cpl_state instead */
377  CPLCHECK_INT ("Could not cleanup memory");
378 
379  gravi_msg_function_exit(1);
380  return (int)cpl_error_get_code();
381 }
382 
383 
cpl_frameset * gravi_frameset_extract_dark_data(cpl_frameset *frameset)
Extract DARK_RAW frame from the input frameset.
Definition: gravi_dfs.c:913
gravi_data * gravi_data_load_rawframe(cpl_frame *frame, cpl_frameset *used_frameset)
Load a RAW FITS file and create a gravi_data.
Definition: gravi_data.c:690
cpl_error_code gravi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: gravi_dfs.c:78
gravi_data * gravi_compute_biasmask(gravi_data *dark_map, gravi_data **flats_data, int nflat, const cpl_parameterlist *params)
Create BIASMASK for SC from raw FLATs and raw DARK.
Definition: gravi_calib.c:2121
const char * gravi_get_license(void)
Get the pipeline copyright and license.
Definition: gravi_utils.c:104
cpl_error_code gravi_data_detector_cleanup(gravi_data *data, const cpl_parameterlist *parlist)
Perform self-bias correction to the SC raw data.
Definition: gravi_data.c:1086
cpl_error_code gravi_data_save_new(gravi_data *self, cpl_frameset *allframes, const char *filename, const char *suffix, const cpl_parameterlist *parlist, cpl_frameset *usedframes, cpl_frame *frame, const char *recipe, cpl_propertylist *applist, const char *proCatg)
Save a gravi data in a CPL-complian FITS file.
Definition: gravi_data.c:896
void gravi_data_delete(gravi_data *self)
Delete a gravi data.
Definition: gravi_data.c:137
gravi_data * gravi_compute_dark(gravi_data *raw_data)
Compute the DARK calibration map.
Definition: gravi_calib.c:122