IIINSTRUMENT Pipeline Reference Manual  0.1.4
rrrecipe.c
1 /* $Id: rrrecipe.c,v 1.33 2013-03-26 17:00:45 jtaylor Exp $
2  *
3  * This file is part of the IIINSTRUMENT 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author: jtaylor $
23  * $Date: 2013-03-26 17:00:45 $
24  * $Revision: 1.33 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include "iiinstrument_utils.h"
37 #include "iiinstrument_pfits.h"
38 #include "iiinstrument_dfs.h"
39 
40 #include <cpl.h>
41 
42 /*-----------------------------------------------------------------------------
43  Plugin registration
44  -----------------------------------------------------------------------------*/
45 
46 int cpl_plugin_get_info(cpl_pluginlist * list);
47 
48 /*-----------------------------------------------------------------------------
49  Private function prototypes
50  -----------------------------------------------------------------------------*/
51 
52 static int rrrecipe_create(cpl_plugin *);
53 static int rrrecipe_exec(cpl_plugin *);
54 static int rrrecipe_destroy(cpl_plugin *);
55 static int rrrecipe(cpl_frameset *, const cpl_parameterlist *);
56 
57 /*-----------------------------------------------------------------------------
58  Static variables
59  -----------------------------------------------------------------------------*/
60 
61 static char rrrecipe_description[] =
62 "This example text is used to describe the recipe.\n"
63 "The description should include the required FITS-files and\n"
64 "their associated tags, e.g.\n"
65 "IIINSTRUMENT-RRRECIPE-raw-file.fits " RRRECIPE_RAW "\n"
66 "and any optional files, e.g.\n"
67 "IIINSTRUMENT-RRRECIPE-flat-file.fits " IIINSTRUMENT_CALIB_FLAT "\n"
68 "\n"
69 "Additionally, it should describe functionality of the expected output."
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  IIINSTRUMENT_BINARY_VERSION,
95  CPL_PLUGIN_TYPE_RECIPE,
96  "rrrecipe",
97  "Short description of rrrecipe",
98  rrrecipe_description,
99  "Firstname Lastname",
100  PACKAGE_BUGREPORT,
101  iiinstrument_get_license(),
102  rrrecipe_create,
103  rrrecipe_exec,
104  rrrecipe_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 rrrecipe_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  /* Fill the parameters list */
162  /* --stropt */
163  p = cpl_parameter_new_value("iiinstrument.rrrecipe.str_option",
164  CPL_TYPE_STRING, "the string option",
165  "iiinstrument.rrrecipe", "NONE");
166  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "stropt");
167  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
168  cpl_parameterlist_append(recipe->parameters, p);
169 
170  p = cpl_parameter_new_value("iiinstrument.rrrecipe.file_option",
171  CPL_TYPE_STRING, "the string option",
172  "iiinstrument.rrrecipe", "NONE");
173  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fileopt");
174  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
175  cpl_parameterlist_append(recipe->parameters, p);
176 
177  /* --boolopt */
178  p = cpl_parameter_new_value("iiinstrument.rrrecipe.bool_option",
179  CPL_TYPE_BOOL, "a flag",
180  "iiinstrument.rrrecipe", TRUE);
181  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "boolopt");
182  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
183  cpl_parameterlist_append(recipe->parameters, p);
184 
185  p = cpl_parameter_new_value("iiinstrument.rrrecipe.int_option",
186  CPL_TYPE_INT, "a flag",
187  "iiinstrument.rrrecipe", 3);
188  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "intopt");
189  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
190  cpl_parameterlist_append(recipe->parameters, p);
191 
192  p = cpl_parameter_new_value("iiinstrument.rrrecipe.float_option", CPL_TYPE_DOUBLE,
193  "A float",
194  "Example", 0.5);
195  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "floatopt");
196  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
197  cpl_parameterlist_append(recipe->parameters, p);
198 
199  p = cpl_parameter_new_range("iiinstrument.rrrecipe.range_option", CPL_TYPE_INT,
200  "This is a value range of type int",
201  "Example", 3, 0, 10);
202  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "rangeopt");
203  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
204  cpl_parameterlist_append(recipe->parameters, p);
205 
206  p = cpl_parameter_new_enum("iiinstrument.rrrecipe.enum_option", CPL_TYPE_STRING,
207  "This is an enumeration of type " "string",
208  "Example", "first", 3, "first", "second", "third");
209  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "enumopt");
210  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
211  cpl_parameterlist_append(recipe->parameters, p);
212 
213 
214  return 0;
215 }
216 
217 /*----------------------------------------------------------------------------*/
223 /*----------------------------------------------------------------------------*/
224 static int rrrecipe_exec(cpl_plugin * plugin)
225 {
226 
227  cpl_recipe * recipe;
228  int recipe_status;
229  cpl_errorstate initial_errorstate = cpl_errorstate_get();
230 
231  /* Return immediately if an error code is already set */
232  if (cpl_error_get_code() != CPL_ERROR_NONE) {
233  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
234  cpl_func, __LINE__, cpl_error_get_where());
235  return (int)cpl_error_get_code();
236  }
237 
238  if (plugin == NULL) {
239  cpl_msg_error(cpl_func, "Null plugin");
240  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
241  }
242 
243  /* Verify plugin type */
244  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
245  cpl_msg_error(cpl_func, "Plugin is not a recipe");
246  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
247  }
248 
249  /* Get the recipe */
250  recipe = (cpl_recipe *)plugin;
251 
252  /* Verify parameter and frame lists */
253  if (recipe->parameters == NULL) {
254  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
255  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
256  }
257  if (recipe->frames == NULL) {
258  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
259  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
260  }
261 
262  /* Invoke the recipe */
263  recipe_status = rrrecipe(recipe->frames, recipe->parameters);
264 
265  /* Ensure DFS-compliance of the products */
266  if (cpl_dfs_update_product_header(recipe->frames)) {
267  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
268  }
269 
270  if (!cpl_errorstate_is_equal(initial_errorstate)) {
271  /* Dump the error history since recipe execution start.
272  At this point the recipe cannot recover from the error */
273  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
274  }
275 
276  return recipe_status;
277 }
278 
279 /*----------------------------------------------------------------------------*/
285 /*----------------------------------------------------------------------------*/
286 static int rrrecipe_destroy(cpl_plugin * plugin)
287 {
288  cpl_recipe * recipe;
289 
290  if (plugin == NULL) {
291  cpl_msg_error(cpl_func, "Null plugin");
292  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
293  }
294 
295  /* Verify plugin type */
296  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
297  cpl_msg_error(cpl_func, "Plugin is not a recipe");
298  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
299  }
300 
301  /* Get the recipe */
302  recipe = (cpl_recipe *)plugin;
303 
304  cpl_parameterlist_delete(recipe->parameters);
305 
306  return 0;
307 }
308 
309 /*----------------------------------------------------------------------------*/
316 /*----------------------------------------------------------------------------*/
317 static int rrrecipe(cpl_frameset * frameset,
318  const cpl_parameterlist * parlist)
319 {
320  const cpl_parameter * param;
321  const char * str_option;
322  int bool_option;
323  const cpl_frame * rawframe;
324  const cpl_frame * flat;
325  double qc_param;
326  cpl_propertylist * plist;
327  cpl_propertylist * applist;
328  cpl_image * image;
329 
330  /* Use the errorstate to detect an error in a function that does not
331  return an error code. */
332  cpl_errorstate prestate = cpl_errorstate_get();
333 
334  /* HOW TO RETRIEVE INPUT PARAMETERS */
335  /* --stropt */
336  param = cpl_parameterlist_find_const(parlist,
337  "iiinstrument.rrrecipe.str_option");
338  str_option = cpl_parameter_get_string(param);
339 
340  /* --boolopt */
341  param = cpl_parameterlist_find_const(parlist,
342  "iiinstrument.rrrecipe.bool_option");
343  bool_option = cpl_parameter_get_bool(param);
344 
345  if (!cpl_errorstate_is_equal(prestate)) {
346  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
347  "Could not retrieve the input "
348  "parameters");
349  }
350 
351  /* Identify the RAW and CALIB frames in the input frameset */
352  cpl_ensure_code(iiinstrument_dfs_set_groups(frameset) == CPL_ERROR_NONE,
353  cpl_error_get_code());
354 
355  /* HOW TO ACCESS INPUT DATA */
356  /* - A required file */
357  rawframe = cpl_frameset_find_const(frameset, RRRECIPE_RAW);
358  if (rawframe == NULL) {
359  /* cpl_frameset_find_const() does not set an error code, when a frame
360  is not found, so we will set one here. */
361  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
362  "SOF does not have any file tagged "
363  "with %s", RRRECIPE_RAW);
364  }
365  /* - A recommended file */
366  flat = cpl_frameset_find(frameset, IIINSTRUMENT_CALIB_FLAT);
367  if (flat == NULL) {
368  cpl_msg_warning(cpl_func, "SOF does not have any file tagged with %s",
369  IIINSTRUMENT_CALIB_FLAT);
370  }
371 
372  /* HOW TO GET THE VALUE OF A FITS KEYWORD */
373  /* - Load only DETector related keys */
374  plist = cpl_propertylist_load_regexp(cpl_frame_get_filename(rawframe),
375  0, "ESO DET ", 0);
376  if (plist == NULL) {
377  /* In this case an error message is added to the error propagation */
378  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
379  "Could not read the FITS header");
380  }
381 
382  qc_param = iiinstrument_pfits_get_dit(plist);
383  cpl_propertylist_delete(plist);
384 
385  /* Check for a change in the CPL error state */
386  /* - if it did change then propagate the error and return */
387  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
388 
389  /* NOW PERFORMING THE DATA REDUCTION */
390  /* Let's just load an image for the example */
391  image = cpl_image_load(cpl_frame_get_filename(rawframe), CPL_TYPE_FLOAT, 0,
392  0);
393  if (image == NULL) {
394  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
395  "Could not load the image");
396  }
397 
398  applist = cpl_propertylist_new();
399 
400  /* Add the product category */
401  cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
402  RRRECIPE_OUT_PROCATG);
403 
404  /* Add a QC parameter */
405  cpl_propertylist_append_double(applist, "ESO QC QCPARAM", qc_param);
406 
407  /* HOW TO SAVE A DFS-COMPLIANT PRODUCT TO DISK */
408  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
409  CPL_BPP_IEEE_FLOAT, "rrrecipe", applist,
410  NULL, PACKAGE "/" PACKAGE_VERSION,
411  "rrrecipe.fits")) {
412  /* Propagate the error */
413  (void)cpl_error_set_where(cpl_func);
414  }
415 
416  cpl_image_delete(image);
417  cpl_propertylist_delete(applist);
418 
419  return (int)cpl_error_get_code();
420 }