GRAVI Pipeline Reference Manual 1.9.3
Loading...
Searching...
No Matches
gravity_dark.c
Go to the documentation of this file.
1/* $Id: gravity_dark.c,v 1.29 2011/03/10 09:16:12 nazouaoui 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/*
22 * $Author: nazouaoui $
23 * $Date: 2011/03/10 09:16:12 $
24 * $Revision: 1.29 $
25 * $Name: $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35
36#include <cpl.h>
37#include <stdio.h>
38#include <string.h>
39#include <time.h>
40
41#include "gravi_utils.h"
42#include "gravi_pfits.h"
43#include "gravi_dfs.h"
44#include "gravi_calib.h"
45
46#include "gravi_data.h"
47
48/*-----------------------------------------------------------------------------
49 Private function prototypes
50 -----------------------------------------------------------------------------*/
51
52static int gravity_dark_create(cpl_plugin *);
53static int gravity_dark_exec(cpl_plugin *);
54static int gravity_dark_destroy(cpl_plugin *);
55static int gravity_dark(cpl_frameset *, const cpl_parameterlist *);
56
57/*-----------------------------------------------------------------------------
58 Static variables
59 -----------------------------------------------------------------------------*/
60
61static char gravity_dark_short[] = "Calibrate the detector noise and background level.";
63 "This recipe computes the DARK calibration for the SC, the FT and the ACQ detectors. The SC detector is first debiased using the biaspixels, before computing the dark mean and rms. For detectors, the mean dark level of each pixel and the stdev of each pixel are saved in the output product.\n"
65 "* Loop on input dark files and concatenate them\n"
66 "* Compute the median and rms of these concatenated files\n"
67 "* Save the product (FT, SC, ACQ camera into same product)\n"
69 GRAVI_DARK_RAW" : raw dark, all shutters closed (DPR.TYPE=DARK)\n"
71 GRAVI_DARK_MAP" : dark calibration\n"
72 "";
73
74/*-----------------------------------------------------------------------------
75 Function code
76 -----------------------------------------------------------------------------*/
77
78/*----------------------------------------------------------------------------*/
88/*----------------------------------------------------------------------------*/
89int cpl_plugin_get_info(cpl_pluginlist * list)
90{
91 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
92 cpl_plugin * plugin = &recipe->interface;
93
94 if (cpl_plugin_init(plugin,
95 CPL_PLUGIN_API,
96 GRAVI_BINARY_VERSION,
97 CPL_PLUGIN_TYPE_RECIPE,
98 "gravity_dark",
101 "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
102 PACKAGE_BUGREPORT,
107 cpl_msg_error(cpl_func, "Plugin initialization failed");
108 (void)cpl_error_set_where(cpl_func);
109 return 1;
110 }
111
112 if (cpl_pluginlist_append(list, plugin)) {
113 cpl_msg_error(cpl_func, "Error adding plugin to list");
114 (void)cpl_error_set_where(cpl_func);
115 return 1;
116 }
117
118 return 0;
119}
120
121/*----------------------------------------------------------------------------*/
129/*----------------------------------------------------------------------------*/
130static int gravity_dark_create(cpl_plugin * plugin)
131{
132 cpl_recipe * recipe;
133 // cpl_parameter * p;
134
135 /* Do not create the recipe if an error code is already set */
136 if (cpl_error_get_code() != CPL_ERROR_NONE) {
137 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
138 cpl_func, __LINE__, cpl_error_get_where());
139 return (int)cpl_error_get_code();
140 }
141
142 if (plugin == NULL) {
143 cpl_msg_error(cpl_func, "Null plugin");
144 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
145 }
146
147 /* Verify plugin type */
148 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
149 cpl_msg_error(cpl_func, "Plugin is not a recipe");
150 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
151 }
152
153 /* Get the recipe */
154 recipe = (cpl_recipe *)plugin;
155
156 /* Create the parameters list in the cpl_recipe object */
157 recipe->parameters = cpl_parameterlist_new();
158 if (recipe->parameters == NULL) {
159 cpl_msg_error(cpl_func, "Parameter list allocation failed");
160 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
161 }
162
163 /* Use static names (output_procatg.fits) */
164 gravi_parameter_add_static_name (recipe->parameters);
165
166 /* Bias-method */
167 gravi_parameter_add_biasmethod (recipe->parameters);
168 gravi_parameter_add_biassub_file (recipe->parameters);
169
170 return 0;
171}
172
173/*----------------------------------------------------------------------------*/
179/*----------------------------------------------------------------------------*/
180static int gravity_dark_exec(cpl_plugin * plugin)
181{
182
183 cpl_recipe * recipe;
184 int recipe_status;
185 cpl_errorstate initial_errorstate = cpl_errorstate_get();
186
187 /* Return immediately if an error code is already set */
188 if (cpl_error_get_code() != CPL_ERROR_NONE) {
189 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
190 cpl_func, __LINE__, cpl_error_get_where());
191 return (int)cpl_error_get_code();
192 }
193
194 if (plugin == NULL) {
195 cpl_msg_error(cpl_func, "Null plugin");
196 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
197 }
198
199 /* Verify plugin type */
200 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
201 cpl_msg_error(cpl_func, "Plugin is not a recipe");
202 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
203 }
204
205 /* Get the recipe */
206 recipe = (cpl_recipe *)plugin;
207
208 /* Verify parameter and frame lists */
209 if (recipe->parameters == NULL) {
210 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
211 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
212 }
213 if (recipe->frames == NULL) {
214 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
215 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
216 }
217
218 /* Invoke the recipe */
219 recipe_status = gravity_dark(recipe->frames, recipe->parameters);
220
221 /* Ensure DFS-compliance of the products */
222 if (cpl_dfs_update_product_header(recipe->frames)) {
223 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
224 }
225
226 if (!cpl_errorstate_is_equal(initial_errorstate)) {
227 /* Dump the error history since recipe execution start.
228 At this point the recipe cannot recover from the error */
229 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
230 }
231
232 return recipe_status;
233}
234
235/*----------------------------------------------------------------------------*/
241/*----------------------------------------------------------------------------*/
242static int gravity_dark_destroy(cpl_plugin * plugin)
243{
244 cpl_recipe * recipe;
245
246 if (plugin == NULL) {
247 cpl_msg_error(cpl_func, "Null plugin");
248 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
249 }
250
251 /* Verify plugin type */
252 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
253 cpl_msg_error(cpl_func, "Plugin is not a recipe");
254 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
255 }
256
257 /* Get the recipe */
258 recipe = (cpl_recipe *)plugin;
259
260 cpl_parameterlist_delete(recipe->parameters);
261
262 return 0;
263}
264
265
266/*----------------------------------------------------------------------------*/
273/*----------------------------------------------------------------------------*/
274static int gravity_dark (cpl_frameset * frameset,
275 const cpl_parameterlist * parlist)
276{
277 cpl_frameset * dark_frameset = NULL, * used_frameset = NULL;
278 cpl_frame * frame = NULL;
279 gravi_data * raw_dark = NULL, * reduced_dark = NULL;
280 int comp, nb_frame;
281
282
283 /* Message */
286
287 /* Identify the RAW and CALIB frames in the input frameset */
288 cpl_ensure_code (gravi_dfs_set_groups (frameset) == CPL_ERROR_NONE, cpl_error_get_code());
289
290 /* Dispatch the framesets */
291 dark_frameset = gravi_frameset_extract_dark_data (frameset);
292
293 /* Check the frameset */
294 if (cpl_frameset_is_empty (dark_frameset)) {
295 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
296 "No DARK_RAW file on the frameset") ;
297 goto cleanup;
298 }
299
300 /* Load all DARK in the frameset into a single data */
301 nb_frame = cpl_frameset_get_size (dark_frameset);
302 used_frameset = cpl_frameset_new ();
303
304 for (comp = 0; comp < nb_frame; comp++){
305 gravi_data * data_tmp;
306 cpl_frame * frame_tmp;
307
308 char filename_suffix[20];
309 snprintf(filename_suffix, 16, "%d", comp);
310
311 /* Load this frame */
312 frame_tmp = cpl_frameset_get_position (dark_frameset, comp);
313 data_tmp = gravi_data_load_rawframe (frame_tmp, used_frameset);
314 gravi_data_detector_cleanup (data_tmp, parlist);
315
316 /* Option save the bias-subtracted file */
317 if (gravi_param_get_bool (parlist,"gravity.dfs.bias-subtracted-file")) {
318
319 gravi_data_save_new (data_tmp, frameset, NULL, filename_suffix, parlist,
320 dark_frameset, frame_tmp, "gravity_dark",
321 NULL, "BIAS_SUBTRACTED");
322
323 CPLCHECK_CLEAN ("Cannot save the BIAS_SUBTRACTED product");
324 }
325
326 /* Cleanup unused data */
327 //gravi_data_erase (data_tmp, GRAVI_METROLOGY_EXT);
330
331 /* FIXME: shall remove the FT except for the first one */
332
333 if (comp == 0) {
334 /* Use the first frame for merging */
335 frame = frame_tmp;
336 raw_dark = data_tmp; data_tmp = NULL;
337 }
338 else {
339 /* Merge to first frame */
340 /* FIX ME: does not use multiple DARK for metrology Volts */
341 int force = 0;
342 gravi_data_append (raw_dark, data_tmp, force);
343 FREE (gravi_data_delete, data_tmp);
344 }
345
346 CPLCHECK_CLEAN ("Cannot load all DARK into a single data");
347 }
348
349
350 /* Compute the reduced DARK */
351 reduced_dark = gravi_compute_dark (raw_dark);
352 FREE (gravi_data_delete, raw_dark);
353
354 CPLCHECK_CLEAN ("Could not compute the DARK map");
355
356 /* Create product frame */
357 gravi_data_save_new (reduced_dark, frameset, NULL, NULL, parlist,
358 used_frameset, frame, "gravity_dark",
359 NULL, GRAVI_DARK_MAP);
360
361 CPLCHECK_CLEAN ("Could not save the DARK map");
362
363 /* Terminate the function */
364 goto cleanup;
365
366cleanup:
367 /* Deallocation of all variables */
368 cpl_msg_info (cpl_func,"Memory cleanup");
369
370 FREE (gravi_data_delete, reduced_dark);
371 FREE (gravi_data_delete, raw_dark);
372 FREE (cpl_frameset_delete, dark_frameset);
373 FREE (cpl_frameset_delete, used_frameset);
374
376 return (int)cpl_error_get_code();
377}
378
379
typedefCPL_BEGIN_DECLS struct _gravi_data_ gravi_data
Definition: gravi_data.h:39
#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_DARK_MAP
Definition: gravi_dfs.h:77
#define GRAVI_RECIPE_INPUT
Definition: gravi_dfs.h:38
cpl_msg_info(cpl_func, "Compute WAVE_SCAN for %s", GRAVI_TYPE(type_data))
#define GRAVI_OPDC_EXT
Definition: gravi_pfits.h:62
#define GRAVI_FDDL_EXT
Definition: gravi_pfits.h:75
#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
static int gravity_dark_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
Definition: gravity_dark.c:242
static int gravity_dark_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
Definition: gravity_dark.c:180
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: gravity_dark.c:89
static char gravity_dark_short[]
Definition: gravity_dark.c:61
static char gravity_dark_description[]
Definition: gravity_dark.c:62
static int gravity_dark_create(cpl_plugin *)
Setup the recipe options
Definition: gravity_dark.c:130
static int gravity_dark(cpl_frameset *, const cpl_parameterlist *)
Compute the master dark for each dark frames.
Definition: gravity_dark.c:274
gravi_data * gravi_compute_dark(gravi_data *raw_data)
Compute the DARK calibration map.
Definition: gravi_calib.c:125
cpl_error_code gravi_data_erase(gravi_data *self, const char *extname)
Erase an extension by its EXTNAME.
Definition: gravi_data.c:2212
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_append(gravi_data *first, const gravi_data *second, int force)
Append a gravi_data into another existing one.
Definition: gravi_data.c:308
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
int gravi_param_get_bool(const cpl_parameterlist *parlist, const char *name)
Definition: gravi_dfs.c:1537
cpl_parameter * gravi_parameter_add_static_name(cpl_parameterlist *self)
Definition: gravi_dfs.c:464
cpl_parameter * gravi_parameter_add_biasmethod(cpl_parameterlist *self)
Definition: gravi_dfs.c:584
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
cpl_parameter * gravi_parameter_add_biassub_file(cpl_parameterlist *self)
Definition: gravi_dfs.c:494
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