IIINSTRUMENT Pipeline Reference Manual  0.0.7
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_range("iiinstrument.rrrecipe.range_option", CPL_TYPE_DOUBLE,
193  "This is a value range of type double",
194  "Example", 0.5, 0.0, 1.0);
195  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "rangeopt");
196  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
197  cpl_parameterlist_append(recipe->parameters, p);
198 
199  p = cpl_parameter_new_enum("iiinstrument.rrrecipe.enum_option", CPL_TYPE_STRING,
200  "This is an enumeration of type " "string",
201  "Example", "first", 3, "first", "second", "third");
202  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "enumopt");
203  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
204  cpl_parameterlist_append(recipe->parameters, p);
205 
206 
207  return 0;
208 }
209 
210 /*----------------------------------------------------------------------------*/
216 /*----------------------------------------------------------------------------*/
217 static int rrrecipe_exec(cpl_plugin * plugin)
218 {
219 
220  cpl_recipe * recipe;
221  int recipe_status;
222  cpl_errorstate initial_errorstate = cpl_errorstate_get();
223 
224  /* Return immediately if an error code is already set */
225  if (cpl_error_get_code() != CPL_ERROR_NONE) {
226  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
227  cpl_func, __LINE__, cpl_error_get_where());
228  return (int)cpl_error_get_code();
229  }
230 
231  if (plugin == NULL) {
232  cpl_msg_error(cpl_func, "Null plugin");
233  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
234  }
235 
236  /* Verify plugin type */
237  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
238  cpl_msg_error(cpl_func, "Plugin is not a recipe");
239  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
240  }
241 
242  /* Get the recipe */
243  recipe = (cpl_recipe *)plugin;
244 
245  /* Verify parameter and frame lists */
246  if (recipe->parameters == NULL) {
247  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
248  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
249  }
250  if (recipe->frames == NULL) {
251  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
252  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
253  }
254 
255  /* Invoke the recipe */
256  recipe_status = rrrecipe(recipe->frames, recipe->parameters);
257 
258  /* Ensure DFS-compliance of the products */
259  if (cpl_dfs_update_product_header(recipe->frames)) {
260  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
261  }
262 
263  if (!cpl_errorstate_is_equal(initial_errorstate)) {
264  /* Dump the error history since recipe execution start.
265  At this point the recipe cannot recover from the error */
266  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
267  }
268 
269  return recipe_status;
270 }
271 
272 /*----------------------------------------------------------------------------*/
278 /*----------------------------------------------------------------------------*/
279 static int rrrecipe_destroy(cpl_plugin * plugin)
280 {
281  cpl_recipe * recipe;
282 
283  if (plugin == NULL) {
284  cpl_msg_error(cpl_func, "Null plugin");
285  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
286  }
287 
288  /* Verify plugin type */
289  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
290  cpl_msg_error(cpl_func, "Plugin is not a recipe");
291  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
292  }
293 
294  /* Get the recipe */
295  recipe = (cpl_recipe *)plugin;
296 
297  cpl_parameterlist_delete(recipe->parameters);
298 
299  return 0;
300 }
301 
302 /*----------------------------------------------------------------------------*/
309 /*----------------------------------------------------------------------------*/
310 static int rrrecipe(cpl_frameset * frameset,
311  const cpl_parameterlist * parlist)
312 {
313  const cpl_parameter * param;
314  const char * str_option;
315  int bool_option;
316  const cpl_frame * rawframe;
317  const cpl_frame * flat;
318  double qc_param;
319  cpl_propertylist * plist;
320  cpl_propertylist * applist;
321  cpl_image * image;
322 
323  /* Use the errorstate to detect an error in a function that does not
324  return an error code. */
325  cpl_errorstate prestate = cpl_errorstate_get();
326 
327  /* HOW TO RETRIEVE INPUT PARAMETERS */
328  /* --stropt */
329  param = cpl_parameterlist_find_const(parlist,
330  "iiinstrument.rrrecipe.str_option");
331  str_option = cpl_parameter_get_string(param);
332 
333  /* --boolopt */
334  param = cpl_parameterlist_find_const(parlist,
335  "iiinstrument.rrrecipe.bool_option");
336  bool_option = cpl_parameter_get_bool(param);
337 
338  if (!cpl_errorstate_is_equal(prestate)) {
339  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
340  "Could not retrieve the input "
341  "parameters");
342  }
343 
344  /* Identify the RAW and CALIB frames in the input frameset */
345  cpl_ensure_code(iiinstrument_dfs_set_groups(frameset) == CPL_ERROR_NONE,
346  cpl_error_get_code());
347 
348  /* HOW TO ACCESS INPUT DATA */
349  /* - A required file */
350  rawframe = cpl_frameset_find_const(frameset, RRRECIPE_RAW);
351  if (rawframe == NULL) {
352  /* cpl_frameset_find_const() does not set an error code, when a frame
353  is not found, so we will set one here. */
354  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
355  "SOF does not have any file tagged "
356  "with %s", RRRECIPE_RAW);
357  }
358  /* - A recommended file */
359  flat = cpl_frameset_find(frameset, IIINSTRUMENT_CALIB_FLAT);
360  if (flat == NULL) {
361  cpl_msg_warning(cpl_func, "SOF does not have any file tagged with %s",
362  IIINSTRUMENT_CALIB_FLAT);
363  }
364 
365  /* HOW TO GET THE VALUE OF A FITS KEYWORD */
366  /* - Load only DETector related keys */
367  plist = cpl_propertylist_load_regexp(cpl_frame_get_filename(rawframe),
368  0, "ESO DET ", 0);
369  if (plist == NULL) {
370  /* In this case an error message is added to the error propagation */
371  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
372  "Could not read the FITS header");
373  }
374 
375  qc_param = iiinstrument_pfits_get_dit(plist);
376  cpl_propertylist_delete(plist);
377 
378  /* Check for a change in the CPL error state */
379  /* - if it did change then propagate the error and return */
380  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
381 
382  /* NOW PERFORMING THE DATA REDUCTION */
383  /* Let's just load an image for the example */
384  image = cpl_image_load(cpl_frame_get_filename(rawframe), CPL_TYPE_FLOAT, 0,
385  0);
386  if (image == NULL) {
387  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
388  "Could not load the image");
389  }
390 
391  applist = cpl_propertylist_new();
392 
393  /* Add the product category */
394  cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
395  RRRECIPE_OUT_PROCATG);
396 
397  /* Add a QC parameter */
398  cpl_propertylist_append_double(applist, "ESO QC QCPARAM", qc_param);
399 
400  /* HOW TO SAVE A DFS-COMPLIANT PRODUCT TO DISK */
401  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
402  CPL_BPP_IEEE_FLOAT, "rrrecipe", applist,
403  NULL, PACKAGE "/" PACKAGE_VERSION,
404  "rrrecipe.fits")) {
405  /* Propagate the error */
406  (void)cpl_error_set_where(cpl_func);
407  }
408 
409  cpl_image_delete(image);
410  cpl_propertylist_delete(applist);
411 
412  return (int)cpl_error_get_code();
413 }