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

Generated on 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1