sinfo_bp_norm.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_norm.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 sinfo_rec_mflat 
00026 
00027  ---------------------------------------------------------------------------*/
00028 
00029 #ifdef HAVE_CONFIG_H
00030 #  include <config.h>
00031 #endif
00032 /*----------------------------------------------------------------------------
00033                                 Includes
00034  ---------------------------------------------------------------------------*/
00035 #include "sinfo_bp_norm.h"
00036 #include "sinfo_image_ops.h"
00037 #include "sinfo_detlin.h"
00038 #include "sinfo_badnorm_ini_by_cpl.h"
00039 #include "sinfo_baddist_ini_by_cpl.h"
00040 #include "sinfo_pro_save.h"
00041 #include "sinfo_functions.h"
00042 #include "sinfo_pro_types.h"
00043 #include "sinfo_hidden.h"
00044 #include "sinfo_error.h"
00045 #include "sinfo_utils_wrappers.h"
00051 /*----------------------------------------------------------------------------
00052                                 Defines
00053  ---------------------------------------------------------------------------*/
00054 /*----------------------------------------------------------------------------
00055                              Function Definitions
00056  ---------------------------------------------------------------------------*/
00095 int 
00096 sinfo_new_bp_search_normal (const char* plugin_id,
00097                      cpl_parameterlist* config, 
00098                      cpl_frameset* sof, 
00099                      cpl_frameset* ref_set, 
00100                      const char* procatg)
00101 {
00102   bad_config * cfg =NULL;
00103   cpl_imagelist * image_list =NULL;
00104   cpl_image ** med=NULL ;
00105   cpl_image * medImage =NULL;
00106   cpl_image * medIm =NULL;
00107   cpl_image * colImage =NULL;
00108   cpl_image * compImage =NULL;
00109   cpl_image * maskImage =NULL;
00110   cpl_image * threshIm =NULL;
00111 
00112 
00113   Stats * stats =NULL;
00114 
00115   cpl_parameter *p=NULL;             
00116 
00117   int no=0;
00118   float lo_cut=0.;
00119   float hi_cut=0.;
00120 
00121   int i=0;
00122   int n=0;
00123   int half_box_size=0 ;
00124 
00125   cpl_frameset* raw=NULL;
00126   cpl_table* qclog_tbl=NULL;
00127   char key_value[FILE_NAME_SZ];
00128 
00129   /* parse the file names and parameters to the bad_config data structure 
00130        cfg */
00131  
00132   sinfo_check_rec_status(0);
00133   check_nomsg(raw=cpl_frameset_new());
00134   sinfo_check_rec_status(1);
00135   if (strcmp(procatg,PRO_BP_MAP_NO) == 0) {
00136     cknull(cfg = sinfo_parse_cpl_input_badnorm(config,sof,procatg,&raw),
00137        "could not parse cpl input!");
00138   } else if (strcmp(procatg,PRO_BP_MAP_DI) == 0) {
00139     cknull(cfg = sinfo_parse_cpl_input_baddist(config,sof,procatg,&raw),
00140        "could not parse cpl input!");
00141   } else if (strcmp(procatg,PRO_DEFAULT) == 0) {
00142     cknull(cfg = sinfo_parse_cpl_input_badnorm(config,sof,procatg,&raw),
00143        "could not parse cpl input!");
00144   } else {
00145     sinfo_msg_error("Error: PRO.CATG %s, not supported!",procatg);
00146     goto cleanup;
00147   }
00148   sinfo_check_rec_status(2);
00149   /* take a clean mean of the frames */
00150   sinfo_msg("Takes a clean mean of the frames");
00151 
00152   check_nomsg(image_list = cpl_imagelist_new());
00153   sinfo_check_rec_status(3);
00154 
00155   for ( i = 0 ; i < cfg->nframes ; i++ ){
00156     if(sinfo_is_fits_file (cfg->framelist[i]) != 1) {
00157       sinfo_msg_error("Input file %s is not FITS",cfg->framelist[i] );
00158       goto cleanup;
00159     }
00160 
00161     check_nomsg(cpl_imagelist_set(image_list, 
00162                 cpl_image_load(cfg->framelist[i],CPL_TYPE_FLOAT,0,0),i));
00163 
00164   }
00165 
00166   /* finally take the average image of the cube by 
00167      rejecting the extreme values */
00168 
00169   check_nomsg(no=cpl_imagelist_get_size(image_list));
00170   lo_cut=(floor)(cfg->loReject*no+0.5);
00171   hi_cut=(floor)(cfg->hiReject*no+0.5);
00172   cknull(medImage=cpl_imagelist_collapse_minmax_create(image_list,
00173                                                        lo_cut,
00174                                                        hi_cut),
00175      "error in sinfo_average_with_rejection") ;
00176  
00177   /* free memory */
00178   sinfo_free_imagelist(&image_list) ;
00179 
00180   /*----------------------------------------------
00181    * remove the intensity tilt from every column
00182    * and compute the standard deviation on a rectangular zone
00183    */
00184   cknull(medIm = sinfo_new_thresh_image(medImage, cfg->mincut, cfg->maxcut),
00185       "error sinfo_new_thresh_image");
00186   cknull(colImage  = sinfo_new_col_tilt( medIm, cfg->sigmaFactor ),
00187         "sinfo_colTilt failed" ) ;
00188 
00189 
00190   cknull(stats = sinfo_new_image_stats_on_rectangle(colImage, 
00191                             cfg->loReject, 
00192                             cfg->hiReject,
00193                             cfg->llx,
00194                             cfg->lly,
00195                             cfg->urx,
00196                             cfg->ury),
00197      " sinfo_get_image_stats_on_vig failed") ;
00198 
00199   sinfo_msg("Clean stdev: %f\n", stats->cleanstdev ) ;
00200   sinfo_msg("Clean mean: %f\n",  stats->cleanmean ) ;
00201 
00202     
00203 
00204 
00205   /*
00206     cknull_nomsg(qclog_tbl = sinfo_qclog_init());
00207     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC ICTILT STDEV",
00208                                      stats->cleanstdev,
00209                                      "Intensity column clean stdev","%g"));
00210  
00211     ck0_nomsg(sinfo_qclog_add_double(qclog_tbl,"QC ICTILT MEAN",
00212                                      stats->cleanmean,
00213                                      "Intensity column clean mean","%g"));
00214 
00215     ck0(sinfo_pro_save_ima(colImage,raw,sof,
00216                            (char*) BP_NORM_INT_COL_TILT_CORR_OUT_FILENAME,
00217                            PRO_INT_COL_TILT_COR,qclog_tbl,plugin_id,config),
00218                            "cannot save ima %s",
00219                            BP_NORM_INT_COL_TILT_CORR_OUT_FILENAME);
00220 
00221     sinfo_free_table(&qclog_tbl);
00222   */
00223     
00224   /* indicate pixels with great deviations from the clean mean as bad */
00225   if (cfg->threshInd == 1) {
00226     cknull(threshIm = sinfo_new_thresh_image(colImage, 
00227                       stats->cleanmean-cfg->meanfactor*stats->cleanstdev, 
00228                       stats->cleanmean+cfg->meanfactor*stats->cleanstdev),
00229        " sinfo_threshImage failed" ) ;
00230 
00231   }
00232 
00233   if (cfg->threshInd == 0 ) {
00234     threshIm = colImage ;
00235   }
00236   /* AMO here invalid fread? */
00237   med = (cpl_image**) cpl_calloc (cfg -> iterations, sizeof(cpl_image*)) ;
00238     
00239 
00240   /* filter iteratively the images by a sinfo_median filter of the nearest 
00241      neighbors under the condition of a deviation greater than a factor 
00242      times the standard deviation */
00243   sinfo_msg("Apply sinfo_median filter on pixel nearest neighbors");
00244   if (cfg->methodInd == 1) {
00245     if (cfg->factor>0) {
00246       cknull(med[0]=sinfo_new_median_image(threshIm,
00247                                            -cfg->factor*stats->cleanstdev),
00248                                            " sinfo_medianImage failed (1)" ) ;
00249 
00250       for ( i = 0 ; i < cfg->iterations - 1 ; i++ ) {
00251     cknull(med[i+1]=sinfo_new_median_image(med[i], 
00252                                                -cfg->factor*stats->cleanstdev),
00253                                                "sinfo_medianImage failed (2)");
00254       }
00255 
00256     } else {
00257       cknull(med[0] = sinfo_new_median_image(threshIm, -cfg->factor),
00258          " sinfo_medianImage failed (1)" ) ;
00259 
00260       for ( i = 0 ; i < cfg->iterations - 1 ; i++ ) {
00261     cknull(med[i+1] = sinfo_new_median_image(med[i], -cfg->factor),
00262            " sinfo_medianImage failed (%d)",i ) ;
00263       }
00264     }
00265   } else if (cfg->methodInd == 2) {
00266 
00267     cknull(med[0] = sinfo_new_abs_dist_image(threshIm, -cfg->factor),
00268        " sinfo_absDistImage failed (1)" ) ;
00269 
00270     for ( i = 0 ; i < cfg->iterations -1 ; i++ ) {
00271       cknull(med[i+1] = sinfo_new_abs_dist_image(med[i], -cfg->factor),
00272          " sinfo_absDistImage failed (2)" ) ;
00273     }
00274   } else if (cfg->methodInd == 3) {
00275     cknull(med[0] = sinfo_new_mean_image_in_spec(threshIm, 
00276                                            -cfg->factor*stats->cleanstdev),
00277                                        "sinfo_meanImageInSpec failed (1)");
00278     for ( i = 0 ; i < cfg->iterations - 1 ; i++ ) {
00279       cknull(med[i+1] = sinfo_new_mean_image_in_spec(med[i], 
00280                                                -cfg->factor*stats->cleanstdev),
00281                                          " sinfo_meanImageInSpec failed (2)");
00282     }
00283   } else if (cfg->methodInd == 4) {
00284     half_box_size = (cfg->urx - cfg->llx) / 2 ;
00285     cknull(med[0] = sinfo_new_local_median_image(threshIm, 
00286                        -cfg->factor,
00287                        cfg->loReject,
00288                        cfg->hiReject,
00289                        half_box_size),
00290        " sinfo_localMedianImage failed (1)" ) ;
00291 
00292     for ( i = 0 ; i < cfg->iterations - 1 ; i++ ) {
00293       cknull(med[i+1] = sinfo_new_local_median_image(med[i],
00294                            -cfg->factor,
00295                            cfg->loReject,
00296                            cfg->hiReject,
00297                            half_box_size),
00298          " sinfo_localMedianImage failed (2)" ) ;
00299     }
00300   } else {
00301     sinfo_msg_error (" wrong indicator methodInd !" ) ;
00302     goto cleanup ;
00303   }
00304 
00305   /* compare the filtered image with the input image */
00306   cknull(compImage = sinfo_new_compare_images(threshIm, 
00307                                               med[cfg->iterations - 1], 
00308                                               medImage),
00309                                               " sinfo_compareImages failed" ) ;
00310     
00311   /* generate the bad pixel mask */
00312   sinfo_msg("Generates bad pixel map");
00313   cknull(maskImage = sinfo_new_promote_image_to_mask( compImage, &n ),
00314      " error in sinfo_promoteImageToMask" ) ;
00315   sinfo_msg("No of bad pixels:  %d\n", n ) ;
00316 
00317   cknull_nomsg(qclog_tbl = sinfo_qclog_init());
00318   check_nomsg(p = cpl_parameterlist_find(config, "sinfoni.bp.method"));
00319   snprintf(key_value, MAX_NAME_SIZE-1,"%s", cpl_parameter_get_string(p));
00320 
00321   ck0_nomsg(sinfo_qclog_add_string(qclog_tbl,"QC BP-MAP METHOD",
00322                                    key_value,"BP search method","%s"));
00323 
00324   ck0_nomsg(sinfo_qclog_add_int(qclog_tbl,"QC BP-MAP NBADPIX",n,
00325                                            "No of bad pixels","%d"));
00326 
00327     ck0(sinfo_pro_save_ima(maskImage,ref_set,sof,cfg->outName,
00328                procatg,qclog_tbl,plugin_id,config),
00329     "cannot save ima %s", cfg->outName);
00330 
00331   sinfo_free_table(&qclog_tbl);
00332   sinfo_free_image(&maskImage);
00333   sinfo_free_image(&compImage);
00334   if (med != NULL) {
00335     for ( i = 0 ; i < cfg->iterations ; i++ ) {
00336       if(med[i] != NULL) {
00337     cpl_image_delete(med[i]) ;
00338     med[i]=NULL;
00339       }
00340     }
00341     cpl_free(med) ; 
00342     med=NULL;
00343   }
00344   if (stats != NULL) {
00345     cpl_free(stats) ;
00346     stats=NULL;
00347   }
00348   sinfo_free_image(&medIm);
00349   sinfo_free_image(&medImage);
00350   sinfo_free_image(&colImage);
00351   if (cfg->threshInd == 1 ) {
00352     sinfo_free_image(&threshIm);
00353   }
00354   sinfo_badnorm_free(&cfg) ;
00355   sinfo_free_frameset(&raw);
00356   return 0;
00357 
00358  cleanup:
00359 
00360 
00361   if (med != NULL) {
00362     for ( i = 0 ; i < cfg->iterations ; i++ ) {
00363       if(med[i] != NULL) {
00364     cpl_image_delete(med[i]) ;
00365     med[i]=NULL;
00366       }
00367     }
00368     cpl_free(med) ; 
00369          med=NULL;
00370   }
00371   sinfo_free_image(&compImage) ;
00372   sinfo_free_image(&maskImage) ;
00373   sinfo_free_image(&threshIm) ;
00374   sinfo_free_table(&qclog_tbl);
00375   sinfo_free_image(&threshIm) ;
00376   if (stats != NULL) {
00377     cpl_free(stats) ;
00378     stats=NULL;
00379   }
00380   sinfo_free_image(&medIm);
00381   sinfo_free_image(&medImage);
00382   sinfo_free_image(&colImage);
00383   sinfo_free_imagelist(&image_list) ;
00384   sinfo_free_frameset(&raw);
00385   sinfo_badnorm_free(&cfg);
00386 
00387   return -1;
00388 
00389 }
00390 

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