SINFONI Pipeline Reference Manual  2.6.0
sinfo_new_prepare_stacked_frames.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004,2005 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 /*----------------------------------------------------------------------------
20 
21  File name : sinfo_new_prepare_stacked_frames.c
22  Author : A. Modigliani
23  Created on : Sep 17, 2003
24  Description :
25 
26  this handles stacks of input frames, that means it takes a clean mean,
27  subtracts the off- from the on-frames, flatfields, corrects for static bad
28  pixels, corrects for a linear tilt of the spectra if necessary, and finally,
29  interleaves dithered exposures or convolves a single exposure with a
30  Gaussian, respectively.
31 
32  ---------------------------------------------------------------------------*/
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
36 
37 /*----------------------------------------------------------------------------
38  Includes
39  ---------------------------------------------------------------------------*/
40 #include "sinfo_new_prepare_stacked_frames.h"
41 #include "sinfo_stack_ini_by_cpl.h"
42 #include "sinfo_coltilt.h"
43 #include "sinfo_image_ops.h"
44 #include "sinfo_merge.h"
45 #include "sinfo_utilities.h"
46 #include "sinfo_wave_calibration.h"
47 #include "sinfo_new_bezier.h"
48 #include "sinfo_shift_images.h"
49 #include "sinfo_product_config.h"
50 
51 #include "sinfo_pro_save.h"
52 #include "sinfo_globals.h"
53 #include "sinfo_utilities.h"
54 #include "sinfo_dfs.h"
55 #include "sinfo_raw_types.h"
56 #include "sinfo_wcal_functions.h"
57 #include "sinfo_new_bezier.h"
58 
59 #include "sinfo_hidden.h"
60 #include "sinfo_pro_types.h"
61 #include "sinfo_functions.h"
62 #include "sinfo_utils_wrappers.h"
63 #include "sinfo_error.h"
64 
65 /*----------------------------------------------------------------------------
66  Defines
67  ---------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
78  Function Definitions
79  ---------------------------------------------------------------------------*/
80 
81 /*----------------------------------------------------------------------------
82  Function : sinfo_new_prepare_stacked_frames()
83  In : ini_file: file name of according .ini file
84  Out : integer (0 if it worked, -1 if it doesn't)
85  Job :
86  this handles stacks of input frames, that means it takes a clean mean,
87  subtracts the off- from the on-frames, flatfields, corrects for static bad
88  pixels, corrects for a linear tilt of the spectra if necessary, and finally,
89  interleaves dithered exposures or convolves a single exposure with a
90  Gaussian, respectively.
91 
92  ---------------------------------------------------------------------------*/
93 static int
94 new_get_names(const char* pcatg, const int ind, stack_config_n ** cfg);
95 
96 static int
97 new_get_names(const char* pcatg, const int ind, stack_config_n ** cfg){
98 
99  if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_OFF) == 0) {
100  strcpy((*cfg)->outName,DISTORTION_STACK_OFF_OUT_FILENAME);
101  }
102  if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_ON) == 0) {
103  strcpy((*cfg)->outName,"out_ns_stack_on.fits");
104  }
105  if (strcmp(pcatg,PRO_FIBRE_NS_STACKED) == 0) {
106  strcpy((*cfg)->outName,"out_ns_stack.fits");
107  }
108  if (strcmp(pcatg,PRO_WAVE_LAMP_STACKED) == 0) {
109  strcpy((*cfg)->outName,"out_wcal_stack.fits");
110  }
111  if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_DIST)== 0) {
112  strcpy((*cfg)->outName,"out_ns_stack_warp.fits");
113  }
114  if (strcmp(pcatg,PRO_WAVE_SLITPOS_STACKED) == 0) {
115  strcpy((*cfg)->outName,"out_slit_pos_stack.fits");
116  }
117 
118  if (strcmp(pcatg,PRO_PSF_CALIBRATOR_STACKED)== 0) {
119  snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
120  }
121  if (strcmp(pcatg,PRO_SKY_PSF_CALIBRATOR_STACKED)== 0) {
122  strcpy((*cfg)->outName,STACKED_OUT_FILENAME);
123  }
124  if (strcmp(pcatg,PRO_STD_NODDING_STACKED) == 0) {
125  snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
126  }
127  /* only 1 frame
128  if (strcmp(pcatg,PRO_STD_NODDING_STACKED) == 0) {
129  strcpy((*cfg)->outName,STACKED_OUT_FILENAME);
130  }
131  */
132 
133  if (strcmp(pcatg,PRO_SKY_NODDING_STACKED) == 0) {
134  strcpy((*cfg)->outName,STACKED_OUT_FILENAME); /*STD*/
135  }
136  if (strcmp(pcatg,PRO_OBJECT_NODDING_STACKED) == 0) {
137  snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
138  }
139  if (strcmp(pcatg,PRO_PUPIL_LAMP_STACKED) == 0) {
140  snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
141  }
142  if (strcmp(pcatg,PRO_STACKED) == 0) {
143  snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
144  }
145 
146  snprintf((*cfg)->sky_name,MAX_NAME_SIZE-1,"%s%d%s","out_sky",ind,".fits");
147 
148  return 0;
149 
150 }
151 
152 
153 int sinfo_new_prepare_stacked_frames (const char* plugin_id,
154  cpl_parameterlist* config,
155  cpl_frameset* sof,
156  cpl_frameset* ref_set,
157  const char* frm_pro_ctg,
158  const int frm_ind,
159  fake* fk)
160 {
161 
162 
163  stack_config_n * cfg =NULL;
164  cpl_imagelist * list_object=NULL ;
165  cpl_imagelist * list_dither_object=NULL;
166  cpl_imagelist * list_dither_sky=NULL;
167  cpl_imagelist * list_sky=NULL;
168  cpl_imagelist * list_dark=NULL;
169  new_Lookup* lookup=NULL;
170  cpl_image * im3=NULL ;
171  cpl_image * im4=NULL ;
172  cpl_image * im5=NULL ;
173  cpl_image * im6=NULL ;
174  cpl_image * im7=NULL ;
175  cpl_image * im8=NULL ;
176  cpl_image * im9=NULL ;
177 
178  cpl_image * ref_im1=NULL ;
179  cpl_image * ref_im2=NULL ;
180  cpl_image ** im=NULL ;
181  cpl_image * im_obj=NULL ;
182  cpl_image* simg=NULL;
183 
184  cpl_image * im_dark=NULL ;
185  cpl_image * im_sky=NULL ;
186  cpl_image * im_dither=NULL ;
187  cpl_image * im_dither_sky=NULL ;
188  cpl_image * im_obj_sub=NULL ;
189  cpl_image * im_obj_flat=NULL ;
190  cpl_image * im_dither_sub=NULL ;
191  cpl_image * im_dither_flat=NULL ;
192  cpl_image * int_im_shifted=NULL ;
193  cpl_image * int_im_dith_shifted=NULL ;
194  cpl_image * im_conv=NULL ;
195  int sy=0;
196 
197  cpl_image * mask_im=NULL ;
198  cpl_image * flat_smooth=NULL ;
199  cpl_image * flat1=NULL ;
200  cpl_image * flat2=NULL ;
201  cpl_image * int_im=NULL ;
202  cpl_image * int_im_dith=NULL ;
203  cpl_image * sky_img_flat=NULL;
204  cpl_image * sky_dist=NULL;
205 
206 
207  cpl_imagelist * iCube=NULL ;
208  cpl_imagelist * jCube=NULL ;
209  cpl_image * X=NULL ;
210  cpl_image * hX=NULL ;
211  cpl_image * Y=NULL ;
212  cpl_image * Z=NULL ;
213  cpl_table * qclog_tbl=NULL;
214  cpl_image* sky_img=NULL;
215  cpl_image* mdark=NULL;
216 
217  char* name=NULL;
218  int typ=0;
219  int pos=0;
220  int i = 0;
221  int n = 0;
222  int cnt = 0 ;
223  float val_x=0;
224  float val_y=0;
225  int status=0;
226 
227  float** slit_edges=NULL;
228  char** in_nam=NULL;
229 
230  int nob = 0;
231  int nsky = 0;
232  int nobjdith = 0;
233  int nskydith = 0;
234  int nda = 0;
235  char name_list[MAX_NAME_SIZE];
236  char fake_sky_name[MAX_NAME_SIZE];
237  int no=0;
238  float lo_cut=0;
239  float hi_cut=0;
240 
241  cpl_imagelist* list_object_tmp=NULL;
242  cpl_imagelist* list_dither_object_tmp=NULL;
243  cpl_imagelist* list_sky_tmp=NULL;
244  cpl_imagelist* list_dither_sky_tmp=NULL;
245 
246  cpl_image* flat1_dist=NULL;
247  cpl_image* flat2_dist=NULL;
248 
249  cpl_frameset* raw=NULL;
250 
251  char file_name[MAX_NAME_SIZE];
252  cpl_table* tbl_index = NULL;
253  cpl_table* tbl_slitpos=NULL;
254  /* int rhead=0; */
255 
256  cpl_frame* sky_frame = NULL;
257  char* sky_name = NULL;
258  /* char* sky_tag = NULL; */
259  qc_wcal* qc=sinfo_qc_wcal_new();
260 
261  cpl_parameter* p=NULL;
262  int pdensity=0;
263  int mflat_norm_smooth=FALSE;
264 
265  int smooth_rad=16;
266  int sub_raw_sky=1;
267 
268  /*
269  -----------------------------------------------------------------
270  1) parse the file names and parameters to the psf_config data
271  structure cfg
272  -----------------------------------------------------------------
273  */
274 
275 
276  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
277  check_nomsg(pdensity=cpl_parameter_get_int(p));
278 
279 
280 
281  check_nomsg(p=cpl_parameterlist_find(config,
282  "sinfoni.stacked.mflat_norm_smooth"));
283  check_nomsg(mflat_norm_smooth=cpl_parameter_get_int(p));
284 
285 
286 
287  check_nomsg(p=cpl_parameterlist_find(config,
288  "sinfoni.stacked.mflat_smooth_rad"));
289  check_nomsg(smooth_rad=cpl_parameter_get_int(p));
290 
291  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.stacked.sub_raw_sky"));
292  check_nomsg(sub_raw_sky=cpl_parameter_get_bool(p));
293 
294 
295  check_nomsg(raw=cpl_frameset_new());
296  cknull(cfg = sinfo_parse_cpl_input_stack(config,sof,&raw, fk),
297  "could not parse cpl input file!") ;
298 
299  ck0(sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc),"computing det ncounts");
300 
301 
302  if(ref_set != NULL) {
303  sinfo_free_frameset(&raw);
304  raw=cpl_frameset_duplicate(ref_set);
305  }
306  /* defines output file name for stack set i */
307  ck0_nomsg(new_get_names(frm_pro_ctg, frm_ind, &cfg));
308 
309  if (cfg->flatInd == 1){
310  if(sinfo_is_fits_file(cfg->flatfield1) != 1) {
311  sinfo_msg_error("Input FF file %s is not FITS",cfg->flatfield1);
312  goto cleanup;
313  }
314 
315  }
316  if (cfg->maskInd == 1) {
317  if(sinfo_is_fits_file(cfg->mask) != 1) {
318  sinfo_msg_error("Input mask file %s is not FITS",cfg->mask);
319  goto cleanup;
320  }
321  if(cfg -> indind == 0) {
322  if(sinfo_is_fits_file(cfg->slitposList) != 1) {
323  sinfo_msg_error("Input slitpos file %s is not FITS",cfg->slitposList);
324  goto cleanup;
325  }
326  }
327  }
328 
329  /*
330  #---------------------------------------------------------
331  # Take a clean mean of several images
332  # input is 1 or more similar images
333  #---------------------------------------------------------
334  */
335 
336  if (cfg->sfInd == 1){
337  if (cfg->contains_dark == 0) {
338  sinfo_msg_warning("no sinfo_dark frames given!");
339  }
340  if (cfg->contains_ref == 0) {
341  sinfo_msg_error("no reference frames given!");
342  goto cleanup;
343  }
344  }
345 
346  /* allocate memory for lists of object, sky and dithered frames */
347  check(list_object=cpl_imagelist_new(),
348  "could not allocate memory for object frame");
349 
350  if (cfg->contains_dither == 1) {
351  check(list_dither_object=cpl_imagelist_new(),
352  "could not allocate memory for dither object frame");
353  }
354 
355  if (cfg->contains_sky == 1) {
356  check(list_sky = cpl_imagelist_new(),
357  "could not allocate memory for off frame list");
358  }
359 
360  if (cfg->contains_dither == 1 && cfg->nditheroff > 0) {
361  check(list_dither_sky = cpl_imagelist_new(),
362  "could not allocate memory for dither frame list");
363  }
364 
365  if (cfg->contains_dark == 1 && cfg->sfInd == 1) {
366  check(list_dark = cpl_imagelist_new(),
367  "could not allocate memory for sinfo_dark frame");
368  }
369 
370  if (cfg->contains_dither == 0 && cfg->nditheroff > 0) {
371  sinfo_msg_error("please use non-dithered off-frames, remove the 2!");
372  goto cleanup;
373  }
374 
375  /* build different image lists for the different cases */
376  cknull_nomsg(im=(cpl_image**)cpl_calloc(cfg -> nframes, sizeof(cpl_image*)));
377 
378  for (i=0; i< cfg->nframes; i++) {
379  name = cfg->framelist[i];
380  if(sinfo_is_fits_file(name) != 1) {
381  sinfo_msg_error("Input file %s is not FITS",name);
382  goto cleanup;
383  }
384  check_nomsg(im[i] = cpl_image_load( name,CPL_TYPE_FLOAT,0,0));
385  }
386 
387  /* up to here leak free */
388  /* rhead=0; */
389  for (i=0; i< cfg->nframes; i++) {
390  typ = sinfo_new_intarray_get_value( cfg->frametype, i );
391  pos = sinfo_new_intarray_get_value( cfg->frameposition, i );
392  cknull(im[i],"could not load image");
393 
394  if (pos == 2) {
395  if (typ == 1) {
396  check_nomsg(cpl_imagelist_set(list_object,
397  cpl_image_duplicate(im[i]),nob));
398  nob = nob + 1;
399  }
400  else if ( typ == 0 ) {
401  check_nomsg(cpl_imagelist_set(list_sky,
402  cpl_image_duplicate(im[i]),nsky));
403  nsky = nsky + 1;
404  if(pdensity > 0) {
405  if(fk->is_fake_sky==1) {
406  snprintf(fake_sky_name,MAX_NAME_SIZE-1,"%s%d%s","out_fake_sky",
407  frm_ind,".fits");
408  check_nomsg(sky_img=cpl_image_load(fake_sky_name,CPL_TYPE_FLOAT,0,0));
409  ck0(sinfo_pro_save_ima(sky_img,raw,sof,fake_sky_name,
410  PRO_SKY_DUMMY,NULL,
411  plugin_id,config),
412  "cannot save sky ima %s", fake_sky_name);
413 
414  sinfo_free_image(&sky_img);
415  }
416  }
417 
418 
419  if((pdensity == 3) || (pdensity == 1) ||
420  (pdensity == 2 && frm_ind == 0)) {
421  check_nomsg(sky_frame = cpl_frameset_get_frame(raw,i));
422  check_nomsg(sky_name = (char*) cpl_frame_get_filename(sky_frame));
423  /* check_nomsg(sky_tag = (char*) cpl_frame_get_tag(sky_frame)); */
424 
425  if ( (strstr(frm_pro_ctg,"OBJECT") != NULL) ||
426  (strstr(frm_pro_ctg,"PSF") != NULL) ||
427  (strstr(frm_pro_ctg,"STD") != NULL) ) {
428  check_nomsg(sky_img = cpl_image_load(sky_name,CPL_TYPE_FLOAT,0,0));
429  snprintf(sky_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_sky",
430  frm_ind,".fits");
431  ck0(sinfo_pro_save_ima(sky_img,raw,sof,sky_name,
432  PRO_SKY_STACKED_DUMMY,NULL,
433  plugin_id,config),
434  "cannot save sky ima %s", sky_name);
435 
436  sinfo_free_image(&sky_img);
437  if (cfg->flatInd == 1) {
438  sinfo_msg("Sky Flatfielding");
439  check(flat1=cpl_image_load(cfg->flatfield1,CPL_TYPE_FLOAT,0,0 ),
440  "could not load flatfield image" );
441 
442  if(mflat_norm_smooth != 0) {
443 
444  if(mflat_norm_smooth == 1) {
445 
446  sy=cpl_image_get_size_y(flat1);
447 
448  cknull(flat_smooth=sinfo_image_smooth_fft(flat1,sy/smooth_rad),
449  "could not smooth flatfield" );
450  } else if(mflat_norm_smooth == 2) {
451  cknull(flat_smooth = sinfo_image_smooth_median_y(flat1,
452  smooth_rad),
453  "could not smooth flatfield" );
454  }
455 
456  check_nomsg(cpl_image_divide(flat1,flat_smooth));
457  sinfo_free_image(&flat_smooth);
458 
459  }
460 
461  check_nomsg(simg = cpl_image_load(sky_name,CPL_TYPE_FLOAT,0,0 ));
462 
463  cknull(sky_img_flat=sinfo_new_div_images_robust(simg,flat1),
464  "could not carry out flatfield division" );
465  sinfo_free_image(&simg);
466  sinfo_free_image(&flat1);
467 
468  /* if (frm_ind == 0) { */
469  if (cfg->warpfixInd == 1){
470  sinfo_msg("Correct sky for distortions");
471  snprintf(file_name,MAX_NAME_SIZE-1,"%s%d%s",
472  STACK_SKY_DIST_OUT_FILENAME,frm_ind,".fits");
473  sky_dist = sinfo_new_image_warp_fits(sky_img_flat,
474  cfg->kernel,
475  cfg->polyFile);
476 
477  ck0(sinfo_pro_save_ima(sky_dist,raw,sof,
478  file_name,
479  PRO_STACK_SKY_DIST,NULL,plugin_id,
480  config),"cannot save ima %s",
481  STACK_SKY_DIST_OUT_FILENAME);
482 
483  sinfo_free_image(&sky_dist);
484  }
485  /* } */
486  sinfo_free_image(&sky_img_flat);
487  } /* end check on flatind */
488  } /* end check on procatg */
489  }
490  } else if ( typ == 5 ) {
491  if (cfg->sfInd == 1) {
492  check_nomsg(cpl_imagelist_set(list_dark,
493  cpl_image_duplicate(im[i]),nda));
494  nda = nda + 1;
495  } else {
496  sinfo_free_image(&(im[i]));
497  }
498  } else if ( typ == 4 ) {
499  if ( cfg->sfInd == 1) {
500  ref_im1 = im[i];
501  } else {
502  sinfo_free_image(&(im[i]));
503  }
504  }
505  } else {
506  if (typ == 1) {
507  check_nomsg(cpl_imagelist_set(list_dither_object,
508  cpl_image_duplicate(im[i]),nobjdith));
509  nobjdith = nobjdith + 1;
510  } else if (typ == 0) {
511  check_nomsg(cpl_imagelist_set(list_dither_object,
512  cpl_image_duplicate(im[i]),nskydith));
513  nskydith = nskydith + 1;
514  } else if (typ == 4) {
515  if (cfg->sfInd == 1) {
516  ref_im2 = cpl_image_duplicate(im[i]);
517  } else {
518  sinfo_free_image(&(im[i]));
519  }
520  }
521  }
522  } /* end for loop on i */
523 
524  if (nob != cfg->nobj ||
525  cfg->noff != nsky ||
526  nobjdith != cfg->nditherobj ||
527  nskydith != cfg->nditheroff) {
528  sinfo_msg_error("something is wrong with the number of the");
529  sinfo_msg_error("different types of frames");
530  /* free memory */
531  goto cleanup;
532 
533  }
534 
535  if (cfg->sfInd == 1 && nda != cfg->ndark) {
536  sinfo_msg_error("something is wrong with the number of sinfo_dark frames");
537  goto cleanup;
538  }
539  sinfo_msg("Create and fill cubes with the different images");
540 
541  /* create and fill cubes with the different image lists */
542  cknull(list_object,"could not create object data cube!");
543 
544  /* shift the images in the cubes-if indicated-in spectral direction
545  with respect to the reference image
546  */
547  if (cfg->sfInd == 1) {
548 
549  /*
550  first take the mean of the sinfo_dark frames and subtract the result
551  from all cubes
552  */
553  sinfo_msg("Shift cube images in spectral direction with "
554  "respect to reference");
555 
556  if (cfg->contains_dark == 1) {
557  sinfo_msg("cfg->contains_dark == 1");
558  if (cfg->loReject*cfg->ndark < 1. && cfg->hiReject * cfg->ndark < 1.) {
559  /*
560  im_dark = sinfo_new_average_cube_to_image( list_dark );
561  */
562  check(im_dark = cpl_imagelist_collapse_create( list_dark ),
563  "sinfo_averageCubeToImage failed" );
564  }
565  else {
566 
567  check_nomsg(no=cpl_imagelist_get_size(list_dark));
568  lo_cut=(floor)( cfg->loReject*no+0.5);
569  hi_cut=(floor)( cfg->hiReject*no+0.5);
570  check(im_dark=cpl_imagelist_collapse_minmax_create(list_dark,lo_cut,
571  hi_cut),
572  "sinfo_average_with_rejection failed" );
573 
574  }
575  sinfo_free_imagelist(&list_dark);
576  check_nomsg(list_object_tmp = cpl_imagelist_duplicate (list_object));
577  check(cpl_imagelist_subtract_image (list_object_tmp, im_dark),
578  "cpl_imagelist_subtract_image failed" );
579  /*
580  cube_object_tmp = sinfo_subImageFromCube (cube_object, im_dark);
581  */
582  } else {
583  sinfo_msg("cfg->contains_dark == 0");
584  check_nomsg(list_object_tmp = cpl_imagelist_duplicate(list_object));
585  }
586  sinfo_free_imagelist(&list_object);
587 
588  cknull(list_object = sinfo_align_cube_to_reference (list_object_tmp,
589  ref_im1,
590  cfg->sfOrder,
591  cfg->sfType),
592  "sinfo_align_cube_to_reference failed" );
593 
594  sinfo_free_imagelist(&list_object_tmp);
595  if (cfg->contains_dither == 1) {
596  if (cfg->contains_dark == 1) {
597  check_nomsg(list_dither_object_tmp =
598  cpl_imagelist_duplicate(list_dither_object));
599  check(cpl_imagelist_subtract_image(list_dither_object_tmp,im_dark),
600  "sinfo_average_with_rejection failed" );
601 
602  /*
603  list_dither_object_tmp =
604  sinfo_new_sub_image_from_cube(list_dither_object,
605  im_dark);
606  */
607 
608  } else {
609  check_nomsg(list_dither_object_tmp=
610  cpl_imagelist_duplicate(list_dither_object));
611  }
612  sinfo_free_imagelist(&list_dither_object);
613 
614  cknull(list_dither_object=
615  sinfo_align_cube_to_reference (list_dither_object_tmp,
616  ref_im2,
617  cfg->sfOrder,
618  cfg->sfType),
619  "sinfo_align_cube_to_reference failed" );
620 
621  sinfo_free_imagelist(&list_dither_object_tmp);
622  }
623  if (cfg->contains_sky == 1) {
624  if (cfg->contains_dark == 1) {
625  check_nomsg(list_sky_tmp = cpl_imagelist_duplicate(list_sky));
626  check(cpl_imagelist_subtract_image (list_sky_tmp, im_dark),
627  "sinfo_average_with_rejection failed" );
628  /*
629  cube_sky_tmp = sinfo_subImageFromCube (cube_sky, im_dark);
630  */
631 
632  } else {
633  check_nomsg(list_sky_tmp = cpl_imagelist_duplicate(list_sky));
634  }
635  check_nomsg(list_sky_tmp=cpl_imagelist_duplicate(list_sky));
636 
637  check(list_sky = sinfo_align_cube_to_reference (list_sky_tmp,
638  ref_im1,
639  cfg->sfOrder,
640  cfg->sfType),
641  "sinfo_alignCubeToReference failed" );
642 
643  check_nomsg(cpl_imagelist_delete(list_sky_tmp));
644  }
645  if (cfg->contains_dither == 1 && cfg->contains_sky == 1) {
646  if (cfg->contains_dark == 1) {
647  check_nomsg(list_dither_sky_tmp=cpl_imagelist_duplicate(list_dither_sky));
648  check(cpl_imagelist_subtract_image(list_dither_sky_tmp,im_dark),
649  "sinfo_average_with_rejection failed" );
650  /*
651  cube_dither_sky_tmp = sinfo_subImageFromCube (cube_dither_sky, im_dark);
652  */
653 
654  } else {
655  check_nomsg(list_dither_sky_tmp=cpl_imagelist_duplicate(list_dither_sky));
656  }
657  sinfo_free_imagelist(&list_dither_sky);
658 
659  check(list_dither_sky=sinfo_align_cube_to_reference(list_dither_sky_tmp,
660  ref_im2,
661  cfg->sfOrder,
662  cfg->sfType),
663  "sinfo_alignCubeToReference failed" );
664 
665  sinfo_free_imagelist(&list_dither_sky_tmp);
666  }
667  sinfo_free_image(&ref_im1);
668  if (cfg->contains_dither == 1) {
669  sinfo_free_image(&ref_im2);
670  }
671  if (cfg->contains_dark == 1) {
672  sinfo_free_image(&im_dark);
673  }
674 
675  } /* end if over sfInd */
676 
677  /* subtracts the master dark from different frames if present */
678  if(cfg->mdark_ind==1){
679  sinfo_msg("Subtract master dark %s ",cfg->mdark);
680 
681  check_nomsg(mdark=cpl_image_load(cfg->mdark,CPL_TYPE_FLOAT,0,0));
682  if (list_object !=NULL) {
683  cpl_imagelist_subtract_image (list_object, mdark);
684  }
685  if (list_sky !=NULL) {
686  cpl_imagelist_subtract_image (list_sky, mdark);
687  }
688  sinfo_free_image(&mdark);
689  }
690  /* take the average with rejection of the different cubes */
691  sinfo_msg("Take the average of the different cubes");
692 
693  if (cfg->loReject*cfg->nobj < 1. && cfg->hiReject * cfg->nobj < 1.) {
694  check(im_obj = cpl_imagelist_collapse_create(list_object),
695  "Average with rejection failed" );
696  } else {
697 
698  check_nomsg(no=cpl_imagelist_get_size(list_object));
699  lo_cut=(floor)( cfg->loReject*no+0.5);
700  hi_cut=(floor)( cfg->hiReject*no+0.5);
701  check(im_obj=cpl_imagelist_collapse_minmax_create(list_object,
702  lo_cut,hi_cut),"Average with rejection failed" );
703 
704  }
705  sinfo_free_imagelist(&list_object);
706 
707  if (cfg->contains_sky == 1) {
708  if (cfg->loReject*nsky < 1. && cfg->hiReject*nsky < 1.) {
709  /* here might explode in dither mode */
710  cknull(im_sky = cpl_imagelist_collapse_create( list_sky ),
711  "Average with rejection failed");
712  } else {
713 
714  check_nomsg(no=cpl_imagelist_get_size(list_sky));
715  lo_cut=(floor)( cfg->loReject*no+0.5);
716  hi_cut=(floor)( cfg->hiReject*no+0.5);
717  check(im_sky=cpl_imagelist_collapse_minmax_create(list_sky,lo_cut,hi_cut),
718  "Average with rejection failed");
719  }
720  sinfo_free_imagelist(&list_sky);
721  }
722 
723  if (cfg->contains_dither == 1) {
724  if (cfg->loReject*nobjdith < 1. && cfg->hiReject*nobjdith < 1.) {
725  check(im_dither = cpl_imagelist_collapse_create( list_dither_object ),
726  "Average with rejection failed");
727  } else {
728 
729  check_nomsg(no=cpl_imagelist_get_size(list_dither_object));
730  lo_cut=(floor)( cfg->loReject*no+0.5);
731  hi_cut=(floor)( cfg->hiReject*no+0.5);
732  check(im_dither=cpl_imagelist_collapse_minmax_create(list_dither_object,
733  lo_cut,hi_cut),
734  "Average with rejection failed");
735 
736  }
737  sinfo_free_imagelist(&list_dither_object);
738  }
739 
740  if (cfg->contains_dither == 1 && nskydith > 0) {
741  if (cfg->loReject*nskydith < 1. && cfg->hiReject*nskydith < 1.) {
742  check(im_dither_sky = cpl_imagelist_collapse_create( list_dither_sky ),
743  "Average with rejection failed");
744  } else {
745 
746  check_nomsg(no=cpl_imagelist_get_size(list_dither_sky));
747  lo_cut=(floor)( cfg->loReject*no+0.5);
748  hi_cut=(floor)( cfg->hiReject*no+0.5);
749  check(im_dither_sky=cpl_imagelist_collapse_minmax_create(list_dither_sky,
750  lo_cut,hi_cut),
751  "Average with rejection failed");
752  }
753  sinfo_free_imagelist(&list_dither_sky);
754  }
755 
756  /*
757  #---------------------------------------------------------
758  # Subtract the resulting off-frame (sky) from the on-frame
759  #-------------------------------------------------------
760  # finally, subtract off from on frames and store the result
761  # in the object cube
762  */
763 
764  if(sub_raw_sky == 1 ) {
765  if (cfg->contains_sky == 1) {
766  sinfo_msg("Subtract the off-frame (sky) from the on-frame");
767  check_nomsg(im_obj_sub = cpl_image_duplicate(im_obj));
768  check(cpl_image_subtract(im_obj_sub, im_sky),
769  "could not sinfo_sub_image");
770 
771  sinfo_free_image(&im_obj);
772  if (((cfg->contains_dither == 1) && (nskydith > 0)) ||
773  cfg->contains_dither == 0) {
774  sinfo_free_image(&im_sky);
775  im_obj = cpl_image_duplicate(im_obj_sub);
776  }
777  }
778 
779  if (cfg->contains_dither == 1 && nskydith > 0) {
780  check_nomsg(im_dither_sub = cpl_image_duplicate(im_dither));
781  check(cpl_image_subtract(im_dither_sub, im_dither_sky),
782  "could not sinfo_sub_image");
783 
784  sinfo_free_image(&im_dither);
785  sinfo_free_image(&im_dither_sky);
786  im_dither = cpl_image_duplicate(im_dither_sub);
787 
788  } else if (cfg->contains_dither == 1 &&
789  nskydith == 0 &&
790  cfg->contains_sky == 1) {
791  check_nomsg(im_dither_sub = cpl_image_duplicate(im_dither));
792  check(cpl_image_subtract(im_dither_sub, im_sky),
793  "could not sinfo_sub_image");
794 
795  sinfo_free_image(&im_dither);
796  sinfo_free_image(&im_sky);
797  im_dither = cpl_image_duplicate(im_dither_sub);
798  }
799  }
800 
801  /*
802  #---------------------------------------------------------
803  # Flatfielding
804  #---------------------------------------------------------
805  */
806 
807  if (cfg->flatInd == 1) {
808  sinfo_msg("Flatfielding");
809  check(flat1 = cpl_image_load (cfg->flatfield1,CPL_TYPE_FLOAT,0,0),
810  "could not load flatfield image" );
811 
812  if(mflat_norm_smooth) {
813 
814  //We normalize the flat by a smoothed flat
815  cknull(flat_smooth = sinfo_image_smooth_median_y(flat1,smooth_rad),
816  "could not smooth flatfield" );
817  check_nomsg(cpl_image_divide(flat1,flat_smooth));
818  sinfo_free_image(&flat_smooth);
819  }
820 
821  cknull(im_obj_flat = sinfo_new_div_images_robust( im_obj, flat1),
822  "could not carry out flatfield division" );
823 
824  /* AMO ** */
825  if(pdensity > 1) {
826  if (frm_ind == 0) {
827  if (cfg->warpfixInd == 1){
828  sinfo_msg("Correct FF for distortions");
829  /* AMO check */
830  cknull_nomsg(flat1_dist=sinfo_new_image_warp_fits(flat1,cfg->kernel,
831  cfg->polyFile));
832 
833  ck0(sinfo_pro_save_ima(flat1_dist,raw,sof,
834  STACK_MFLAT_DIST_OUT_FILENAME,
835  PRO_STACK_MFLAT_DIST,NULL,plugin_id,config),
836  "cannot save ima %s", STACK_MFLAT_DIST_OUT_FILENAME);
837  sinfo_free_image(&flat1_dist);
838  }
839  }
840  }
841  sinfo_free_image(&flat1);
842  sinfo_free_image(&im_obj);
843  im_obj = cpl_image_duplicate(im_obj_flat);
844 
845  if(pdensity > 1) {
846  if (cfg->contains_dither == 1) {
847  check(flat2 = cpl_image_load (cfg->flatfield2,CPL_TYPE_FLOAT,0,0),
848  "could not load flatfield image" );
849 
850  if(mflat_norm_smooth) {
851 
852  //We normalize the flat by a smoothed flat
853  cknull(flat_smooth = sinfo_image_smooth_median_y(flat2,smooth_rad),
854  "could not smooth flatfield" );
855  check_nomsg(cpl_image_divide(flat2,flat_smooth));
856  sinfo_free_image(&flat_smooth);
857 
858  }
859 
860 
861  cknull(im_dither_flat = sinfo_new_div_images_robust( im_dither, flat2),
862  "could not carry out flatfield division" );
863 
864  if (frm_ind == 0) {
865  if (cfg->warpfixInd == 1) {
866  sinfo_msg("Correct FF for distortions");
867  flat2_dist = sinfo_new_image_warp_fits(flat2,
868  cfg->kernel, cfg->polyFile);
869 
870  ck0(sinfo_pro_save_ima(flat2_dist,raw,sof,
871  STACK_MFLAT_DITHER_DIST_OUT_FILENAME,
872  PRO_STACK_MFLAT_DITHER_DIST,
873  NULL,plugin_id,config),
874  "cannot save ima %s", STACK_MFLAT_DITHER_DIST_OUT_FILENAME);
875  sinfo_free_image(&flat2_dist);
876  }
877  }
878  sinfo_free_image(&flat2);
879  sinfo_free_image(&im_dither);
880  im_dither = cpl_image_duplicate(im_dither_flat);
881  }
882  }
883  }
884 
885  /*
886  #---------------------------------------------------------
887  # static bad pixel correction
888  #---------------------------------------------------------
889  */
890 
891  if (cfg->maskInd == 1) {
892  sinfo_msg("Static bad pixel correction");
893  check(mask_im = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
894  "could not load static bad pixel mask" );
895 
896  ck0(sinfo_new_change_mask(mask_im, im_obj),
897  "sinfo_changeMask failed" );
898 
899  if (cfg->indind == 0) {
900  /* open the ASCII list of the slitlet positions */
901  /*READ TFITS TABLE*/
902  strcpy(file_name,cfg->slitposList);
903  check(tbl_slitpos = cpl_table_load(file_name,1,0),
904  "error loading slitpos tbl %s ",file_name);
905  if(cpl_table_has_column(tbl_slitpos,"pos1") != 1) {
906  sinfo_msg_error("Column 'pos1' not found in %s table %s",
907  PRO_SLIT_POS,file_name);
908  goto cleanup;
909  }
910  if(cpl_table_has_column(tbl_slitpos,"pos2") != 1) {
911  sinfo_msg_error("Column 'pos2' not found in %s table %s",
912  PRO_SLIT_POS,file_name);
913  goto cleanup;
914  }
915 
916  check_nomsg(n = cpl_table_get_nrow(tbl_slitpos));
917  cknull_nomsg(slit_edges = sinfo_new_2Dfloatarray(n, 2));
918  for (i =0 ; i< n; i++){
919  check_nomsg(val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status));
920  check_nomsg(val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status));
921  check_nomsg(sinfo_new_array2D_set_value(slit_edges,val_x,i,0));
922  check_nomsg(sinfo_new_array2D_set_value(slit_edges,val_y,i,1));
923  }
924 
925  sinfo_free_table(&tbl_slitpos);
926  cknull(int_im = sinfo_interpol_source_image (im_obj,
927  mask_im,
928  cfg->maxRad,
929  slit_edges),
930  "could not carry out sinfo_interpolSourceImage" );
931 
932  sinfo_free_image(&im_obj);
933  im_obj = cpl_image_duplicate(int_im);
934 
935  if (cfg->contains_dither == 1) {
936  cknull(int_im_dith = sinfo_interpol_source_image (im_dither,
937  mask_im,
938  cfg->maxRad,
939  slit_edges),
940  "could not carry out sinfo_interpolSourceImage" );
941 
942  sinfo_free_image(&im_dither);
943  im_dither = cpl_image_duplicate(int_im_dith);
944  }
945  sinfo_new_destroy_2Dfloatarray(&slit_edges, 32);
946  } else {
947  cknull(int_im = sinfo_new_mult_image_by_mask(im_obj, mask_im),
948  "could not carry out sinfo_multImageByMask" );
949 
950  sinfo_free_image(&im_obj);
951  im_obj = cpl_image_duplicate(int_im);
952  if (cfg->contains_dither == 1) {
953  cknull(int_im_dith=sinfo_new_mult_image_by_mask(im_dither, mask_im),
954  "could not carry out sinfo_multImageByMask" );
955 
956  sinfo_free_image(&im_dither);
957  im_dither = cpl_image_duplicate(int_im_dith);
958  }
959  }
960  sinfo_free_image(&mask_im);
961  }
962 
963  /*
964  #---------------------------------------------------------
965  # static bad pixel correction BEZIER
966  #---------------------------------------------------------
967  */
968 
969  if (cfg->maskInd == 2){
970  sinfo_msg("Static bad pixel correction BEZIER");
971  check(mask_im = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
972  "could not load static bad pixel mask" );
973 
974  ck0(sinfo_new_change_mask(mask_im, im_obj),
975  "sinfo_changeMask failed" );
976 
977  /* #open the FITS table of the slitlet positions-*/
978  strcpy(file_name,cfg->slitposList);
979  check_nomsg(tbl_slitpos = cpl_table_load(file_name,1,0));
980 
981  check_nomsg(n = cpl_table_get_nrow(tbl_slitpos));
982  slit_edges = sinfo_new_2Dfloatarray(n, 2);
983 
984  for (i =0 ; i< n; i++){
985  val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status);
986  val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status);
987  sinfo_new_array2D_set_value(slit_edges,val_x,i,0);
988  sinfo_new_array2D_set_value(slit_edges,val_y,i,1);
989  }
990  sinfo_free_table(&tbl_slitpos);
991 
992  strcpy(file_name,cfg->indexlist);
993 
994  check_nomsg(tbl_index = cpl_table_load(file_name,1,0));
995 
996  check_nomsg(n = cpl_table_get_nrow(tbl_index));
997 
998  cknull_nomsg(in_nam = (char**) cpl_calloc(n,sizeof(char*)));
999 
1000  for (i =0 ; i< n; i++){
1001  strcpy(in_nam[i],cpl_table_get_string(tbl_index,"name",i));
1002  }
1003  sinfo_free_table(&tbl_index);
1004 
1005  for (i=0;i<cnt;i++) {
1006  if (strcmp("ICube.fits", name) != 0){
1007  check_nomsg(iCube=cpl_imagelist_load ("ICube.fits",CPL_TYPE_FLOAT,0));
1008  }
1009  else if (strcmp("JCube.fits", name) != 0) {
1010  check_nomsg(jCube = cpl_imagelist_load ("JCube.fits",CPL_TYPE_FLOAT,0));
1011  }
1012  else if (strcmp("X.fits", name) != 0) {
1013  check_nomsg(X=cpl_image_load("X.fits",CPL_TYPE_FLOAT,0,0));
1014  }
1015  else if (strcmp("Y.fits", name) != 0) {
1016  check_nomsg(Y=cpl_image_load("Y.fits",CPL_TYPE_FLOAT,0,0));
1017  }
1018  else if (strcmp("Z.fits", name) != 0) {
1019  check_nomsg(Z=cpl_image_load("Z.fits",CPL_TYPE_FLOAT,0,0));
1020  }
1021  else if (strcmp("cX.fits", name) != 0) {
1022  check_nomsg(hX=cpl_image_load("cX.fits",CPL_TYPE_FLOAT,0,0));
1023  }
1024  else {
1025  sinfo_msg_error("wrong name in index list or needed file not there!");
1026  goto cleanup;
1027  }
1028  }
1029  lookup = sinfo_new_lookup();
1030 
1031  lookup->id=iCube;
1032  lookup->jd=jCube;
1033  lookup->X=X;
1034  lookup->Y=Y;
1035  lookup->Z=Z;
1036  lookup->hX=hX;
1037 
1038 
1039  cknull(im_obj=sinfo_new_c_bezier_interpolate_image(im_obj,
1040  mask_im,
1041  lookup,
1042  cfg->maxRad,
1043  cfg->maxRad,
1044  cfg->maxRad,
1045  2,
1046  slit_edges),
1047  "could not carry out sinfo_new_c_bezier_interpolate_image" );
1048 
1049 
1050  cknull(im_obj=sinfo_new_c_bezier_find_bad( im_obj,
1051  mask_im,
1052  cfg->maxRad,
1053  cfg->maxRad,
1054  cfg->maxRad,
1055  0,
1056  cpl_image_get_size_x(im_obj),
1057  0,
1058  cpl_image_get_size_y(im_obj),
1059  cfg->sigmaFactor),
1060  "could not carry out sinfo_new_c_bezier_find_bad" );
1061 
1062 
1063  if (cfg->contains_dither == 1) {
1064  cknull(im_dither=sinfo_new_c_bezier_interpolate_image(im_dither,
1065  mask_im,
1066  lookup,
1067  cfg->maxRad,
1068  cfg->maxRad,
1069  cfg->maxRad,
1070  2,
1071  slit_edges),
1072  "could not carry out new_c_bezier_Interpolate_Image on dithered frame" );
1073 
1074  cknull(im_dither=sinfo_new_c_bezier_find_bad(im_dither,
1075  mask_im,
1076  cfg->maxRad,
1077  cfg->maxRad,
1078  cfg->maxRad,
1079  0,
1080  cpl_image_get_size_x(im_obj),
1081  0,
1082  cpl_image_get_size_y(im_obj),
1083  cfg->sigmaFactor),
1084  "could not carry out new_c_bezier_find_bad on dithered frame");
1085 
1086  }
1087  sinfo_new_destroy_2Dfloatarray(&slit_edges, 32);
1088  sinfo_free_image(&mask_im);
1089  sinfo_free_imagelist(&iCube);
1090  sinfo_free_imagelist(&jCube);
1091  sinfo_free_image(&lookup->X);
1092  sinfo_free_image(&lookup->X);
1093  sinfo_free_image(&lookup->Y);
1094  sinfo_free_image(&lookup->hX);
1095  sinfo_new_destroy_lookup(lookup);
1096  }
1097 
1098  if (cfg->maskInd == 3) {
1099  cknull(mask_im = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
1100  "could not load static bad pixel mask" );
1101 
1102  /* #open the ASCII list of the slitlet positions-- */
1103  strcpy(file_name,cfg->slitposList);
1104  check_nomsg(tbl_slitpos = cpl_table_load(file_name,1,0));
1105  if(cpl_table_has_column(tbl_slitpos,"pos1") != 1) {
1106  sinfo_msg_error("Column 'pos1' not found in %s table %s",
1107  PRO_SLIT_POS,file_name);
1108  goto cleanup;
1109  }
1110 
1111  if(cpl_table_has_column(tbl_slitpos,"pos2") != 1) {
1112  sinfo_msg_error("Column 'pos2' not found in %s table %s",
1113  PRO_SLIT_POS,file_name);
1114  goto cleanup;
1115  }
1116 
1117  check_nomsg(n = cpl_table_get_nrow(tbl_slitpos));
1118  slit_edges = sinfo_new_2Dfloatarray(n, 2);
1119 
1120  for (i =0 ; i< n; i++) {
1121  val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status);
1122  val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status);
1123  sinfo_new_array2D_set_value(slit_edges,val_x,i,0);
1124  sinfo_new_array2D_set_value(slit_edges,val_y,i,1);
1125  }
1126  sinfo_free_table(&tbl_slitpos);
1127 
1128  strcpy(file_name,cfg->indexlist);
1129  check_nomsg(tbl_index = cpl_table_load(file_name,1,0));
1130  check_nomsg(n = cpl_table_get_nrow(tbl_index));
1131  cknull_nomsg(in_nam = (char**) cpl_calloc(n,sizeof(char*)));
1132 
1133  for (i =0 ; i< n; i++){
1134  strcpy(in_nam[i],cpl_table_get_string(tbl_index,"name",i));
1135  }
1136  sinfo_free_table(&tbl_index);
1137 
1138  for (i=0;i<cnt;i++){
1139  if (strcmp("ICube.fits", in_nam[i]) != 0){
1140  check_nomsg(iCube=cpl_imagelist_load("ICube.fits",CPL_TYPE_FLOAT,0));
1141  }
1142  else if (strcmp("JCube.fits", in_nam[i]) != 0){
1143  check_nomsg(jCube=cpl_imagelist_load("JCube.fits",CPL_TYPE_FLOAT,0));
1144  }
1145  else if (strcmp("X.fits", in_nam[i]) != 0){
1146  check_nomsg(X=cpl_image_load("X.fits",CPL_TYPE_FLOAT,0,0));
1147  }
1148  else if (strcmp("Y.fits", in_nam[i]) != 0){
1149  check_nomsg(Y=cpl_image_load("Y.fits",CPL_TYPE_FLOAT,0,0));
1150  }
1151  else if (strcmp("Z.fits", in_nam[i]) != 0){
1152  check_nomsg(Z=cpl_image_load("Z.fits",CPL_TYPE_FLOAT,0,0));
1153  }
1154  else if (strcmp("cX.fits", in_nam[i]) != 0) {
1155  check_nomsg(hX=cpl_image_load("cX.fits",CPL_TYPE_FLOAT,0,0));
1156  } else {
1157  sinfo_msg_error("wrong name in index list or needed file not there!");
1158  goto cleanup;
1159  }
1160  }
1161  lookup = sinfo_new_lookup();
1162  lookup->id=iCube;
1163  lookup->jd=jCube;
1164  lookup->X=X;
1165  lookup->Y=Y;
1166  lookup->Z=Z;
1167  lookup->hX=hX;
1168 
1169  cknull(im_obj = sinfo_new_c_bezier_interpolate_image(im_obj,
1170  mask_im,
1171  lookup,
1172  cfg->maxRad,
1173  cfg->maxRad,
1174  cfg->maxRad,
1175  2,
1176  slit_edges ),
1177  "could not carry out sinfo_new_c_bezier_interpolate_image" );
1178 
1179  if (cfg->contains_dither == 1) {
1180  cknull(im_dither=sinfo_new_c_bezier_interpolate_image( im_dither,
1181  mask_im,
1182  lookup,
1183  cfg->maxRad,
1184  cfg->maxRad,
1185  cfg->maxRad,
1186  2,
1187  slit_edges ),
1188  "could not carry out sinfo_new_c_bezier_interpolate_image"
1189  " on dithered frame" );
1190 
1191  }
1192  sinfo_new_destroy_2Dfloatarray(&slit_edges, 32);
1193  sinfo_free_image(&mask_im);
1194  sinfo_free_imagelist(&iCube);
1195  sinfo_free_imagelist(&jCube);
1196  sinfo_free_image(&lookup->X);
1197  sinfo_free_image(&lookup->Y);
1198  sinfo_free_image(&lookup->Z);
1199  sinfo_free_image(&lookup->hX);
1200  sinfo_new_destroy_lookup(lookup);
1201  }
1202 
1203 
1204  /*
1205  #---------------------------------------------------------
1206  # correction of distortions
1207  #---------------------------------------------------------
1208  */
1209 
1210  /* here memory leak */
1211  //sinfo_msg("cfg->warpfixInd=%d",cfg->warpfixInd);
1212  if (cfg->warpfixInd == 1){
1213  /*#open ASCII file containing the slope parameter and read it*/
1214  sinfo_msg("Correct object for distortions");
1215  cknull(int_im_shifted=sinfo_new_image_warp_fits(im_obj,
1216  cfg->kernel,
1217  cfg->polyFile),
1218  "could not carry out sinfo_image_warp_fits" );
1219 
1220  sinfo_free_image(&im_obj);
1221  im_obj = cpl_image_duplicate(int_im_shifted);
1222  if (cfg->contains_dither == 1){
1223  cknull(int_im_dith_shifted=sinfo_new_image_warp_fits(im_dither,
1224  cfg->kernel,
1225  cfg->polyFile),
1226  "could not carry out sinfo_image_warp_fits" );
1227  sinfo_free_image(&im_dither);
1228  im_dither = cpl_image_duplicate(int_im_dith_shifted);
1229  }
1230  }
1231 
1232  /*
1233  #---------------------------------------------------------
1234  # merge (interleave) both resulting frames
1235  #---------------------------------------------------------
1236  */
1237  if (cfg->interInd == 1 && cfg->contains_dither == 1){
1238  if( pdensity>1) {
1239  sinfo_msg("Merge (interleave) frames");
1240  cknull(im3 = cpl_image_new(cpl_image_get_size_x(im_obj),
1241  cpl_image_get_size_y(im_obj),CPL_TYPE_FLOAT),
1242  "could not allocate an image" );
1243  cknull(im4=sinfo_new_remove_general_offset( im_obj, im_dither,
1244  im3, cfg->noRows ),
1245  "sinfo_removeGeneralOffset failed" );
1246 
1247  cknull(im5 = sinfo_new_remove_regional_tilt ( im_obj, im4, im3 ),
1248  "sinfo_removeRegionalTilt failed" );
1249 
1250  cknull(im6 = sinfo_new_remove_column_offset ( im_obj, im5, im3 ),
1251  "sinfo_removeColumnOffset failed" );
1252 
1253  cknull(im7 = sinfo_new_remove_residual_tilt ( im6, im3 ),
1254  "sinfo_removeResidualTilt failed" );
1255 
1256  cknull(im8 = sinfo_new_remove_residual_offset ( im7, im3 ),
1257  "sinfo_removeResidualOffset failed");
1258  cknull(im9 = sinfo_sinfo_merge_images(im_obj, im8, im3),
1259  "sinfo_mergeImages failed" );
1260 
1261  ck0(sinfo_pro_save_ima(im9,raw,sof,cfg->outName,
1262  frm_pro_ctg,NULL,plugin_id,config),
1263  "cannot save ima %s", cfg->outName);
1264 
1265  sinfo_free_image(&im3);
1266  sinfo_free_image(&im4);
1267  sinfo_free_image(&im5);
1268  sinfo_free_image(&im6);
1269  sinfo_free_image(&im7);
1270  sinfo_free_image(&im8);
1271  sinfo_free_image(&im9);
1272  sinfo_free_image(&im_obj);
1273  sinfo_free_image(&im_dither);
1274  }
1275  /*
1276  #---------------------------------------------------------
1277  # convolve spectra with Gaussian
1278  #---------------------------------------------------------
1279  */
1280  } else if (cfg->gaussInd == 1 && cfg->interInd == 0) {
1281  sinfo_msg("Convolve spectra with Gaussian");
1282  if(pdensity > 1) {
1283  cknull(im_conv = sinfo_new_convolve_image_by_gauss ( im_obj, cfg->hw ),
1284  "sinfo_convolveImageByGauss failed" );
1285 
1286  ck0(sinfo_pro_save_ima(im_conv,raw,sof,cfg->outName,
1287  frm_pro_ctg,NULL,plugin_id,config),
1288  "cannot save ima %s", cfg->outName);
1289 
1290  sinfo_free_image(&im_obj);
1291  sinfo_free_image(&im_conv);
1292  if (cfg->contains_dither == 1){
1293  sinfo_free_image(&im_dither);
1294  }
1295  }
1296  } else {
1297 
1298  sinfo_msg("Add QC LOG");
1299  /* add QC-LOG */
1300  /* sinfo_det_ncounts(raw, cfg->qc_thresh_max); */
1301  cknull_nomsg(qclog_tbl = sinfo_qclog_init());
1302 
1303  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMON MEANFLUX",
1304  qc->avg_on,"Average of flux","%g"));
1305  /* ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMON MEANFLUX",
1306  qc->avg_on,"Average of flux","%g"));*/
1307  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMOFF MEANFLUX",
1308  qc->avg_of,"Average of flux","%g"));
1309  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMDIF MEANFLUX",
1310  qc->avg_di,"Average of flux","%g"));
1311  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMON MAXFLUX",
1312  qc->max_on,"Max of flux","%g"));
1313  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMOFF MAXFLUX",
1314  qc->max_of,"Max of flux","%g"));
1315  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMDIF MAXFLUX",
1316  qc->max_di,"Max of flux","%g"));
1317  ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FRMON NPIXSAT",
1318  qc->nsat,
1319  "Number of saturated pixels","%d"));
1320  update_bad_pixel_map(im_obj);
1321  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMDIF MEANSTD",
1322  cpl_image_get_mean(im_obj),"mean of the image","%13.6f"));
1323 
1324  ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,
1325  "QC FRMDIF STDEV",
1326  cpl_image_get_stdev(im_obj),
1327  "standard deviation of the image",
1328  "%13.6f"));
1329 
1330  ck0(sinfo_pro_save_ima(im_obj,raw,sof,cfg->outName,
1331  frm_pro_ctg,qclog_tbl,plugin_id,config),
1332  "cannot dump ima %s", cfg->outName);
1333 
1334  sinfo_free_image(&im_obj);
1335  sinfo_free_table(&qclog_tbl);
1336 
1337  if (cfg->contains_dither == 1 && cfg->interInd == 0) {
1338  if (strstr(cfg->outName, ".fits" ) != NULL ) {
1339  snprintf(name_list, MAX_NAME_SIZE-1,"%s%s",
1340  sinfo_new_get_rootname(cfg->outName), "_dith.fits");
1341  strcpy(cfg->outName,name_list);
1342  } else {
1343  strcat(cfg->outName,"_dith");
1344  }
1345  cpl_free(name_list);
1346 
1347 
1348 
1349  ck0(sinfo_pro_save_ima(im_dither,raw,sof,cfg->outName,
1350  frm_pro_ctg,NULL,plugin_id,config),
1351  "cannot save ima %s", cfg->outName);
1352 
1353  if (cfg->contains_dither == 1) {
1354  sinfo_free_image(&im_dither);
1355  }
1356  }
1357 
1358  }
1359 
1360  /* the following generates a valgrind error
1361  but without it 8 bytes are leaked */
1362  sinfo_free_image_array(&im,cfg->nframes);
1363 
1364  /*AMO: check if those images can be deleted before */
1365  sinfo_free_image(&int_im_shifted);
1366  sinfo_free_image(&int_im);
1367  sinfo_free_image(&im_obj_flat);
1368  sinfo_free_image(&im_obj_sub);
1369 
1370  sinfo_stack_free(&cfg);
1371  sinfo_free_frameset(&raw);
1372  sinfo_qc_wcal_delete(&qc);
1373 
1374  return 0;
1375 
1376  cleanup:
1377  sinfo_free_table(&qclog_tbl);
1378  sinfo_free_table(&tbl_slitpos);
1379  sinfo_free_table(&tbl_index);
1380  if(slit_edges!=NULL) sinfo_new_destroy_2Dfloatarray(&slit_edges, 32);
1381  sinfo_free_image(&flat1_dist);
1382  sinfo_free_image(&flat1);
1383  sinfo_free_image(&flat_smooth);
1384  sinfo_free_image(&flat2_dist);
1385  sinfo_free_image(&int_im);
1386  sinfo_free_image(&int_im_dith);
1387  sinfo_free_image(&int_im_shifted);
1388  sinfo_free_image(&im_dither);
1389  sinfo_free_image(&flat2);
1390  sinfo_free_image(&im_obj_flat);
1391  sinfo_free_image(&im_obj_sub);
1392  sinfo_free_image(&im_obj);
1393  sinfo_free_image(&mask_im);
1394  sinfo_free_image(&im_sky);
1395  sinfo_free_imagelist(&list_object_tmp);
1396  sinfo_free_image(&sky_img_flat);
1397  sinfo_free_image(&sky_dist);
1398  sinfo_free_image(&sky_img);
1399  sinfo_free_imagelist(&list_object);
1400  sinfo_free_imagelist(&list_sky);
1401  if(im != NULL) sinfo_free_image_array(&im,cfg->nframes);
1402  sinfo_stack_free(&cfg);
1403  sinfo_free_frameset(&raw);
1404  sinfo_qc_wcal_delete(&qc);
1405 
1406  return -1 ;
1407 
1408 }
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69
#define sinfo_msg_warning(...)
Print an warning message.
Definition: sinfo_msg.h:93