SINFONI Pipeline Reference Manual  2.6.0
sinfo_utl_ima_arith.c
1 /* $Id: sinfo_utl_ima_arith.c,v 1.14 2009-01-30 14:56:12 amodigli Exp $
2  *
3  * This file is part of the SINFONI 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-01-30 14:56:12 $
24  * $Revision: 1.14 $
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 <sinfo_tpl_utils.h>
44 #include <sinfo_pfits.h>
45 #include <sinfo_tpl_dfs.h>
46 #include <sinfo_key_names.h>
47 #include <sinfo_pro_types.h>
48 #include <sinfo_functions.h>
49 #include <sinfo_msg.h>
50 #include <sinfo_error.h>
51 #include <sinfo_utils_wrappers.h>
52 
53 /*-----------------------------------------------------------------------------
54  Functions prototypes
55  ----------------------------------------------------------------------------*/
56 
57 static int sinfo_utl_ima_arith_create(cpl_plugin *) ;
58 static int sinfo_utl_ima_arith_exec(cpl_plugin *) ;
59 static int sinfo_utl_ima_arith_destroy(cpl_plugin *) ;
60 static int sinfo_utl_ima_arith(cpl_parameterlist *, cpl_frameset *) ;
61 
62 /*-----------------------------------------------------------------------------
63  Static variables
64  ----------------------------------------------------------------------------*/
65 
66 static char sinfo_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 IMA.\n"
70  "The output is an image resulting from the IMA op IMA where op indicates\n"
71  "the operation to be performed specified by the parameter \n"
72  "sinfoni.sinfo_utl_ima_arith.op having alias 'op'\n"
73  "\n";
74 
75 /*-----------------------------------------------------------------------------
76  Functions code
77  ----------------------------------------------------------------------------*/
78 /*---------------------------------------------------------------------------*/
82 /*---------------------------------------------------------------------------*/
83 
85 /*---------------------------------------------------------------------------*/
93 /*---------------------------------------------------------------------------*/
94 int cpl_plugin_get_info(cpl_pluginlist * list)
95 {
96  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
97  cpl_plugin * plugin = &recipe->interface ;
98 
99  cpl_plugin_init(plugin,
100  CPL_PLUGIN_API,
101  SINFONI_BINARY_VERSION,
102  CPL_PLUGIN_TYPE_RECIPE,
103  "sinfo_utl_ima_arith",
104  "Computes result of ima1 op ima2",
105  sinfo_utl_ima_arith_description,
106  "Andrea Modigliani",
107  "Andrea.Modigliani@eso.org",
108  sinfo_get_license(),
109  sinfo_utl_ima_arith_create,
110  sinfo_utl_ima_arith_exec,
111  sinfo_utl_ima_arith_destroy) ;
112 
113  cpl_pluginlist_append(list, plugin) ;
114 
115  return 0;
116 }
117 
118 /*---------------------------------------------------------------------------*/
127 /*---------------------------------------------------------------------------*/
128 static int sinfo_utl_ima_arith_create(cpl_plugin * plugin)
129 {
130  cpl_recipe * recipe ;
131  cpl_parameter * p ;
132 
133  /* Get the recipe out of the plugin */
134  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
135  recipe = (cpl_recipe *)plugin ;
136  else return -1 ;
137  cpl_error_reset();
138  irplib_reset();
139 
140  /* Create the parameters list in the cpl_recipe object */
141  recipe->parameters = cpl_parameterlist_new() ;
142 
143  /* Fill the parameters list */
144  /* --stropt */
145  p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_arith.op",
146  CPL_TYPE_STRING,
147  "A possible operation",
148  "sinfoni.sinfo_utl_ima_arith","+");
149  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
150  cpl_parameterlist_append(recipe->parameters, p) ;
151 
152  /* --doubleopt */
153  p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_arith.value",
154  CPL_TYPE_DOUBLE, "a value", "sinfoni.sinfo_utl_ima_arith", 9999.) ;
155  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "value") ;
156  cpl_parameterlist_append(recipe->parameters, p) ;
157 
158  /* Return */
159  return 0;
160 }
161 
162 /*---------------------------------------------------------------------------*/
168 /*---------------------------------------------------------------------------*/
169 static int sinfo_utl_ima_arith_exec(cpl_plugin * plugin)
170 {
171  cpl_recipe * recipe ;
172  int code=0;
173  cpl_errorstate initial_errorstate = cpl_errorstate_get();
174 
175  /* Get the recipe out of the plugin */
176  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
177  recipe = (cpl_recipe *)plugin ;
178  else return -1 ;
179  cpl_error_reset();
180  irplib_reset();
181  code = sinfo_utl_ima_arith(recipe->parameters, recipe->frames) ;
182 
183 
184  if (!cpl_errorstate_is_equal(initial_errorstate)) {
185  /* Dump the error history since recipe execution start.
186  At this point the recipe cannot recover from the error */
187  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
188  }
189 
190  return code ;
191 }
192 
193 /*---------------------------------------------------------------------------*/
199 /*---------------------------------------------------------------------------*/
200 static int sinfo_utl_ima_arith_destroy(cpl_plugin * plugin)
201 {
202  cpl_recipe * recipe ;
203 
204  /* Get the recipe out of the plugin */
205  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
206  recipe = (cpl_recipe *)plugin ;
207  else return -1 ;
208 
209  cpl_parameterlist_delete(recipe->parameters) ;
210  return 0 ;
211 }
212 
213 /*---------------------------------------------------------------------------*/
220 /*---------------------------------------------------------------------------*/
221 static int
222 sinfo_utl_ima_arith( cpl_parameterlist * parlist,
223  cpl_frameset * framelist)
224 {
225  cpl_parameter * param= NULL ;
226  const char * operation=NULL;
227  double value=1 ;
228  cpl_frame * frm_ima1=NULL ;
229  cpl_frame * frm_ima2=NULL ;
230  cpl_image * ima1=NULL ;
231  cpl_image * ima2=NULL ;
232  int switch_ima2 = 0;
233  const char * name_o=NULL ;
234  cpl_propertylist * plist=NULL ;
235  cpl_image * image=NULL ;
236  cpl_frame * product_frame=NULL;
237  cpl_frameset * raw_set=NULL;
238  int nraw=0;
239  int n=0;
240  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
241  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
242 
243  /* HOW TO RETRIEVE INPUT PARAMETERS */
244  /* --stropt */
245  check_nomsg(param=cpl_parameterlist_find(parlist,
246  "sinfoni.sinfo_utl_ima_arith.op"));
247  check_nomsg(operation=cpl_parameter_get_string(param));
248 
249  /* --boolopt */
250  check_nomsg(param=cpl_parameterlist_find(parlist,
251  "sinfoni.sinfo_utl_ima_arith.value"));
252  check_nomsg(value = cpl_parameter_get_double(param)) ;
253 
254  /* Identify the RAW and CALIB frames in the input frameset */
255  check(sinfo_dfs_set_groups(framelist),
256  "Cannot identify RAW and CALIB frames") ;
257 
258  /* HOW TO ACCESS INPUT DATA */
259  n=cpl_frameset_get_size(framelist);
260  if(n<1) {
261  sinfo_msg_error("Empty input frame list!");
262  goto cleanup ;
263  }
264 
265  /* HOW TO ACCESS INPUT DATA */
266  check_nomsg(raw_set=cpl_frameset_new());
267 
268  check(sinfo_contains_frames_kind(framelist,raw_set,PRO_IMA),
269  "Found no input frames with tag %s",PRO_IMA);
270  check_nomsg(nraw=cpl_frameset_get_size(raw_set));
271  if (nraw<1) {
272  sinfo_msg_error("Found no input frames with tag %s",PRO_IMA);
273  goto cleanup;
274  } else {
275  check_nomsg(frm_ima1=cpl_frameset_get_frame(framelist,0));
276  check_nomsg(ima1=cpl_image_load(cpl_frame_get_filename(frm_ima1),
277  CPL_TYPE_FLOAT,0,0));
278  if (nraw>1) {
279  check_nomsg(frm_ima2=cpl_frameset_get_frame(framelist,1));
280  check_nomsg(ima2 = cpl_image_load(cpl_frame_get_filename(frm_ima2),
281  CPL_TYPE_FLOAT,0,0));
282  switch_ima2=1;
283  } else if (value == 9999.) {
284  sinfo_msg_error("Found only one input frames with tag %s",PRO_IMA);
285  goto cleanup;
286  } else {
287  sinfo_msg("Perform image arithmetics on frame %s",PRO_IMA);
288  }
289  }
290 
291  sinfo_free_frameset(&raw_set);
292 
293  /* HOW TO GET THE VALUE OF A FITS KEYWORD */
294  check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_ima1),0),
295  "Cannot read the FITS header") ;
296 
297  /* Now performing the data reduction */
298  /* Let's generate one image for the example */
299  if (value == 9999.) {
300 
301  if(ima1 != NULL && ima2 != NULL) {
302  sinfo_msg("ima1 %s ima2",operation);
303  if (strcmp(operation,"+") == 0 ) {
304  check(image = cpl_image_add_create(ima1, ima2),
305  "Cannot generate the %s image",operation) ;
306  } else if (strcmp(operation,"-") == 0 ) {
307  check(image = cpl_image_subtract_create(ima1, ima2),
308  "Cannot generate the %s image",operation) ;
309  } else if (strcmp(operation,"*") == 0 ) {
310  check(image = cpl_image_multiply_create(ima1, ima2),
311  "Cannot generate the %s image",operation) ;
312  } else if (strcmp(operation,"/") == 0 ) {
313  check(image = cpl_image_divide_create(ima1, ima2),
314  "Cannot generate the %s image",operation) ;
315  } else {
316  sinfo_msg_error("Operation %s not supported",operation);
317  goto cleanup;
318  }
319  sinfo_free_image(&ima1);
320  sinfo_free_image(&ima2);
321 
322  }
323 
324  } else {
325  sinfo_msg("ima1 %s %f",operation,value);
326 
327  if(switch_ima2 == 1) {
328  sinfo_free_image(&ima2);
329  }
330 
331  if (strcmp(operation,"+") == 0 ) {
332  check(image = cpl_image_add_scalar_create(ima1, value),
333  "Cannot apply the %s operator",operation) ;
334  } else if (strcmp(operation,"-") == 0 ) {
335  check(image = cpl_image_subtract_scalar_create(ima1, value),
336  "Cannot apply the %s operator",operation) ;
337  } else if (strcmp(operation,"*") == 0 ) {
338  check(image = cpl_image_multiply_scalar_create(ima1, value),
339  "Cannot apply the %s operator",operation) ;
340  } else if (strcmp(operation,"/") == 0 ) {
341  check(image = cpl_image_divide_scalar_create(ima1, value),
342  "Cannot apply the %s operator",operation) ;
343  } else {
344  sinfo_msg_error("Operation %s not supported",operation);
345  goto cleanup;
346  }
347 
348  sinfo_free_image(&ima1);
349 
350  }
351 
352 
353  /* HOW TO SAVE A PRODUCT ON DISK */
354  /* Set the file name */
355  name_o = "ima_res.fits" ;
356 
357  /* Create product frame */
358  check_nomsg(product_frame = cpl_frame_new());
359  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
360  check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_IMA_ARITH_PROIMA)) ;
361  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
362  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
363  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
364  "Error while initialising the product frame") ;
365 
366  /* Add DataFlow keywords */
367  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
368  check(cpl_dfs_setup_product_header(plist,
369  product_frame,
370  framelist,
371  parlist,
372  "sinfo_utl_ima_arith",
373  "SINFONI",
374  KEY_VALUE_HPRO_DID,NULL),
375  "Problem in the product DFS-compliance") ;
376 
377  /* Save the file */
378  check(cpl_image_save(image,
379  name_o,
380  CPL_BPP_IEEE_FLOAT,
381  plist,
382  CPL_IO_DEFAULT),
383  "Could not save product");
384  sinfo_free_propertylist(&plist) ;
385  sinfo_free_image(&image);
386 
387  /* Log the saved file in the input frameset */
388  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
389 
390 
391  cleanup:
392 
393  sinfo_free_image(&ima1);
394  sinfo_free_image(&ima2);
395  sinfo_free_frameset(&raw_set);
396  sinfo_free_propertylist(&plist) ;
397  /* This is usually freed by esorex: but what about if errors occurs?
398  sinfo_free_frame(&product_frame) ;
399  */
400  sinfo_free_image(&image) ;
401 
402  if (cpl_error_get_code()) {
403  return -1 ;
404  } else {
405  return 0 ;
406  }
407 
408 }
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
void irplib_reset(void)
Reset IRPLIB state.
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69