SINFONI Pipeline Reference Manual  2.6.0
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, "%10f %10f",&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  if(files != NULL) {
216  cpl_free(files);
217  files=NULL;
218  }
219  sinfo_msg_error("cannot open %s\n", name_i) ;
220  return -1 ;
221  }
222 
223 
224  cnt=0;
225  while ( fscanf( file_list, "%f %f",&tmpoffx,&tmpoffy ) != EOF ) {
226  frame=cpl_frameset_get_frame(framelist,cnt);
227  files[cnt]= cpl_frame_get_filename(frame);
228  times[cnt]=sinfo_pfits_get_exptime(files[cnt]);
229  offsetx[cnt]=tmpoffx;
230  offsety[cnt]=tmpoffy;
231  plist=cpl_propertylist_load(files[cnt],0);
232  size_x=sinfo_pfits_get_naxis1(plist);
233  size_y=sinfo_pfits_get_naxis2(plist);
234  sinfo_free_propertylist(&plist);
235  if(size_x < min_size_x) min_size_x=size_x;
236  if(size_y < min_size_y) min_size_y=size_y;
237  cnt ++ ;
238  }
239  sinfo_msg("Min input cube size x=%d y=%d",min_size_x,min_size_y);
240 
241  nframes=cnt;
242  fclose(file_list);
243 
244  ck0(sinfo_auto_size_cube(offsetx,offsety,nframes,
245  &ref_offx,&ref_offy,&size_x,&size_y),
246  "Error resizing cube");
247  sinfo_msg("output ima size=%dx%d",size_x,size_y);
248  cknull(cube_object = cpl_calloc(nframes,sizeof(cpl_imagelist*)),
249  "could not allocate memory") ;
250 
251  for (i=0;i<nframes;i++) {
252  frame=cpl_frameset_get_frame(framelist,i);
253  name=cpl_frame_get_filename(frame);
254  check_nomsg(cube_object[i]=cpl_imagelist_load(name,CPL_TYPE_FLOAT,0));
255  ck0(sinfo_new_assign_offset2(i,name,offsetx,offsety,
256  ref_offx,ref_offy),
257  "Error assigning offsets");
258  }
259  onp = cpl_imagelist_get_size(cube_object[0]);
260 
261 
262  check(cube_jitter = cpl_imagelist_new(),"allocating new data cube object");
263  check(cube_mask = cpl_imagelist_new(),"allocating new data cube mask");
264 
265  check(plist=cpl_propertylist_load(files[0],0),
266  "Cannot read the FITS header") ;
267 
268  if(scales_sky == 1) {
269  sinfo_msg("Subtract spatial sinfo_median to each cube plane");
270  for(n=0;n<nframes;n++) {
271  sinfo_msg("process cube %d\n",n);
272  sinfo_new_sinfoni_correct_median_it(&(cube_object[n]));
273  }
274  }
275 
276  for(z=0;z<onp;z+=z_stp) {
277  z_siz=(z_stp < (onp-z)) ? z_stp : (onp-z);
278  z_min=z;
279  z_max=z_min+z_siz;
280  sinfo_msg("Coadding cubes: range [%4.4d,%4.4d) of 0-%d\n",
281  z_min,z_max,onp);
282 
283  for(j=z_min;j<z_max;j++) {
284 
285  check_nomsg(j_img=cpl_image_new(size_x,size_y,CPL_TYPE_FLOAT));
286  check_nomsg(cpl_imagelist_set(cube_jitter,j_img,j));
287  check_nomsg(m_img = cpl_image_new(size_x,size_y,CPL_TYPE_FLOAT));
288  check_nomsg(cpl_imagelist_set(cube_mask,m_img,j));
289  }
290  if(ks_clip == 1){
291  sinfo_new_combine_jittered_cubes_thomas_range(cube_object,
292  cube_jitter,
293  cube_mask,
294  nframes,
295  offsetx,offsety,
296  times,
297  (char*) "tanh",
298  z_min,
299  z_max,
300  kappa);
301  } else {
302  sinfo_new_combine_jittered_cubes_range(cube_object,
303  cube_jitter,
304  cube_mask,
305  nframes,
306  offsetx,
307  offsety,
308  times,
309  (char*) "tanh",
310  z_min,
311  z_max) ;
312  }
313 
314  }
315  sinfo_new_convert_0_to_ZERO_for_cubes(cube_jitter) ;
316 
317  name_m="out_cube_mask.fits";
318  /* HOW TO SAVE A PRODUCT ON DISK */
319 
320  /* Create product frame */
321  check_nomsg(product_frame = cpl_frame_new());
322  check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
323  check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_CUBE_COMBINE_PROCUBE));
324  check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
325  check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
326  check_nomsg(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL)) ;
327 
328  /* Add DataFlow keywords */
329  check(cpl_dfs_setup_product_header(plist, product_frame,
330  framelist, parlist,
331  "sinfo_utl_cube_combine",
332  "SINFONI", KEY_VALUE_HPRO_DID,NULL),
333  "Problem in the product DFS-compliance") ;
334 
335  check(cpl_imagelist_save(cube_jitter, name_o, CPL_BPP_IEEE_FLOAT, plist,
336  CPL_IO_DEFAULT),"Could not save product");
337  check_nomsg(cpl_frameset_insert(framelist, product_frame));
338 
339 
340  check_nomsg(prod_frm = cpl_frame_new());
341  check_nomsg(cpl_frame_set_filename(prod_frm, name_m)) ;
342  check_nomsg(cpl_frame_set_tag(prod_frm, SI_UTL_CUBE_COMBINE_PROMASK)) ;
343  check_nomsg(cpl_frame_set_type(prod_frm, CPL_FRAME_TYPE_IMAGE)) ;
344  check_nomsg(cpl_frame_set_group(prod_frm, CPL_FRAME_GROUP_PRODUCT)) ;
345  check_nomsg(cpl_frame_set_level(prod_frm, CPL_FRAME_LEVEL_FINAL)) ;
346 
347  check(cpl_imagelist_save(cube_mask, name_m, CPL_BPP_IEEE_FLOAT, plist,
348  CPL_IO_DEFAULT),"Could not save product");
349  check_nomsg(cpl_frameset_insert(framelist, prod_frm));
350 
351  cleanup:
352  sinfo_free_imagelist(&cube_jitter) ;
353  sinfo_free_image(&image) ;
354  sinfo_free_imagelist(&cube_mask) ;
355 
356 
357  if(cube_object != NULL) {
358  for (i=0;i< nframes;i++){
359  sinfo_free_imagelist(&(cube_object[i]));
360  }
361  sinfo_free_array_imagelist(&cube_object);
362  }
363  sinfo_free_propertylist(&plist);
364  if(files != NULL) {
365  cpl_free(files);
366  files=NULL;
367  }
368  sinfo_free_double(&times);
369  sinfo_free_float(&offsetx);
370  sinfo_free_float(&offsety);
371 
372  /* Return */
373  if (cpl_error_get_code()) {
374  return -1 ;
375  }
376  else {
377  return 0 ;
378  }
379 }
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69