36 #include "sinfo_utl_cube_combine.h"
37 #include "sinfo_utilities.h"
38 #include "sinfo_utilities_scired.h"
40 #include "sinfo_key_names.h"
41 #include "sinfo_error.h"
42 #include "sinfo_utils_wrappers.h"
43 #include "sinfo_pro_save.h"
74 int sinfo_utl_cube_combine(
75 cpl_parameterlist * parlist,
76 cpl_frameset * framelist)
78 cpl_parameter * param=NULL ;
79 const char * name_o=NULL ;
80 const char * name_i=NULL ;
81 const char * name_m=NULL ;
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;
106 cpl_image* j_img=NULL;
107 cpl_image* m_img=NULL;
109 cpl_imagelist ** cube_object=NULL;
110 cpl_imagelist * cube_jitter=NULL;
111 cpl_imagelist * cube_mask=NULL;
113 const char** files=NULL;
117 float * offsetx=NULL;
118 float * offsety=NULL;
127 const char* name=NULL;
130 FILE* file_list=NULL;
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));
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));
145 check_nomsg(param = cpl_parameterlist_find(parlist,
146 "sinfoni.sinfo_utl_cube_combine.kappa"));
147 check_nomsg(kappa = cpl_parameter_get_double(param));
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));
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));
169 check(sinfo_dfs_set_groups(framelist),
170 "Cannot identify RAW and CALIB frames") ;
174 n=cpl_frameset_get_size(framelist);
180 frame=cpl_frameset_get_frame(framelist,i);
181 cpl_frame_set_group(frame,CPL_FRAME_GROUP_RAW);
189 if ( NULL == (file_list = fopen (name_i,
"r" ) ) )
196 while ( fscanf( file_list,
"%f %f",&tmpoffx, &tmpoffy) != EOF )
203 cknull(times = (
double*) cpl_calloc (nframes,
sizeof (
double)),
204 " could not allocate memory!") ;
206 cknull(offsetx = (
float*) cpl_calloc (nframes,
sizeof(
float)),
207 " could not allocate memory!") ;
209 cknull(offsety = (
float*) cpl_calloc (nframes,
sizeof(
float)),
210 " could not allocate memory!") ;
212 files = (
const char**) cpl_calloc(MAX_NAME_SIZE,
sizeof(
const char*));
213 if ( NULL == (file_list = fopen (name_i,
"r" ) ) )
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;
235 sinfo_msg(
"Min input cube size x=%d y=%d",min_size_x,min_size_y);
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") ;
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,
253 "Error assigning offsets");
255 onp = cpl_imagelist_get_size(cube_object[0]);
258 check(cube_jitter = cpl_imagelist_new(),
"allocating new data cube object");
259 check(cube_mask = cpl_imagelist_new(),
"allocating new data cube mask");
261 check(plist=cpl_propertylist_load(files[0],0),
262 "Cannot read the FITS header") ;
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]));
272 for(z=0;z<onp;z+=z_stp) {
273 z_siz=(z_stp < (onp-z)) ? z_stp : (onp-z);
276 sinfo_msg(
"Coadding cubes: range [%4.4d,%4.4d) of 0-%d\n",
279 for(j=z_min;j<z_max;j++) {
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));
287 sinfo_new_combine_jittered_cubes_thomas_range(cube_object,
298 sinfo_new_combine_jittered_cubes_range(cube_object,
311 sinfo_new_convert_0_to_ZERO_for_cubes(cube_jitter) ;
313 name_m=
"out_cube_mask.fits";
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)) ;
325 check(cpl_dfs_setup_product_header(plist, product_frame,
327 "sinfo_utl_cube_combine",
328 "SINFONI", KEY_VALUE_HPRO_DID,NULL),
329 "Problem in the product DFS-compliance") ;
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));
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)) ;
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));
348 sinfo_free_imagelist(&cube_jitter) ;
349 sinfo_free_image(&image) ;
350 sinfo_free_imagelist(&cube_mask) ;
353 if(cube_object != NULL) {
354 for (i=0;i< nframes;i++){
355 sinfo_free_imagelist(&(cube_object[i]));
357 sinfo_free_array_imagelist(&cube_object);
359 sinfo_free_propertylist(&plist);
364 sinfo_free_double(×);
365 sinfo_free_float(&offsetx);
366 sinfo_free_float(&offsety);
369 if (cpl_error_get_code()) {