sinfo_new_prepare_stacked_frames.c

00001 /*
00002  * This file is part of the ESO SINFONI Pipeline
00003  * Copyright (C) 2004,2005 European Southern Observatory
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00018  */
00019 /*----------------------------------------------------------------------------
00020  
00021      File name    :     sinfo_new_prepare_stacked_frames.c
00022    Author       :   A. Modigliani
00023    Created on   :   Sep 17, 2003
00024    Description  : 
00025 
00026   this handles stacks of input frames, that means it takes a clean mean,
00027   subtracts the off- from the on-frames, flatfields, corrects for static bad 
00028   pixels, corrects for a linear tilt of the spectra if necessary, and finally, 
00029   interleaves dithered exposures or convolves a single exposure with a 
00030   Gaussian, respectively.
00031 
00032  ---------------------------------------------------------------------------*/
00033 #ifdef HAVE_CONFIG_H
00034 #  include <config.h>
00035 #endif
00036 
00037 /*----------------------------------------------------------------------------
00038                                 Includes
00039  ---------------------------------------------------------------------------*/
00040 #include "sinfo_new_prepare_stacked_frames.h"
00041 #include "sinfo_stack_ini_by_cpl.h"
00042 #include "sinfo_coltilt.h"
00043 #include "sinfo_image_ops.h"
00044 #include "sinfo_merge.h"
00045 #include "sinfo_utilities.h"
00046 #include "sinfo_wave_calibration.h"
00047 #include "sinfo_new_bezier.h"
00048 #include "sinfo_shift_images.h"
00049 #include "sinfo_product_config.h"
00050 
00051 #include "sinfo_pro_save.h"
00052 #include "sinfo_globals.h"
00053 #include "sinfo_utilities.h"
00054 #include "sinfo_dfs.h"
00055 #include "sinfo_raw_types.h"
00056 #include "sinfo_wcal_functions.h"
00057 
00058 #include "sinfo_hidden.h"
00059 #include "sinfo_pro_types.h"
00060 #include "sinfo_functions.h"
00061 #include "sinfo_utils_wrappers.h"
00062 #include "sinfo_error.h"
00063 
00064 /*----------------------------------------------------------------------------
00065                                 Defines
00066  ---------------------------------------------------------------------------*/
00076 /*----------------------------------------------------------------------------
00077                              Function Definitions
00078  ---------------------------------------------------------------------------*/
00079 
00080 /*----------------------------------------------------------------------------
00081    Function     :       sinfo_new_prepare_stacked_frames()
00082    In           :       ini_file: file name of according .ini file
00083    Out          :       integer (0 if it worked, -1 if it doesn't) 
00084    Job          : 
00085   this handles stacks of input frames, that means it takes a clean mean,
00086   subtracts the off- from the on-frames, flatfields, corrects for static bad 
00087   pixels, corrects for a linear tilt of the spectra if necessary, and finally, 
00088   interleaves dithered exposures or convolves a single exposure with a 
00089   Gaussian, respectively.
00090 
00091  ---------------------------------------------------------------------------*/
00092 static int 
00093 new_get_names(const char* pcatg, const int ind, stack_config_n ** cfg);
00094 
00095 static int 
00096 new_get_names(const char* pcatg, const int ind, stack_config_n **  cfg){
00097 
00098   if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_OFF) == 0) {
00099      strcpy((*cfg)->outName,"out_ns_stack_off.fits");
00100   }
00101   if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_ON)  == 0) {
00102      strcpy((*cfg)->outName,"out_ns_stack_on.fits");
00103   }
00104   if (strcmp(pcatg,PRO_FIBRE_NS_STACKED)     == 0) {
00105      strcpy((*cfg)->outName,"out_ns_stack.fits");
00106   }
00107   if (strcmp(pcatg,PRO_WAVE_LAMP_STACKED)    == 0) {
00108      strcpy((*cfg)->outName,"out_wcal_stack.fits");
00109   }
00110   if (strcmp(pcatg,PRO_FIBRE_NS_STACKED_DIST)== 0) {
00111      strcpy((*cfg)->outName,"out_ns_stack_warp.fits");
00112   }
00113   if (strcmp(pcatg,PRO_WAVE_SLITPOS_STACKED) == 0) {
00114      strcpy((*cfg)->outName,"out_slit_pos_stack.fits");
00115   }
00116 
00117   if (strcmp(pcatg,PRO_PSF_CALIBRATOR_STACKED)== 0) {
00118      snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
00119   }
00120   if (strcmp(pcatg,PRO_SKY_PSF_CALIBRATOR_STACKED)== 0) {
00121      strcpy((*cfg)->outName,STACKED_OUT_FILENAME);
00122   }
00123   if (strcmp(pcatg,PRO_STD_NODDING_STACKED) == 0) {
00124      snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
00125   }
00126   /* only 1 frame
00127   if (strcmp(pcatg,PRO_STD_NODDING_STACKED) == 0) {
00128     strcpy((*cfg)->outName,STACKED_OUT_FILENAME); 
00129   }
00130   */
00131 
00132   if (strcmp(pcatg,PRO_SKY_NODDING_STACKED) == 0) {
00133     strcpy((*cfg)->outName,STACKED_OUT_FILENAME); /*STD*/
00134   }
00135   if (strcmp(pcatg,PRO_OBJECT_NODDING_STACKED) == 0) {
00136     snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
00137   }
00138   if (strcmp(pcatg,PRO_PUPIL_LAMP_STACKED) == 0) {
00139     snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
00140   }
00141   if (strcmp(pcatg,PRO_STACKED) == 0) {
00142     snprintf((*cfg)->outName,MAX_NAME_SIZE-1,"%s%d%s","out_stack",ind,".fits");
00143   }
00144 
00145   snprintf((*cfg)->sky_name,MAX_NAME_SIZE-1,"%s%d%s","out_sky",ind,".fits");
00146 
00147   return 0;
00148 
00149 }
00150 
00151 
00152 int sinfo_new_prepare_stacked_frames (const char* plugin_id,
00153                                 cpl_parameterlist* config, 
00154                                 cpl_frameset* sof, 
00155                                 const char* frm_pro_ctg,
00156                                 const int frm_ind, 
00157                                 fake* fk)
00158 {
00159 
00160 
00161   stack_config_n * cfg =NULL;
00162   cpl_imagelist * list_object=NULL ;
00163   cpl_imagelist * list_dither_object=NULL;
00164   cpl_imagelist * list_dither_sky=NULL;
00165   cpl_imagelist * list_sky=NULL;
00166   cpl_imagelist * list_dark=NULL;
00167   new_Lookup* lookup=NULL;
00168   cpl_image * im3=NULL ;
00169   cpl_image * im4=NULL ;
00170   cpl_image * im5=NULL ;
00171   cpl_image * im6=NULL ;
00172   cpl_image * im7=NULL ;
00173   cpl_image * im8=NULL ;
00174   cpl_image * im9=NULL ;
00175 
00176   cpl_image * ref_im1=NULL ;
00177   cpl_image * ref_im2=NULL ;
00178   cpl_image ** im=NULL ;
00179   cpl_image * im_obj=NULL ;
00180   cpl_image* simg=NULL;
00181 
00182   cpl_image * im_dark=NULL ;
00183   cpl_image * im_sky=NULL ;
00184   cpl_image * im_dither=NULL ;
00185   cpl_image * im_dither_sky=NULL ;
00186   cpl_image * im_obj_sub=NULL ;
00187   cpl_image * im_obj_flat=NULL ;
00188   cpl_image * im_dither_sub=NULL ;
00189   cpl_image * im_dither_flat=NULL ;
00190   cpl_image * int_im_shifted=NULL ;
00191   cpl_image * int_im_dith_shifted=NULL ;
00192   cpl_image * im_conv=NULL ;
00193 
00194    
00195   cpl_image * mask_im=NULL ;
00196   cpl_image * flat1=NULL ;
00197   cpl_image * flat2=NULL ;
00198   cpl_image * int_im=NULL ;
00199   cpl_image * int_im_dith=NULL ;
00200   cpl_image * sky_img_flat=NULL;
00201   cpl_image * sky_dist=NULL;
00202 
00203 
00204   cpl_imagelist * iCube=NULL ;
00205   cpl_imagelist * jCube=NULL ;
00206   cpl_image * X=NULL ;
00207   cpl_image * hX=NULL ;
00208   cpl_image * Y=NULL ;
00209   cpl_image * Z=NULL ;
00210   cpl_table * qclog_tbl=NULL;
00211   cpl_image* sky_img=NULL;
00212   cpl_image* mdark=NULL;
00213 
00214   char* name=NULL;
00215   int typ=0;
00216   int pos=0;
00217   int i = 0;
00218   int n = 0;
00219   int cnt = 0 ;
00220   float val_x=0;
00221   float val_y=0;
00222   int status=0;
00223 
00224   float** slit_edges=NULL;
00225   char** in_nam=NULL;
00226 
00227   int nob = 0;
00228   int nsky = 0; 
00229   int nobjdith = 0;
00230   int nskydith = 0; 
00231   int nda = 0;
00232   char name_list[MAX_NAME_SIZE];
00233   int no=0;
00234   float lo_cut=0;
00235   float hi_cut=0;
00236 
00237   cpl_imagelist* list_object_tmp=NULL;
00238   cpl_imagelist* list_dither_object_tmp=NULL;
00239   cpl_imagelist* list_sky_tmp=NULL;
00240   cpl_imagelist* list_dither_sky_tmp=NULL;
00241 
00242   cpl_image* flat1_dist=NULL;
00243   cpl_image* flat2_dist=NULL;
00244  
00245   cpl_frameset* raw=NULL;
00246 
00247   char file_name[MAX_NAME_SIZE];
00248   cpl_table* tbl_index = NULL;
00249   cpl_table* tbl_slitpos=NULL;
00250   int rhead=0;
00251 
00252   cpl_frame*     sky_frame = NULL;
00253   char* sky_name  = NULL;
00254   char* sky_tag   = NULL;
00255   qc_wcal* qc=sinfo_qc_wcal_new();
00256 
00257   cpl_parameter* p=NULL;
00258   int pdensity=0;
00259 
00260 
00261   /*
00262        -----------------------------------------------------------------
00263        1) parse the file names and parameters to the psf_config data 
00264           structure cfg
00265        -----------------------------------------------------------------
00266   */
00267 
00268 
00269   check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
00270   check_nomsg(pdensity=cpl_parameter_get_int(p));
00271   check_nomsg(raw=cpl_frameset_new());
00272   cknull(cfg = sinfo_parse_cpl_input_stack(config,sof,&raw, fk),
00273         "could not parse cpl input file!") ;
00274 
00275   ck0(sinfo_det_ncounts(raw, cfg->qc_thresh_max,qc),"computing det ncounts");
00276   /* defines output file name for stack set i */
00277   ck0_nomsg(new_get_names(frm_pro_ctg, frm_ind, &cfg));
00278   
00279   if (cfg->flatInd == 1){
00280     if(sinfo_is_fits_file(cfg->flatfield1) != 1) {
00281        sinfo_msg_error("Input FF file %s is not FITS",cfg->flatfield1);
00282        goto cleanup;
00283     }
00284   
00285   }
00286   if (cfg->maskInd == 1) {
00287     if(sinfo_is_fits_file(cfg->mask) != 1) {
00288          sinfo_msg_error("Input mask file %s is not FITS",cfg->mask);
00289          goto cleanup;
00290     }
00291     if(cfg -> indind == 0) {
00292       if(sinfo_is_fits_file(cfg->slitposList) != 1) {
00293     sinfo_msg_error("Input slitpos file %s is not FITS",cfg->slitposList);
00294     goto cleanup;
00295       }
00296     }
00297   }
00298 
00299   /*
00300    #---------------------------------------------------------
00301    # Take a clean mean of several images
00302    # input is 1 or more similar images
00303    #---------------------------------------------------------
00304   */
00305 
00306   if (cfg->sfInd == 1){
00307     if (cfg->contains_dark == 0) {
00308       sinfo_msg_warning("no sinfo_dark frames given!");
00309     }
00310     if (cfg->contains_ref == 0) {
00311       sinfo_msg_error("no reference frames given!");
00312       goto cleanup;
00313     }
00314   }
00315 
00316   /* allocate memory for lists of object, sky and dithered frames */
00317   check(list_object=cpl_imagelist_new(),
00318         "could not allocate memory for object frame");
00319  
00320   if (cfg->contains_dither == 1) {
00321     check(list_dither_object=cpl_imagelist_new(),
00322           "could not allocate memory for dither object frame");
00323   }
00324 
00325   if (cfg->contains_sky == 1) {
00326     check(list_sky = cpl_imagelist_new(),
00327        "could not allocate memory for off frame list");
00328   }
00329 
00330   if (cfg->contains_dither == 1 && cfg->nditheroff > 0) {
00331      check(list_dither_sky = cpl_imagelist_new(),
00332        "could not allocate memory for dither frame list");
00333   }
00334 
00335   if (cfg->contains_dark == 1 && cfg->sfInd == 1) {
00336      check(list_dark = cpl_imagelist_new(),
00337        "could not allocate memory for sinfo_dark frame");
00338   }
00339 
00340   if (cfg->contains_dither == 0 && cfg->nditheroff > 0) {
00341      sinfo_msg_error("please use non-dithered off-frames, remove the 2!");
00342      goto cleanup;
00343   }
00344 
00345   /* build different image lists for the different cases */
00346   cknull_nomsg(im=(cpl_image**)cpl_calloc(cfg -> nframes, sizeof(cpl_image*)));
00347 
00348   for (i=0; i< cfg->nframes; i++) {
00349      name = cfg->framelist[i];
00350      if(sinfo_is_fits_file(name) != 1) {
00351        sinfo_msg_error("Input file %s is not FITS",name);
00352        goto cleanup;
00353      }
00354      check_nomsg(im[i] = cpl_image_load( name,CPL_TYPE_FLOAT,0,0));
00355   }
00356 
00357   /* up to here leak free */
00358   rhead=0;
00359   for (i=0; i< cfg->nframes; i++) {
00360     typ = sinfo_new_intarray_get_value( cfg->frametype, i );
00361     pos = sinfo_new_intarray_get_value( cfg->frameposition, i );
00362     cknull(im[i],"could not load image");
00363  
00364     if (pos == 2) {
00365       if (typ == 1) {
00366         check_nomsg(cpl_imagelist_set(list_object,
00367                                       cpl_image_duplicate(im[i]),nob));  
00368         nob = nob + 1;
00369       }
00370       else if ( typ == 0 ) {
00371         check_nomsg(cpl_imagelist_set(list_sky,
00372                     cpl_image_duplicate(im[i]),nsky));  
00373         nsky = nsky + 1;
00374         if((pdensity == 2) || (pdensity == 1 && frm_ind == 0)) {
00375       check_nomsg(sky_frame = cpl_frameset_get_frame(raw,i));
00376       check_nomsg(sky_name  = (char*) cpl_frame_get_filename(sky_frame));
00377       check_nomsg(sky_tag   = (char*) cpl_frame_get_tag(sky_frame));
00378 
00379           if ( (strstr(frm_pro_ctg,"OBJECT") != NULL) ||
00380                (strstr(frm_pro_ctg,"PSF") != NULL) ||
00381                (strstr(frm_pro_ctg,"STD") != NULL) ) { 
00382         check_nomsg(sky_img = cpl_image_load(sky_name,CPL_TYPE_FLOAT,0,0));
00383         snprintf(sky_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_sky",frm_ind,".fits");
00384         ck0(sinfo_pro_save_ima(sky_img,raw,sof,sky_name,
00385                        PRO_SKY_STACKED_DUMMY,NULL,
00386                                    plugin_id,config),
00387         "cannot save sky ima %s", sky_name);
00388 
00389         sinfo_free_image(&sky_img);
00390         if (cfg->flatInd == 1) {
00391           sinfo_msg("Sky Flatfielding");
00392           check(flat1=cpl_image_load(cfg->flatfield1,CPL_TYPE_FLOAT,0,0 ),
00393             "could not load flatfield image" );
00394  
00395 
00396           check_nomsg(simg = cpl_image_load(sky_name,CPL_TYPE_FLOAT,0,0 ));
00397           cknull(sky_img_flat=sinfo_new_div_images_robust(simg,flat1),
00398             "could not carry out flatfield division" );
00399           sinfo_free_image(&simg);
00400           sinfo_free_image(&flat1);
00401 
00402           /* if (frm_ind == 0) { */
00403           if (cfg->warpfixInd == 1){
00404             sinfo_msg("correct FF for distortions");
00405                 snprintf(file_name,MAX_NAME_SIZE-1,"%s%d%s",STACK_SKY_DIST_OUT_FILENAME,
00406                         frm_ind,".fits");
00407             sky_dist = sinfo_new_image_warp_fits(sky_img_flat, 
00408                                                     cfg->kernel, 
00409                                                     cfg->polyFile);
00410 
00411             ck0(sinfo_pro_save_ima(sky_dist,raw,sof,
00412                        file_name,
00413                        PRO_STACK_SKY_DIST,NULL,plugin_id,
00414                        config),"cannot save ima %s", 
00415             STACK_SKY_DIST_OUT_FILENAME);
00416 
00417         sinfo_free_image(&sky_dist);
00418           }
00419           /* } */
00420           sinfo_free_image(&sky_img_flat);
00421         } /* end check on flatind */
00422       } /* end check on procatg */
00423     }
00424       } else if ( typ == 5 ) {
00425     if (cfg->sfInd == 1) {
00426       check_nomsg(cpl_imagelist_set(list_dark,
00427                       cpl_image_duplicate(im[i]),nda));  
00428       nda = nda + 1;
00429     } else {
00430       sinfo_free_image(&(im[i]));
00431     }
00432       } else if ( typ == 4 ) {
00433     if ( cfg->sfInd == 1) {
00434       ref_im1 = im[i];
00435     } else {
00436       sinfo_free_image(&(im[i]));
00437     }
00438       }
00439     } else {
00440       if (typ == 1) {
00441        check_nomsg(cpl_imagelist_set(list_dither_object,
00442                                      cpl_image_duplicate(im[i]),nobjdith));  
00443        nobjdith = nobjdith + 1;
00444       } else if (typ == 0) {
00445     check_nomsg(cpl_imagelist_set(list_dither_object,
00446                                      cpl_image_duplicate(im[i]),nskydith));  
00447     nskydith = nskydith + 1;
00448       } else if (typ == 4) {
00449     if (cfg->sfInd == 1) {
00450       ref_im2 = cpl_image_duplicate(im[i]);
00451     } else {
00452       sinfo_free_image(&(im[i]));
00453     }
00454       }
00455     }
00456   } /* end for loop on i */
00457 
00458   if (nob != cfg->nobj || 
00459       cfg->noff != nsky || 
00460       nobjdith != cfg->nditherobj || 
00461       nskydith != cfg->nditheroff) {
00462     sinfo_msg_error("something is wrong with the number of the");
00463     sinfo_msg_error("different types of frames");
00464     /* free memory */
00465     goto cleanup;
00466 
00467   }
00468 
00469   if (cfg->sfInd == 1 && nda != cfg->ndark) {
00470     sinfo_msg_error("something is wrong with the number of sinfo_dark frames");
00471     goto cleanup;
00472   }
00473   sinfo_msg("Create and fill cubes with the different image lists");
00474 
00475   /* create and fill cubes with the different image lists */
00476   cknull(list_object,"could not create object data cube!");
00477 
00478   /* shift the images in the cubes-if indicated-in spectral direction 
00479      with respect to the reference image
00480   */
00481   if (cfg->sfInd == 1) {
00482 
00483   /*
00484       first take the mean of the sinfo_dark frames and subtract the result 
00485       from all cubes
00486   */
00487   sinfo_msg("shift cube images in spectral direction with "
00488             "respect to reference");
00489 
00490   if (cfg->contains_dark == 1) {
00491     sinfo_msg("cfg->contains_dark == 1");
00492     if (cfg->loReject*cfg->ndark < 1. && cfg->hiReject * cfg->ndark < 1.) {
00493       /*
00494           im_dark = sinfo_new_average_cube_to_image( list_dark );
00495       */
00496       check(im_dark = cpl_imagelist_collapse_create( list_dark ),
00497      "sinfo_averageCubeToImage failed" );
00498      } 
00499      else {
00500 
00501        check_nomsg(no=cpl_imagelist_get_size(list_dark));
00502        lo_cut=(floor)( cfg->loReject*no+0.5);
00503        hi_cut=(floor)( cfg->hiReject*no+0.5);
00504        check(im_dark=cpl_imagelist_collapse_minmax_create(list_dark,lo_cut,
00505                                                           hi_cut),
00506                                      "sinfo_average_with_rejection failed" );
00507 
00508      }
00509      sinfo_free_imagelist(&list_dark);
00510      check_nomsg(list_object_tmp = cpl_imagelist_duplicate (list_object));
00511      check(cpl_imagelist_subtract_image (list_object_tmp, im_dark),
00512             "cpl_imagelist_subtract_image failed" );
00513      /*
00514        cube_object_tmp = sinfo_subImageFromCube (cube_object, im_dark);
00515      */
00516   } else {
00517      sinfo_msg("cfg->contains_dark == 0");
00518      check_nomsg(list_object_tmp = cpl_imagelist_duplicate(list_object));
00519   }
00520   sinfo_free_imagelist(&list_object);
00521 
00522   cknull(list_object = sinfo_new_align_cube_to_reference (list_object_tmp,
00523                               ref_im1,
00524                               cfg->sfOrder,
00525                               cfg->sfType),
00526      "sinfo_new_align_cube_to_reference failed" );
00527 
00528   sinfo_free_imagelist(&list_object_tmp);
00529   if (cfg->contains_dither == 1) {
00530     if (cfg->contains_dark == 1) {
00531        check_nomsg(list_dither_object_tmp = 
00532                    cpl_imagelist_duplicate(list_dither_object));
00533        check(cpl_imagelist_subtract_image(list_dither_object_tmp,im_dark),
00534              "sinfo_average_with_rejection failed" );
00535 
00536        /*     
00537         list_dither_object_tmp = 
00538               sinfo_new_sub_image_from_cube(list_dither_object, 
00539                                                     im_dark);
00540     */
00541 
00542     } else {
00543        check_nomsg(list_dither_object_tmp=
00544                    cpl_imagelist_duplicate(list_dither_object));
00545     }
00546     sinfo_free_imagelist(&list_dither_object);
00547 
00548     cknull(list_dither_object=
00549             sinfo_new_align_cube_to_reference (list_dither_object_tmp,
00550                                  ref_im2, 
00551                                  cfg->sfOrder, 
00552                                  cfg->sfType),
00553        "sinfo_new_align_cube_to_reference failed" );
00554    
00555     sinfo_free_imagelist(&list_dither_object_tmp);
00556   }
00557   if (cfg->contains_sky == 1) {
00558     if (cfg->contains_dark == 1) {
00559        check_nomsg(list_sky_tmp = cpl_imagelist_duplicate(list_sky));
00560        check(cpl_imagelist_subtract_image (list_sky_tmp, im_dark),
00561          "sinfo_average_with_rejection failed" );
00562        /*
00563       cube_sky_tmp = sinfo_subImageFromCube (cube_sky, im_dark);
00564        */
00565 
00566     } else {
00567        check_nomsg(list_sky_tmp = cpl_imagelist_duplicate(list_sky));
00568     }
00569     check_nomsg(list_sky_tmp=cpl_imagelist_duplicate(list_sky));
00570       
00571     check(list_sky = sinfo_new_align_cube_to_reference (list_sky_tmp, 
00572                             ref_im1,
00573                             cfg->sfOrder, 
00574                             cfg->sfType),
00575       "sinfo_alignCubeToReference failed" );
00576 
00577     check_nomsg(cpl_imagelist_delete(list_sky_tmp));
00578   }
00579   if (cfg->contains_dither == 1 && cfg->contains_sky == 1) {
00580     if (cfg->contains_dark == 1) {
00581       check_nomsg(list_dither_sky_tmp=cpl_imagelist_duplicate(list_dither_sky));
00582       check(cpl_imagelist_subtract_image(list_dither_sky_tmp,im_dark),
00583         "sinfo_average_with_rejection failed" );
00584        /*
00585        cube_dither_sky_tmp = sinfo_subImageFromCube (cube_dither_sky, im_dark);
00586        */
00587 
00588     } else {
00589       check_nomsg(list_dither_sky_tmp=cpl_imagelist_duplicate(list_dither_sky));
00590     }
00591     sinfo_free_imagelist(&list_dither_sky);
00592 
00593     check(list_dither_sky=sinfo_new_align_cube_to_reference(list_dither_sky_tmp, 
00594                                 ref_im2, 
00595                                 cfg->sfOrder, 
00596                                 cfg->sfType),
00597       "sinfo_alignCubeToReference failed" );
00598 
00599     sinfo_free_imagelist(&list_dither_sky_tmp);
00600   }
00601   sinfo_free_image(&ref_im1);
00602   if (cfg->contains_dither == 1) {
00603     sinfo_free_image(&ref_im2);
00604   }
00605   if (cfg->contains_dark == 1) {
00606     sinfo_free_image(&im_dark);
00607   }
00608 
00609   } /* end if over sfInd */
00610 
00611   /* subtracts the master dark from different frames if present */
00612   if(cfg->mdark_ind==1){
00613     sinfo_msg("Subtract master dark %s ",cfg->mdark);
00614 
00615     check_nomsg(mdark=cpl_image_load(cfg->mdark,CPL_TYPE_FLOAT,0,0));
00616     if (list_object !=NULL) {
00617       cpl_imagelist_subtract_image (list_object, mdark);
00618     }
00619     if (list_sky !=NULL) {
00620       cpl_imagelist_subtract_image (list_sky, mdark);
00621     }
00622     sinfo_free_image(&mdark);
00623   }
00624   /* take the average with rejection of the different cubes */
00625   sinfo_msg("take the average of the different cubes");
00626 
00627   if (cfg->loReject*cfg->nobj < 1. && cfg->hiReject * cfg->nobj < 1.) {
00628     check(im_obj = cpl_imagelist_collapse_create(list_object),
00629       "average with rejection failed" );
00630   } else {
00631 
00632     check_nomsg(no=cpl_imagelist_get_size(list_object));
00633     lo_cut=(floor)( cfg->loReject*no+0.5);
00634     hi_cut=(floor)( cfg->hiReject*no+0.5);
00635     check(im_obj=cpl_imagelist_collapse_minmax_create(list_object,
00636                  lo_cut,hi_cut),"average with rejection failed" );
00637 
00638   }  
00639   sinfo_free_imagelist(&list_object); 
00640 
00641   if (cfg->contains_sky == 1) {
00642     if (cfg->loReject*nsky < 1. && cfg->hiReject*nsky < 1.) {
00643       /* here might explode in dither mode */
00644       cknull(im_sky = cpl_imagelist_collapse_create( list_sky ),
00645          "average with rejection failed");
00646     } else {
00647 
00648       check_nomsg(no=cpl_imagelist_get_size(list_sky));
00649       lo_cut=(floor)( cfg->loReject*no+0.5);
00650       hi_cut=(floor)( cfg->hiReject*no+0.5);
00651       check(im_sky=cpl_imagelist_collapse_minmax_create(list_sky,lo_cut,hi_cut),
00652          "average with rejection failed");
00653     }
00654     sinfo_free_imagelist(&list_sky);
00655   }
00656 
00657   if (cfg->contains_dither == 1) {
00658     if (cfg->loReject*nobjdith < 1. && cfg->hiReject*nobjdith < 1.) {
00659       check(im_dither = cpl_imagelist_collapse_create( list_dither_object ),
00660         "average with rejection failed");
00661     } else {
00662 
00663       check_nomsg(no=cpl_imagelist_get_size(list_dither_object));
00664       lo_cut=(floor)( cfg->loReject*no+0.5);
00665       hi_cut=(floor)( cfg->hiReject*no+0.5);
00666       check(im_dither=cpl_imagelist_collapse_minmax_create(list_dither_object,
00667                                lo_cut,hi_cut),
00668         "average with rejection failed");
00669 
00670     }
00671     sinfo_free_imagelist(&list_dither_object);
00672   }
00673   
00674   if (cfg->contains_dither == 1 && nskydith > 0) {
00675     if (cfg->loReject*nskydith < 1. && cfg->hiReject*nskydith < 1.) {
00676       check(im_dither_sky = cpl_imagelist_collapse_create( list_dither_sky ),
00677         "average with rejection failed");
00678     } else {
00679 
00680       check_nomsg(no=cpl_imagelist_get_size(list_dither_sky));
00681       lo_cut=(floor)( cfg->loReject*no+0.5);
00682       hi_cut=(floor)( cfg->hiReject*no+0.5);
00683       check(im_dither_sky=cpl_imagelist_collapse_minmax_create(list_dither_sky,
00684                                    lo_cut,hi_cut),
00685         "average with rejection failed");
00686     }
00687     sinfo_free_imagelist(&list_dither_sky);
00688   }
00689 
00690   /*
00691   #---------------------------------------------------------
00692   # Subtract the resulting off-frame (sky) from the on-frame 
00693   #-------------------------------------------------------
00694   # finally, subtract off from on frames and store the result 
00695   # in the object cube
00696   */
00697 
00698   if (cfg->contains_sky == 1) {
00699     sinfo_msg("Subtract the resulting off-frame (sky) from the on-frame");
00700     check_nomsg(im_obj_sub = cpl_image_duplicate(im_obj));
00701     check(cpl_image_subtract(im_obj_sub, im_sky),
00702       "could not sinfo_sub_image");
00703 
00704     sinfo_free_image(&im_obj);
00705     if (((cfg->contains_dither == 1) && (nskydith > 0)) || 
00706           cfg->contains_dither == 0) {
00707       sinfo_free_image(&im_sky);
00708       im_obj = cpl_image_duplicate(im_obj_sub);
00709     }
00710   }
00711 
00712   if (cfg->contains_dither == 1 && nskydith > 0) {
00713     check_nomsg(im_dither_sub = cpl_image_duplicate(im_dither));
00714     check(cpl_image_subtract(im_dither_sub, im_dither_sky),
00715       "could not sinfo_sub_image");
00716 
00717     sinfo_free_image(&im_dither);
00718     sinfo_free_image(&im_dither_sky);
00719     im_dither = cpl_image_duplicate(im_dither_sub);
00720 
00721   } else if (cfg->contains_dither == 1 && 
00722              nskydith == 0 && 
00723              cfg->contains_sky == 1) {
00724     check_nomsg(im_dither_sub = cpl_image_duplicate(im_dither));
00725     check(cpl_image_subtract(im_dither_sub, im_sky),
00726       "could not sinfo_sub_image");
00727 
00728     sinfo_free_image(&im_dither);
00729     sinfo_free_image(&im_sky);
00730     im_dither = cpl_image_duplicate(im_dither_sub);
00731   }
00732 
00733 
00734   /*
00735   #---------------------------------------------------------
00736   # Flatfielding
00737   #---------------------------------------------------------
00738   */
00739 
00740   if (cfg->flatInd == 1) {
00741     sinfo_msg("Flatfielding");
00742     check(flat1 = cpl_image_load (cfg->flatfield1,CPL_TYPE_FLOAT,0,0),
00743       "could not load flatfield image" );
00744 
00745     cknull(im_obj_flat = sinfo_new_div_images_robust( im_obj, flat1),
00746        "could not carry out flatfield division" );
00747  
00748     /* AMO ** */
00749     if (frm_ind == 0) {
00750       if (cfg->warpfixInd == 1){
00751     sinfo_msg("correct FF for distortions");
00752     /* AMO check */
00753     cknull_nomsg(flat1_dist=sinfo_new_image_warp_fits(flat1,cfg->kernel,
00754                                  cfg->polyFile));
00755 
00756     ck0(sinfo_pro_save_ima(flat1_dist,raw,sof,
00757                    STACK_MFLAT_DIST_OUT_FILENAME,
00758                    PRO_STACK_MFLAT_DIST,NULL,plugin_id,config),
00759         "cannot save ima %s", STACK_MFLAT_DIST_OUT_FILENAME);
00760     sinfo_free_image(&flat1_dist);
00761       }
00762     }
00763 
00764     sinfo_free_image(&flat1);
00765     sinfo_free_image(&im_obj);  
00766     im_obj = cpl_image_duplicate(im_obj_flat);
00767 
00768     if (cfg->contains_dither == 1) {
00769       check(flat2 = cpl_image_load (cfg->flatfield2,CPL_TYPE_FLOAT,0,0),
00770         "could not load flatfield image" );
00771 
00772       cknull(im_dither_flat = sinfo_new_div_images_robust( im_dither, flat2),
00773          "could not carry out flatfield division" );
00774 
00775       if (frm_ind == 0) {
00776     if (cfg->warpfixInd == 1) {
00777       sinfo_msg("correct FF for distortions");
00778       flat2_dist = sinfo_new_image_warp_fits(flat2, 
00779                                                  cfg->kernel, cfg->polyFile);
00780 
00781       ck0(sinfo_pro_save_ima(flat2_dist,raw,sof,
00782                                  STACK_MFLAT_DITHER_DIST_OUT_FILENAME,
00783                      PRO_STACK_MFLAT_DITHER_DIST,
00784                                  NULL,plugin_id,config),
00785           "cannot save ima %s", STACK_MFLAT_DITHER_DIST_OUT_FILENAME);
00786       sinfo_free_image(&flat2_dist);
00787     }
00788       }
00789       sinfo_free_image(&flat2);
00790       sinfo_free_image(&im_dither);  
00791       im_dither = cpl_image_duplicate(im_dither_flat);
00792     }
00793   }
00794 
00795   /*
00796   #---------------------------------------------------------
00797   # static bad pixel correction
00798   #---------------------------------------------------------
00799    */
00800 
00801   if (cfg->maskInd == 1) {
00802     sinfo_msg("static bad pixel correction");
00803     check(mask_im = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
00804       "could not load static bad pixel mask" );
00805 
00806     ck0(sinfo_new_change_mask(mask_im, im_obj),
00807     "sinfo_changeMask failed" );
00808   
00809     if (cfg->indind == 0) {
00810       /* open the ASCII list of the slitlet positions */
00811       /*READ TFITS TABLE*/
00812       strcpy(file_name,cfg->slitposList);
00813       check(tbl_slitpos = cpl_table_load(file_name,1,0),
00814         "error loading slitpos tbl %s ",file_name);
00815 
00816       check_nomsg(n = cpl_table_get_nrow(tbl_slitpos));
00817       cknull_nomsg(slit_edges = sinfo_new_2Dfloatarray(n, 2));
00818 
00819       for (i =0 ; i< n; i++){
00820     val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status);
00821     val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status);
00822     sinfo_new_array2D_set_value(slit_edges,val_x,i,0);
00823     sinfo_new_array2D_set_value(slit_edges,val_y,i,1);
00824       }
00825       sinfo_free_table(&tbl_slitpos);
00826 
00827       cknull(int_im = sinfo_interpol_source_image (im_obj,
00828                          mask_im,
00829                          cfg->maxRad,
00830                          slit_edges),
00831      "could not carry out sinfo_interpolSourceImage" );
00832 
00833       sinfo_free_image(&im_obj);
00834       im_obj = cpl_image_duplicate(int_im);
00835 
00836      if (cfg->contains_dither == 1) {
00837        cknull(int_im_dith = sinfo_interpol_source_image (im_dither,
00838                                mask_im,
00839                                cfg->maxRad,
00840                                slit_edges),
00841           "could not carry out sinfo_interpolSourceImage" );
00842 
00843        sinfo_free_image(&im_dither);
00844        im_dither = cpl_image_duplicate(int_im_dith);
00845      }
00846 
00847      sinfo_new_destroy_2Dfloatarray(&slit_edges, 32);
00848     } else {
00849       cknull(int_im = sinfo_new_mult_image_by_mask(im_obj, mask_im),
00850          "could not carry out sinfo_multImageByMask" );
00851 
00852       sinfo_free_image(&im_obj);
00853       im_obj = cpl_image_duplicate(int_im);
00854       if (cfg->contains_dither == 1) {
00855     cknull(int_im_dith=sinfo_new_mult_image_by_mask(im_dither, mask_im),
00856            "could not carry out sinfo_multImageByMask" );
00857 
00858     sinfo_free_image(&im_dither);
00859     im_dither = cpl_image_duplicate(int_im_dith);
00860       }
00861     }
00862     sinfo_free_image(&mask_im);
00863   }
00864 
00865   /*
00866   #---------------------------------------------------------
00867   # static bad pixel correction BEZIER
00868   #---------------------------------------------------------
00869   */
00870 
00871   if (cfg->maskInd == 2){
00872     sinfo_msg("static bad pixel correction BEZIER");
00873     check(mask_im = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
00874       "could not load static bad pixel mask" );
00875 
00876     ck0(sinfo_new_change_mask(mask_im, im_obj),
00877     "sinfo_changeMask failed" );
00878 
00879     /* #open the FITS table of the slitlet positions-*/
00880     strcpy(file_name,cfg->slitposList);
00881     check_nomsg(tbl_slitpos = cpl_table_load(file_name,1,0));
00882 
00883     check_nomsg(n = cpl_table_get_nrow(tbl_slitpos));
00884     slit_edges = sinfo_new_2Dfloatarray(n, 2);
00885 
00886     for (i =0 ; i< n; i++){
00887       val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status);
00888       val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status);
00889       sinfo_new_array2D_set_value(slit_edges,val_x,i,0);
00890       sinfo_new_array2D_set_value(slit_edges,val_y,i,1);
00891     }
00892     sinfo_free_table(&tbl_slitpos);
00893  
00894     strcpy(file_name,cfg->indexlist);
00895 
00896     check_nomsg(tbl_index = cpl_table_load(file_name,1,0));
00897 
00898     check_nomsg(n = cpl_table_get_nrow(tbl_index));
00899 
00900     cknull_nomsg(in_nam = (char**) cpl_calloc(n,sizeof(char*)));
00901          
00902     for (i =0 ; i< n; i++){
00903       strcpy(in_nam[i],cpl_table_get_string(tbl_index,"name",i));
00904     }
00905     sinfo_free_table(&tbl_index);
00906 
00907     for (i=0;i<cnt;i++) {
00908       if (strcmp("ICube.fits", name) != 0){
00909     check_nomsg(iCube=cpl_imagelist_load ("ICube.fits",CPL_TYPE_FLOAT,0));
00910       }
00911       else if (strcmp("JCube.fits", name) != 0) {
00912     check_nomsg(jCube = cpl_imagelist_load ("JCube.fits",CPL_TYPE_FLOAT,0));
00913       }
00914       else if (strcmp("X.fits",  name) != 0) {
00915     check_nomsg(X=cpl_image_load("X.fits",CPL_TYPE_FLOAT,0,0));
00916       }
00917       else if (strcmp("Y.fits",  name) != 0) {
00918     check_nomsg(Y=cpl_image_load("Y.fits",CPL_TYPE_FLOAT,0,0));
00919       }
00920       else if (strcmp("Z.fits",  name) != 0) {
00921     check_nomsg(Z=cpl_image_load("Z.fits",CPL_TYPE_FLOAT,0,0));
00922       }
00923       else if (strcmp("cX.fits", name) != 0) {
00924     check_nomsg(hX=cpl_image_load("cX.fits",CPL_TYPE_FLOAT,0,0));
00925       }
00926       else {
00927     sinfo_msg_error("wrong name in index list or needed file not there!");
00928     goto cleanup;
00929       }
00930     }
00931     lookup = sinfo_new_lookup();
00932 
00933     lookup->id=iCube;
00934     lookup->jd=jCube;
00935     lookup->X=X;
00936     lookup->Y=Y;
00937     lookup->Z=Z;
00938     lookup->hX=hX;
00939 
00940 
00941     cknull(im_obj=sinfo_new_c_bezier_interpolate_image(im_obj,
00942                                                        mask_im,
00943                                                        lookup, 
00944                                cfg->maxRad,
00945                                cfg->maxRad,
00946                                                        cfg->maxRad, 
00947                                                        2, 
00948                                                        slit_edges),
00949        "could not carry out sinfo_new_c_bezier_interpolate_image" );
00950 
00951 
00952     cknull(im_obj=sinfo_new_c_bezier_find_bad( im_obj, 
00953                            mask_im,
00954                            cfg->maxRad,
00955                            cfg->maxRad,
00956                            cfg->maxRad,
00957                            0,
00958                            cpl_image_get_size_x(im_obj), 
00959                            0,
00960                            cpl_image_get_size_y(im_obj), 
00961                            cfg->sigmaFactor),
00962        "could not carry out sinfo_new_c_bezier_find_bad" );
00963 
00964 
00965     if (cfg->contains_dither == 1) {
00966          cknull(im_dither=sinfo_new_c_bezier_interpolate_image(im_dither, 
00967                                    mask_im,
00968                                    lookup,
00969                                    cfg->maxRad,
00970                                    cfg->maxRad, 
00971                                    cfg->maxRad,
00972                                                                2, 
00973                                                                slit_edges),
00974     "could not carry out new_c_bezier_Interpolate_Image on dithered frame" );
00975 
00976      cknull(im_dither=sinfo_new_c_bezier_find_bad(im_dither, 
00977                               mask_im,
00978                               cfg->maxRad,
00979                               cfg->maxRad, 
00980                               cfg->maxRad,
00981                               0,
00982                               cpl_image_get_size_x(im_obj), 
00983                               0,
00984                               cpl_image_get_size_y(im_obj), 
00985                               cfg->sigmaFactor),
00986         "could not carry out new_c_bezier_find_bad on dithered frame");
00987 
00988     }
00989     sinfo_new_destroy_2Dfloatarray(&slit_edges, 32);
00990     sinfo_free_image(&mask_im);
00991     sinfo_free_imagelist(&iCube);
00992     sinfo_free_imagelist(&jCube);
00993     sinfo_free_image(&lookup->X); 
00994     sinfo_free_image(&lookup->X);
00995     sinfo_free_image(&lookup->Y);
00996     sinfo_free_image(&lookup->hX);
00997     new_destroy_lookup(lookup);
00998   }
00999 
01000   if (cfg->maskInd == 3) {
01001     cknull(mask_im = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
01002        "could not load static bad pixel mask" );
01003 
01004     /* #open the ASCII list of the slitlet positions-- */
01005     strcpy(file_name,cfg->slitposList);
01006     check_nomsg(tbl_slitpos = cpl_table_load(file_name,1,0));
01007 
01008     check_nomsg(n = cpl_table_get_nrow(tbl_slitpos));
01009     slit_edges = sinfo_new_2Dfloatarray(n, 2); 
01010 
01011     for (i =0 ; i< n; i++) {
01012       val_x=cpl_table_get_double(tbl_slitpos,"pos1",i,&status);
01013       val_y=cpl_table_get_double(tbl_slitpos,"pos2",i,&status);
01014       sinfo_new_array2D_set_value(slit_edges,val_x,i,0);
01015       sinfo_new_array2D_set_value(slit_edges,val_y,i,1);
01016     }
01017     sinfo_free_table(&tbl_slitpos);
01018 
01019     strcpy(file_name,cfg->indexlist);
01020     check_nomsg(tbl_index = cpl_table_load(file_name,1,0));
01021     check_nomsg(n = cpl_table_get_nrow(tbl_index));
01022     cknull_nomsg(in_nam = (char**) cpl_calloc(n,sizeof(char*)));
01023 
01024     for (i =0 ; i< n; i++){
01025        strcpy(in_nam[i],cpl_table_get_string(tbl_index,"name",i));
01026     }
01027     sinfo_free_table(&tbl_index);
01028     
01029     for (i=0;i<cnt;i++){
01030       if (strcmp("ICube.fits", in_nam[i]) != 0){
01031        check_nomsg(iCube=cpl_imagelist_load("ICube.fits",CPL_TYPE_FLOAT,0));
01032       }
01033       else if (strcmp("JCube.fits", in_nam[i]) != 0){
01034       check_nomsg(jCube=cpl_imagelist_load("JCube.fits",CPL_TYPE_FLOAT,0));
01035       }
01036       else if (strcmp("X.fits", in_nam[i]) != 0){
01037     check_nomsg(X=cpl_image_load("X.fits",CPL_TYPE_FLOAT,0,0));
01038       }
01039       else if (strcmp("Y.fits", in_nam[i]) != 0){
01040     check_nomsg(Y=cpl_image_load("Y.fits",CPL_TYPE_FLOAT,0,0));
01041       }
01042       else if (strcmp("Z.fits", in_nam[i]) != 0){
01043     check_nomsg(Z=cpl_image_load("Z.fits",CPL_TYPE_FLOAT,0,0));
01044       }
01045       else if (strcmp("cX.fits", in_nam[i]) != 0) {
01046     check_nomsg(hX=cpl_image_load("cX.fits",CPL_TYPE_FLOAT,0,0));
01047       } else {
01048      sinfo_msg_error("wrong name in index list or needed file not there!");
01049      goto cleanup;
01050       }
01051     }
01052     lookup = sinfo_new_lookup();
01053     lookup->id=iCube;
01054     lookup->jd=jCube;
01055     lookup->X=X;
01056     lookup->Y=Y;
01057     lookup->Z=Z;
01058     lookup->hX=hX;
01059 
01060     cknull(im_obj = sinfo_new_c_bezier_interpolate_image(im_obj, 
01061                              mask_im,
01062                              lookup,
01063                              cfg->maxRad,
01064                              cfg->maxRad,
01065                              cfg->maxRad,
01066                              2,
01067                              slit_edges ),
01068        "could not carry out sinfo_new_c_bezier_interpolate_image" );
01069 
01070     if (cfg->contains_dither == 1) {
01071         cknull(im_dither=sinfo_new_c_bezier_interpolate_image( im_dither, 
01072                                    mask_im, 
01073                                    lookup,
01074                                    cfg->maxRad,
01075                                    cfg->maxRad,
01076                                    cfg->maxRad,
01077                                    2,
01078                                    slit_edges ),
01079                   "could not carry out sinfo_new_c_bezier_interpolate_image"
01080                   " on dithered frame" );
01081 
01082     }
01083     sinfo_new_destroy_2Dfloatarray(&slit_edges, 32);
01084     sinfo_free_image(&mask_im);
01085     sinfo_free_imagelist(&iCube);
01086     sinfo_free_imagelist(&jCube);
01087     sinfo_free_image(&lookup->X);
01088     sinfo_free_image(&lookup->Y);
01089     sinfo_free_image(&lookup->Z);
01090     sinfo_free_image(&lookup->hX);
01091     new_destroy_lookup(lookup);
01092   }
01093 
01094 
01095   /*
01096   #---------------------------------------------------------
01097   # correction of distortions
01098   #---------------------------------------------------------
01099   */
01100 
01101   /* here memory leak */
01102   sinfo_msg("cfg->warpfixInd=%d",cfg->warpfixInd);
01103   if (cfg->warpfixInd == 1){
01104     /*#open ASCII file containing the slope parameter and read it*/
01105     sinfo_msg("correct object for distortions");
01106     cknull(int_im_shifted=sinfo_new_image_warp_fits(im_obj, 
01107                                                     cfg->kernel, 
01108                                                     cfg->polyFile),
01109                                 "could not carry out sinfo_image_warp_fits" );
01110 
01111     sinfo_free_image(&im_obj);
01112     im_obj = cpl_image_duplicate(int_im_shifted);
01113     if (cfg->contains_dither == 1){
01114       cknull(int_im_dith_shifted=sinfo_new_image_warp_fits(im_dither, 
01115                                                            cfg->kernel, 
01116                                                            cfg->polyFile),
01117                                  "could not carry out sinfo_image_warp_fits" );
01118       sinfo_free_image(&im_dither);
01119       im_dither = cpl_image_duplicate(int_im_dith_shifted);
01120     }
01121   }
01122 
01123   /*  
01124   #---------------------------------------------------------
01125   # merge (interleave) both resulting frames
01126   #---------------------------------------------------------
01127   */
01128   if (cfg->interInd == 1 && cfg->contains_dither == 1){
01129     sinfo_msg("merge (interleave) both resulting frames");
01130     cknull(im3 = cpl_image_new(cpl_image_get_size_x(im_obj), 
01131                                cpl_image_get_size_y(im_obj),CPL_TYPE_FLOAT),
01132        "could not allocate an image" );
01133     cknull(im4=sinfo_new_remove_general_offset( im_obj, im_dither, 
01134                                                 im3, cfg->noRows ),
01135                                        "sinfo_removeGeneralOffset failed" );
01136 
01137     cknull(im5 = sinfo_new_remove_regional_tilt ( im_obj, im4, im3 ),
01138        "sinfo_removeRegionalTilt failed" );
01139 
01140     cknull(im6 = sinfo_new_remove_column_offset ( im_obj, im5, im3 ),
01141        "sinfo_removeColumnOffset failed" );
01142 
01143     cknull(im7 = sinfo_new_remove_residual_tilt ( im6, im3 ),
01144        "sinfo_removeResidualTilt failed" );
01145 
01146     cknull(im8 = sinfo_new_remove_residual_offset ( im7, im3 ),
01147        "sinfo_removeResidualOffset failed");
01148     cknull(im9 = sinfo_sinfo_merge_images(im_obj, im8, im3),
01149        "sinfo_mergeImages failed" );
01150 
01151     ck0(sinfo_pro_save_ima(im9,raw,sof,cfg->outName,
01152                frm_pro_ctg,NULL,plugin_id,config),
01153     "cannot save ima %s", cfg->outName);
01154 
01155     sinfo_free_image(&im3);
01156     sinfo_free_image(&im4);
01157     sinfo_free_image(&im5);
01158     sinfo_free_image(&im6);
01159     sinfo_free_image(&im7);
01160     sinfo_free_image(&im8);
01161     sinfo_free_image(&im9);
01162     sinfo_free_image(&im_obj);
01163     sinfo_free_image(&im_dither);
01164     /*
01165     #---------------------------------------------------------
01166     # convolve spectra with Gaussian
01167     #---------------------------------------------------------
01168     */
01169   } else if (cfg->gaussInd == 1 && cfg->interInd == 0) {
01170     sinfo_msg("convolve spectra with Gaussian");
01171     cknull(im_conv = sinfo_new_convolve_image_by_gauss ( im_obj, cfg->hw ),
01172        "sinfo_convolveImageByGauss failed" );
01173 
01174     ck0(sinfo_pro_save_ima(im_conv,raw,sof,cfg->outName,
01175                frm_pro_ctg,NULL,plugin_id,config),
01176     "cannot save ima %s", cfg->outName);
01177 
01178     sinfo_free_image(&im_obj);
01179     sinfo_free_image(&im_conv);
01180     if (cfg->contains_dither == 1){
01181       sinfo_free_image(&im_dither);
01182     }
01183   } else {
01184 
01185     sinfo_msg("Add QC LOG");
01186     /* add QC-LOG */
01187     /* sinfo_det_ncounts(raw, cfg->qc_thresh_max); */
01188     cknull_nomsg(qclog_tbl = sinfo_qclog_init());
01189 
01190     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMON MEANFLUX",
01191               qc->avg_on,"Average of flux","%g"));
01192     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMOFF MEANFLUX",
01193               qc->avg_of,"Average of flux","%g"));
01194     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMDIF MEANFLUX",
01195               qc->avg_di,"Average of flux","%g"));
01196     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMON MAXFLUX",
01197               qc->max_on,"Max of flux","%g"));
01198     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMOFF MAXFLUX",
01199               qc->max_of,"Max of flux","%g"));
01200     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FRMDIF MAXFLUX",
01201               qc->max_di,"Max of flux","%g"));
01202     ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FRMON NPIXSAT",
01203               qc->nsat,"Number of saturated pixels","%d"));
01204 
01205     ck0(sinfo_pro_save_ima(im_obj,raw,sof,cfg->outName,
01206                frm_pro_ctg,qclog_tbl,plugin_id,config),
01207     "cannot dump ima %s", cfg->outName);
01208 
01209     sinfo_free_image(&im_obj);
01210     sinfo_free_table(&qclog_tbl);
01211 
01212     if (cfg->contains_dither == 1 && cfg->interInd == 0) {
01213        if (strstr(cfg->outName, ".fits" ) != NULL ) {
01214      snprintf(name_list, MAX_NAME_SIZE-1,"%s%s", sinfo_new_get_rootname(cfg->outName), 
01215                  "_dith.fits");
01216      strcpy(cfg->outName,name_list);
01217        } else {
01218      strcat(cfg->outName,"_dith");
01219        }
01220        cpl_free(name_list);
01221  
01222 
01223        ck0(sinfo_pro_save_ima(im_dither,raw,sof,cfg->outName,
01224                   frm_pro_ctg,NULL,plugin_id,config),
01225        "cannot save ima %s", cfg->outName);
01226        if (cfg->contains_dither == 1) {
01227      sinfo_free_image(&im_dither);
01228        }
01229     }
01230 
01231   }
01232 
01233   /* the following generates a valgrind error 
01234      but without it 8 bytes are leaked */
01235   sinfo_free_image_array(&im,cfg->nframes);
01236  
01237   /*AMO: check if those images can be deleted before */
01238   sinfo_free_image(&int_im_shifted);
01239   sinfo_free_image(&int_im);
01240   sinfo_free_image(&im_obj_flat);
01241   sinfo_free_image(&im_obj_sub);
01242 
01243   sinfo_stack_free(&cfg);
01244   sinfo_free_frameset(&raw);
01245   sinfo_qc_wcal_delete(&qc);
01246 
01247   return 0;
01248 
01249  cleanup:
01250   sinfo_free_table(&qclog_tbl);
01251   sinfo_free_table(&tbl_slitpos);
01252   sinfo_free_table(&tbl_index);
01253   if(slit_edges!=NULL) sinfo_new_destroy_2Dfloatarray(&slit_edges, 32);
01254   sinfo_free_image(&flat1_dist);
01255   sinfo_free_image(&flat1);
01256   sinfo_free_image(&flat2_dist);
01257   sinfo_free_image(&int_im);
01258   sinfo_free_image(&int_im_dith);
01259   sinfo_free_image(&int_im_shifted);
01260   sinfo_free_image(&im_dither);
01261   sinfo_free_image(&flat2);
01262   sinfo_free_image(&im_obj_flat);
01263   sinfo_free_image(&im_obj_sub);
01264   sinfo_free_image(&im_obj);
01265   sinfo_free_image(&mask_im);
01266   sinfo_free_image(&im_sky);
01267   sinfo_free_imagelist(&list_object_tmp);
01268   sinfo_free_image(&sky_img_flat);
01269   sinfo_free_image(&sky_dist);
01270   sinfo_free_image(&sky_img);
01271   sinfo_free_imagelist(&list_object);
01272   sinfo_free_imagelist(&list_sky);
01273   if(im != NULL) sinfo_free_image_array(&im,cfg->nframes);
01274   sinfo_stack_free(&cfg);
01275   sinfo_free_frameset(&raw);
01276   sinfo_qc_wcal_delete(&qc);
01277 
01278   return -1 ;
01279 
01280 }

Generated on Wed Jan 17 08:33:43 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4