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