sinfo_new_objnod.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_objnod.c
00022    Author       :    J. Schreiber
00023    Created on   :    December 3, 2003
00024    Description  :    Creates data cubes or merges data cubes 
00025                         out of jittered object-sky
00026                         nodding observations 
00027  ---------------------------------------------------------------------------*/
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 /*----------------------------------------------------------------------------
00033                                 Includes
00034  ---------------------------------------------------------------------------*/
00035 #include "sinfo_new_objnod.h"
00036 #include "sinfo_hidden.h"
00037 #include "sinfo_pro_save.h"
00038 #include "sinfo_objnod_ini_by_cpl.h"
00039 #include "sinfo_functions.h"
00040 #include "sinfo_pfits.h"
00041 #include "sinfo_utilities_scired.h" 
00042 #include "sinfo_wave_calibration.h"
00043 #include "sinfo_cube_construct.h"
00044 #include "sinfo_error.h"
00045 #include "sinfo_utils_wrappers.h"
00046 
00047 /*----------------------------------------------------------------------------
00048                                 Defines
00049  ---------------------------------------------------------------------------*/
00050 #define PI_NUMB        (3.1415926535897932384626433832795) /* pi */
00051 
00052 
00053 /*----------------------------------------------------------------------------
00054                              Function Definitions
00055  ---------------------------------------------------------------------------*/
00056 
00065 /*----------------------------------------------------------------------------
00066    Function     : sinfo_new_objnod()
00067    In           : ini_file: file name of according .ini file
00068    Out          : integer (0 if it worked, -1 if it doesn't) 
00069    Job          : this routine carries through the data cube creation of an 
00070                   object science observation using object-sky nodding
00071                   and jittering. This script expects jittered frames that
00072           were already sky-subtracted
00073                   averaged, flatfielded, spectral tilt corrected and 
00074         interleaved if necessary
00075  ---------------------------------------------------------------------------*/
00076 int sinfo_new_objnod (const char* plugin_id,cpl_parameterlist* config, 
00077             cpl_frameset* sof, const char* procatg)
00078 {
00079 
00080     object_config * cfg=NULL ;
00081     cpl_image * im=NULL ;
00082     cpl_image * wavemapim=NULL ;
00083     cpl_image * resampledImage=NULL ;
00084     cpl_image * calim=NULL ;
00085     cpl_image * halospec=NULL ;
00086     cpl_image * sky_im=NULL;
00087     cpl_image* res_flat=NULL;
00088     cpl_image* res_sky=NULL;
00089     cpl_image* flat_im=NULL;
00090     cpl_image* jitter_image=NULL;
00091     cpl_image* eima_avg=NULL;
00092     cpl_image* eima_med=NULL;
00093     cpl_imagelist  * cube=NULL ;
00094     cpl_imagelist  * outcube=NULL ;
00095     cpl_imagelist  * outcube2=NULL ;
00096     cpl_imagelist  ** cubeobject=NULL ;
00097     cpl_imagelist  ** cube_tmp=NULL ;
00098     cpl_imagelist  * jittercube=NULL ;
00099     cpl_imagelist  * maskcube=NULL ;
00100     cpl_imagelist* cflat=NULL;
00101     cpl_imagelist* cflat2=NULL;
00102     cpl_imagelist* csky=NULL;
00103     cpl_imagelist* csky2=NULL;
00104 
00105 
00106     int i=0;
00107     int n=0;
00108     int partind = 0 ;
00109     int centralpix=0 ;
00110     int z_siz=0;
00111     int z_min=0;
00112     int z_max=0;
00113     int z=0;
00114     int z_stp=100;
00115     int scales_sky=0;
00116     int ks_clip=0;
00117     double kappa=2.0;
00118 
00119     float ref_offx=0;
00120     float ref_offy=0;
00121     float mi=0 ;
00122     float ma=0 ;
00123     float fcol=0 ;
00124     float center_x=0;
00125     float newcenter_x=0 ;
00126     float center_y=0;
00127     float newcenter_y=0;
00128     float cd1_1=0;
00129     float cd1_2=0;
00130     float cd2_1=0;
00131     float cd2_2=0;
00132     float pixelscale=0;
00133     float angle=0;
00134     float radangle=0;
00135     double exptime=0;
00136 
00137     float *  correct_dist=NULL ;
00138     float * distances=NULL ;
00139     double * times=NULL ;
00140     float * offsetx=NULL;
00141     float * offsety=NULL;
00142     float ** slit_edges=NULL ;
00143     float offx_min=1.e10;
00144     float offy_min=1.e10;
00145     float offx_max=-1.e10;
00146     float offy_max=-1.e10;
00147 
00148     double dis=0;
00149     double centralLambda=0;
00150 
00151     char name_jitter[MAX_NAME_SIZE] ;
00152     char pro_mjit[MAX_NAME_SIZE];
00153     char pro_obs[MAX_NAME_SIZE];
00154     char pro_med[MAX_NAME_SIZE];
00155 
00156 
00157     char * name=NULL ;
00158     char * partname=NULL;
00159     char * partname2=NULL;
00160     char file_name[MAX_NAME_SIZE];
00161     int vllx=0;
00162     int vlly=0;
00163     int vurx=0;
00164     int vury=0;
00165 
00166     int onp=0;
00167     int j=0;
00168     cpl_image* j_img=NULL;
00169     cpl_image* m_img=NULL;
00170     cpl_table* qclog_tbl=NULL;
00171     cpl_image* ill_cor=NULL;
00172     cpl_frame* frame=NULL;
00173     cpl_frameset* stk=NULL;
00174     cpl_parameter* p=NULL;
00175     cpl_propertylist* plist=NULL;
00176     int mosaic_max_size=0;
00177 
00178     check_nomsg(p=cpl_parameterlist_find(config,
00179                      "sinfoni.objnod.mosaic_max_size"));
00180     check_nomsg(mosaic_max_size=cpl_parameter_get_int(p));
00181 
00182      if (strcmp(procatg,PRO_COADD_STD) == 0) {
00183     strcpy(pro_mjit,PRO_MASK_COADD_STD);
00184     strcpy(pro_obs,PRO_OBS_STD);
00185     strcpy(pro_med,PRO_MED_COADD_STD);
00186 
00187     } else if (strcmp(procatg,PRO_COADD_PSF) == 0) {
00188     strcpy(pro_mjit,PRO_MASK_COADD_PSF);
00189     strcpy(pro_obs,PRO_OBS_PSF);
00190     strcpy(pro_med,PRO_MED_COADD_PSF);
00191     } else {
00192     strcpy(pro_mjit,PRO_MASK_COADD_OBJ);
00193     strcpy(pro_obs,PRO_OBS_OBJ);
00194     strcpy(pro_med,PRO_MED_COADD_OBJ);
00195     }
00196 
00197 
00198     /*----parse input data and parameters to set cube_config cfg---*/
00199     check_nomsg(stk = cpl_frameset_new());
00200 
00201     cknull(cfg = sinfo_parse_cpl_input_objnod(config,sof,&stk),
00202        "Error setting parameter configuration");
00203  
00204     ck0(sinfo_check_input_data(cfg),"error checking input");
00205 
00206     if ( cfg->jitterind == 1 )
00207     {
00208         cknull(times = (double*) cpl_calloc (cfg->nframes, sizeof (double)), 
00209            " could not allocate memory!") ;
00210  
00211         cknull(offsetx = (float*) cpl_calloc (cfg->nframes, sizeof(float)),
00212            " could not allocate memory!") ;
00213 
00214         cknull(offsety = (float*) cpl_calloc (cfg->nframes, sizeof(float)),
00215            " could not allocate memory!") ;
00216     }
00217   
00218     if (cfg->jitterind == 0)
00219     {
00220         if ( NULL != (partname = strtok(cfg->outName, "."))) 
00221         {
00222             partname2 = strtok (NULL, ".") ;
00223             partind = 1 ;
00224         }
00225     }
00226 
00227     ck0(sinfo_auto_size_cube5(cfg,&ref_offx,&ref_offy,
00228                                 &offx_min,&offy_min,
00229                                 &offx_max,&offy_max),
00230                              "Error resizing cube");
00231   
00232 
00233 
00234     check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.objnod.fcol"));
00235     check_nomsg(fcol=cpl_parameter_get_double(p));
00236    
00237     if(NULL != cpl_frameset_find(sof,PRO_ILL_COR)) {
00238      sinfo_msg("Illumination correction cube is provided");
00239      frame = cpl_frameset_find(sof,PRO_ILL_COR);
00240      ill_cor=cpl_image_load(cpl_frame_get_filename(frame),CPL_TYPE_FLOAT,0,0);
00241     } else {
00242      sinfo_msg("Illumination correction cube not provided");
00243      cpl_error_reset();
00244     }
00245 
00246      for ( n = 0 ; n < cfg->nframes ; n++ )
00247      {
00248 
00249         sinfo_msg("Read FITS information");
00250         name = cfg->framelist[n] ;    
00251         if (n == 0)
00252     {
00253         strcpy (name_jitter, name) ;
00254     }   
00255         if( sinfo_is_fits_file(name) != 1) {
00256           sinfo_msg_error("Input file %s is not FITS",name);
00257           goto cleanup;
00258     }
00259     
00260     /* get some header values and compute the CD-sinfo_matrix */
00261         plist=cpl_propertylist_load(name,0);
00262     pixelscale = sinfo_pfits_get_pixscale(plist) /2;
00263     angle = sinfo_pfits_get_posangle(plist) ;
00264         /* in PUPIL data there is not posangle info: we reset the error */
00265         if(cpl_error_get_code() != CPL_ERROR_NONE) {
00266       cpl_error_reset();
00267         }
00268     sinfo_free_propertylist(&plist);
00269     radangle = angle * PI_NUMB / 180. ;
00270     cd1_1 = cos(radangle) ;
00271     cd1_2 = sin(radangle) ;
00272     cd2_1 = -sin(radangle) ;
00273     cd2_2 = cos(radangle) ;
00274     
00275     sinfo_msg("frame no.: %d, name: %s\n", n, name) ;
00276     cknull(im = cpl_image_load(name,CPL_TYPE_FLOAT,0,0),
00277            " could not load frame %s!",name) ;
00278 
00279     if (cfg->jitterind == 1)
00280     {
00281       exptime = sinfo_pfits_get_ditndit(name) ;
00282 
00283       if (exptime == FLAG)
00284         {
00285           sinfo_msg_error("could not read fits header keyword exptime!");
00286           goto cleanup;
00287         }
00288       times[n] = exptime ;
00289           ck0(sinfo_new_assign_offset(n,name,offsetx,offsety,
00290                                       ref_offx,ref_offy),
00291                                       "Error assigning offsets");
00292 
00293     }
00294 
00295         /*
00296          *--------------------------------------------------------------    
00297          *---------------------RESAMPLING-------------------------------
00298      *--------------------------------------------------------------
00299          */
00300         sinfo_msg("Resampling object");
00301     cknull(wavemapim = cpl_image_load(cfg->wavemap,CPL_TYPE_FLOAT,0,0),
00302            "could not load wavemap");
00303 
00304         cknull(resampledImage = sinfo_new_defined_resampling( im, 
00305                                   wavemapim, 
00306                                   cfg->ncoeffs,
00307                                   &cfg->nrows,
00308                                   &dis,
00309                                   &mi,
00310                                   &ma,
00311                                   &centralLambda,
00312                                   &centralpix),
00313            " sinfo_definedResampling() failed" ) ;
00314 
00315  
00316     if(n ==0) {
00317           if(strcmp(cfg->mflat_dist,"not_found") != 0) {
00318         sinfo_msg("Resampling master flat");
00319         cknull(flat_im=cpl_image_load(cfg->mflat_dist,CPL_TYPE_FLOAT,0,0),
00320                    "Distorted master flat field not found\n"
00321                    "You may have set --stack-flat_ind=FALSE\n"
00322                    "Flat field resampling skipped");
00323             cknull(res_flat = sinfo_new_defined_resampling(flat_im, 
00324                                wavemapim,
00325                                cfg->ncoeffs,
00326                                &cfg->nrows,
00327                                &dis,
00328                                &mi,
00329                                &ma,
00330                                &centralLambda,
00331                                &centralpix),
00332            " sinfo_definedResampling() failed" ) ;
00333 
00334             sinfo_free_image(&flat_im) ;
00335       }
00336      
00337       if(strcmp(cfg->sky_dist,"no_sky")!=0) {
00338         sinfo_msg("Resampling sky");
00339         check_nomsg(sky_im=cpl_image_load(cfg->sky_dist,
00340                                               CPL_TYPE_FLOAT,0,0));
00341         cknull(res_sky = sinfo_new_defined_resampling(sky_im,
00342                               wavemapim,
00343                               cfg->ncoeffs,
00344                               &cfg->nrows,
00345                               &dis,
00346                               &mi,
00347                               &ma,
00348                               &centralLambda,
00349                               &centralpix),
00350            " sinfo_definedResampling() failed" );
00351 
00352         sinfo_free_image(&sky_im) ;
00353 
00354        
00355       }
00356     }
00357 
00358         sinfo_msg ("dispersion %f\n", dis) ;
00359         sinfo_msg ("lambda min %f max %f cent %f\n", mi,ma,centralLambda ) ;
00360         sinfo_msg ("central pixel %d\n", centralpix) ; 
00361 
00362         sinfo_free_image(&im) ;
00363         sinfo_free_image(&wavemapim) ;
00364 
00365     /*
00366          *-------------------------------------------------------------------
00367          *----------------Calibration----------------------------------------
00368          *-------------------------------------------------------------------
00369     */
00370         /*----Multiply with calibrated halogen lamp spectrum----*/ 
00371         if (cfg->halocorrectInd == 1)
00372         {     
00373       sinfo_msg("Calibration");
00374       check_nomsg(halospec = cpl_image_load(cfg->halospectrum,
00375                                                 CPL_TYPE_FLOAT,0,0)) ;
00376 
00377       cknull(calim = sinfo_new_multiply_image_with_spectrum(resampledImage,
00378                                                                 halospec),
00379                         " sinfo_new_multiply_image_with_spectrum() failed" ) ;
00380 
00381       sinfo_free_image(&halospec) ;
00382       sinfo_free_image(&resampledImage) ;
00383       resampledImage = cpl_image_duplicate(calim) ;
00384       sinfo_free_image(&calim);
00385         }
00386     
00387         /*
00388          *-------------------------------------------------------------------
00389          *------------------CUBECREATION-------------------------------------
00390          *-------------------------------------------------------------------
00391      */
00392     sinfo_msg("Cube creation");
00393     /*---select north-south-test or fitting of slitlet edges--*/
00394     if (cfg->northsouthInd == 0) {
00395       sinfo_msg("cfg->northsouthInd == 0");
00396       cknull(slit_edges = sinfo_read_slitlets_edges(cfg->nslits,cfg->poslist),
00397          "error reading slitlets edges");
00398     } else {
00399       sinfo_msg("cfg->northsouthInd != 0");
00400       cknull(distances = sinfo_read_distances(cfg->nslits,cfg->distlist),
00401          "error reading distances");
00402     }
00403  
00404     cknull(correct_dist = (float*) cpl_calloc(cfg->nslits, sizeof (float)),
00405            " could not allocate memory!") ;
00406 
00407     sinfo_msg("Create cube object");
00408     if (cfg->northsouthInd ==0 ) {
00409 
00410         cknull(cube = sinfo_new_make_cube_spi(resampledImage,
00411                                                   slit_edges,
00412                                                   correct_dist),
00413                                  " could not construct data cube!") ;
00414 
00415     }  else {
00416        cknull(cube = sinfo_new_make_cube_dist(resampledImage,
00417                                                   fcol,
00418                                                   distances,
00419                                                   correct_dist),
00420                                          " could not construct a data cube!") ;
00421     }
00422     sinfo_free_image(&resampledImage);
00423 
00424     if(n==0) {
00425       if(strcmp(cfg->mflat_dist,"not_found")!=0) {
00426         sinfo_msg("Create cube master flat");
00427         if (cfg->northsouthInd ==0 ) {
00428           cknull(cflat=sinfo_new_make_cube_spi(res_flat,
00429                                                    slit_edges,
00430                                                    correct_dist),
00431                                 " could not construct data cube!") ;
00432         }  else {
00433           cknull(cflat = sinfo_new_make_cube_dist(res_flat,
00434                                                       fcol,
00435                                                       distances,
00436                                                       correct_dist),
00437                                         " could not construct a data cube!") ;
00438         }
00439         sinfo_free_image(&res_flat);
00440       }
00441       if(strcmp(cfg->sky_dist,"no_sky")!=0) {
00442 
00443         sinfo_msg("Create cube sky");
00444         if (cfg->northsouthInd ==0 ) {
00445           cknull(csky = sinfo_new_make_cube_spi(res_sky,
00446                                                     slit_edges,
00447                                                     correct_dist),
00448                                            " could not construct data cube!") ;
00449         }  else {
00450           cknull(csky = sinfo_new_make_cube_dist(res_sky,
00451                                                      fcol,
00452                                                      distances,
00453                                                      correct_dist),
00454                                          " could not construct a data cube!") ;
00455         }
00456         sinfo_free_image(&res_sky);
00457       }
00458     }
00459 
00460 
00461         if (cfg->northsouthInd ==0 )
00462      {
00463        sinfo_new_destroy_2Dfloatarray(&slit_edges,cfg->nslits);
00464      }
00465     else
00466       {
00467             sinfo_new_destroy_array(&distances);
00468       }
00469 
00470         /*
00471          *--------------------------------------------------------------------
00472          *------------------------FINETUNING----------------------------------
00473          *--------------------------------------------------------------------
00474          * shift the rows of the reconstructed images of the data cube to the 
00475          * correct sub pixel position select the shift method: polynomial 
00476          * interpolation, FFT or cubic spline interpolation
00477          *--------------------------------------------------------------------
00478          */
00479  
00480     if(n==0) {
00481       if(strcmp(cfg->sky_dist,"no_sky")!=0) {
00482          cknull(csky2=sinfo_new_fine_tune(csky,
00483                                              correct_dist,
00484                                              cfg->method,
00485                                              cfg->order,
00486                           cfg->nslits),
00487             " could not fine tune the data cube") ;
00488 
00489          sinfo_free_imagelist(&csky);
00490          sinfo_msg("Stretch output cube along Y direction");
00491  
00492          cknull(csky = sinfo_new_bin_cube(csky2,1,2,0,63,0,63),
00493             "error rebinning sky cube");
00494          sinfo_free_imagelist(&csky2);
00495 
00496        
00497      
00498          ck0(sinfo_pro_save_ims(csky,sof,sof,"out_sky_cube.fits",
00499                     PRO_OBS_SKY,NULL,plugin_id,config),
00500          "cannot dump cube %s", "out_sky_cube.fits");
00501  
00502          cknull(eima_med=sinfo_new_median_cube(csky),
00503                 "Creating an average image");
00504          check_nomsg(center_x = cpl_image_get_size_x(eima_med)/ 2. + 0.5);
00505          check_nomsg(center_y = cpl_image_get_size_y(eima_med)/ 2. + 0.5);
00506 
00507             sinfo_new_set_wcs_cube(csky, "out_sky_cube.fits", centralLambda, 
00508                           dis, centralpix, center_x, center_y);
00509 
00510          sinfo_free_imagelist(&csky) ;
00511 
00512          ck0(sinfo_pro_save_ima(eima_med,sof,sof,"out_sky_med.fits",
00513                     PRO_SKY_MED,NULL,plugin_id,config),
00514          "cannot save ima %s", "out_sky_med.fits");
00515 
00516             sinfo_new_set_wcs_image(eima_med,"out_sky_med.fits", 
00517                                      center_x, center_y);
00518          sinfo_free_image(&eima_med);
00519       }
00520 
00521 
00522       if(strcmp(cfg->mflat_dist,"not_found")!=0) {
00523 
00524         cknull(cflat2=sinfo_new_fine_tune(cflat,correct_dist,
00525                                               cfg->method,cfg->order,
00526                                               cfg->nslits),
00527                                        " could not fine tune the data cube") ;
00528 
00529         sinfo_free_imagelist(&cflat);
00530         sinfo_msg("Stretch output cube along Y direction");
00531  
00532         cknull(cflat = sinfo_new_bin_cube(cflat2,1,2,0,63,0,63),
00533            "Error binning flat cube");
00534         sinfo_free_imagelist(&cflat2);
00535 
00536         ck0(sinfo_pro_save_ims(cflat,sof,sof,OBJNOD_OUT_MFLAT_CUBE_FILENAME,
00537                    PRO_MFLAT_CUBE,NULL,plugin_id,config),
00538         "cannot save cube %s", OBJNOD_OUT_MFLAT_CUBE_FILENAME);
00539 
00540         cknull(eima_avg=sinfo_new_average_cube_to_image(cflat),
00541                "Creating an average image");
00542 
00543         ck0(sinfo_pro_save_ima(eima_avg,sof,sof,"out_mflat_avg.fits",
00544                    "MFLAT_AVG",NULL,plugin_id,config),
00545         "cannot save ima %s", "out_mflat_avg.fits");
00546 
00547         sinfo_free_image(&eima_avg);
00548 
00549         cknull(eima_med=sinfo_new_median_cube(cflat),
00550            "Error computing median on cube flat");
00551 
00552         ck0(sinfo_pro_save_ima(eima_med,sof,sof,"out_mflat_med.fits",
00553                    "MFLAT_MED",NULL,plugin_id,config),
00554         "cannot save ima %s", "out_mflat_med.fits");
00555 
00556         sinfo_free_imagelist(&cflat); 
00557         sinfo_free_image(&eima_med);
00558       }
00559     }
00560        
00561 
00562         cknull(outcube2=sinfo_new_fine_tune(cube,
00563                                             correct_dist,
00564                                             cfg->method,
00565                                             cfg->order,
00566                                             cfg->nslits),
00567                                      " could not fine tune the data cube") ;
00568 
00569         sinfo_msg("Stretch output cube along Y direction");
00570         cknull(outcube = sinfo_new_bin_cube(outcube2,1,2,0,63,0,63),
00571            "Error binning cube");
00572         sinfo_free_imagelist(&cube);
00573         cknull_nomsg(qclog_tbl=sinfo_qclog_init());
00574         sinfo_get_pupil_shift(outcube,n,&qclog_tbl);
00575         snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s",
00576          "out_cube_obj",n,".fits");
00577         ck0(sinfo_pro_save_ims(outcube,sof,sof,file_name,
00578                    pro_obs,qclog_tbl,plugin_id,config),
00579         "cannot save cube %s", file_name);
00580 
00581 
00582         sinfo_free_table(&qclog_tbl);
00583     check_nomsg(center_x = cpl_image_get_size_x(
00584                                cpl_imagelist_get(outcube,0))/2.+0.5) ;
00585     check_nomsg(center_y = cpl_image_get_size_y(
00586                                cpl_imagelist_get(outcube,0))/2.+0.5 );
00587    
00588 
00589     sinfo_new_set_wcs_cube(outcube, file_name, centralLambda, dis, 
00590                      centralpix, center_x, center_y);
00591 
00592        /* free memory */
00593         /* to prevent error message comment next line */
00594         sinfo_free_imagelist(&outcube2);
00595         sinfo_free_imagelist(&outcube) ;
00596     sinfo_free_float(&correct_dist) ;
00597 
00598 
00599     } /* end loop over n (nframes) */
00600  
00601     /* leak free */
00602     if(cfg->jitterind == 0) {
00603       goto exit;
00604     }   
00605      
00606     /* Here in case of autojitter we estimate the sky */
00607     if( (cfg->size_x*cfg->size_y*cfg->nframes) > (100*mosaic_max_size) ) {
00608       sinfo_msg_warning("Coadd cube size:%d,%d. N frames: %d",
00609                            cfg->size_x,cfg->size_y,cfg->nframes);
00610       sinfo_msg_warning("Max allowed should be such that "
00611                         "sixeX*sixeY*Nframes < 100*%d",mosaic_max_size);
00612       goto exit;
00613     } 
00614  
00615     if ( cfg->jitterind == 1 )
00616     {  
00617         check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.objnod.vllx"));
00618         check_nomsg(vllx = cpl_parameter_get_int(p));
00619         check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.objnod.vlly"));
00620         check_nomsg(vlly = cpl_parameter_get_int(p));
00621         check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.objnod.vurx"));
00622         check_nomsg(vurx = cpl_parameter_get_int(p));
00623         check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.objnod.vury"));
00624         check_nomsg(vury = cpl_parameter_get_int(p));
00625  
00626         cknull(cube_tmp =(cpl_imagelist**) cpl_calloc(cfg->nframes, 
00627                                                       sizeof (cpl_imagelist*)),
00628                                      "Could not allocate memory for cube_tmp");
00629         cknull(cubeobject =(cpl_imagelist**) cpl_calloc(cfg->nframes, 
00630                                                         sizeof(cpl_imagelist*)),
00631                                     "Could not allocate memory for cubeobject");
00632 
00633     for ( n = 0 ; n < cfg->nframes ; n++ ) {
00634           snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_cube_obj",
00635                      n,".fits");
00636       check_nomsg(cube_tmp[n] = cpl_imagelist_load(file_name,
00637                                CPL_TYPE_FLOAT,0));
00638       check_nomsg(cubeobject[n] = sinfo_new_cube_getvig(cube_tmp[n],
00639                                 1+vllx,1+vlly,
00640                                 64-vurx,64-vury));
00641           check_nomsg(sinfo_free_imagelist(&cube_tmp[n]));
00642     }
00643         sinfo_free_array_imagelist(&cube_tmp);
00644 
00645     }
00646 
00647     /*
00648         ---------------------------------------------------------------------
00649         ------------------------JITTERING------------------------------------
00650         ---------------------------------------------------------------------
00651     */
00652 
00653     if (cfg->jitterind == 1)
00654     {
00655     sinfo_msg("Jittering...");
00656 
00657         sinfo_msg("Coadded cube size. x: %d y: %d",
00658              cfg->size_x,cfg->size_y);
00659         check_nomsg(jittercube = cpl_imagelist_new()) ;
00660 
00661 
00662     /* 
00663         ---------------------------------------------------------------------
00664         -------------------THOMAS ALGORITHM----------------------------------
00665         ---------------------------------------------------------------------
00666     */
00667        
00668         check_nomsg(p=cpl_parameterlist_find(config,
00669                                              "sinfoni.objnod.scales_sky"));
00670         check_nomsg(scales_sky=cpl_parameter_get_bool(p));
00671         check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.objnod.ks_clip"));
00672         check_nomsg(ks_clip = cpl_parameter_get_bool(p));
00673         check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.objnod.kappa"));
00674         check_nomsg(kappa = cpl_parameter_get_double(p));
00675 
00676 
00677     if(scales_sky == 1) {
00678       sinfo_msg("Subtract spatial sinfo_median to each cube plane");
00679       for(n=0;n<cfg->nframes;n++) {
00680         sinfo_msg("process cube %d\n",n);
00681         sinfo_new_sinfoni_correct_median_it(&(cubeobject[n]));
00682       }
00683     }
00684 
00685 
00686         /* AMO CHECK */
00687 
00688         cknull(maskcube=cpl_imagelist_new(),"could not allocate cube!");
00689         
00690     /* Illumination correction */ 
00691         if(ill_cor != NULL) {
00692       for(n=0;n<cfg->nframes;n++) {
00693         sinfo_msg("Illumination correction is applied");
00694         cpl_imagelist_divide_image(cubeobject[n],ill_cor);
00695       }
00696     }
00697         sinfo_free_image(&ill_cor);
00698     
00699     sinfo_msg("Combine jittered cubes");
00700 
00701 
00702     if(ks_clip == 1) {
00703       sinfo_msg("Cube coaddition with kappa-sigma");
00704     }
00705         check_nomsg(onp=cpl_imagelist_get_size(cubeobject[0]));
00706     for(z=0;z<onp;z+=z_stp) {
00707       z_siz=(z_stp < (onp-z)) ? z_stp : (onp-z);
00708       z_min=z;
00709       z_max=z_min+z_siz;
00710       sinfo_msg("Coadding cubes: range [%4.4d,%4.4d) of 0-%d\n",
00711                            z_min,z_max,onp);
00712 
00713       for(j=z_min;j<z_max;j++) {
00714             check_nomsg(j_img=cpl_image_new(cfg->size_x,
00715                                             cfg->size_y,CPL_TYPE_FLOAT));
00716         check_nomsg(cpl_imagelist_set(jittercube,j_img,j));
00717             check_nomsg(m_img = cpl_image_new(cfg->size_x,
00718                                               cfg->size_y,CPL_TYPE_FLOAT));
00719             check_nomsg(cpl_imagelist_set(maskcube,m_img,j));
00720       }
00721       if(ks_clip == 1) {
00722         sinfo_new_combine_jittered_cubes_thomas_range(cubeobject,
00723                             jittercube,
00724                             maskcube,
00725                             cfg->nframes,
00726                             offsetx,offsety,
00727                             times,
00728                             cfg->kernel_type,
00729                             z_min,
00730                             z_max,
00731                             kappa);
00732 
00733       } else {
00734         sinfo_new_combine_jittered_cubes_range(cubeobject, 
00735                                              jittercube,
00736                                              maskcube,
00737                          cfg->nframes,
00738                                              offsetx,
00739                          offsety, 
00740                                              times,
00741                          cfg->kernel_type,
00742                                              z_min,
00743                                              z_max) ;
00744       }
00745     }
00746         sinfo_new_convert_0_to_ZERO_for_cubes(jittercube) ; 
00747 
00748       if (jittercube == NULL)
00749     {
00750         sinfo_msg_error(" could not allocate new data cube!") ;
00751         goto cleanup;
00752     }
00753 
00754         if (maskcube == NULL)
00755     {
00756         sinfo_msg_error(" could not merge the jittered data cubes\n") ;
00757             goto cleanup;
00758     }
00759 
00760     for ( i = 0 ; i <cfg->nframes  ; i++ ) {
00761       sinfo_free_imagelist(&cubeobject[i]);
00762     }
00763     sinfo_free_array_imagelist(&cubeobject);
00764 
00765         newcenter_x = cfg->size_x / 2. + 0.5 ;
00766     newcenter_y = cfg->size_y / 2. + 0.5 ;
00767 
00768         ck0(sinfo_pro_save_ims(jittercube,sof,sof,cfg->outName,
00769                    procatg,NULL,plugin_id,config),
00770         "cannot save cube %s", cfg->outName);
00771 
00772       sinfo_new_set_wcs_cube(jittercube, cfg->outName, centralLambda, 
00773                                dis, centralpix, center_x, center_y);
00774 
00775         cknull(jitter_image = sinfo_new_median_cube(jittercube),
00776                " could not do sinfo_medianCube()");
00777 
00778 
00779         ck0(sinfo_pro_save_ima(jitter_image,sof,sof,cfg->med_cube_name,
00780                    pro_med,NULL,plugin_id,config),
00781         "cannot save ima %s", cfg->outName);
00782 
00783       sinfo_new_set_wcs_image(jitter_image, cfg->med_cube_name,
00784                                 center_x,center_y);
00785 
00786         sinfo_free_image(&jitter_image);
00787 
00788         ck0(sinfo_pro_save_ims(maskcube,sof,sof,cfg->maskname,
00789                    pro_mjit,NULL,plugin_id,config),
00790         "cannot save cube %s", cfg->maskname);
00791 
00792      sinfo_new_set_wcs_cube(maskcube, cfg->maskname, centralLambda, 
00793                                dis, centralpix, center_x, center_y);
00794 
00795         sinfo_free_double(&times) ;
00796         sinfo_free_float(&offsetx) ;
00797         sinfo_free_float(&offsety) ;
00798         sinfo_free_imagelist(&maskcube) ;
00799         sinfo_free_imagelist(&jittercube) ;         
00800 
00801     } /* end of jittering */
00802 
00803  exit:
00804 
00805  /* free memory */
00806     sinfo_objnod_free(&cfg);
00807     sinfo_free_frameset(&stk);
00808     return 0;   
00809 
00810  cleanup:
00811     sinfo_free_propertylist(&plist);
00812     sinfo_free_image(&jitter_image);
00813     sinfo_free_imagelist(&jittercube) ;
00814     sinfo_free_imagelist(&maskcube) ;
00815 
00816     if(cfg != NULL) {
00817       if(cube_tmp != NULL) {
00818     for ( n = 0 ; n < cfg->nframes ; n++ ) {
00819       sinfo_free_imagelist(&(cube_tmp[n]));
00820     }
00821         sinfo_free_array_imagelist(&cube_tmp);
00822       }
00823       if(cubeobject != NULL) {
00824     for ( n = 0 ; n < cfg->nframes ; n++ ) {
00825       sinfo_free_imagelist(&(cubeobject[n]));
00826     }
00827         sinfo_free_array_imagelist(&cubeobject);
00828       }
00829 
00830     }
00831 
00832     sinfo_free_imagelist(&outcube2) ;
00833     sinfo_free_imagelist(&outcube) ;
00834     sinfo_free_table(&qclog_tbl);
00835     sinfo_free_image(&eima_avg);
00836     sinfo_free_image(&eima_med);
00837     sinfo_free_imagelist(&cflat) ;
00838     sinfo_free_imagelist(&cflat2) ;
00839     sinfo_free_imagelist(&cube) ;
00840     sinfo_free_imagelist(&csky) ;
00841     sinfo_free_imagelist(&csky2) ;
00842 
00843     if(cfg!=NULL) {
00844       if (cfg->northsouthInd ==0 ) {
00845     if(slit_edges != NULL) {
00846        sinfo_new_destroy_2Dfloatarray(&slit_edges,cfg->nslits);
00847     }
00848       } else {
00849     if (distances != NULL ) {
00850             sinfo_new_destroy_array(&distances);
00851     }
00852       }
00853     }
00854 
00855     sinfo_free_float(&correct_dist);
00856     sinfo_free_image(&res_flat);
00857     sinfo_free_image(&res_sky);
00858     sinfo_free_image(&calim);
00859     sinfo_free_image(&halospec) ;
00860     sinfo_free_image(&sky_im) ;
00861     sinfo_free_image(&resampledImage);
00862     sinfo_free_image(&flat_im) ;
00863     sinfo_free_image(&wavemapim);
00864     sinfo_free_image(&im);
00865     sinfo_free_image(&ill_cor);
00866     sinfo_free_float(&offsety);
00867     sinfo_free_float(&offsetx);
00868     sinfo_free_double(&times);
00869     sinfo_objnod_free(&cfg);
00870     sinfo_free_frameset(&stk);
00871 
00872     return -1;   
00873 
00874 
00875 
00876 }

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1