GRAVI Pipeline Reference Manual 1.8.0
Loading...
Searching...
No Matches
gravity_biasmask.c
Go to the documentation of this file.
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
47static int gravity_biasmask_create(cpl_plugin *);
48static int gravity_biasmask_exec(cpl_plugin *);
49static int gravity_biasmask_destroy(cpl_plugin *);
50static int gravity_biasmask(cpl_frameset *, const cpl_parameterlist *);
51
52/*-----------------------------------------------------------------------------
53 Static variables
54 -----------------------------------------------------------------------------*/
55
56static char gravity_biasmask_short[] = GRAVI_UNOFFERED"Determine which pixels can be used to measure the bias of SC detector.";
57static 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"
59 "* Load the input files\n"
60 "* Identify the mask\n"
61 "* Write product\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"
66 GRAVI_BIASMASK_MAP" : biaspixel mask calibration \n"
67 "\n";
68
69/*-----------------------------------------------------------------------------
70 Function code
71 -----------------------------------------------------------------------------*/
72
73/*----------------------------------------------------------------------------*/
83/*----------------------------------------------------------------------------*/
84int 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",
96 "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
97 PACKAGE_BUGREPORT,
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/*----------------------------------------------------------------------------*/
125static 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/*----------------------------------------------------------------------------*/
173static 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/*----------------------------------------------------------------------------*/
235static 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/*----------------------------------------------------------------------------*/
267static 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 */
282
283 /* Get the input frameset */
284 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE, cpl_error_get_code()) ;
285
286 /* Init the used frameset */
287 used_frameset = cpl_frameset_new ();
288
289 /* Extract DARK frameset */
290 dark_frameset = gravi_frameset_extract_dark_data (frameset);
291
292 /* Extract FLAT frameset */
293 flat_frameset = gravi_frameset_extract_flat_data (frameset);
294
295 /* To use this recipe the frameset must contain the p2vm, wave and
296 * gain calibration file. */
297 if ( cpl_frameset_is_empty (dark_frameset) ||
298 cpl_frameset_get_size (dark_frameset) != 1 ||
299 cpl_frameset_is_empty (flat_frameset) ||
300 cpl_frameset_get_size (flat_frameset) != 4 ) {
301 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
302 "Need 1 DARK_RAW and 4 FLAT_RAW");
303 goto cleanup;
304 }
305
306
307 /*
308 * (1) Identify and extract the dark file
309 */
310
311 cpl_msg_info (cpl_func, " ***** Compute DARK map ***** ");
312
313 /* Load this DARK_RAW */
314 frame = cpl_frameset_get_position (dark_frameset, 0);
315 data = gravi_data_load_rawframe (frame, used_frameset);
316 gravi_data_detector_cleanup (data, parlist);
317
318 /* Compute the dark */
319 dark_map = gravi_compute_dark (data);
320 FREE (gravi_data_delete, data);
321
322 CPLCHECK_CLEAN ("Cannot compute the DARK map");
323
324 /*
325 * (2) Load the FLAT files
326 */
327
328 cpl_msg_info (cpl_func, " ***** Load FLATs ***** ");
329
330 /* Identify the flat files */
331 nb_frame_gain = cpl_frameset_get_size (flat_frameset);
332 raw_data = cpl_calloc (nb_frame_gain, sizeof(gravi_data *));
333
334 /* Build the list of FLAT files and output file name */
335 for (int i = 0; i < nb_frame_gain; i++) {
336 frame = cpl_frameset_get_position (flat_frameset, i);
337 raw_data[i] = gravi_data_load_rawframe (frame, used_frameset);
338 gravi_data_detector_cleanup (raw_data[i], parlist);
339 }
340
341 /*
342 * (2) Compute the BIASMASK from the DARK and FLATs
343 */
344
345 cpl_msg_info (cpl_func, " ***** Compute BIAS_MASK map ***** ");
346 biasmask_map = gravi_compute_biasmask (dark_map, raw_data,
347 nb_frame_gain, parlist);
348
349 CPLCHECK_CLEAN("Cannot compute the BIAS_MASK");
350
351 /* Free the list of files */
352 FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
353
354 /* Save the BAD */
355 frame = cpl_frameset_get_position (dark_frameset, 0);
356 gravi_data_save_new (biasmask_map, frameset, NULL, NULL, parlist,
357 NULL, frame, "gravity_biasmask",
358 NULL, GRAVI_BIASMASK_MAP);
359
360 CPLCHECK_CLEAN ("Could not save the BAD pixel map");
361
362 /* Deallocation of all variables */
363 cleanup:
364 cpl_msg_info (cpl_func,"Cleanup memory");
365
366 FREE (cpl_frameset_delete, dark_frameset);
367 FREE (gravi_data_delete, dark_map);
368 FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
369 FREE (gravi_data_delete, biasmask_map);
370 FREE (cpl_frameset_delete, flat_frameset);
371 FREE (cpl_frameset_delete, used_frameset);
372 FREE (gravi_data_delete, data);
373
374 /* FIXME: check a *change* of cpl_state instead */
375 CPLCHECK_INT ("Could not cleanup memory");
376
378 return (int)cpl_error_get_code();
379}
380
381
typedefCPL_BEGIN_DECLS struct _gravi_data_ gravi_data
Definition: gravi_data.h:38
#define GRAVI_RECIPE_OUTPUT
Definition: gravi_dfs.h:39
#define GRAVI_DARK_RAW
Definition: gravi_dfs.h:46
#define GRAVI_RECIPE_FLOW
Definition: gravi_dfs.h:37
#define GRAVI_BIASMASK_MAP
Definition: gravi_dfs.h:84
#define GRAVI_UNOFFERED
Definition: gravi_dfs.h:36
#define GRAVI_RECIPE_INPUT
Definition: gravi_dfs.h:38
#define GRAVI_FLAT_RAW
Definition: gravi_dfs.h:50
cpl_msg_info(cpl_func, "Compute WAVE_SCAN for %s", GRAVI_TYPE(type_data))
#define CPLCHECK_INT(msg)
Definition: gravi_utils.h:51
#define CPLCHECK_CLEAN(msg)
Definition: gravi_utils.h:54
#define gravi_msg_function_exit(flag)
Definition: gravi_utils.h:85
#define FREE(function, variable)
Definition: gravi_utils.h:69
#define gravi_msg_function_start(flag)
Definition: gravi_utils.h:84
#define FREELOOP(function, variable, n)
Definition: gravi_utils.h:72
static int gravity_biasmask_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
static int gravity_biasmask_create(cpl_plugin *)
Setup the recipe options
static char gravity_biasmask_description[]
static char gravity_biasmask_short[]
static int gravity_biasmask_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
static int gravity_biasmask(cpl_frameset *, const cpl_parameterlist *)
Compute the DARK, BAD, FLAT, WAVE, P2VM from a list of calibration set.
gravi_data * gravi_compute_dark(gravi_data *raw_data)
Compute the DARK calibration map.
Definition: gravi_calib.c:125
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:2271
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:1232
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:716
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:925
void gravi_data_delete(gravi_data *self)
Delete a gravi data.
Definition: gravi_data.c:146
cpl_parameter * gravi_parameter_add_static_name(cpl_parameterlist *self)
Definition: gravi_dfs.c:464
cpl_frameset * gravi_frameset_extract_flat_data(cpl_frameset *frameset)
Definition: gravi_dfs.c:1315
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
void gravity_print_banner(void)
Definition: gravi_dfs.c:61
cpl_frameset * gravi_frameset_extract_dark_data(cpl_frameset *frameset)
Extract DARK_RAW frame from the input frameset.
Definition: gravi_dfs.c:1311
const char * gravi_get_license(void)
Get the pipeline copyright and license.
Definition: gravi_utils.c:104