GRAVI Pipeline Reference Manual  0.7.12
gravi_all_dark.c
1 /* $Id: gravi_all_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 
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 
52 static int gravi_all_dark_create(cpl_plugin *);
53 static int gravi_all_dark_exec(cpl_plugin *);
54 static int gravi_all_dark_destroy(cpl_plugin *);
55 static int gravi_all_dark(cpl_frameset *, const cpl_parameterlist *);
56 
57 /*-----------------------------------------------------------------------------
58  Static variables
59  -----------------------------------------------------------------------------*/
60 
61 static char gravi_all_dark_description[] =
62 "This recipe is used to compute the median of a raw dark set of integration.\n"
63 "The median of each pixel is computed along the time axis for the FT and SC channel.\n"
64 "The master dark frame is created from these two median images.\n"
65 "Description DO category\n"
66 "Required input :\n"
67 " Raw dark file (one or more) " GRAVI_DARK "\n"
68 "Output :\n"
69 " Master dark (one per input file) " DARK "\n"
70 "\n";
71 
72 /*-----------------------------------------------------------------------------
73  Function code
74  -----------------------------------------------------------------------------*/
75 
76 /*----------------------------------------------------------------------------*/
86 /*----------------------------------------------------------------------------*/
87 int cpl_plugin_get_info(cpl_pluginlist * list)
88 {
89  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
90  cpl_plugin * plugin = &recipe->interface;
91 
92  if (cpl_plugin_init(plugin,
93  CPL_PLUGIN_API,
94  GRAVI_BINARY_VERSION,
95  CPL_PLUGIN_TYPE_RECIPE,
96  "gravi_all_dark",
97  "This recipe is used to compute the median of a raw dark.",
98  gravi_all_dark_description,
99  "Firstname Lastname",
100  PACKAGE_BUGREPORT,
101  gravi_get_license(),
102  gravi_all_dark_create,
103  gravi_all_dark_exec,
104  gravi_all_dark_destroy)) {
105  cpl_msg_error(cpl_func, "Plugin initialization failed");
106  (void)cpl_error_set_where(cpl_func);
107  return 1;
108  }
109 
110  if (cpl_pluginlist_append(list, plugin)) {
111  cpl_msg_error(cpl_func, "Error adding plugin to list");
112  (void)cpl_error_set_where(cpl_func);
113  return 1;
114  }
115 
116  return 0;
117 }
118 
119 /*----------------------------------------------------------------------------*/
127 /*----------------------------------------------------------------------------*/
128 static int gravi_all_dark_create(cpl_plugin * plugin)
129 {
130  cpl_recipe * recipe;
131  cpl_parameter * p;
132 
133  /* Do not create the recipe if an error code is already set */
134  if (cpl_error_get_code() != CPL_ERROR_NONE) {
135  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
136  cpl_func, __LINE__, cpl_error_get_where());
137  return (int)cpl_error_get_code();
138  }
139 
140  if (plugin == NULL) {
141  cpl_msg_error(cpl_func, "Null plugin");
142  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
143  }
144 
145  /* Verify plugin type */
146  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
147  cpl_msg_error(cpl_func, "Plugin is not a recipe");
148  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
149  }
150 
151  /* Get the recipe */
152  recipe = (cpl_recipe *)plugin;
153 
154  /* Create the parameters list in the cpl_recipe object */
155  recipe->parameters = cpl_parameterlist_new();
156  if (recipe->parameters == NULL) {
157  cpl_msg_error(cpl_func, "Parameter list allocation failed");
158  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
159  }
160 
161  return 0;
162 }
163 
164 /*----------------------------------------------------------------------------*/
170 /*----------------------------------------------------------------------------*/
171 static int gravi_all_dark_exec(cpl_plugin * plugin)
172 {
173 
174  cpl_recipe * recipe;
175  int recipe_status;
176  cpl_errorstate initial_errorstate = cpl_errorstate_get();
177 
178  /* Return immediately if an error code is already set */
179  if (cpl_error_get_code() != CPL_ERROR_NONE) {
180  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
181  cpl_func, __LINE__, cpl_error_get_where());
182  return (int)cpl_error_get_code();
183  }
184 
185  if (plugin == NULL) {
186  cpl_msg_error(cpl_func, "Null plugin");
187  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
188  }
189 
190  /* Verify plugin type */
191  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
192  cpl_msg_error(cpl_func, "Plugin is not a recipe");
193  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
194  }
195 
196  /* Get the recipe */
197  recipe = (cpl_recipe *)plugin;
198 
199  /* Verify parameter and frame lists */
200  if (recipe->parameters == NULL) {
201  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
202  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
203  }
204  if (recipe->frames == NULL) {
205  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
206  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
207  }
208 
209  /* Invoke the recipe */
210  recipe_status = gravi_all_dark(recipe->frames, recipe->parameters);
211 
212  /* Ensure DFS-compliance of the products */
213  if (cpl_dfs_update_product_header(recipe->frames)) {
214  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
215  }
216 
217  if (!cpl_errorstate_is_equal(initial_errorstate)) {
218  /* Dump the error history since recipe execution start.
219  At this point the recipe cannot recover from the error */
220  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
221  }
222 
223  return recipe_status;
224 }
225 
226 /*----------------------------------------------------------------------------*/
232 /*----------------------------------------------------------------------------*/
233 static int gravi_all_dark_destroy(cpl_plugin * plugin)
234 {
235  cpl_recipe * recipe;
236 
237  if (plugin == NULL) {
238  cpl_msg_error(cpl_func, "Null plugin");
239  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
240  }
241 
242  /* Verify plugin type */
243  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
244  cpl_msg_error(cpl_func, "Plugin is not a recipe");
245  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
246  }
247 
248  /* Get the recipe */
249  recipe = (cpl_recipe *)plugin;
250 
251  cpl_parameterlist_delete(recipe->parameters);
252 
253  return 0;
254 }
255 
256 
257 /*----------------------------------------------------------------------------*/
264 /*----------------------------------------------------------------------------*/
265 static int gravi_all_dark(cpl_frameset * frameset,
266  const cpl_parameterlist * parlist)
267 {
268  cpl_frameset * dark_frameset = NULL, * usedframes;
269  cpl_frame * frame;
270  cpl_propertylist * applist, * primary_hdr;
271  const char * input, * filename, * temp;
272  char * output;
273  gravi_data * raw_dark, * reduced_dark, * bad_map;
274  int * shutter;
275 // double dit;
276  int comp, nb_frame;
277 
278  /* Message */
279  cpl_msg_set_time_on();
280  cpl_msg_set_component_on();
281  cpl_msg_info(cpl_func,"Start function");
282 
283  /* Identify the RAW and CALIB frames in the input frameset */
284  cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
285  cpl_error_get_code()) ;
286 
287  /* - Extract a set of frame dark */
288  dark_frameset = gravi_frameset_extract_dark(frameset);
289 
290  if (cpl_frameset_is_empty(dark_frameset)) {
291  cpl_frameset_delete(dark_frameset);
292  /* To use this recipe the frameset must contain at least
293  * one dark frame. */
294  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
295  "No dark frame in the frameset") ;
296  }
297 
298  /* Apply the treatment for each dark frame contained in the frameset */
299  nb_frame = cpl_frameset_get_size(dark_frameset);
300 
301  for (comp = 0; comp < nb_frame; comp++){
302 
303  /* Find the file name and initialization of the output name */
304  frame = cpl_frameset_get_position(dark_frameset, comp);
305 
306  input = cpl_frame_get_filename(frame) ;
307  if (input == NULL) {
308  cpl_frameset_delete(dark_frameset);
309 
310  /* The file name does not exist */
311  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
312  "Input file does not exist") ;
313  }
314  temp = strrchr(input, '/');
315  filename = temp ? temp + 1 : input;
316  cpl_msg_info (cpl_func, "This file %s is a raw dark file", filename);
317 
318  // output = cpl_sprintf("master_dark.%s", filename);
319  output = gravi_data_product_name (filename, "dark");
320 
321  /* Compute the median of a set of dark frames */
322  raw_dark = gravi_data_load(input);
323 
324  primary_hdr = gravi_data_get_propertylist(raw_dark,
325  GRAVI_PRIMARY_HDR_NAME_EXT);
326  shutter = gravi_shutters_check(primary_hdr);
327 
328  if (cpl_error_get_code()) {
329  gravi_data_delete(raw_dark);
330  cpl_frameset_delete(dark_frameset);
331  cpl_free(output);
332 
333  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
334  "Error while getting the shutter values");
335  }
336 
337  if ((shutter [0] != 0) || (shutter [1] != 0) ||
338  (shutter [2] != 0) || (shutter [3] != 0)){
339  cpl_msg_info (cpl_func, "The shutters of this file are not all close");
340  // gravi_data_delete(raw_dark);
341  // cpl_free(shutter);
342  // cpl_free(output);
343 
344 // continue;
345  }
346  cpl_free (shutter);
347  reduced_dark = gravi_compute_dark( raw_dark );
348 
349  if (cpl_error_get_code()) {
350  gravi_data_delete(reduced_dark);
351  gravi_data_delete(raw_dark);
352  cpl_frameset_delete(dark_frameset);
353  cpl_free(output);
354 
355  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
356  "Error while computing the master dark");
357  }
358 
359  primary_hdr = gravi_data_get_propertylist(reduced_dark,
360  GRAVI_PRIMARY_HDR_NAME_EXT);
361 // dit = gravi_pfits_get_dit(primary_hdr);
362  //insname = gravi_pfits_get_insname(primary_hdr_dark);
363 
364  /* Create product frame, add DataFlow keywords, save the file, log the
365  * saved file in the input frameset*/
366  usedframes = cpl_frameset_new();
367  cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame));
368  applist = gravi_propertylist_get_qc (primary_hdr);
369 
370 
371  cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, DARK);
372 // cpl_propertylist_append_double(applist, GRAVI_DET_DIT, dit);
373 
374  if (gravi_data_save(reduced_dark, frameset, output, parlist,
375  usedframes, frame, "gravi_all_dark", applist)
376  != CPL_ERROR_NONE){
377  gravi_data_delete(reduced_dark);
378  gravi_data_delete(raw_dark);
379  cpl_frameset_delete(dark_frameset);
380  cpl_frameset_delete(usedframes);
381  cpl_propertylist_delete(applist);
382  cpl_free(output);
383  return (int) cpl_error_set_message(cpl_func,
384  CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the reduced_dark"
385  " on the output file");
386  }
387 
388  gravi_data_delete(reduced_dark);
389  gravi_data_delete(raw_dark);
390  cpl_frameset_delete(usedframes);
391  cpl_propertylist_delete(applist);
392  cpl_free(output);
393  }
394 
395  /* Deallocation of all variables */
396 
397  cpl_frameset_delete(dark_frameset);
398  return (int)cpl_error_get_code();
399 }
400 
401