SINFONI Pipeline Reference Manual  2.5.2
sinfoni/sinfo_utl_cube_combine.c
1 /* $Id: sinfo_utl_cube_combine.c,v 1.20 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.20 $
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_utl_cube_combine.h"
37 #include "sinfo_utilities.h"
38 #include "sinfo_utilities_scired.h"
39 #include <stdio.h>
40 #include "sinfo_key_names.h"
41 #include "sinfo_error.h"
42 #include "sinfo_utils_wrappers.h"
43 #include "sinfo_pro_save.h"
44 /*-----------------------------------------------------------------------------
45  Functions prototypes
46  ----------------------------------------------------------------------------*/
47 
48 
49 /*-----------------------------------------------------------------------------
50  Static variables
51  ----------------------------------------------------------------------------*/
52 
53 
54 /*-----------------------------------------------------------------------------
55  Functions code
56  ----------------------------------------------------------------------------*/
66 /*---------------------------------------------------------------------------*/
73 /*---------------------------------------------------------------------------*/
74 int sinfo_utl_cube_combine(
75  cpl_parameterlist * parlist,
76  cpl_frameset * framelist)
77 {
78  cpl_parameter * param=NULL ;
79  const char * name_o=NULL ;
80  const char * name_i=NULL ;
81  const char * name_m=NULL ;
82 
83  cpl_propertylist * plist=NULL ;
84  cpl_image * image=NULL ;
85  cpl_frame * product_frame=NULL;
86  cpl_frame * prod_frm=NULL;
87  cpl_frame * frame=NULL;
88 
89  int z=0;
90  int z_min=0;
91  int z_max=0;
92  int z_siz=0;
93  int z_stp=100;
94  /* not used later
95  int xsize=0;
96  int ysize=0;
97  */
98  int scales_sky=0;
99 
100  int i=0;
101  int j=0;
102  int n=0;
103 
104  int size_x=0;
105  int size_y=0;
106  cpl_image* j_img=NULL;
107  cpl_image* m_img=NULL;
108 
109  cpl_imagelist ** cube_object=NULL;
110  cpl_imagelist * cube_jitter=NULL;
111  cpl_imagelist * cube_mask=NULL;
112 
113  const char** files=NULL;
114  int nframes=0;
115 
116  double * times=NULL;
117  float * offsetx=NULL;
118  float * offsety=NULL;
119  float ref_offx=0;
120  float ref_offy=0;
121  float tmpoffx=0;
122  float tmpoffy=0;
123  double kappa=2;
124  int ks_clip=0;
125  int min_size_x=9999;
126  int min_size_y=9999;
127  const char* name=NULL;
128 
129 
130  FILE* file_list=NULL;
131  int cnt=0;
132  int onp=0;
133 
134  /* HOW TO RETRIEVE INPUT PARAMETERS */
135  /* --stropt */
136  check_nomsg(param = cpl_parameterlist_find(parlist,
137  "sinfoni.sinfo_utl_cube_combine.ks_clip"));
138  check_nomsg(ks_clip = cpl_parameter_get_bool(param));
139 
140  check_nomsg(param = cpl_parameterlist_find(parlist,
141  "sinfoni.sinfo_utl_cube_combine.scale_sky"));
142  check_nomsg(scales_sky = cpl_parameter_get_bool(param));
143 
144 
145  check_nomsg(param = cpl_parameterlist_find(parlist,
146  "sinfoni.sinfo_utl_cube_combine.kappa"));
147  check_nomsg(kappa = cpl_parameter_get_double(param));
148 
149  check_nomsg(param = cpl_parameterlist_find(parlist,
150  "sinfoni.sinfo_utl_cube_combine.name_i"));
151  check_nomsg(name_i = cpl_parameter_get_string(param));
152 
153  check_nomsg(param = cpl_parameterlist_find(parlist,
154  "sinfoni.sinfo_utl_cube_combine.name_o"));
155  check_nomsg(name_o = cpl_parameter_get_string(param));
156 
157 
158  /* not used later
159  check_nomsg(param = cpl_parameterlist_find(parlist,
160  "sinfoni.sinfo_utl_cube_combine.xsize"));
161  check_nomsg(xsize = cpl_parameter_get_int(param));
162 
163  check_nomsg(param = cpl_parameterlist_find(parlist,
164  "sinfoni.sinfo_utl_cube_combine.ysize"));
165  check_nomsg(ysize = cpl_parameter_get_int(param));
166  */
167 
168  /* Identify the RAW and CALIB frames in the input frameset */
169  check(sinfo_dfs_set_groups(framelist),
170  "Cannot identify RAW and CALIB frames") ;
171 
172 
173  /* HOW TO ACCESS INPUT DATA */
174  n=cpl_frameset_get_size(framelist);
175  if(n<1) {
176  sinfo_msg_error("Empty input frame list!");
177  goto cleanup ;
178  }
179  for (i=0;i<n;i++) {
180  frame=cpl_frameset_get_frame(framelist,i);
181  cpl_frame_set_group(frame,CPL_FRAME_GROUP_RAW);
182  }
183 
184 
185 
186  /* Now performing the data reduction */
187  /* Let's generate one image for the example */
188 
189  if ( NULL == (file_list = fopen (name_i, "r" ) ) )
190  {
191  sinfo_msg_error("cannot open %s\n", name_i) ;
192  goto cleanup ;
193  }
194 
195  cnt = 0 ;
196  while ( fscanf( file_list, "%f %f",&tmpoffx, &tmpoffy) != EOF )
197  {
198  cnt ++ ;
199  }
200  fclose(file_list);
201 
202  nframes= cnt ;
203  cknull(times = (double*) cpl_calloc (nframes, sizeof (double)),
204  " could not allocate memory!") ;
205 
206  cknull(offsetx = (float*) cpl_calloc (nframes, sizeof(float)),
207  " could not allocate memory!") ;
208 
209  cknull(offsety = (float*) cpl_calloc (nframes, sizeof(float)),
210  " could not allocate memory!") ;
211 
212  files = (const char**) cpl_calloc(MAX_NAME_SIZE,sizeof(const char*));
213  if ( NULL == (file_list = fopen (name_i, "r" ) ) )
214  {
215  sinfo_msg_error("cannot open %s\n", name_i) ;
216  return -1 ;
217  }
218 
219 
220  cnt=0;
221  while ( fscanf( file_list, "%f %f",&tmpoffx,&tmpoffy ) != EOF ) {
222  frame=cpl_frameset_get_frame(framelist,cnt);
223  files[cnt]= cpl_frame_get_filename(frame);
224  times[cnt]=sinfo_pfits_get_exptime(files[cnt]);
225  offsetx[cnt]=tmpoffx;
226  offsety[cnt]=tmpoffy;
227  plist=cpl_propertylist_load(files[cnt],0);
228  size_x=sinfo_pfits_get_naxis1(plist);
229  size_y=sinfo_pfits_get_naxis2(plist);
230  sinfo_free_propertylist(&plist);
231  if(size_x < min_size_x) min_size_x=size_x;
232  if(size_y < min_size_y) min_size_y=size_y;
233  cnt ++ ;
234  }
235  sinfo_msg("Min input cube size x=%d y=%d",min_size_x,min_size_y);
236 
237  nframes=cnt;
238  fclose(file_list);
239 
240  ck0(sinfo_auto_size_cube(offsetx,offsety,nframes,
241  &ref_offx,&ref_offy,&size_x,&size_y),
242  "Error resizing cube");
243  sinfo_msg("output ima size=%dx%d",size_x,size_y);
244  cknull(cube_object = cpl_calloc(nframes,sizeof(cpl_imagelist*)),
245  "could not allocate memory") ;
246 
247  for (i=0;i<nframes;i++) {
248  frame=cpl_frameset_get_frame(framelist,i);
249  name=cpl_frame_get_filename(frame);
250  check_nomsg(cube_object[i]=cpl_imagelist_load(name,CPL_TYPE_FLOAT,0));
251  ck0(sinfo_new_assign_offset2(i,name,offsetx,offsety,
252  ref_offx,ref_offy),
253  "Error assigning offsets");
254  }
255  onp = cpl_imagelist_get_size(cube_object[0]);
256 
257 
258  check(cube_jitter = cpl_imagelist_new(),"allocating new data cube object");
259  check(cube_mask = cpl_imagelist_new(),"allocating new data cube mask");
260 
261  check(plist=cpl_propertylist_load(files[0],0),
262  "Cannot read the FITS header") ;
263 
264  if(scales_sky == 1) {
265  sinfo_msg("Subtract spatial sinfo_median to each cube plane");
266  for(n=0;n<nframes;n++) {
267  sinfo_msg("process cube %d\n",n);
268  sinfo_new_sinfoni_correct_median_it(&(cube_object[n]));
269  }
270  }
271 
272  for(z=0;z<onp;z+=z_stp) {
273  z_siz=(z_stp < (onp-z)) ? z_stp : (onp-z);
274  z_min=z;
275  z_max=z_min+z_siz;
276  sinfo_msg("Coadding cubes: range [%4.4d,%4.4d) of 0-%d\n",
277  z_min,z_max,onp);
278 
279  for(j=z_min;j<z_max;j++) {
280 
281  check_nomsg(j_img=cpl_image_new(size_x,size_y,CPL_TYPE_FLOAT));
282  check_nomsg(cpl_imagelist_set(cube_jitter,j_img,j));
283  check_nomsg(m_img = cpl_image_new(size_x,size_y,CPL_TYPE_FLOAT));
284  check_nomsg(cpl_imagelist_set(cube_mask,m_img,j));
285  }
286  if(ks_clip == 1){
287  sinfo_new_combine_jittered_cubes_thomas_range(cube_object,
288  cube_jitter,
289  cube_mask,
290  nframes,
291  offsetx,offsety,
292  times,
293  (char*) "tanh",
294  z_min,
295  z_max,
296  kappa);
297  } else {
298  sinfo_new_combine_jittered_cubes_range(cube_object,
299  cube_jitter,
300  cube_mask,
301  nframes,
302  offsetx,
303  offsety,
304  times,
305  (char*) "tanh",
306  z_min,
307  z_max) ;
308  }
309 
310  }
311  sinfo_new_convert_0_to_ZERO_for_cubes(cube_jitter) ;
312 
313  name_m="out_cube_mask.fits";
314  /* HOW TO SAVE A PRODUCT ON DISK */
315 
316  /* Create product frame */
317  check_nomsg(product_frame = cpl_frame_new());
318  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
319  check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE_COMBINE_PROCUBE));
320  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
321  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
322  check_nomsg(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL)) ;
323 
324  /* Add DataFlow keywords */
325  check(cpl_dfs_setup_product_header(plist, product_frame,
326  framelist, parlist,
327  "sinfo_utl_cube_combine",
328  "SINFONI", KEY_VALUE_HPRO_DID,NULL),
329  "Problem in the product DFS-compliance") ;
330 
331  check(cpl_imagelist_save(cube_jitter, name_o, CPL_BPP_IEEE_FLOAT, plist,
332  CPL_IO_DEFAULT),"Could not save product");
333  check_nomsg(cpl_frameset_insert(framelist, product_frame));
334 
335 
336  check_nomsg(prod_frm = cpl_frame_new());
337  check_nomsg(cpl_frame_set_filename(prod_frm, name_m)) ;
338  check_nomsg(cpl_frame_set_tag(prod_frm, SI_UTL_CUBE_COMBINE_PROMASK)) ;
339  check_nomsg(cpl_frame_set_type(prod_frm, CPL_FRAME_TYPE_IMAGE)) ;
340  check_nomsg(cpl_frame_set_group(prod_frm, CPL_FRAME_GROUP_PRODUCT)) ;
341  check_nomsg(cpl_frame_set_level(prod_frm, CPL_FRAME_LEVEL_FINAL)) ;
342 
343  check(cpl_imagelist_save(cube_mask, name_m, CPL_BPP_IEEE_FLOAT, plist,
344  CPL_IO_DEFAULT),"Could not save product");
345  check_nomsg(cpl_frameset_insert(framelist, prod_frm));
346 
347  cleanup:
348  sinfo_free_imagelist(&cube_jitter) ;
349  sinfo_free_image(&image) ;
350  sinfo_free_imagelist(&cube_mask) ;
351 
352 
353  if(cube_object != NULL) {
354  for (i=0;i< nframes;i++){
355  sinfo_free_imagelist(&(cube_object[i]));
356  }
357  sinfo_free_array_imagelist(&cube_object);
358  }
359  sinfo_free_propertylist(&plist);
360  if(files != NULL) {
361  cpl_free(files);
362  files=NULL;
363  }
364  sinfo_free_double(&times);
365  sinfo_free_float(&offsetx);
366  sinfo_free_float(&offsety);
367 
368  /* Return */
369  if (cpl_error_get_code()) {
370  return -1 ;
371  }
372  else {
373  return 0 ;
374  }
375 }