sinfo_new_cubes_build.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_cubes_build.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_cubes_build.h"
00036 #include "sinfo_pfits.h"
00037 #include "sinfo_msg.h"
00038 #include "sinfo_pro_save.h"
00039 #include "sinfo_objnod_ini_by_cpl.h"
00040 #include "sinfo_functions.h"
00041 #include "sinfo_hidden.h"
00042 #include "sinfo_utilities_scired.h"
00043 #include "sinfo_wave_calibration.h"
00044 #include "sinfo_cube_construct.h"
00045 #include "sinfo_skycor.h"
00046 #include "sinfo_product_config.h"
00047 #include "sinfo_error.h"
00048 #include "sinfo_utils_wrappers.h"
00049 #include "sinfo_atmo_disp.h"
00050 /*----------------------------------------------------------------------------
00051   Defines
00052   ---------------------------------------------------------------------------*/
00053 #define PI_NUMB        (3.1415926535897932384626433832795) /* pi */
00054 
00055 
00056 static void sinfo_atm_correction(cpl_imagelist** ppCube,
00057         cpl_frameset* sof,
00058         const char* polyshiftname,
00059         double dis_cube,
00060         double centralLambda_cube,
00061         int centralpix_cube);
00062 
00063 /*----------------------------------------------------------------------------
00064   Function Definitions
00065   ---------------------------------------------------------------------------*/
00066 /* Temporally commented out as not used
00067 static cpl_image*
00068 sinfo_flux_corr(const cpl_image* inp,const cpl_image* wav,const double dis);
00069 */
00078 /*----------------------------------------------------------------------------
00079   Function     : sinfo_new_cubes_build()
00080   In           : ini_file: file name of according .ini file
00081   Out          : integer (0 if it worked, -1 if it doesn't)
00082   Job          : this routine carries through the data cube creation of an
00083   object science observation using object-sky nodding
00084   and jittering. This script expects jittered frames that
00085   were already sky-subtracted
00086   averaged, flatfielded, spectral tilt corrected and
00087   interleaved if necessary
00088   ---------------------------------------------------------------------------*/
00089 int sinfo_new_cubes_build (const char* plugin_id,cpl_parameterlist* config,
00090                cpl_frameset* sof, const char* procatg, const int frame_index)
00091 {
00092 
00093   object_config * cfg=NULL ;
00094   cpl_image * im=NULL ;
00095   cpl_image * wavemapim=NULL ;
00096   cpl_image * wim=NULL ;
00097   cpl_image * res_obj=NULL ;
00098   cpl_image * calim=NULL ;
00099   cpl_image * halospec=NULL ;
00100   cpl_image * sky_im=NULL;
00101   cpl_image* res_ima=NULL;
00102   cpl_image* res_wim=NULL;
00103   cpl_image* res_flat=NULL;
00104   cpl_image* res_sky=NULL;
00105   cpl_image* flat_im=NULL;
00106   cpl_image* eima_avg=NULL;
00107   cpl_image* eima_med=NULL;
00108   cpl_imagelist  * cube=NULL ;
00109   cpl_imagelist  * outcube=NULL ;
00110   cpl_imagelist  * outcube2=NULL ;
00111   cpl_imagelist* cflat=NULL;
00112   cpl_imagelist* cflat2=NULL;
00113   cpl_imagelist* csky=NULL;
00114   cpl_imagelist* csky2=NULL;
00115   cpl_propertylist* plist=NULL;
00116   cpl_frame* obj_frm=NULL;
00117   cpl_frame* sky_frm=NULL;
00118   cpl_imagelist* obj_cor=NULL;
00119 
00120   int sky_cor=0;
00121   int flux_cor=0;
00122   int n=0 ;
00123   int cpix=0 ;
00124   float mi=0 ;
00125   float ma=0 ;
00126   float fcol=0 ;
00127   float center_x=0;
00128   float center_y=0;
00129   float *  correct_dist=NULL ;
00130   float * distances=NULL ;
00131   float ** slit_edges=NULL ;
00132   int nx=0;
00133   int ny=0;
00134   cpl_image* dif=NULL;
00135   float* pd=NULL;
00136   float* pw=NULL;
00137   int i=0;
00138   int j=0;
00139 
00140   double dis=0 ;
00141   double cwav=0 ;
00142 
00143   char pro_mjit[MAX_NAME_SIZE];
00144   char pro_obs[MAX_NAME_SIZE];
00145   char pro_med[MAX_NAME_SIZE];
00146 
00147 
00148   char * name=NULL ;
00149   char file_name[FILE_NAME_SZ];
00150 
00151   cpl_table* qclog_tbl=NULL;
00152   cpl_frameset* stk=NULL;
00153   cpl_parameter* p=NULL;
00154   int pdensity=0;
00155   sinfo_skycor_qc* sqc=NULL;
00156   cpl_table* int_obj=NULL;
00157 
00158   check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
00159   check_nomsg(pdensity=cpl_parameter_get_int(p));
00160 
00161   if (strcmp(procatg,PRO_COADD_STD) == 0) {
00162     strcpy(pro_mjit,PRO_MASK_COADD_STD);
00163     strcpy(pro_obs,PRO_OBS_STD);
00164     strcpy(pro_med,PRO_MED_COADD_STD);
00165 
00166   } else if (strcmp(procatg,PRO_COADD_PSF) == 0) {
00167     strcpy(pro_mjit,PRO_MASK_COADD_PSF);
00168     strcpy(pro_obs,PRO_OBS_PSF);
00169     strcpy(pro_med,PRO_MED_COADD_PSF);
00170   } else {
00171     strcpy(pro_mjit,PRO_MASK_COADD_OBJ);
00172     strcpy(pro_obs,PRO_OBS_OBJ);
00173     strcpy(pro_med,PRO_MED_COADD_OBJ);
00174   }
00175 
00176 
00177   /*----parse input data and parameters to set cube_config cfg---*/
00178   check_nomsg(stk = cpl_frameset_new());
00179 
00180   cknull(cfg = sinfo_parse_cpl_input_objnod(config,sof,&stk),
00181      "Error setting parameter configuration");
00182 
00183   ck0(sinfo_check_input_data(cfg),"error checking input");
00184 
00185 
00186   check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.objnod.fcol"));
00187   check_nomsg(fcol=(float)cpl_parameter_get_double(p));
00188 
00189   for ( n = 0 ; n < cfg->nframes ; n++ )
00190     {
00191 
00192       sinfo_msg_debug("Read FITS information");
00193       name = cfg->framelist[n] ;
00194       if( sinfo_is_fits_file(name) != 1) {
00195     sinfo_msg_error("Input file %s is not FITS",name);
00196     goto cleanup;
00197       }
00198 
00199 
00200       sinfo_msg_debug("frame no.: %d, name: %s\n", n, name) ;
00201       cknull(im = cpl_image_load(name,CPL_TYPE_FLOAT,0,0),
00202          " could not load frame %s!",name) ;
00203 
00204 
00205       /*
00206        *--------------------------------------------------------------
00207        *---------------------RESAMPLING-------------------------------
00208        *--------------------------------------------------------------
00209        */
00210       sinfo_msg("Resampling object");
00211       cknull(wavemapim = cpl_image_load(cfg->wavemap,CPL_TYPE_FLOAT,0,0),
00212          "could not load wavemap");
00213       check_nomsg(wim=cpl_image_duplicate(wavemapim));
00214 
00215       check_nomsg(p=cpl_parameterlist_find(config,
00216                        "sinfoni.objnod.flux_cor"));
00217       check_nomsg(flux_cor=cpl_parameter_get_bool(p));
00218 
00219 
00220       nx=cpl_image_get_size_x(wim);
00221       ny=cpl_image_get_size_y(wim);
00222       /*
00223         check_nomsg(pd=cpl_image_get_data(im));
00224         //To compare statistics we make sure that the input image has all 1s
00225 
00226     for(i=0;i<nx;i++) {
00227     for(j=0;j<ny;j++) {
00228     pd[nx*j+i]=1.;
00229     }
00230     }
00231     cpl_image_save(im,"im.fits", CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00232       */
00233 
00234       cknull(res_ima = sinfo_new_defined_resampling(im,
00235                             wim,
00236                             cfg->ncoeffs,
00237                             &cfg->nrows,
00238                             &dis,
00239                             &mi,
00240                             &ma,
00241                             &cwav,
00242                             &cpix),
00243          " sinfo_definedResampling() failed" ) ;
00244 
00245 
00246 
00247       //cpl_image_save(res_ima,"res_im.fits",
00248       //CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00249 
00250       //We create an image with the derivatives
00251       check_nomsg(dif=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00252       pw=cpl_image_get_data(wim);
00253       pd=cpl_image_get_data(dif);
00254 
00255 
00256       for(i=1;i<nx-1;i++) {
00257     for(j=1;j<ny-1;j++) {
00258       if(!isnan(pd[nx*j+i])) {
00259         pd[nx*j+i]=2.0*dis/(pw[nx*(j+1)+i]-pw[nx*(j-1)+i]);
00260       }
00261     }
00262     if(!isnan(pd[i])) {
00263       pd[i]=dis/(pw[nx+i]-pw[i]);
00264     }
00265     if(!isnan(pd[nx*(ny-1)+i])) {
00266       pd[nx*(ny-1)+i]=dis/(pw[nx*(ny-1)+i]-pw[nx*(ny-2)+i]);
00267     }
00268       }
00269 
00270 
00271       //cpl_image_save(dif,"diff.fits",
00272       //CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00273 
00274 
00275 
00276       cknull(res_wim = sinfo_new_defined_resampling(dif,
00277                             wim,
00278                             cfg->ncoeffs,
00279                             &cfg->nrows,
00280                             &dis,
00281                             &mi,
00282                             &ma,
00283                             &cwav,
00284                             &cpix),
00285          " sinfo_definedResampling() failed" ) ;
00286 
00287 
00288 
00289       //cpl_image_save(res_wim,"res_diff.fits",
00290       //CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00291 
00292 
00293 
00294 
00295 
00296       //To rescale in flux we divide the resampled image and
00297       //the resampled derivatives. At this point res_obj should have same
00298       //flux as input image (im.diff)
00299 
00300       res_obj=cpl_image_duplicate(res_ima);
00301       sinfo_free_image(&res_ima);
00302       if(flux_cor) {
00303     sinfo_msg("Apply flux correction");
00304     cpl_image_divide(res_obj,res_wim);
00305       }
00306       //cpl_image_save(res_obj,"res_obj.fits",
00307       //CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00308 
00309 
00310       sinfo_free_image(&wim);
00311 
00312       if((pdensity == 3) || (pdensity == 2 && frame_index == 0)) {
00313     snprintf(file_name,MAX_NAME_SIZE-1,"%s%d%s",
00314          RESAMPLED_OUT_OBJ_FILENAME,
00315          frame_index,".fits");
00316     ck0(sinfo_pro_save_ima(res_obj,sof,sof,file_name,
00317                    PRO_RESAMPLED_OBJ,
00318                                qclog_tbl,plugin_id,config),
00319         "cannot save image %s", file_name);
00320       }
00321 
00322 
00323       if(strcmp(cfg->sky_dist,"no_sky")!=0) {
00324     sinfo_msg("Resampling sky");
00325     check_nomsg(sky_im=cpl_image_load(cfg->sky_dist,CPL_TYPE_FLOAT,0,0));
00326     check_nomsg(wim=cpl_image_duplicate(wavemapim));
00327     cknull(res_ima = sinfo_new_defined_resampling(sky_im,
00328                               wim,
00329                               cfg->ncoeffs,
00330                               &cfg->nrows,
00331                               &dis,
00332                               &mi,
00333                               &ma,
00334                               &cwav,
00335                               &cpix),
00336            " sinfo_definedResampling() failed" );
00337         res_sky=cpl_image_duplicate(res_ima);
00338         sinfo_free_image(&res_ima);
00339         if(flux_cor) {
00340           sinfo_msg("Apply flux correction");
00341       cpl_image_divide(res_sky,res_wim);
00342     }
00343 
00344     sinfo_free_image(&wim);
00345     sinfo_free_image(&sky_im) ;
00346     if((pdensity == 3) || (pdensity == 2 && frame_index == 0)){
00347 
00348       snprintf(file_name,MAX_NAME_SIZE-1,"%s%d%s",
00349            RESAMPLED_OUT_SKY_FILENAME,frame_index,".fits");
00350       ck0(sinfo_pro_save_ima(res_sky,sof,sof,file_name,
00351                  PRO_RESAMPLED_SKY,
00352                  qclog_tbl,plugin_id,config),
00353           "cannot save image %s", file_name);
00354     }
00355 
00356 
00357 
00358       }
00359 
00360 
00361       if(n ==0) {
00362     if(strcmp(cfg->mflat_dist,"not_found") != 0) {
00363       sinfo_msg("Resampling master flat");
00364       cknull(flat_im=cpl_image_load(cfg->mflat_dist,CPL_TYPE_FLOAT,0,0),
00365          "Distorted master flat field not found\n"
00366          "You may have set --stack-flat_ind=FALSE\n"
00367          "Flat field resampling skipped");
00368       check_nomsg(wim=cpl_image_duplicate(wavemapim));
00369       cknull(res_ima = sinfo_new_defined_resampling(flat_im,
00370                             wim,
00371                             cfg->ncoeffs,
00372                             &cfg->nrows,
00373                             &dis,
00374                             &mi,
00375                             &ma,
00376                             &cwav,
00377                             &cpix),
00378          " sinfo_definedResampling() failed" ) ;
00379 
00380 
00381       res_flat=cpl_image_duplicate(res_ima);
00382       sinfo_free_image(&res_ima);
00383       if(flux_cor) {
00384         sinfo_msg("Apply flux correction");
00385         cpl_image_divide(res_flat,res_wim);
00386       }
00387       sinfo_free_image(&wim);
00388       sinfo_free_image(&flat_im) ;
00389       if((pdensity == 3) || (pdensity == 2 && frame_index == 0)) {
00390         snprintf(file_name,MAX_NAME_SIZE-1,"%s%d%s",
00391              RESAMPLED_OUT_FLAT_FILENAME,frame_index,".fits");
00392         ck0(sinfo_pro_save_ima(res_flat,sof,sof,file_name,
00393                    PRO_RESAMPLED_FLAT_LAMP,
00394                    qclog_tbl,plugin_id,config),
00395         "cannot save image %s", file_name);
00396       }
00397 
00398 
00399     }
00400 
00401       }
00402 
00403       sinfo_msg("wmin %f wmax %f wcent %f wstep %f cpix %d",
00404         mi,ma,cwav,dis,cpix);
00405       sinfo_free_image(&res_wim);
00406       sinfo_free_image(&im) ;
00407       sinfo_free_image(&wavemapim) ;
00408 
00409       /*
00410        *-------------------------------------------------------------------
00411        *----------------Calibration----------------------------------------
00412        *-------------------------------------------------------------------
00413        */
00414       /*----Multiply with calibrated halogen lamp spectrum----*/
00415       if (cfg->halocorrectInd == 1)
00416         {
00417       sinfo_msg("Calibration");
00418       check_nomsg(halospec = cpl_image_load(cfg->halospectrum,
00419                                                 CPL_TYPE_FLOAT,0,0)) ;
00420 
00421       cknull(calim = sinfo_new_multiply_image_with_spectrum(res_obj,
00422                                                                 halospec),
00423          " sinfo_new_multiply_image_with_spectrum() failed" ) ;
00424 
00425       sinfo_free_image(&halospec) ;
00426       sinfo_free_image(&res_obj) ;
00427       res_obj = cpl_image_duplicate(calim) ;
00428       sinfo_free_image(&calim);
00429         }
00430 
00431       /*
00432        *-------------------------------------------------------------------
00433        *------------------CUBECREATION-------------------------------------
00434        *-------------------------------------------------------------------
00435        */
00436       sinfo_msg("Cube creation");
00437       /*---select north-south-test or fitting of slitlet edges--*/
00438       if (cfg->northsouthInd == 0) {
00439     sinfo_msg("cfg->northsouthInd == 0");
00440     cknull(slit_edges=sinfo_read_slitlets_edges(cfg->nslits,cfg->poslist),
00441            "error reading slitlets edges");
00442       } else {
00443     sinfo_msg("cfg->northsouthInd != 0");
00444     cknull(distances = sinfo_read_distances(cfg->nslits,cfg->distlist),
00445            "error reading distances");
00446       }
00447 
00448       cknull(correct_dist = (float*) cpl_calloc(cfg->nslits, sizeof (float)),
00449          " could not allocate memory!") ;
00450 
00451       sinfo_msg("Create cube object");
00452       if (cfg->northsouthInd ==0 ) {
00453 
00454         cknull(cube = sinfo_new_make_cube_spi(res_obj,slit_edges,
00455                           correct_dist),
00456            "could not construct data cube!") ;
00457 
00458       }  else {
00459     cknull(cube = sinfo_new_make_cube_dist(res_obj,fcol,distances,
00460                            correct_dist),
00461            "could not construct a data cube!") ;
00462       }
00463       sinfo_free_image(&res_obj);
00464 
00465 
00466       if(strcmp(cfg->sky_dist,"no_sky")!=0) {
00467 
00468         sinfo_msg("Create cube sky");
00469         if (cfg->northsouthInd ==0 ) {
00470           cknull(csky = sinfo_new_make_cube_spi(res_sky,slit_edges,
00471                         correct_dist),
00472          "could not construct data cube!") ;
00473         }  else {
00474           cknull(csky = sinfo_new_make_cube_dist(res_sky,fcol,distances,
00475                          correct_dist),
00476          "could not construct a data cube!") ;
00477         }
00478         sinfo_free_image(&res_sky);
00479       }
00480 
00481       if(n==0) {
00482     if(strcmp(cfg->mflat_dist,"not_found")!=0) {
00483       sinfo_msg("Create cube master flat");
00484       if (cfg->northsouthInd ==0 ) {
00485         cknull(cflat=sinfo_new_make_cube_spi(res_flat,slit_edges,
00486                          correct_dist),
00487            "could not construct data cube!") ;
00488       }  else {
00489         cknull(cflat = sinfo_new_make_cube_dist(res_flat,fcol,distances,
00490                             correct_dist),
00491            "could not construct a data cube!") ;
00492       }
00493       sinfo_free_image(&res_flat);
00494     }
00495       }
00496 
00497       if (cfg->northsouthInd ==0 )
00498     {
00499       sinfo_new_destroy_2Dfloatarray(&slit_edges,cfg->nslits);
00500     }
00501       else
00502     {
00503       sinfo_new_destroy_array(&distances);
00504     }
00505 
00506       /*
00507        *--------------------------------------------------------------------
00508        *------------------------FINETUNING----------------------------------
00509        *--------------------------------------------------------------------
00510        * shift the rows of the reconstructed images of the data cube to the
00511        * correct sub pixel position select the shift method: polynomial
00512        * interpolation, FFT or cubic spline interpolation
00513        *--------------------------------------------------------------------
00514        */
00515 
00516       if(strcmp(cfg->sky_dist,"no_sky")!=0) {
00517     if(pdensity > 0) {
00518       cknull(csky2=sinfo_new_fine_tune(csky,
00519                        correct_dist,
00520                        cfg->method,
00521                        cfg->order,
00522                        cfg->nslits),
00523          " could not fine tune the data cube") ;
00524 
00525       sinfo_free_imagelist(&csky);
00526       sinfo_msg("Stretch output cube along Y direction");
00527 
00528       cknull(csky = sinfo_new_bin_cube(csky2,1,2,0,63,0,63),
00529          "error rebinning sky cube");
00530       sinfo_free_imagelist(&csky2);
00531 
00532 
00533 
00534       snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_sky_cube",
00535            frame_index,".fits");
00536       ck0(sinfo_pro_save_ims(csky,sof,sof,file_name,
00537                  PRO_OBS_SKY,NULL,plugin_id,config),
00538           "cannot dump cube %s", file_name);
00539       cknull(eima_med=sinfo_new_median_cube(csky),
00540          "Creating an average image");
00541       check_nomsg(center_x = cpl_image_get_size_x(eima_med)/ 2. + 0.5);
00542       check_nomsg(center_y = cpl_image_get_size_y(eima_med)/ 2. + 0.5);
00543 
00544       sinfo_new_set_wcs_cube(csky,file_name, cwav,
00545                  dis, cpix, center_x, center_y);
00546 
00547       sinfo_free_imagelist(&csky) ;
00548 
00549       snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_sky_med",
00550            frame_index,".fits");
00551       ck0(sinfo_pro_save_ima(eima_med,sof,sof,file_name,
00552                  PRO_SKY_MED,NULL,plugin_id,config),
00553           "cannot save ima %s",file_name);
00554 
00555       check_nomsg(sinfo_new_set_wcs_image(eima_med,file_name, center_x, center_y));
00556       sinfo_free_image(&eima_med);
00557     }
00558       }
00559 
00560 
00561 
00562       if(n==0) {
00563     if(strcmp(cfg->mflat_dist,"not_found")!=0) {
00564       if(pdensity > 1) {
00565         cknull(cflat2=sinfo_new_fine_tune(cflat,
00566                           correct_dist,
00567                           cfg->method,
00568                           cfg->order,
00569                           cfg->nslits),
00570            " could not fine tune the data cube") ;
00571 
00572         sinfo_free_imagelist(&cflat);
00573         sinfo_msg("Stretch output cube along Y direction");
00574 
00575         cknull(cflat = sinfo_new_bin_cube(cflat2,1,2,0,63,0,63),
00576            "Error binning flat cube");
00577         sinfo_free_imagelist(&cflat2);
00578 
00579         ck0(sinfo_pro_save_ims(cflat,sof,sof,OBJNOD_OUT_MFLAT_CUBE_FILENAME,
00580                    PRO_MFLAT_CUBE,NULL,plugin_id,config),
00581         "cannot save cube %s", OBJNOD_OUT_MFLAT_CUBE_FILENAME);
00582 
00583         sinfo_new_set_wcs_cube(cflat,OBJNOD_OUT_MFLAT_CUBE_FILENAME,cwav, dis,
00584                    cpix, center_x, center_y);
00585 
00586         cknull(eima_avg=sinfo_new_average_cube_to_image(cflat),
00587            "Creating an average image");
00588 
00589         ck0(sinfo_pro_save_ima(eima_avg,sof,sof,"out_mflat_avg.fits",
00590                    "MFLAT_AVG",NULL,plugin_id,config),
00591         "cannot save ima %s", "out_mflat_avg.fits");
00592 
00593         sinfo_free_image(&eima_avg);
00594 
00595         cknull(eima_med=sinfo_new_median_cube(cflat),
00596            "Error computing median on cube flat");
00597 
00598         ck0(sinfo_pro_save_ima(eima_med,sof,sof,"out_mflat_med.fits",
00599                    "MFLAT_MED",NULL,plugin_id,config),
00600         "cannot save ima %s", "out_mflat_med.fits");
00601 
00602         sinfo_free_imagelist(&cflat);
00603         sinfo_free_image(&eima_med);
00604       }
00605     }
00606       }
00607 
00608       cknull(outcube2=sinfo_new_fine_tune(cube,
00609                       correct_dist,
00610                       cfg->method,
00611                       cfg->order,
00612                       cfg->nslits),
00613          " could not fine tune the data cube") ;
00614 
00615       sinfo_msg("Stretch output cube along Y direction");
00616       cknull(outcube = sinfo_new_bin_cube(outcube2,1,2,0,63,0,63),
00617          "Error binning cube");
00618       sinfo_free_imagelist(&cube);
00619       cknull_nomsg(qclog_tbl=sinfo_qclog_init());
00620       sinfo_get_pupil_shift(outcube,n,&qclog_tbl);
00621       snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_cube_obj",
00622            frame_index,".fits");
00623 
00624       check_nomsg(center_x = cpl_image_get_size_x(
00625                           cpl_imagelist_get(outcube,0))/2.+0.5) ;
00626       check_nomsg(center_y = cpl_image_get_size_y(
00627                           cpl_imagelist_get(outcube,0))/2.+0.5 );
00628 
00629       /*----------- atmospheric correction --------------*/
00630       if (cfg->polyshiftname && strlen(cfg->polyshiftname))
00631       {
00632           sinfo_atm_correction(&outcube,
00633                 sof,
00634                 cfg->polyshiftname,
00635                 dis,
00636                 cwav,
00637                 cpix);
00638       }
00639       /*-------------------------------------------------*/
00640 
00641       ck0(sinfo_pro_save_ims(outcube,sof,sof,file_name,
00642                  pro_obs,qclog_tbl,plugin_id,config),
00643       "cannot save cube %s", file_name);
00644 
00645       sinfo_new_set_wcs_cube(outcube, file_name, cwav, dis,
00646                  cpix, center_x, center_y);
00647       /* free memory */
00648       /* to prevent error message comment next line */
00649 
00650       check_nomsg(p=cpl_parameterlist_find(config,
00651                        "sinfoni.objnod.sky_cor"));
00652 
00653       check_nomsg(sky_cor=cpl_parameter_get_bool(p));
00654       if(sky_cor == 1 && (pdensity == 1 || pdensity == 3) &&
00655               strcmp(cfg->sky_dist,"no_sky")!=0)
00656       {
00657         obj_frm=cpl_frameset_find(sof,pro_obs);
00658         sky_frm=cpl_frameset_find(sof,PRO_OBS_SKY);
00659         sqc=sinfo_skycor_qc_new();
00660         ck0(sinfo_skycor(config, obj_frm,sky_frm,sqc,&obj_cor,&int_obj),
00661             "determining sky residuals corrected object");
00662         cpl_frameset_erase(sof,pro_obs);
00663         snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_cube_obj_cor",
00664              frame_index,".fits");
00665         ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC SKYCOR THBKGFIT",
00666                           sqc->th_fit,"Thermal background fit success","%d"));
00667 
00668         ck0(sinfo_pro_save_ims(obj_cor,sof,sof,file_name,
00669                    pro_obs,qclog_tbl,plugin_id,config),
00670         "cannot save cube %s", file_name);
00671 
00672         sinfo_skycor_qc_delete(&sqc);
00673 
00674         sinfo_new_set_wcs_cube(obj_cor, file_name, cwav, dis,
00675                    cpix, center_x, center_y);
00676 
00677         sinfo_free_imagelist(&obj_cor) ;
00678         snprintf(file_name,MAX_NAME_SIZE-1,"%s%2.2d%s","out_int_obj",
00679          frame_index,".fits");
00680 
00681         ck0(sinfo_pro_save_tbl(int_obj,sof,sof,file_name,
00682                    PRO_SPECTRA_QC,qclog_tbl,plugin_id,config),
00683         "cannot save cube %s", file_name);
00684         sinfo_free_table(&int_obj) ;
00685       }
00686       sinfo_free_table(&qclog_tbl);
00687       sinfo_free_imagelist(&outcube2);
00688       sinfo_free_imagelist(&outcube) ;
00689       sinfo_free_float(&correct_dist) ;
00690 
00691     } /* end loop over n (nframes) */
00692 
00693   /* free memory */
00694   sinfo_objnod_free(&cfg);
00695   sinfo_free_frameset(&stk);
00696 
00697 
00698   return 0;
00699 
00700  cleanup:
00701   sinfo_skycor_qc_delete(&sqc);
00702   sinfo_free_imagelist(&obj_cor) ;
00703   sinfo_free_propertylist(&plist);
00704   sinfo_free_imagelist(&outcube2) ;
00705   sinfo_free_imagelist(&outcube) ;
00706   sinfo_free_table(&qclog_tbl);
00707   sinfo_free_image(&eima_avg);
00708   sinfo_free_image(&eima_med);
00709   sinfo_free_imagelist(&cflat) ;
00710   sinfo_free_imagelist(&cflat2) ;
00711   sinfo_free_imagelist(&cube) ;
00712   sinfo_free_imagelist(&csky) ;
00713   sinfo_free_imagelist(&csky2) ;
00714 
00715   if(cfg!=NULL) {
00716     if (cfg->northsouthInd ==0 ) {
00717       if(slit_edges != NULL) {
00718     sinfo_new_destroy_2Dfloatarray(&slit_edges,cfg->nslits);
00719       }
00720     } else {
00721       if (distances != NULL ) {
00722     sinfo_new_destroy_array(&distances);
00723       }
00724     }
00725   }
00726 
00727   sinfo_free_float(&correct_dist);
00728   sinfo_free_image(&res_flat);
00729   sinfo_free_image(&res_sky);
00730   sinfo_free_image(&res_wim);
00731   sinfo_free_image(&calim);
00732   sinfo_free_image(&halospec) ;
00733   sinfo_free_image(&sky_im) ;
00734   sinfo_free_image(&res_obj);
00735   sinfo_free_image(&flat_im) ;
00736   sinfo_free_image(&wavemapim);
00737   sinfo_free_image(&wim);
00738   sinfo_free_image(&im);
00739   sinfo_objnod_free(&cfg);
00740   sinfo_free_frameset(&stk);
00741 
00742   return -1;
00743 
00744 
00745 
00746 }
00747 
00748 static void 
00749 sinfo_atm_correction(cpl_imagelist** ppCube,
00750         cpl_frameset* sof,
00751         const char* polyshiftname,
00752         double dis_cube,
00753         double centralLambda_cube,
00754         int centralpix_cube)
00755 {
00756     cpl_polynomial* poly = NULL;
00757     cpl_frame* first_frame=NULL;
00758     const char* ref_file=NULL;
00759     cpl_propertylist *plist_cube = NULL;
00760     cpl_propertylist* ppolylist = NULL;
00761     cpl_imagelist* retcube = NULL;
00762 
00763         /* we use the pointer outcube as a handle to  *ppCube 
00764              (don't initialize to NULL!)*/
00765     cpl_imagelist* outcube = *ppCube; 
00766        /* Get the reference file  */
00767     first_frame = cpl_frameset_get_first(sof) ;
00768     ref_file = cpl_frame_get_filename(first_frame) ;
00769     if (ref_file && strlen(ref_file))
00770     {
00771         /* Get FITS header from reference file */
00772         sinfo_msg_warning("adjusting atmospheric correction ref_file[%s]", ref_file);
00773         plist_cube = cpl_propertylist_load(ref_file, 0);
00774         if (plist_cube)
00775         {
00776               // check the OPTI1.NAME
00777                 ppolylist = cpl_propertylist_load(polyshiftname,0);
00778             if (ppolylist)
00779             {
00780                 double pixelscale = sinfo_pfits_get_pixscale(plist_cube);
00781                 double poly_pixelscale = sinfo_pfits_get_pixscale(ppolylist);
00782                 if (fabs(poly_pixelscale - pixelscale) < 1E-8)
00783                 {
00784                     poly = sinfo_atmo_load_polynom(polyshiftname);
00785                     if (!poly)
00786                     {
00787                         sinfo_msg_warning("Cannot load polynom from [%s]", polyshiftname);
00788                     }
00789                     else
00790                     {
00791                         sinfo_msg_warning("polynom from [%s] is loaded", polyshiftname);
00792                     }
00793                 }
00794                 else
00795                 {
00796                     sinfo_msg_warning("pixelscale for the polynomial fit "
00797                             "is different: provided[%f] expected[%f]",
00798                             poly_pixelscale, pixelscale);
00799                 }
00800                 sinfo_free_propertylist(&ppolylist);
00801             }
00802             if (!poly)
00803             {
00804                 sinfo_msg_warning("Pixel shift due atmospheric refraction"
00805                       " would not be applied");
00806             }
00807         }
00808         else
00809         {
00810             sinfo_msg_warning("cannot load propertylist for the frame");
00811         }
00812         if (poly)
00813         {
00814             double airmass = (
00815                   (cpl_propertylist_get_double(plist_cube, "ESO TEL AIRM START") +
00816                   cpl_propertylist_get_double(plist_cube, "ESO TEL AIRM END"))
00817                   / 2
00818                   );
00819             double angle = (
00820                   (cpl_propertylist_get_double(plist_cube, "ESO ADA ABSROT START") +
00821                   cpl_propertylist_get_double(plist_cube, "ESO ADA ABSROT END"))
00822                   / 2
00823                   ) * PI_NUMB / 180;
00824             sinfo_msg("dis_cube[%f] centralLambda_cube[%f] centralpix_cube[%d]",dis_cube,centralLambda_cube,centralpix_cube);
00825             retcube = sinfo_atmo_apply_cube_polynomial_shift(
00826                     poly, outcube,
00827                     centralLambda_cube,
00828                     airmass,
00829                     angle,
00830                     dis_cube,
00831                     centralpix_cube);
00832          sinfo_free_polynomial(&poly);
00833          if (retcube)
00834          {
00835              sinfo_free_imagelist(&outcube);
00836              *ppCube = retcube;
00837          }
00838       }
00839       sinfo_free_propertylist(&plist_cube);
00840     }
00841 
00842 
00843     return;
00844 }
00845 
00846 /* Temporally commented out as not yet used
00847 static cpl_image*
00848 sinfo_flux_corr(const cpl_image* inp,const cpl_image* wav,const double dis)
00849 {
00850 
00851   cpl_image* out=NULL;
00852   cpl_image* dif=NULL;
00853   const float* pi=NULL;
00854   const float* pw=NULL;
00855   float* po=NULL;
00856   float* pd=NULL;
00857 
00858   int i=0;
00859   int j=0;
00860   int nx=0;
00861   int ny=0;
00862   int stat=0;
00863   nx=cpl_image_get_size_x(inp);
00864   ny=cpl_image_get_size_y(inp);
00865 
00866   out=cpl_image_new(nx,ny,CPL_TYPE_FLOAT);
00867   dif=cpl_image_new(nx,ny,CPL_TYPE_FLOAT);
00868   pi=cpl_image_get_data_const(inp);
00869   pw=cpl_image_get_data_const(wav);
00870   po=cpl_image_get_data(out);
00871   pd=cpl_image_get_data(dif);
00872 
00873   sinfo_msg("dif=%10.8f",2.*dis/(cpl_image_get(wav,500,501,&stat)-
00874                  cpl_image_get(wav,500,499,&stat)));
00875 
00876   sinfo_msg("dif=%10.8f",2.*dis/(cpl_image_get(wav,500,101,&stat)-
00877                  cpl_image_get(wav,500,99,&stat)));
00878 
00879   sinfo_msg("dif=%10.8f",2.*dis/(cpl_image_get(wav,500,1001,&stat)-
00880                  cpl_image_get(wav,500,999,&stat)));
00881 
00882   for(i=0;i<nx;i++) {
00883     for(j=1;j<ny-1;j++) {
00884       if(pi[nx*j+i] != ZERO || pw[nx*j+i] != ZERO) {
00885     po[nx*j+i]=pi[nx*j+i]*dis/((pw[nx*(j+1)+i]-pw[nx*(j-1)+i])*0.5);
00886     pd[nx*j+i]=dis/((pw[nx*(j+1)+i]-pw[nx*(j-1)+i])*0.5);
00887       } else {
00888     po[nx*j+i]=ZERO;
00889     pd[nx*j+i]=ZERO;
00890       }
00891     }
00892 
00893 
00894     if(pi[i] != ZERO || pw[i] != ZERO) {
00895       po[i]=pi[i]*dis/(pw[nx+i]-pw[i]);
00896       pd[i]=dis/(pw[nx+i]-pw[i]);
00897     } else {
00898       po[i]=ZERO;
00899       pd[i]=ZERO;
00900     }
00901 
00902 
00903 
00904     if(pi[nx*(ny-1)+i] != ZERO || pw[nx*(ny-1)+i] != ZERO) {
00905       po[nx*(ny-1)+i]=pi[nx*(ny-1)+i]*dis/(pw[nx*(ny-1)+i]-pw[nx*(ny-2)+i]);
00906       pd[nx*(ny-1)+i]=dis/(pw[nx*(ny-1)+i]-pw[nx*(ny-2)+i]);
00907     } else {
00908       po[nx*(ny-1)+i]=ZERO;
00909       pd[nx*(ny-1)+i]=ZERO;
00910     }
00911 
00912   }
00913   //cpl_image_save(dif,"diff.fits", CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
00914 
00915   sinfo_free_image(&dif);
00916 
00917 
00918   return out;
00919 
00920 }
00921 */

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