SINFONI Pipeline Reference Manual  2.5.2
sinfoni/sinfo_utl_cube2ima.c
1 /* $Id: sinfo_utl_cube2ima.c,v 1.8 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.8 $
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 "sinfo_functions.h"
37 #include "sinfo_new_cube_ops.h"
38 #include "sinfo_utl_cube2ima.h"
39 #include "sinfo_key_names.h"
40 #include <sinfo_error.h>
41 #include <sinfo_utils_wrappers.h>
42 
43 /*-----------------------------------------------------------------------------
44  Functions prototypes
45  -----------------------------------------------------------------------------*/
46 
47 
48 
49 /*-----------------------------------------------------------------------------
50  Static variables
51  -----------------------------------------------------------------------------*/
52 
53 /*-----------------------------------------------------------------------------
54  Functions code
55  -----------------------------------------------------------------------------*/
56 
64 /*----------------------------------------------------------------------------*/
71 /*----------------------------------------------------------------------------*/
72 int sinfo_utl_cube2ima(
73  cpl_parameterlist * parlist,
74  cpl_frameset * framelist)
75 {
76  cpl_parameter * param =NULL ;
77  const char * name_i =NULL;
78  const char * name_o =NULL;
79 
80  double ws=0 ;
81  double we=0 ;
82  double wc=0 ;
83  double wd=0 ;
84 
85 
86  int naxis3=0;
87  double lams=0;
88  double lame=0;
89 
90  cpl_frame * frm_cub=NULL ;
91 
92  cpl_propertylist * plist=NULL ;
93  cpl_frame * product_frame=NULL;
94 
95  cpl_imagelist * cube=NULL;
96  cpl_image * img=NULL;
97 
98 
99  /* HOW TO RETRIEVE INPUT PARAMETERS */
100  /* --stropt */
101  name_o ="out_ima.fits";
102 
103  /* --doubleopt */
104  check_nomsg(param = cpl_parameterlist_find(parlist,
105  "sinfoni.sinfo_utl_cube2ima.ws"));
106  check_nomsg(ws = cpl_parameter_get_double(param)) ;
107 
108  /* --doubleopt */
109  check_nomsg(param = cpl_parameterlist_find(parlist,
110  "sinfoni.sinfo_utl_cube2ima.we"));
111  check_nomsg(we = cpl_parameter_get_double(param)) ;
112 
113  /* Identify the RAW and CALIB frames in the input frameset */
114  check(sinfo_dfs_set_groups(framelist),
115  "Cannot identify RAW and CALIB frames");
116 
117  /* HOW TO ACCESS INPUT DATA */
118  check(frm_cub = cpl_frameset_find(framelist, SI_UTL_CUBE2IMA_CUBE),
119  "SOF does not have a file tagged as %s",SI_UTL_CUBE2IMA_CUBE);
120 
121  check_nomsg(name_i = cpl_frame_get_filename(frm_cub));
122 
123  plist=cpl_propertylist_load(name_i,0);
124  naxis3=sinfo_pfits_get_naxis3(plist);
125  wd=sinfo_pfits_get_cdelt3(plist);
126  wc=sinfo_pfits_get_crval3(plist);
127  sinfo_free_propertylist(&plist);
128 
129  lams=wc-naxis3*wd/2+wd;
130  lame=lams+(naxis3-2)*wd;
131 
132  check_nomsg(param = cpl_parameterlist_find(parlist,
133  "sinfoni.sinfo_utl_cube2ima.ws"));
134 
135  if(ws != cpl_parameter_get_default_double(param)) {
136  if (ws < lams) {
137  sinfo_msg_warning("ws =%f too low set it to %f",ws,lams);
138  ws=lams;
139  }
140  else if (ws >= lame) {
141  sinfo_msg_warning("ws =%f too high set it to %f",ws,lams);
142  ws=lams;
143  }
144  } else {
145  ws=lams;
146  }
147 
148  check_nomsg(param = cpl_parameterlist_find(parlist,
149  "sinfoni.sinfo_utl_cube2ima.we"));
150  if(we != cpl_parameter_get_default_double(param)) {
151  if (we > lame) {
152  sinfo_msg_warning("we =%f too low set it to %f",we,lame);
153  we=lame;
154  }
155  else if (we <= lams) {
156  sinfo_msg_warning("we =%f too low set it to %f",we,lame);
157  we=lame;
158  }
159  } else {
160  we=lame;
161  }
162 
163  check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm_cub),0),
164  "Cannot read the FITS header") ;
165 
166  /* recipe's algorithm */
167  check_nomsg(cube = cpl_imagelist_load((char*)name_i,CPL_TYPE_FLOAT,0));
168  if(NULL==(img=sinfo_new_average_cube_to_image_between_waves(cube,wd,wc,
169  ws,we))) {
170  sinfo_msg_error("Cannot average cube to image in wavelength "
171  "range [%f,%f] um",ws,we) ;
172 
173  sinfo_free_imagelist(&cube);
174  sinfo_free_propertylist(&plist);
175 
176  }
177  sinfo_free_imagelist(&cube);
178 
179  /* HOW TO SAVE A PRODUCT ON DISK */
180  /* Create product frame */
181  check_nomsg(product_frame = cpl_frame_new());
182  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
183  check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE2IMA_PROIMA)) ;
184  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
185  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
186  check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
187  "Error while initialising the product frame") ;
188 
189 
190  /* Add DataFlow keywords */
191  check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
192  check(cpl_dfs_setup_product_header(plist,
193  product_frame,
194  framelist,
195  parlist,
196  "si_sinfo_utl_cube2ima",
197  "SINFONI",
198  KEY_VALUE_HPRO_DID,NULL),
199  "Problem in the product DFS-compliance") ;
200 
201  /* Save the file */
202  check_nomsg(cpl_propertylist_erase_regexp(plist, "^CTYPE3",0));
203  check_nomsg(cpl_propertylist_erase_regexp(plist, "^CRPIX3",0));
204  check_nomsg(cpl_propertylist_erase_regexp(plist, "^CRVAL3",0));
205  check_nomsg(cpl_propertylist_erase_regexp(plist, "^CDELT3",0));
206  check_nomsg(cpl_propertylist_erase_regexp(plist, "^CUNIT3",0));
207 
208  check(cpl_image_save(img,
209  name_o,
210  CPL_BPP_IEEE_FLOAT,
211  plist,
212  CPL_IO_DEFAULT),
213  "Could not save product");
214 
215  check_nomsg(sinfo_free_propertylist(&plist)) ;
216 
217  /* Log the saved file in the input frameset */
218  check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
219  check_nomsg(sinfo_free_image(&img)) ;
220 
221  /* Return */
222 
223  cleanup:
224  sinfo_free_imagelist(&cube);
225  sinfo_free_propertylist(&plist);
226  sinfo_free_image(&img);
227 
228  if (cpl_error_get_code())
229  return -1 ;
230  else
231  return 0 ;
232 }