sinfo_bp_lin.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_bp_lin.c
00022    Author       :    J. Schreiber
00023    Created on   :    May 5, 2003
00024    Description  :    Different methods for searching for bad pixels
00025                         used in the recipe spiffi_badsearch
00026 
00027  ---------------------------------------------------------------------------*/
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 /*----------------------------------------------------------------------------
00033                                 Includes
00034  ---------------------------------------------------------------------------*/
00035 //#include <cpl_imagelist_basic.h>
00036 #include <cpl.h>
00037 #include "sinfo_bp_lin.h"
00038 #include "sinfo_detlin.h"
00039 #include "sinfo_detlin_ini_by_cpl.h"
00040 #include "sinfo_pro_save.h"
00041 #include "sinfo_pro_types.h"
00042 #include "sinfo_functions.h"
00043 #include "sinfo_hidden.h"
00044 #include "sinfo_error.h"
00045 #include "sinfo_utils_wrappers.h"
00046 
00047 
00054 /*----------------------------------------------------------------------------
00055                                 Defines
00056  ---------------------------------------------------------------------------*/
00057 /*----------------------------------------------------------------------------
00058                              Function Definitions
00059  ---------------------------------------------------------------------------*/
00086 int
00087 sinfo_new_bp_search_lin(const char* plugin_id,
00088                         cpl_parameterlist* config,
00089                         cpl_frameset* sof)
00090 {
00091   detlin_config * cfg=NULL;
00092   cpl_imagelist * imgl=NULL ;
00093   cpl_imagelist * coeffs_imgl=NULL;
00094   cpl_image * img_tmp=NULL ;
00095   cpl_image * mask=NULL;
00096   cpl_vector* cube_mean=NULL;
00097   cpl_frameset* raw=NULL;
00098   cpl_frameset* raw_on=NULL;
00099   cpl_frameset* raw_of=NULL;
00100   cpl_frame* frm=NULL;
00101   cpl_frame* frm_dup=NULL;
00102 
00103   cpl_table* qclog_tbl=NULL;
00104 
00105   cpl_parameter *p=NULL;
00106   cpl_polynomial *pol=NULL;
00107   cpl_vector* vec_adl=NULL;
00108   cpl_vector* vec_med=NULL;
00109   double* mse=NULL;
00110   int i=0;
00111   int n_bad=0 ;
00112   int nff=0;
00113   char key_value[FILE_NAME_SZ];
00114   char key_name[FILE_NAME_SZ];
00115   cpl_table* det_lin=NULL;
00116   int nraw=0;
00117   int* status=NULL;
00118   cpl_table* gain=NULL;
00119   int ngain=0;
00120   int sz_imgl=0;
00121 
00122   int llx=270;
00123   int lly=1030;
00124   int urx=310;
00125   int ury=1060;
00126   int zone[4];
00127   //int kappa=5;
00128   //int nclip=25;
00129   int pdensity=0;
00130 
00131 
00132   check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
00133   check_nomsg(pdensity=cpl_parameter_get_int(p));
00134 
00135 
00136   /* parse the file names and parameters to the bad_config
00137      data structure cfg */
00138   check_nomsg(raw=cpl_frameset_new());
00139   cknull(cfg = sinfo_parse_cpl_input_detlin(config,sof,&raw),
00140      "could not parse .ini file!");
00141 
00142   /* ======================================================================
00143      DETERMINES LINEARITY COEFF AS DFO DOES
00144      ======================================================================
00145   */
00146   nff=cpl_frameset_get_size(raw);
00147   raw_on=cpl_frameset_new();
00148   raw_of=cpl_frameset_new();
00149   /* separates on and off frames */
00150 
00151   for(i=0;i<nff;i++) {
00152     frm=cpl_frameset_get_frame(raw,i);
00153     frm_dup=cpl_frame_duplicate(frm);
00154     if(sinfo_frame_is_on(frm)) {
00155       cpl_frameset_insert(raw_on,frm_dup);
00156     } else {
00157       cpl_frameset_insert(raw_of,frm_dup);
00158     }
00159   }
00160   /*
00161     cknull(det_lin=sinfo_get_linearity(raw_on,raw_of),
00162     "Error computing linearity");
00163   */
00164   zone[0]=llx;
00165   zone[1]=lly;
00166   zone[2]=urx;
00167   zone[3]=ury;
00168 
00169   //If one refers to a larger area
00170   zone[0]=20;
00171   zone[1]=2028;
00172   zone[2]=20;
00173   zone[3]=2028;
00174 
00175   /*
00176     cknull(det_lin=irplib_compute_linearity(raw_on,raw_of),
00177     "Error computing linearity");
00178   */
00179 
00180   if(pdensity > 1 ) {
00181 
00182     sinfo_msg("Computes linearity");
00183     cknull(det_lin=sinfo_compute_linearity(raw_on,raw_of),
00184        "Error computing linearity");
00185 
00186     check_nomsg(nraw=cpl_table_get_nrow(det_lin));
00187     check_nomsg(vec_adl=cpl_vector_new(nraw));
00188     check_nomsg(vec_med=cpl_vector_new(nraw));
00189 
00190     for(i=0;i<nraw;i++) {
00191       cpl_vector_set(vec_adl,i,cpl_table_get_double(det_lin,"adl",i,status));
00192       cpl_vector_set(vec_med,i,cpl_table_get_double(det_lin,"med",i,status));
00193     }
00194     check_nomsg(pol=sinfo_polynomial_fit_1d_create(vec_adl,vec_med,
00195                          cfg->order,mse));
00196     sinfo_free_my_vector(&vec_adl);
00197     sinfo_free_my_vector(&vec_med);
00198 
00199     cknull_nomsg(qclog_tbl=sinfo_qclog_init());
00200 
00201     for(i=0;i<cfg->order+1;i++) {
00202 
00203       snprintf(key_name,MAX_NAME_SIZE-1,"%s%i%s","QC BP-MAP LIN",i," MED");
00204       sinfo_qclog_add_double(qclog_tbl,key_name,
00205                  cpl_polynomial_get_coeff(pol,&i),
00206                  "Linearity Polynomial Coeff","%g");
00207     }
00208     sinfo_free_polynomial(&pol);
00209 
00210     check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.bp.method"));
00211     snprintf(key_value, MAX_NAME_SIZE-1,"%s",cpl_parameter_get_string(p));
00212     ck0_nomsg(sinfo_qclog_add_string(qclog_tbl,"QC BP-MAP METHOD",key_value,
00213                      "BP search method","%s"));
00214 
00215     ck0(sinfo_pro_save_tbl(det_lin,raw,sof,BP_LIN_LIN_DET_INFO_OUT_FILENAME,
00216                PRO_LIN_DET_INFO,qclog_tbl,plugin_id,config),
00217     "cannot dump ims %s",BP_LIN_LIN_DET_INFO_OUT_FILENAME);
00218 
00219     sinfo_free_table(&det_lin);
00220     sinfo_free_table(&qclog_tbl);
00221   }
00222 
00223   /*=======================================================*/
00224   if(pdensity > 1 ) {
00225     sinfo_msg("Computes gain");
00226 
00227     cknull(gain=sinfo_compute_gain(raw_on,raw_of),"Error computing gain");
00228 
00229     //cknull(gain=irplib_compute_gain(raw_on,raw_of,zone,kappa,nclip),
00230     //         "Error computing gain");
00231 
00232     sinfo_free_frameset(&raw_on);
00233     sinfo_free_frameset(&raw_of);
00234 
00235     check_nomsg(ngain=cpl_table_get_nrow(gain));
00236     cknull_nomsg(qclog_tbl=sinfo_qclog_init());
00237 
00238     for(i=0;i<ngain;i++) {
00239       snprintf(key_name,MAX_NAME_SIZE-1,"%s%i","QC GAIN",i);
00240       ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,
00241                        cpl_table_get_double(gain,"gain",i,status),
00242                        "Detector gain","%g"));
00243     }
00244 
00245     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC GAIN",
00246                      cpl_table_get_column_median(gain,"gain"),"Detector gain median","%g"));
00247     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC GAINERR",
00248                      cpl_table_get_column_stdev(gain,"gain"),"Detector gain error","%g"));
00249 
00250     ck0(sinfo_pro_save_tbl(gain,raw,sof,BP_LIN_GAIN_OUT_FILENAME,PRO_GAIN_INFO,
00251                qclog_tbl,plugin_id,config),
00252     "cannot dump tbl %s", BP_LIN_GAIN_OUT_FILENAME);
00253 
00254     sinfo_free_table(&gain);
00255     sinfo_free_table(&qclog_tbl);
00256   }
00257 
00258   /* =======================================================================
00259      DETERMINES LINEARITY COEFF AS MPE DOES
00260      =======================================================================
00261   */
00262 
00263 
00264   check_nomsg(imgl = cpl_imagelist_new());
00265   for ( i = 0 ; i < cfg->nframes ; i++ )
00266     {
00267       if(sinfo_is_fits_file (cfg->framelist[i]) != 1) {
00268     sinfo_msg_error("Input file %s is not FITS",cfg->framelist[i] );
00269     return -1;
00270       }
00271       check_nomsg(img_tmp=cpl_image_load(cfg->framelist[i],CPL_TYPE_FLOAT,0,0));
00272       check_nomsg(cpl_imagelist_set(imgl,img_tmp,i));
00273     }
00274 
00275   /*----------------------------------------------------------------
00276    *---------------------- SEARCH FOR BAD PIXELS---------------------
00277    *--------------------------------------------------------------*/
00278   sinfo_msg("Search for bad pixels");
00279   cknull(coeffs_imgl=sinfo_new_fit_intensity_course(imgl,
00280                             cfg->order,
00281                             cfg->loReject,
00282                             cfg->hiReject),
00283      "could not fit polynomial and store coeffs in a data cube!");
00284 
00285   sinfo_free_imagelist(&imgl) ;
00286   check_nomsg(sz_imgl=cpl_imagelist_get_size(coeffs_imgl));
00287 
00288 
00289   /*---store the polynomial fit coefficients in a data cube----*/
00290   /* CUBE NOT DUMPED BECAUSE PROBLEMS ON LAST PLANE */
00291   cube_mean=cpl_vector_new(sz_imgl);
00292 
00293   /* QC LOG */
00294   cknull_nomsg(qclog_tbl=sinfo_qclog_init());
00295   for(i=0;i<sz_imgl;i++) {
00296     cpl_vector_set(cube_mean,i,
00297            cpl_image_get_median(cpl_imagelist_get(coeffs_imgl,i)));
00298 
00299     snprintf(key_name,MAX_NAME_SIZE-1,"%s%i%s","QC BP-MAP LIN",i," MEAN");
00300     snprintf(key_value,MAX_NAME_SIZE-1,"%g",cpl_vector_get(cube_mean,i));
00301     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,key_name,
00302                      cpl_vector_get(cube_mean,i),
00303                      "Linearity Polynomial Coeff","%g"));
00304 
00305   }
00306   sinfo_free_my_vector(&cube_mean);
00307   check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.bp.method"));
00308   snprintf(key_value, MAX_NAME_SIZE-1,"%s",cpl_parameter_get_string(p));
00309   ck0_nomsg(sinfo_qclog_add_string(qclog_tbl,"QC BP-MAP METHOD",key_value,
00310                    "BP search method","%s"));
00311 
00312 
00313   ck0(sinfo_pro_save_ims(coeffs_imgl,raw,sof,cfg->coeffsCubeName,
00314              PRO_BP_COEFF,qclog_tbl,plugin_id,config),
00315       "cannot dump ims %s", cfg->coeffsCubeName);
00316 
00317   sinfo_free_table(&qclog_tbl);
00318 
00319   /* =======================================================================
00320      DETERMINES BAD PIXEL MAP
00321      =======================================================================
00322   */
00323   sinfo_msg("Generates bad pixel map");
00324   cknull(mask = sinfo_new_search_bad_pixels (coeffs_imgl,
00325                          cfg->threshSigmaFactor,
00326                          cfg->nonlinearThresh,
00327                          cfg->loReject,
00328                          cfg->hiReject),
00329      "could not create bad pixel mask!") ;
00330 
00331   sinfo_free_imagelist(&coeffs_imgl) ;
00332 
00333   check_nomsg(n_bad = sinfo_new_count_bad_pixels(mask)) ;
00334   sinfo_msg( "No of bad pixels: %d", n_bad ) ;
00335 
00336   /* QC LOG */
00337   cknull_nomsg(qclog_tbl=sinfo_qclog_init());
00338   check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.bp.method"));
00339   snprintf(key_value, MAX_NAME_SIZE-1,"%s",cpl_parameter_get_string(p));
00340   ck0_nomsg(sinfo_qclog_add_string(qclog_tbl,"QC BP-MAP METHOD",key_value,
00341                    "BP search method","%s"));
00342 
00343   ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC BP-MAP NBADPIX",n_bad,
00344                 "No of bad pixels","%d"));
00345 
00346   ck0(sinfo_pro_save_ima(mask,raw,sof,cfg->outName,PRO_BP_MAP_NL,qclog_tbl,
00347              plugin_id,config),
00348       "cannot save ima %s", cfg->outName);
00349 
00350 
00351   /* ======================================================================
00352      FREE MEMORY
00353      ======================================================================
00354   */
00355 
00356 
00357 
00358   sinfo_free_table(&qclog_tbl);
00359   sinfo_free_image(&mask);
00360   sinfo_free_frameset(&raw);
00361   sinfo_detlin_free(&cfg);
00362 
00363   return 0;
00364 
00365  cleanup:
00366   sinfo_free_frameset(&raw_on);
00367   sinfo_free_frameset(&raw_of);
00368 
00369   sinfo_free_image(&mask) ;
00370   sinfo_free_my_vector(&cube_mean);
00371   sinfo_free_imagelist(&imgl);
00372   sinfo_free_imagelist(&coeffs_imgl);
00373   sinfo_free_table(&gain);
00374   sinfo_free_table(&qclog_tbl);
00375   sinfo_free_table(&det_lin);
00376   sinfo_free_my_vector(&vec_adl);
00377   sinfo_free_my_vector(&vec_med);
00378   sinfo_free_polynomial(&pol);
00379   sinfo_detlin_free(&cfg);
00380   sinfo_free_frameset(&raw);
00381 
00382   return -1;
00383 
00384 
00385 }

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