SINFONI Pipeline Reference Manual  2.5.2
sinfoni/sinfo_utl_cube_arith.c
1 /* $Id: sinfo_utl_cube_arith.c,v 1.14 2012-03-03 10:17:31 amodigli 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: amodigli $
23  * $Date: 2012-03-03 10:17:31 $
24  * $Revision: 1.14 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32 /*----------------------------------------------------------------------------
33  Includes
34  ----------------------------------------------------------------------------*/
35 
36 #include "sinfo_utl_cube_arith.h"
37 #include <sinfo_spectrum_ops.h>
38 #include <sinfo_new_cube_ops.h>
39 #include "sinfo_key_names.h"
40 #include "sinfo_pro_types.h"
41 #include <sinfo_error.h>
42 #include <sinfo_utilities.h>
43 #include <sinfo_utils_wrappers.h>
44 
45 
46 
47 /*-----------------------------------------------------------------------------
48  Functions prototypes
49  ----------------------------------------------------------------------------*/
50 /*-----------------------------------------------------------------------------
51  Static variables
52  ----------------------------------------------------------------------------*/
53 
61 /*-----------------------------------------------------------------------------
62  Functions code
63  ----------------------------------------------------------------------------*/
64 
65 /*---------------------------------------------------------------------------*/
72 /*---------------------------------------------------------------------------*/
73 int sinfo_utl_cube_arith(
74  cpl_parameterlist * parlist,
75  cpl_frameset * framelist)
76 {
77  cpl_parameter * param =NULL;
78  const char * operation =NULL;
79  const char * name_c =NULL;
80  const char * name_s =NULL;
81  const char * name_o =NULL;
82  const char * name_d ="start";
83 
84  /* double temp=0 ; */
85  double value=0 ;
86  double def_value=0 ;
87 
88  cpl_parameter * p=NULL;
89  cpl_frame * frm_cube=NULL ;
90  cpl_frame * frm_spct=NULL ;
91 
92  cpl_propertylist * plist=NULL ;
93  cpl_image * image=NULL ;
94  cpl_frame * product_frame=NULL;
95  cpl_imagelist * cub_ims=NULL;
96  cpl_imagelist * cube_i=NULL;
97  cpl_imagelist * cube_o=NULL;
98  cpl_imagelist * cube_d=NULL;
99  cpl_image * std_star=NULL;
100  Vector * bb=NULL;
101  Vector* spectrum=NULL;
102  cpl_vector* vec=NULL;
103  /* Identify the RAW and CALIB frames in the input frameset */
104  check(sinfo_dfs_set_groups(framelist),
105  "Cannot identify RAW and CALIB frames") ;
106 
107 
108  /* HOW TO RETRIEVE INPUT PARAMETERS */
109  /* --stropt */
110  check_nomsg(param = cpl_parameterlist_find(parlist,
111  "sinfoni.sinfo_utl_cube_arith.op"));
112  check_nomsg(operation = cpl_parameter_get_string(param));
113  name_o = "out_cube.fits";
114 
115 
116  /* --doubleopt */
117  check_nomsg(param = cpl_parameterlist_find(parlist,
118  "sinfoni.sinfo_utl_cube_arith.value"));
119  check_nomsg(value = cpl_parameter_get_double(param));
120 
121  /* HOW TO ACCESS INPUT DATA */
122  check(frm_cube = cpl_frameset_find(framelist, SI_UTL_CUBE_ARITH_CUBE),
123  "SOF does not have a file tagged as %s",SI_UTL_CUBE_ARITH_CUBE);
124 
125  check_nomsg(name_c = cpl_frame_get_filename(frm_cube));
126  check_nomsg(p = cpl_parameterlist_find(parlist,
127  "sinfoni.sinfo_utl_cube_arith.value"));
128  check_nomsg(def_value = cpl_parameter_get_default_double(p));
129  if (value == def_value) {
130  if ((frm_spct = cpl_frameset_find(framelist,
131  SI_UTL_CUBE_ARITH_SPECTRUM))==NULL) {
132  sinfo_msg( "SOF does not have a file tagged as %s",
133  SI_UTL_CUBE_ARITH_SPECTRUM);
134  cpl_error_reset();
135  } else {
136  sinfo_msg( "SOF contains a file tagged as %s",
137  SI_UTL_CUBE_ARITH_SPECTRUM);
138  name_s = cpl_frame_get_filename(frm_spct);
139  name_d = "spectrum";
140  }
141  }
142 
143  if (value == def_value) {
144  if ((frm_spct = cpl_frameset_find(framelist, PRO_SPECTRUM))!=NULL) {
145  sinfo_msg( "SOF contains a file tagged as %s",PRO_SPECTRUM);
146  name_s = cpl_frame_get_filename(frm_spct);
147  name_d = "spectrum";
148  } else if ((frm_spct = cpl_frameset_find(framelist, PRO_IMA))!=NULL) {
149  sinfo_msg( "SOF contains a file tagged as %s",PRO_IMA);
150  name_s = cpl_frame_get_filename(frm_spct);
151  name_d = "image";
152  } else {
153  sinfo_msg( "SOF does not contains a file tagged as %s nor as %s",
154  PRO_SPECTRUM,PRO_IMA);
155  sinfo_msg( "Assume operation by a constant");
156  cpl_error_reset();
157  }
158  }
159  check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cube),0),
160  "Cannot read the FITS header") ;
161 
162 
163  /* Now performing the data reduction */
164  /* Let's generate one image for the example */
165  check_nomsg(cube_i= cpl_imagelist_load((char*)name_c,CPL_TYPE_FLOAT,0));
166  if (value == def_value) {
167  sinfo_msg("value equal to default");
168  if( strcmp(name_d,"spectrum") == 0 ) {
169  sinfo_msg("Operation by spectrum");
170  sinfo_msg("loading spectrum image file %s",name_s);
171  check_nomsg(std_star=cpl_image_load((char*)name_s,CPL_TYPE_FLOAT,0,0));
172 
173  //check_nomsg(std_star=sinfo_vector_to_image(vec,CPL_TYPE_FLOAT));
174  check_nomsg(spectrum = sinfo_new_image_to_vector(std_star));
175  if (strcmp(operation,"/") == 0) {
176  cube_o = sinfo_new_div_cube_by_spectrum (cube_i, spectrum);
177  } else if (strcmp(operation,"-") == 0) {
178  cube_o = sinfo_new_sub_spectrum_from_cube(cube_i, spectrum);
179  } else if (strcmp(operation,"+") == 0) {
180  cube_o = sinfo_new_add_spectrum_to_cube(cube_i, spectrum);
181  } else if (strcmp(operation,"*") == 0) {
182  cube_o = sinfo_new_mul_spectrum_to_cube(cube_i, spectrum);
183  /*
184  } else if (strcmp(operation,"calib") == 0) {
185  bb = sinfo_new_blackbody_spectrum ((char*)name_s, temp);
186  cube_d = sinfo_new_div_cube_by_Spectrum (cube_i, spectrum);
187  cube_o = sinfo_new_mul_spectrum_to_cube(cube_d, bb);
188  */
189 
190  } else {
191  sinfo_msg_error("operation %s not supported",operation);
192  sinfo_msg("If an input spectrum is given and no value is set");
193  sinfo_msg("operations supported are: `/`,`-`,`+`,'*'");
194  goto cleanup;
195  }
196  if (strcmp(operation,"calib") == 0) {
197  goto cleanup;
198  }
199  } else if ( strcmp(name_d,"image") == 0) {
200  sinfo_msg("Operation by image");
201  check_nomsg(std_star=cpl_image_load ((char*)name_s,
202  CPL_TYPE_FLOAT,0,0));
203  if(strcmp(operation,"+") == 0) {
204  cknull(cube_o = sinfo_new_add_image_to_cube(cube_i,std_star),
205  "operation %s failed",operation);
206  } else if (strcmp(operation,"-") == 0) {
207  cknull(cube_o = sinfo_new_sub_image_from_cube(cube_i,std_star),
208  "operation %s failed",operation);
209  } else if (strcmp(operation,"*") == 0) {
210  cknull(cube_o = sinfo_new_mul_image_to_cube(cube_i,std_star),
211  "operation %s failed",operation);
212  } else if (strcmp(operation,"/") == 0) {
213  cknull(cube_o = sinfo_new_div_cube_by_image(cube_i,std_star),
214  "operation %s failed",operation);
215  } else {
216  sinfo_msg_error("operation %s not supported",operation);
217  sinfo_msg("If an input image is set");
218  sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
219  goto cleanup;
220  }
221  } else {
222  sinfo_msg("Operation %s by constant",operation);
223 
224  check_nomsg(cube_o = cpl_imagelist_duplicate(cube_i));
225 
226  if(strcmp(operation,"+") == 0) {
227  check(cpl_imagelist_add_scalar(cube_o,value),
228  "operation %s failed",operation);
229  } else if (strcmp(operation,"-") == 0) {
230  check(cpl_imagelist_subtract_scalar(cube_o,value),
231  "operation %s failed",operation);
232  } else if (strcmp(operation,"*") == 0) {
233  check(cpl_imagelist_multiply_scalar(cube_o,value),
234  "operation %s failed",operation);
235  } else if (strcmp(operation,"/") == 0) {
236  check(cpl_imagelist_divide_scalar(cube_o,value),
237  "operation %s failed",operation);
238  } else {
239  sinfo_msg_error("operation %s not supported",operation);
240  sinfo_msg("If an input value is set");
241  sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
242  goto cleanup;
243  }
244 
245  } /* env case op by constant && value == default */
246 
247  } else {
248 
249  sinfo_msg("Operation %s by constant",operation);
250 
251  check_nomsg(cube_o = cpl_imagelist_duplicate(cube_i));
252 
253  if(strcmp(operation,"+") == 0) {
254  check(cpl_imagelist_add_scalar(cube_o,value),
255  "operation %s failed",operation);
256  } else if (strcmp(operation,"-") == 0) {
257  check(cpl_imagelist_subtract_scalar(cube_o,value),
258  "operation %s failed",operation);
259  } else if (strcmp(operation,"*") == 0) {
260  check(cpl_imagelist_multiply_scalar(cube_o,value),
261  "operation %s failed",operation);
262  } else if (strcmp(operation,"/") == 0) {
263  check(cpl_imagelist_divide_scalar(cube_o,value),
264  "operation %s failed",operation);
265  } else {
266  sinfo_msg_error("operation %s not supported",operation);
267  sinfo_msg("If an input value is set");
268  sinfo_msg("operations supported are: `-`, `+`, `*`, `/`");
269  goto cleanup;
270  }
271 
272  }
273  /* HOW TO SAVE A PRODUCT ON DISK */
274  /* Set the file name */
275  /* Create product frame */
276  check_nomsg(product_frame = cpl_frame_new());
277  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
278  check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE_ARITH_PROCUBE)) ;
279  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
280  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
281  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
282  "Error while initialising the product frame") ;
283  /* Add DataFlow keywords */
284  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
285 
286  check(cpl_dfs_setup_product_header(plist, product_frame, framelist,
287  parlist,
288  "si_sinfo_utl_cube_arith",
289  "SINFONI", KEY_VALUE_HPRO_DID,NULL),
290  "Problem in the product DFS-compliance") ;
291 
292  /* Save the file */
293  check(cpl_imagelist_save(cube_o, name_o, CPL_BPP_IEEE_FLOAT, plist,
294  CPL_IO_DEFAULT),
295  "Could not save product");
296 
297 
298  /* Log the saved file in the input frameset */
299  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
300 
301 
302 
303  cleanup:
304  sinfoni_free_vector(&vec);
305  sinfo_free_svector(&spectrum);
306  sinfo_free_svector(&bb);
307 
308  sinfo_free_imagelist(&cube_d);
309  sinfo_free_imagelist(&cube_i);
310  sinfo_free_imagelist(&cube_o);
311  //If I free the next image I get core dump!
312  //sinfo_free_image(&std_star);
313  sinfo_free_image(&image);
314  sinfo_free_imagelist(&cub_ims);
315  sinfo_free_propertylist(&plist);
316 
317  /* Return */
318  if (cpl_error_get_code())
319  return -1 ;
320  else
321  return 0 ;
322 }