sinfo_new_stdstar.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_stdstar.c
00022    Author       :    J. Schreiber
00023    Created on   :    December 3, 2003
00024    Description  :    this routine doess the optimal extraction of a spectrum
00025                         of an already reduced data cube of a standard star
00026                         observation. Additionally, a conversion factor from
00027                         mag to counts/sec can be determined if the magnitude
00028                         of the standard star is known.
00029                         This is done for a number of jittered data cubes and
00030                         the results are averaged by rejecting the extreme
00031                         values
00032 
00033  ---------------------------------------------------------------------------*/
00034 
00035 #ifdef HAVE_CONFIG_H
00036 #  include <config.h>
00037 #endif
00038 /*----------------------------------------------------------------------------
00039                                 Includes
00040  ---------------------------------------------------------------------------*/
00041 #include <math.h>
00042 
00043 //Used only for sinfo_propertylist_has
00044 #include <irplib_stdstar.h>
00045 #include "irplib_utils.h"
00046 #include "sinfo_dfs.h"
00047 #include "sinfo_tpl_utils.h"
00048 
00049 #include "sinfo_new_stdstar.h"
00050 #include "sinfo_standstar_ini_by_cpl.h"
00051 #include "sinfo_pro_save.h"
00052 #include "sinfo_pfits.h"
00053 #include "sinfo_utilities_scired.h"
00054 #include "sinfo_spectrum_ops.h"
00055 #include "sinfo_hidden.h"
00056 #include "sinfo_functions.h"
00057 #include "sinfo_utl_efficiency.h"
00058 #include "sinfo_error.h"
00059 #include "sinfo_utils_wrappers.h"
00060 /*----------------------------------------------------------------------------
00061                                 Defines
00062  ---------------------------------------------------------------------------*/
00063 
00064 
00065 /*----------------------------------------------------------------------------
00066                              Function Definitions
00067  ---------------------------------------------------------------------------*/
00068 /* temporally commented out as not yet used
00069 static int
00070 sinfo_compute_efficiency(cpl_frameset* sof,
00071                               const char* name,
00072                   standstar_config ** cfg,
00073                   cpl_imagelist  * cube,
00074              cpl_table** tbl_spectrum);
00075 
00076 */
00077 
00085 /*----------------------------------------------------------------------------
00086    Function     :       sinfo_stdstar()
00087    In           :       ini_file: file name of according .ini file
00088    Out          :       integer (0 if it worked, -1 if it doesn't)
00089    Job          :     this routine carries through the data cube creation of an
00090                         object science observation using object-sky nodding
00091                         and jittering. This script expects jittered frames that
00092                 were already sky-subtracted
00093                         averaged, flatfielded, spectral tilt corrected and
00094             interleaved if necessary
00095  ---------------------------------------------------------------------------*/
00096 int
00097 sinfo_new_stdstar(const char* plugin_id,
00098                   cpl_parameterlist* config,
00099                   cpl_frameset* sof,cpl_frameset* ref_set)
00100 {
00101 
00102   cpl_errorstate clean_state = cpl_errorstate_get();
00103     standstar_config * cfg=NULL ;
00104     cpl_imagelist  * cube=NULL ;
00105 
00106     cpl_imagelist  * list_object=NULL ;
00107     cpl_image ** spectrum=NULL ;
00108     cpl_image * img_spct=NULL ;
00109 
00110 
00111 
00112     cpl_frameset* raw=NULL;
00113     cpl_frame* frame=NULL;
00114     cpl_image* std_med_ima=NULL;
00115     cpl_image* std_med_dup=NULL;
00116 
00117     cpl_table* qclog_tbl=NULL;
00118     cpl_table* tbl_spectrum=NULL;
00119     cpl_propertylist* plist=NULL;
00120     //char band[FILE_NAME_SZ];
00121 
00122     char * name=NULL ;
00123     int fra=0;
00124     float exptime=0 ;
00125     double convfactor=0;
00126     double cleanfactor=0;
00127     float* factor=NULL;
00128 
00129     char std_med_filename[MAX_NAME_SIZE];
00130     char std_cub_filename[MAX_NAME_SIZE];
00131 
00132     double max_ima_cx=0;
00133     double max_ima_cy=0;
00134     int max_ima_x=0;
00135     int max_ima_y=0;
00136     double norm=0;
00137     double xcen=0;
00138     double ycen=0;
00139     double sig_x=0;
00140     double sig_y=0;
00141     double fwhm_x=0;
00142     double fwhm_y=0;
00143     double disp=0;
00144     double dispersion=0;
00145     int i=0;
00146     int wllx=0;
00147     int wlly=0;
00148     int wurx=0;
00149     int wury=0;
00150     int psf_sz=40;
00151     int qc_info=0;
00152     int ima_szx=0;
00153     int ima_szy=0;
00154     int check1=0;
00155     int check2=0;
00156     int check3=0;
00157     int check4=0;
00158     double xshift=0;
00159     double yshift=0;
00160 
00161     float cenpix = 0;
00162     float cenLambda = 0;
00163 
00164     int no=0;
00165     double lo_cut=0.;
00166     double hi_cut=0.;
00167 
00168     const char* stdstars=NULL;
00169     const char* sed=NULL;
00170     cpl_frame* frm_sci=NULL;
00171     cpl_frame* frm_atmext=NULL;
00172     cpl_frame* frm_std_cat=NULL;
00173     cpl_table* tot_eff=NULL;
00174     double fpar[7];
00175     double dpar[7];
00176     int mpar[7];
00177     int do_compute_eff=0;
00178     cpl_parameter* p=NULL;
00179 
00180 
00181     //For new way to compute efficiency
00182     //char band[80];
00183     //const char  *   seds_file=NULL;
00184     //const char  *   filter=NULL;
00185     //cpl_table* tbl_eff=NULL;
00186     /*
00187        parse the file names and parameters to the cube_config
00188        data structure cfg
00189      */
00190 
00191     /* sinfo_msg("Parse cpl input"); */
00192      check_nomsg(raw=cpl_frameset_new());
00193 
00194     cknull(cfg=sinfo_parse_cpl_input_standstar(config,sof,&raw),
00195        "could not parse cpl input!") ;
00196 
00197  
00198     cknull_nomsg(p = cpl_parameterlist_find(config,
00199                      "sinfoni.std_star.compute_eff"));
00200  
00201     check_nomsg(do_compute_eff = cpl_parameter_get_bool(p));
00202      cknull(list_object = cpl_imagelist_new (),
00203        "could not allocate memory");
00204 
00205 
00206     sed = sinfo_extract_filename(sof, SINFO_CALIB_SED) ;
00207     stdstars = sinfo_extract_filename(sof, SINFO_CALIB_STDSTARS) ;
00208 
00209 
00210     if (cfg->convInd == 1) {
00211       factor = sinfo_new_floatarray(cfg->nframes);
00212     }
00213 
00214     if(NULL != cpl_frameset_find(sof,PRO_COADD_STD)) {
00215       frame = cpl_frameset_find(sof,PRO_COADD_STD);
00216       strcpy(std_cub_filename,cpl_frame_get_filename(frame));
00217     } else if(NULL != cpl_frameset_find(sof,PRO_COADD_PSF)) {
00218       frame = cpl_frameset_find(sof,PRO_COADD_PSF);
00219       strcpy(std_cub_filename,cpl_frame_get_filename(frame));
00220     } else if(NULL != cpl_frameset_find(sof,PRO_COADD_OBJ)) {
00221       frame = cpl_frameset_find(sof,PRO_COADD_OBJ);
00222       strcpy(std_cub_filename,cpl_frame_get_filename(frame));
00223     } else if(NULL != cpl_frameset_find(sof,PRO_COADD_PUPIL)) {
00224       frame = cpl_frameset_find(sof,PRO_COADD_PUPIL);
00225       strcpy(std_cub_filename,cpl_frame_get_filename(frame));
00226     } else {
00227       sinfo_msg_error("Frame %s, %s, %s or %s not found! Exit!",
00228                        PRO_COADD_STD,PRO_COADD_PSF,
00229                        PRO_COADD_OBJ,PRO_COADD_PUPIL );
00230       goto cleanup;
00231     }
00232 
00233 
00234     cknull(plist = cpl_propertylist_load(std_cub_filename, 0),
00235       "getting header from reference ima frame %s",std_cub_filename);
00236 
00237     cenpix = sinfo_pfits_get_crpix3(plist);
00238     cenLambda = sinfo_pfits_get_crval3(plist);
00239     dispersion = sinfo_pfits_get_cdelt3(plist);
00240 
00241 
00242     if (sinfo_propertylist_has(plist, KEY_NAME_CDELT3)) {
00243       disp=cpl_propertylist_get_double(plist, KEY_NAME_CDELT3);
00244     } else {
00245       sinfo_msg_warning("Keyword %s not found.",KEY_NAME_CDELT3);
00246     }
00247 
00248 
00249     sinfo_free_propertylist(&plist) ;
00250 
00251     /* we find automatiocally extraction parameters */
00252 
00253 
00254     if(NULL != cpl_frameset_find(sof,PRO_MED_COADD_STD)) {
00255       frame = cpl_frameset_find(sof,PRO_MED_COADD_STD);
00256       strcpy(std_med_filename,cpl_frame_get_filename(frame));
00257       check_nomsg(std_med_ima=cpl_image_load(std_med_filename,
00258                                              CPL_TYPE_FLOAT,0,0));
00259     } else if(NULL != cpl_frameset_find(sof,PRO_OBS_STD)) {
00260       check_nomsg(frame = cpl_frameset_find(sof,PRO_OBS_STD));
00261       strcpy(std_cub_filename,cpl_frame_get_filename(frame));
00262       check_nomsg(cube = cpl_imagelist_load(std_cub_filename,
00263                                              CPL_TYPE_FLOAT,0));
00264       strcpy(std_med_filename,STDSTAR_OUT_MED_CUBE);
00265       check_nomsg(std_med_ima=cpl_imagelist_collapse_median_create(cube));
00266       sinfo_free_imagelist(&cube);
00267 
00268       ck0(sinfo_pro_save_ima(std_med_ima,ref_set,sof,STDSTAR_OUT_MED_CUBE,
00269                  PRO_MED_OBS_PSF,NULL,plugin_id,config),
00270       "Error saving image %s tag %s",STDSTAR_OUT_MED_CUBE,PRO_MED_OBS_PSF);
00271 
00272     } else if(NULL != cpl_frameset_find(sof,PRO_MED_COADD_PSF)) {
00273       check_nomsg(frame = cpl_frameset_find(sof,PRO_MED_COADD_PSF));
00274       strcpy(std_med_filename,cpl_frame_get_filename(frame));
00275       check_nomsg(std_med_ima=cpl_image_load(std_med_filename,
00276                                              CPL_TYPE_FLOAT,0,0));
00277     } else if(NULL != cpl_frameset_find(sof,PRO_OBS_PSF)) {
00278       check_nomsg(frame = cpl_frameset_find(sof,PRO_OBS_PSF));
00279       strcpy(std_cub_filename,cpl_frame_get_filename(frame));
00280       check_nomsg(cube=cpl_imagelist_load(std_cub_filename,CPL_TYPE_FLOAT,0));
00281       strcpy(std_med_filename,STDSTAR_OUT_MED_CUBE);
00282       check_nomsg(std_med_ima=cpl_imagelist_collapse_median_create(cube));
00283       sinfo_free_imagelist(&cube);
00284 
00285       ck0(sinfo_pro_save_ima(std_med_ima,ref_set,sof,STDSTAR_OUT_MED_CUBE,
00286                  PRO_MED_OBS_PSF,NULL,plugin_id,config),
00287       "Error saving image %s tag %s",STDSTAR_OUT_MED_CUBE,PRO_MED_OBS_PSF);
00288 
00289     } else if(NULL != cpl_frameset_find(sof,PRO_MED_COADD_OBJ)) {
00290       check_nomsg(frame = cpl_frameset_find(sof,PRO_MED_COADD_OBJ));
00291       strcpy(std_med_filename,cpl_frame_get_filename(frame));
00292       check_nomsg(std_med_ima=cpl_image_load(std_med_filename,
00293                                              CPL_TYPE_FLOAT,0,0));
00294     } else if(NULL != cpl_frameset_find(sof,PRO_OBS_OBJ)) {
00295       check_nomsg(frame = cpl_frameset_find(sof,PRO_OBS_OBJ));
00296       strcpy(std_cub_filename,cpl_frame_get_filename(frame));
00297       check_nomsg(cube = cpl_imagelist_load(std_cub_filename,
00298                                              CPL_TYPE_FLOAT,0));
00299       strcpy(std_med_filename,STDSTAR_OUT_MED_CUBE);
00300       check_nomsg(std_med_ima=cpl_imagelist_collapse_median_create(cube));
00301       sinfo_free_imagelist(&cube);
00302 
00303       ck0(sinfo_pro_save_ima(std_med_ima,ref_set,sof,STDSTAR_OUT_MED_CUBE,
00304                  PRO_MED_OBS_OBJ,NULL,plugin_id,config),
00305       "Error saving image %s tag %s",STDSTAR_OUT_MED_CUBE,PRO_MED_OBS_OBJ);
00306     } else {
00307       sinfo_msg_error("Frame %s %s %s %s %s %s not found! Exit!",
00308               PRO_MED_COADD_STD, PRO_OBS_STD,
00309                       PRO_MED_COADD_PSF, PRO_OBS_PSF,
00310                       PRO_MED_COADD_OBJ, PRO_OBS_OBJ);
00311       goto cleanup;
00312     }
00313 
00314 
00315     check_nomsg(std_med_dup=cpl_image_duplicate(std_med_ima));
00316     sinfo_clean_nan(&std_med_dup);
00317     check_nomsg(cpl_image_get_maxpos(std_med_dup,&max_ima_x,&max_ima_y));
00318     sinfo_free_image(&std_med_dup);
00319 
00320 
00321     ima_szx=cpl_image_get_size_x(std_med_ima);
00322     ima_szy=cpl_image_get_size_y(std_med_ima);
00323     wllx= ((max_ima_x-psf_sz)>0)       ? (max_ima_x-psf_sz) : 1;
00324     wlly= ((max_ima_y-psf_sz)>0)       ? (max_ima_y-psf_sz) : 1;
00325     wurx= ((max_ima_x+psf_sz)<ima_szx) ? (max_ima_x+psf_sz) : ima_szx ;
00326     wury= ((max_ima_y+psf_sz)<ima_szy) ? (max_ima_y+psf_sz) : ima_szy ;
00327     /*
00328     sinfo_msg("wllx=%d wlly=%d wurx=%d wury=%d\n",wllx,wlly,wurx,wury);
00329     cpl_image_get_maxpos_window(std_med_ima,wllx,wlly,wurx,wury,
00330                                 &max_ima_x,&max_ima_y);
00331     */
00332     check_nomsg(qclog_tbl = sinfo_qclog_init());
00333     check_nomsg(max_ima_cx=cpl_image_get_centroid_x_window(std_med_ima,wllx,
00334                                                            wlly,wurx,wury));
00335     check_nomsg(max_ima_cy=cpl_image_get_centroid_y_window(std_med_ima,wllx,
00336                                                              wlly,wurx,wury));
00337 
00338 
00339       xshift=max_ima_cx-ima_szx/2;
00340       yshift=max_ima_cy-ima_szy/2;
00341 
00342       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SHIFTX",xshift,
00343                                        "X shift centroid - center image","%f"));
00344 
00345       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC SHIFTY",yshift,
00346                                        "Y shift centroid - center image","%f"));
00347 
00348     if(
00349           ((max_ima_x-psf_sz) < 1) ||
00350           ((max_ima_y-psf_sz) < 1) ||
00351           ((max_ima_x+psf_sz) > ima_szx) ||
00352           ((max_ima_x+psf_sz) > ima_szy)
00353       )
00354       {
00355         psf_sz = (psf_sz < (max_ima_x-1))     ? psf_sz : (max_ima_x-1);
00356         psf_sz = (psf_sz < (max_ima_y-1))     ? psf_sz : (max_ima_y-1);
00357         psf_sz = (psf_sz < ima_szx-max_ima_x) ? psf_sz : (ima_szx-max_ima_x);
00358         psf_sz = (psf_sz < ima_szy-max_ima_y) ? psf_sz : (ima_szy-max_ima_y);
00359         //added to prevent seg fault by cpl_image_fit_gaussian
00360         psf_sz = (psf_sz > 4) ? psf_sz : 4;
00361       }
00362 
00363 
00364       ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM LLX",cfg->llx,
00365                                     "STD star FWHM LLX","%d"));
00366       ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM LLY",cfg->lly,
00367                                     "STD star FWHM LLY","%d"));
00368       ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM HBX",cfg->halfbox_x,
00369                                     "STD star FWHM HBX","%d"));
00370       ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC FWHM HBX",cfg->halfbox_y,
00371                                     "STD star FWHM HBY","%d"));
00372 
00373 
00374     /* call the 2D-Gaussian fit routine */
00375     for ( i = 0 ; i < 7 ; i++ )
00376     {
00377         mpar[i] = 1 ;
00378     }
00379 
00380 
00381      if(-1 == sinfo_new_fit_2d_gaussian(std_med_ima,
00382                                   fpar,
00383                                   dpar,
00384                                   mpar,
00385                                   cfg->llx,
00386                                   cfg->lly,
00387                                   cfg->halfbox_x,
00388                                   cfg->halfbox_y,
00389                                   &check4 ) ) {
00390       irplib_error_recover(clean_state,"2d Gaussian fit failed");
00391       /* return 0; */
00392 
00393      } else {
00394 
00395       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MAJ",fpar[4],
00396                                        "STD star FWHM on major axis","%f"));
00397       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHM MIN",fpar[5],
00398                                        "STD star FWHM on minor axis","%f"));
00399       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC THETA",fpar[6],
00400                                        "STD star ellipsis angle theta","%f"));
00401 
00402 
00403      }
00404 
00405 
00406     /*
00407     sinfo_msg("Gauss fit params: xc,yc,amp,bkg,fwhm_x,fwhm_y,angle\n");
00408     for ( i = 0 ; i < 7 ; i++ )
00409       {
00410         sinfo_msg("fpar[%d]=%f dpar[%d]=%f\n",i,fpar[i],i,dpar[i]);
00411       }
00412     */
00413     if(CPL_ERROR_NONE == cpl_image_fit_gaussian(std_med_ima,
00414                                                 max_ima_x,
00415                                                 max_ima_y,
00416                                                 psf_sz,
00417                                                 &norm,
00418                                                 &xcen,
00419                                                 &ycen,
00420                                                 &sig_x,
00421                                                 &sig_y,
00422                                                 &fwhm_x,
00423                                                 &fwhm_y)) {
00424 
00425 
00426       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHMX",fwhm_x,
00427                                        "STD star FWHM on X","%f"));
00428       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC FWHMY",fwhm_y,
00429                                        "STD star FWHM on Y","%f"));
00430 
00431       cfg -> halfbox_x =  (floor)(0.5*(fwhm_x+fwhm_y)*cfg->fwhm_factor+0.5);
00432 
00433       cfg -> halfbox_y =  (floor)(0.5*(fwhm_x+fwhm_y)*cfg->fwhm_factor+0.5);
00434 
00435     } else {
00436 
00437       irplib_error_recover(clean_state,"Problem fitting Gaussian");
00438       cpl_error_reset();
00439 
00440     }
00441     sinfo_free_image(&std_med_ima);
00442 
00443       /*
00444       sinfo_msg("max ima=%d %d psf_sz=%d\n",max_ima_x,max_ima_y,psf_sz);
00445       sinfo_msg("centroid ima=%f %f\n",max_ima_cx,max_ima_cy);
00446       sinfo_msg("gauss=norm=%f xcen=%f ycen=%f sig_x=%f "
00447                 "sig_y=%f fwhm_x=%f fwhm_y=%f\n",
00448                           norm,xcen,ycen,sig_x,sig_y,fwhm_x,fwhm_y);
00449       */
00450 
00451       cfg -> llx = (int)(xcen-cfg->halfbox_x);
00452       cfg -> llx = (cfg -> llx  > 0 ) ? cfg -> llx  : 1;
00453 
00454       if((cfg->llx+2*cfg->halfbox_x) >= ima_szx) {
00455     cfg -> halfbox_x=(int) ((ima_szx-cfg->llx-1)/2);
00456         check1++;
00457       }
00458 
00459       cfg -> lly = (int)(ycen-cfg->halfbox_y);
00460       cfg -> lly = (cfg -> lly  > 0 ) ? cfg -> lly  : 1;
00461       if((cfg->lly+2*cfg->halfbox_y) >= ima_szy) {
00462     cfg -> halfbox_y=(int) ((ima_szy-cfg->lly-1)/2);
00463         check1++;
00464       }
00465      ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK1",check1,
00466                                     "Check on evaluation box","%d"));
00467 
00468 
00469     /*
00470       sinfo_msg("llx= %d lly= %d\n",cfg->llx, cfg->lly);
00471       sinfo_msg("halfbox_x=%d halfbox_y=%d\n",cfg->halfbox_x,cfg->halfbox_y);
00472      */
00473 
00474     /*
00475 #----------------------------------------------------------------------
00476 #---------------------------EXTRACTION---------------------------------
00477 #----------------------------------------------------------------------
00478     */
00479 
00480       sinfo_msg("Extraction");
00481       cknull(spectrum = (cpl_image**) cpl_calloc (cfg -> nframes,
00482                                                   sizeof(cpl_image*)),
00483                             "Could not allocate memory for spectrum image");
00484 
00485       for (fra=0; fra < cfg->nframes; fra++) {
00486          name = cfg->inFrameList[fra];
00487          if(sinfo_is_fits_file(name) != 1) {
00488             sinfo_msg_error("Input file %s is not FITS",name);
00489             goto cleanup;
00490      }
00491          cknull(cube = cpl_imagelist_load(name,CPL_TYPE_FLOAT,0),
00492         "could not load data cube" );
00493 
00494          if (exptime == FLAG) {
00495        sinfo_msg_error("could not find exposure time in the fits header");
00496        return -1;
00497      }
00498          exptime = sinfo_pfits_get_ditndit(name);
00499 
00500          sinfo_msg("cfg->gain %f",cfg->gain);
00501          check_nomsg(tbl_spectrum=cpl_table_new(cpl_imagelist_get_size(cube)));
00502         if(NULL==(spectrum[fra]=sinfo_new_optimal_extraction_from_cube( cube,
00503                                       cfg->llx,
00504                                       cfg->lly,
00505                                       cfg->halfbox_x,
00506                                       cfg->halfbox_y,
00507                                       cfg->fwhm_factor,
00508                                       BKG_VARIANCE,
00509                                       SKY_FLUX,
00510                                       cfg->gain,
00511                                       exptime,
00512                                       name,
00513                                       &tbl_spectrum,
00514                                       qc_info,
00515                                       &check2))){
00516 
00517       irplib_error_recover(clean_state,
00518                "could not do sinfo_optimalExtractionFromCube");
00519      } else {
00520      check_nomsg(cpl_imagelist_set(list_object,
00521                      cpl_image_duplicate(spectrum[fra]), fra));
00522 
00523      }
00524 
00525 
00526 
00527     ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK2",
00528                                        check2,"Check on evaluation box","%d"));
00529 
00530 
00531      //NEW EFFICIENCY
00532      /*
00533          plist=cpl_propertylist_load(cpl_frame_get_filename(frame),0);
00534          strcpy(band,sinfo_pfits_get_band(plist));
00535          sinfo_free_propertylist(&plist);
00536 
00537          sinfo_msg("Observing in band filter = %s",band);
00538      if(-1 == sinfo_calib_flux_std(sed,
00539                    stdstars,band,frame,
00540                                    tbl_spectrum,cfg->mag)) {
00541            sinfo_msg_warning("Problems computing efficiency new way");
00542            cpl_error_reset();
00543      } else {
00544         ck0(sinfo_pro_save_tbl(tbl_spectrum,ref_set,sof,
00545                                       (char*)STDSTAR_OUT_TABLE,
00546                       PRO_STD_STAR_SPECTRA,qclog_tbl,
00547                 plugin_id,config),
00548          "cannot dump ima %s", "out_std_star_spectrum.fits");
00549         //sinfo_free_table(&tbl_eff);
00550      }
00551 
00552 
00553      if(-1 == sinfo_compute_efficiency(sof,name,&cfg,cube,
00554                        &tbl_spectrum)) {
00555            sinfo_msg_warning("Problems computing efficiency");
00556 
00557       irplib_error_recover(clean_state,
00558                            "could not do sinfo_optimalExtractionFromCube");
00559      }
00560      */
00561         ck0(sinfo_pro_save_tbl(tbl_spectrum,ref_set,sof,
00562                                       (char*)STDSTAR_OUT_TABLE,
00563                       PRO_STD_STAR_SPECTRA,qclog_tbl,
00564                 plugin_id,config),
00565          "cannot dump ima %s", "out_std_star_spectrum.fits");
00566 
00567 
00568      sinfo_free_table(&qclog_tbl);
00569 
00570      if (do_compute_eff) {
00571         sinfo_msg("compute efficiency");
00572         frm_sci     = cpl_frameset_find(sof,PRO_STD_STAR_SPECTRA);
00573         frm_std_cat = cpl_frameset_find(sof,FLUX_STD_CATALOG);
00574         frm_atmext  = cpl_frameset_find(sof,EXTCOEFF_TABLE);
00575 
00576 
00577 
00578         check_nomsg(tot_eff=sinfo_efficiency_compute(frm_sci,frm_std_cat,
00579                                                      frm_atmext));
00580         ck0(sinfo_pro_save_tbl(tot_eff,ref_set,sof,(char*)EFFICIENCY_FILENAME,
00581                                PRO_EFFICIENCY,qclog_tbl,plugin_id,config),
00582             "cannot dump ima %s", "out_.fits");
00583      }
00584 
00585 
00586 
00587 
00588      /*
00589          if(spectrum[fra] != NULL ) {
00590          sinfo_free_image(&(spectrum)[fra]);
00591          }
00592      */
00593      /*----determine the intensity conversion factor if wished--------*/
00594      if (cfg->convInd == 1) {
00595        sinfo_msg("Determines convertion factor");
00596 
00597            convfactor = sinfo_new_determine_conversion_factor( cube,
00598                                                 cfg->mag,
00599                                                 exptime,
00600                                                 cfg->llx,
00601                                                 cfg->lly,
00602                                                 cfg->halfbox_x,
00603                                                 cfg->halfbox_y,
00604                                                 &check3 );
00605 
00606 
00607             if (convfactor < -100000.) {
00608             sinfo_msg_warning("could not do sinfo_determineConversionFactor!" );
00609            /* goto cleanup; */
00610         } else {
00611              sinfo_new_array_set_value(factor, convfactor, fra);
00612         }
00613      }
00614      sinfo_free_imagelist(&cube);
00615       } /* end loop over fra */
00616 
00617       sinfo_free_table(&tbl_spectrum);
00618       sinfo_free_image_array(&spectrum,cfg->nframes);
00619       if (cfg->convInd == 1) {
00620     sinfo_msg("Determines clean factor");
00621         cleanfactor = sinfo_new_clean_mean(factor,
00622                                  cfg->nframes,
00623                                  cfg->lo_reject*100.,
00624                  cfg->hi_reject*100.);
00625       }
00626       if (cleanfactor > 100000. || cleanfactor == FLAG) {
00627     sinfo_msg_error("could not do sinfo_clean_mean!" );
00628         goto cleanup;
00629       }
00630 
00631 
00632   /*---read the fits header to change the gain and noise parameter-----*/
00633       sinfo_msg("Average with rejection");
00634 
00635       no=cpl_imagelist_get_size(list_object);
00636       lo_cut=(floor)(cfg->lo_reject*no+0.5);
00637       hi_cut=(floor)(cfg->hi_reject*no+0.5);
00638       if(no > 0) {
00639          cknull(img_spct=cpl_imagelist_collapse_minmax_create(list_object,
00640                                                               lo_cut,hi_cut),
00641                           "sinfo_average_with_rejection failed" );
00642       }
00643 
00644       sinfo_free_imagelist(&list_object);
00645       if(no > 0) {
00646     check_nomsg(qclog_tbl = sinfo_qclog_init());
00647 
00648         ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC CONVFCT",cleanfactor,
00649                                          "Conversion factor","%g"));
00650 
00651         ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC CHECK3",check3,
00652                                       "Check evaluation box","%d"));
00653 
00654 
00655         ck0(sinfo_pro_save_ima(img_spct,ref_set,sof,cfg->outName,
00656                     PRO_STD_STAR_SPECTRUM,qclog_tbl,
00657                  plugin_id,config),
00658         "cannot dump ima %s", cfg->outName);
00659 
00660         sinfo_new_set_wcs_spectrum(img_spct,cfg->outName,cenLambda,disp,cenpix);
00661         sinfo_free_table(&qclog_tbl);
00662       }
00663       /*#---free memory---*/
00664       if(factor != NULL) sinfo_new_destroy_array(&factor);
00665       sinfo_free_image(&img_spct);
00666       sinfo_stdstar_free(&cfg);
00667       sinfo_free_frameset(&raw);
00668 
00669       return 0;
00670 
00671  cleanup:
00672       sinfo_free_table(&tbl_spectrum);
00673       sinfo_free_table(&qclog_tbl);
00674       sinfo_free_imagelist(&list_object);
00675       if(spectrum != NULL) sinfo_free_image_array(&spectrum,cfg->nframes);
00676       sinfo_free_image(&std_med_ima);
00677       sinfo_free_image(&std_med_dup);
00678       sinfo_free_imagelist(&cube);
00679       sinfo_free_propertylist(&plist) ;
00680       if(factor != NULL) sinfo_new_destroy_array(&factor);
00681       sinfo_free_image(&img_spct);
00682       sinfo_stdstar_free (&cfg);
00683       sinfo_free_frameset(&raw);
00684     return -1;
00685 
00686 }
00687 
00688 /* temporally commented out as not yet used
00689 static int
00690 sinfo_compute_efficiency(cpl_frameset* sof,
00691                               const char* name,
00692                   standstar_config ** cfg,
00693                   cpl_imagelist  * cube,
00694                   cpl_table** tbl_spectrum)
00695 {
00696 
00697 
00698   int dit=0;
00699   int i=0;
00700   int status=0;
00701 
00702 
00703   double dispersion=0;
00704   double h=0;
00705   double c=0;
00706   double k=0;
00707   double temp=0;
00708   double surface=0;
00709   double gain=0;
00710   double num=0;
00711   double den=0;
00712   double fctr=0;
00713   double wc=0;
00714   double wave=0;
00715   double std_ra=0;
00716   double std_dec=0;
00717   double mag=0;
00718 
00719 
00720   double int_spct=0;
00721   double bb_flux_norm=0;
00722   double efficiency=0;
00723   double f0=0;
00724   char band[FILE_NAME_SZ];
00725   sinfo_band std_band=0;
00726   cpl_propertylist* plist=NULL;
00727   const char* sed=NULL;
00728   const char* stdstars=NULL;
00729 
00730     char                    star_name[MAX_NAME_SIZE] ;
00731     char                    star_type[MAX_NAME_SIZE] ;
00732 
00733 
00734     sed = sinfo_extract_filename(sof, SINFO_CALIB_SED) ;
00735     stdstars = sinfo_extract_filename(sof, SINFO_CALIB_STDSTARS) ;
00736 
00737   plist=cpl_propertylist_load(name,0);
00738   strcpy(band,sinfo_pfits_get_band(plist));
00739   std_ra =(double) sinfo_pfits_get_ra(plist);
00740   std_dec=(double) sinfo_pfits_get_dec(plist);
00741   sinfo_free_propertylist(&plist);
00742 
00743   if (strcmp(band,"H+K") == 0) {
00744     f0=8.00e-11;   // erg/s/cm/cm/A
00745     wc=1.950;
00746         std_band=SINFO_BAND_K;
00747   } else if (strcmp(band,"K") == 0) {
00748     f0=4.10e-11;
00749     wc=2.175;
00750         std_band=SINFO_BAND_K;
00751   } else if (strcmp(band,"J") == 0) {
00752     f0=3.11e-10;
00753     wc=1.225;
00754         std_band=SINFO_BAND_J;
00755   } else if (strcmp(band,"H") == 0) {
00756     f0=1.15e-10;
00757     wc=1.675;
00758         std_band=SINFO_BAND_H;
00759   }
00760 
00761 
00762   sinfo_msg("Extracting magnitude from catalog");
00763   if(irplib_stdstar_get_mag(stdstars,std_ra, std_dec,band,
00764                             "all",&mag,star_name, star_type,2.0) == -1) {
00765     sinfo_msg_warning("cannot find the star in  the catalog.");
00766     sinfo_msg_warning("Use a default magnitude of 0");
00767   } else {
00768     (*cfg)->mag=mag;;
00769   }
00770   sinfo_msg("Catalogue magnitude=%g",mag);
00771 
00772   plist=cpl_propertylist_load(name,0);
00773   dispersion = sinfo_pfits_get_cdelt3(plist);
00774   dit = sinfo_pfits_get_dit(plist); // s
00775   sinfo_free_propertylist(&plist);
00776   dispersion = 1e4 * dispersion;   // A/pix
00777   h=PLANCK;                        // J s
00778   c=SPEED_OF_LIGHT;                 // m / s
00779   k= BOLTZMANN;                     // J / K
00780   temp=1e4;                         // K, star temperature
00781   surface=1e4 * TELESCOPE_SURFACE;  // cm * cm
00782   gain = 2.42;                      // e / ADU
00783 
00784   num= gain * pow(10,(mag/2.5)) * 1e7 * h * c; // e / ADU erg s  m / s
00785   den= dit * surface * dispersion * wc * 1e-6; // s cm cm  A / pix m
00786 
00787   fctr = num/den;                              // e pix/ADU erg/s/cm/cm/A
00788   sinfo_msg("Dispersion: %f A/pix",dispersion);
00789 
00790      // erg/s/cm/cm/A
00791      // int_spct * fctr  = e erg/s/cm/cm/A
00792 
00793   check_nomsg(cpl_table_new_column(*tbl_spectrum,"efficiency",CPL_TYPE_FLOAT));
00794 
00795 
00796   for (i=0;i<cpl_imagelist_get_size(cube);i++) {
00797 
00798     check_nomsg(wave=cpl_table_get_float(*tbl_spectrum,"wavelength",i,&status));
00799     check_nomsg(int_spct=cpl_table_get_float(*tbl_spectrum,"counts_bkg",
00800                                              i,&status));
00801 
00802     bb_flux_norm = pow(wc/wave,5) * (exp(h*c/(wc * 1e-6*k*temp))-1)/
00803                                        (exp(h*c/(wave*1e-6*k*temp))-1);
00804 
00805 
00806     efficiency = int_spct*fctr/(bb_flux_norm *f0);
00807     check_nomsg(cpl_table_set_float(*tbl_spectrum,"efficiency",i,efficiency));
00808 
00809 
00810   }
00811 
00812 return 0;
00813 
00814  cleanup:
00815   sinfo_free_propertylist(&plist);
00816  return -1;
00817 
00818 }
00819 */
00820 
00821 
00822 

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