UVES Pipeline Reference Manual  5.4.6
uves_utl_ima_arith.c
1 /* $Id: uves_utl_ima_arith.c,v 1.1 2009-02-18 12:19:38 amodigli Exp $
2  *
3  * This file is part of the UVES 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: amodigli $
23  * $Date: 2009-02-18 12:19:38 $
24  * $Revision: 1.1 $
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 #include <string.h>
36 
37 /* cpl */
38 #include <cpl.h>
39 
40 /* irplib */
41 #include <irplib_utils.h>
42 
43 //#include <uves_tpl_utils.h>
44 #include <uves_pfits.h>
45 #include <uves_dfs.h>
46 //#include <uves_key_names.h>
47 //#include <uves_pro_types.h>
48 //#include <uves_functions.h>
49 #include <uves_msg.h>
50 #include <uves_error.h>
51 #include <uves_utils_wrappers.h>
52 #define KEY_VALUE_HPRO_DID "PRO-1.15"
53 /*-----------------------------------------------------------------------------
54  Functions prototypes
55  ----------------------------------------------------------------------------*/
56 
57 static int uves_utl_ima_arith_create(cpl_plugin *) ;
58 static int uves_utl_ima_arith_exec(cpl_plugin *) ;
59 static int uves_utl_ima_arith_destroy(cpl_plugin *) ;
60 static int uves_utl_ima_arith(cpl_parameterlist *, cpl_frameset *) ;
61 
62 /*-----------------------------------------------------------------------------
63  Static variables
64  ----------------------------------------------------------------------------*/
65 
66 static char uves_utl_ima_arith_description[] =
67 "This recipe performs image computation.\n"
68 "The input files are 2 images\n"
69 "their associated tags should be RAW_IMA.\n"
70 "The output is an image resulting from the RAW_IMA op RAW_IMA where op indicates\n"
71 "the operation to be performed specified by the parameter \n"
72 "uves.uves_utl_ima_arith.op having alias 'op'\n"
73 "Information on relevant parameters can be found with\n"
74 "esorex --params uves_utl_ima_arith\n"
75 "esorex --help uves_utl_ima_arith\n"
76 "\n";
77 
78 /*-----------------------------------------------------------------------------
79  Functions code
80  ----------------------------------------------------------------------------*/
81 /*---------------------------------------------------------------------------*/
85 /*---------------------------------------------------------------------------*/
86 
88 /*---------------------------------------------------------------------------*/
96 /*---------------------------------------------------------------------------*/
97 int cpl_plugin_get_info(cpl_pluginlist * list)
98 {
99  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
100  cpl_plugin * plugin = &recipe->interface ;
101 
102  cpl_plugin_init(plugin,
103  CPL_PLUGIN_API,
104  UVES_BINARY_VERSION,
105  CPL_PLUGIN_TYPE_RECIPE,
106  "uves_utl_ima_arith",
107  "Computes result of ima1 op ima2",
108  uves_utl_ima_arith_description,
109  "Andrea Modigliani",
110  "Andrea.Modigliani@eso.org",
115 
116  cpl_pluginlist_append(list, plugin) ;
117 
118  return 0;
119 }
120 
121 /*---------------------------------------------------------------------------*/
130 /*---------------------------------------------------------------------------*/
131 static int uves_utl_ima_arith_create(cpl_plugin * plugin)
132 {
133  cpl_recipe * recipe ;
134  cpl_parameter * p ;
135 
136  /* Get the recipe out of the plugin */
137  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
138  recipe = (cpl_recipe *)plugin ;
139  else return -1 ;
140  cpl_error_reset();
141  irplib_reset();
142 
143  /* Create the parameters list in the cpl_recipe object */
144  recipe->parameters = cpl_parameterlist_new() ;
145 
146  /* Fill the parameters list */
147  /* --stropt */
148  p = cpl_parameter_new_value("uves.uves_utl_ima_arith.op",
149  CPL_TYPE_STRING,
150  "A possible operation",
151  "uves.uves_utl_ima_arith","+");
152  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
153  cpl_parameterlist_append(recipe->parameters, p) ;
154 
155  /* --doubleopt */
156  p = cpl_parameter_new_value("uves.uves_utl_ima_arith.value",
157  CPL_TYPE_DOUBLE, "a value", "uves.uves_utl_ima_arith", 9999.) ;
158  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "value") ;
159  cpl_parameterlist_append(recipe->parameters, p) ;
160 
161  /* Return */
162  return 0;
163 }
164 
165 /*---------------------------------------------------------------------------*/
171 /*---------------------------------------------------------------------------*/
172 static int uves_utl_ima_arith_exec(cpl_plugin * plugin)
173 {
174  cpl_recipe * recipe ;
175  int code=0;
176  cpl_errorstate initial_errorstate = cpl_errorstate_get();
177 
178  /* Get the recipe out of the plugin */
179  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
180  recipe = (cpl_recipe *)plugin ;
181  else return -1 ;
182  cpl_error_reset();
183  irplib_reset();
184  code = uves_utl_ima_arith(recipe->parameters, recipe->frames) ;
185 
186 
187  if (!cpl_errorstate_is_equal(initial_errorstate)) {
188  /* Dump the error history since recipe execution start.
189  At this point the recipe cannot recover from the error */
190  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
191  }
192 
193  return code ;
194 }
195 
196 /*---------------------------------------------------------------------------*/
202 /*---------------------------------------------------------------------------*/
203 static int uves_utl_ima_arith_destroy(cpl_plugin * plugin)
204 {
205  cpl_recipe * recipe ;
206 
207  /* Get the recipe out of the plugin */
208  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
209  recipe = (cpl_recipe *)plugin ;
210  else return -1 ;
211 
212  cpl_parameterlist_delete(recipe->parameters) ;
213  return 0 ;
214 }
215 
216 /*---------------------------------------------------------------------------*/
223 /*---------------------------------------------------------------------------*/
224 static int
225 uves_utl_ima_arith( cpl_parameterlist * parlist,
226  cpl_frameset * framelist)
227 {
228  cpl_parameter * param= NULL ;
229  const char * operation=NULL;
230  double value=1 ;
231  cpl_frame * frm_ima1=NULL ;
232  cpl_frame * frm_ima2=NULL ;
233  cpl_image * ima1=NULL ;
234  cpl_image * ima2=NULL ;
235  int switch_ima2 = 0;
236  const char * name_o=NULL ;
237  cpl_propertylist * plist=NULL ;
238  cpl_image * image=NULL ;
239  cpl_frame * product_frame=NULL;
240  cpl_frameset * raw_set=NULL;
241  int nraw=0;
242  int n=0;
243  uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
244  UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
245 
246  /* HOW TO RETRIEVE INPUT PARAMETERS */
247  /* --stropt */
248  check_nomsg(param=cpl_parameterlist_find(parlist,
249  "uves.uves_utl_ima_arith.op"));
250  check_nomsg(operation=cpl_parameter_get_string(param));
251 
252  /* --boolopt */
253  check_nomsg(param=cpl_parameterlist_find(parlist,
254  "uves.uves_utl_ima_arith.value"));
255  check_nomsg(value = cpl_parameter_get_double(param)) ;
256 
257  /* Identify the RAW and CALIB frames in the input frameset */
258  check(uves_dfs_set_groups(framelist),
259  "Cannot identify RAW and CALIB frames") ;
260 
261  /* HOW TO ACCESS INPUT DATA */
262  n=cpl_frameset_get_size(framelist);
263  if(n<1) {
264  uves_msg_error("Empty input frame list!");
265  goto cleanup ;
266  }
267 
268  /* HOW TO ACCESS INPUT DATA */
269  check_nomsg(raw_set=cpl_frameset_new());
270 
271  check(uves_contains_frames_kind(framelist,raw_set,RAW_IMA),
272  "Found no input frames with tag %s",RAW_IMA);
273  check_nomsg(nraw=cpl_frameset_get_size(raw_set));
274  if (nraw<1) {
275  uves_msg_error("Found no input frames with tag %s",RAW_IMA);
276  goto cleanup;
277  } else {
278  check_nomsg(frm_ima1=cpl_frameset_get_frame(framelist,0));
279  check_nomsg(ima1=cpl_image_load(cpl_frame_get_filename(frm_ima1),
280  CPL_TYPE_FLOAT,0,0));
281  if (nraw>1) {
282  check_nomsg(frm_ima2=cpl_frameset_get_frame(framelist,1));
283  check_nomsg(ima2 = cpl_image_load(cpl_frame_get_filename(frm_ima2),
284  CPL_TYPE_FLOAT,0,0));
285  switch_ima2=1;
286  } else if (value == 9999.) {
287  uves_msg_error("Found only one input frames with tag %s",RAW_IMA);
288  goto cleanup;
289  } else {
290  uves_msg("Perform image arithmetics on frame %s",RAW_IMA);
291  }
292  }
293 
294  uves_free_frameset(&raw_set);
295 
296  /* HOW TO GET THE VALUE OF A FITS KEYWORD */
297  check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_ima1),0),
298  "Cannot read the FITS header") ;
299 
300  /* Now performing the data reduction */
301  /* Let's generate one image for the example */
302  if (value == 9999.) {
303 
304  if(ima1 != NULL && ima2 != NULL) {
305  uves_msg("ima1 %s ima2",operation);
306  if (strcmp(operation,"+") == 0 ) {
307  check(image = cpl_image_add_create(ima1, ima2),
308  "Cannot generate the %s image",operation) ;
309  } else if (strcmp(operation,"-") == 0 ) {
310  check(image = cpl_image_subtract_create(ima1, ima2),
311  "Cannot generate the %s image",operation) ;
312  } else if (strcmp(operation,"*") == 0 ) {
313  check(image = cpl_image_multiply_create(ima1, ima2),
314  "Cannot generate the %s image",operation) ;
315  } else if (strcmp(operation,"/") == 0 ) {
316  check(image = cpl_image_divide_create(ima1, ima2),
317  "Cannot generate the %s image",operation) ;
318  } else {
319  uves_msg_error("Operation %s not supported",operation);
320  goto cleanup;
321  }
322  uves_free_image(&ima1);
323  uves_free_image(&ima2);
324 
325  }
326 
327  } else {
328  uves_msg("ima1 %s %f",operation,value);
329 
330  if(switch_ima2 == 1) {
331  uves_free_image(&ima2);
332  }
333 
334  if (strcmp(operation,"+") == 0 ) {
335  check(image = cpl_image_add_scalar_create(ima1, value),
336  "Cannot apply the %s operator",operation) ;
337  } else if (strcmp(operation,"-") == 0 ) {
338  check(image = cpl_image_subtract_scalar_create(ima1, value),
339  "Cannot apply the %s operator",operation) ;
340  } else if (strcmp(operation,"*") == 0 ) {
341  check(image = cpl_image_multiply_scalar_create(ima1, value),
342  "Cannot apply the %s operator",operation) ;
343  } else if (strcmp(operation,"/") == 0 ) {
344  check(image = cpl_image_divide_scalar_create(ima1, value),
345  "Cannot apply the %s operator",operation) ;
346  } else {
347  uves_msg_error("Operation %s not supported",operation);
348  goto cleanup;
349  }
350 
351  uves_free_image(&ima1);
352 
353  }
354 
355 
356  /* HOW TO SAVE A PRODUCT ON DISK */
357  /* Set the file name */
358  name_o = "ima_res.fits" ;
359 
360  /* Create product frame */
361  check_nomsg(product_frame = cpl_frame_new());
362  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
363  check_nomsg(cpl_frame_set_tag(product_frame, RAW_IMA)) ;
364  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
365  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
366  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
367  "Error while initialising the product frame") ;
368 
369  /* Add DataFlow keywords */
370  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
371 
372  check(cpl_dfs_setup_product_header(plist,
373  product_frame,
374  framelist,
375  parlist,
376  "uves_utl_ima_arith",
377  "UVES",
378  KEY_VALUE_HPRO_DID,NULL),
379  "Problem in the product DFS-compliance") ;
380 
381  /* Save the file */
382  check(cpl_image_save(image,
383  name_o,
384  CPL_BPP_IEEE_FLOAT,
385  plist,
386  CPL_IO_DEFAULT),
387  "Could not save product");
388 
389  if (plist != NULL) {
390  cpl_propertylist_delete(plist) ;
391  plist=NULL;
392  }
393 
394  uves_free_image(&image);
395 
396  /* Log the saved file in the input frameset */
397  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
398 
399 
400  cleanup:
401 
402  uves_free_image(&ima1);
403  uves_free_image(&ima2);
404  uves_free_frameset(&raw_set);
405  if (plist != NULL) {
406  cpl_propertylist_delete(plist) ;
407  plist=NULL;
408  }
409  /* This is usually freed by esorex: but what about if errors occurs?
410  uves_free_frame(&product_frame) ;
411  */
412  uves_free_image(&image) ;
413 
414  if (cpl_error_get_code()) {
415  return -1 ;
416  } else {
417  return 0 ;
418  }
419 
420 }
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
#define uves_msg_error(...)
Print an error message.
Definition: uves_msg.h:64
static int uves_utl_ima_arith_create(cpl_plugin *)
Setup the recipe options.
void irplib_reset(void)
Reset IRPLIB state.
#define check_nomsg(CMD)
Definition: uves_error.h:204
const char * uves_get_license(void)
Get the pipeline copyright and license.
Definition: uves_utils.c:1676
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
Definition: uves_msg.h:119
static int uves_utl_ima_arith_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
static int uves_utl_ima_arith_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
static int uves_utl_ima_arith(cpl_parameterlist *, cpl_frameset *)
Get the command line options and execute the data reduction.
#define check(CMD,...)
Definition: uves_error.h:198