bp_noise.c

00001 /*----------------------------------------------------------------------------
00002    
00003    File name    :       bp_noise.c
00004    Author       :   J. Schreiber
00005    Created on   :   May 5, 2003
00006    Description  :   Different methods for searching for bad pixels
00007                         used in the recipe spiffi_bp_noise 
00008 
00009  ---------------------------------------------------------------------------*/
00010 
00011 /*----------------------------------------------------------------------------
00012                                 Includes
00013  ---------------------------------------------------------------------------*/
00014 #include "bp_noise.h"
00015 #include "detnoise_ini_by_cpl.h"
00016 #include "sinfoni_pro_save.h"
00017 #include "sinfoni_functions.h"
00018 
00019 /*----------------------------------------------------------------------------
00020                                 Defines
00021  ---------------------------------------------------------------------------*/
00022 
00023 
00024 /*----------------------------------------------------------------------------
00025                              Function Definitions
00026  ---------------------------------------------------------------------------*/
00027 
00028 /*----------------------------------------------------------------------------
00029    Function     :       badSearchNoise()
00030    In           :       ini_file: file name of according .ini file
00031    Out          :       integer (0 if it worked, -1 if it doesn't) 
00032    Job          :       
00033    this function searches for static bad pixels in stacks of dark frames
00034    taken with NDIT = 1 and equal exposure times
00035    The noise in each pixel is computed and compared to the clean mean noise 
00036    A pixel is declared bad if the deviation exceeds a threshold
00037  ---------------------------------------------------------------------------*/
00038 int badSearchNoise (const char* plugin_id,cpl_parameterlist* config, 
00039                     cpl_frameset* sof, const char* out_name )
00040 {
00041 
00042   const char* _id = "badSearchNoise";
00043 
00044     detnoise_config * cfg =NULL;
00045 
00046     OneImage       ** imagelist ;
00047     OneImage       *  mask=NULL ;
00048     OneCube        *  cubeflat=NULL ;
00049 
00050     int i=0;
00051     int n_bad =0;
00052   
00053     cpl_parameter *p=NULL;
00054      
00055     cpl_image* bp_img=NULL;
00056     cpl_image* bp_imgw=NULL;
00057 
00058     cpl_frameset* raw=NULL;
00059 
00060     cpl_table* qclog_tbl=NULL;
00061 
00062     char* key_value=NULL;
00063 
00064     /*parse the file names and parameters to the detnoise_config data 
00065       structure cfg*/
00066     raw=cpl_frameset_new();
00067     sinfoni_extract_raw_frames_type1(sof,raw,RAW_DARK);
00068     cfg = parse_cpl_input_detnoise(config,sof,&raw) ;
00069 
00070     if ( cfg == NULL )
00071     {
00072         cpl_msg_error(_id," could not parse .ini file!\n") ;
00073         cpl_frameset_delete(raw);
00074         return -1 ;  
00075     }
00076 
00077     imagelist = (OneImage**) cpl_calloc (cfg -> nframes, sizeof(OneImage*)) ;
00078     for ( i = 0 ; i < cfg->nframes ; i++ )
00079     {
00080         if(is_fits_file (cfg->framelist[i]) != 1) {
00081            cpl_msg_error(_id,"Input file %s is not FITS",cfg->framelist[i] );
00082            return -1;
00083         }
00084         imagelist[i] = load_image(cfg->framelist[i]) ;
00085     }
00086 
00087     cubeflat = list_make_cube(imagelist, cfg-> nframes) ;
00088     if ( cubeflat == NULL )
00089     {
00090         cpl_msg_error(_id,"could not make a cube list!\n") ;
00091         cpl_free(imagelist);
00092         detnoise_free(cfg);
00093         cpl_frameset_delete(raw);
00094     return -1 ;
00095     } 
00096     
00097     /*-----------------------------------------------------------------
00098      *---------------------- SEARCH FOR BAD PIXELS---------------------
00099      *-----------------------------------------------------------------*/
00100     cpl_msg_info(_id,"Noise Search for bad pixels");
00101     /*---generate the bad pixel mask-------------*/
00102     mask = searchBadPixelsViaNoise (cubeflat, 
00103                                     cfg->threshSigmaFactor, 
00104                                     cfg->loReject, 
00105                                     cfg->hiReject) ;
00106     if (mask == NULL)
00107     {
00108         cpl_msg_error (_id," could not create bad pixel mask!") ;
00109          destroy_cube (cubeflat) ;
00110          destroy_image(mask) ;
00111          detnoise_free(cfg);
00112          cpl_free(imagelist) ;
00113         return -1 ;
00114     }
00115     
00116 
00117 
00118 
00119     n_bad = countBadPixels(mask) ;
00120     cpl_msg_info (_id,"number of bad pixels: %d\n", n_bad) ;
00121 
00122     /* QC LOG */
00123     qclog_tbl = sinfoni_qclog_init(2);
00124     key_value = cpl_calloc(FILE_NAME_SZ,sizeof(char));
00125  
00126     p = cpl_parameterlist_find(config, "sinfoni.bp.method");
00127     sprintf(key_value, "%s",cpl_parameter_get_string(p));
00128     sinfoni_qclog_add(qclog_tbl,0,"QC BP-MAP METHOD","CPL_TYPE_STRING",
00129               key_value,"BP search method");
00130 
00131     sprintf(key_value,"%d",n_bad);
00132     sinfoni_qclog_add(qclog_tbl,1,"QC BP-MAP NBADPIX","CPL_TYPE_INT",
00133               key_value,"No of bad pixels");
00134 
00135     bp_imgw=cpl_image_wrap_float(mask->lx,mask->ly,mask->data);
00136     bp_img=cpl_image_duplicate(bp_imgw);
00137     cpl_image_unwrap(bp_imgw);
00138 
00139     if(-1 == sinfoni_pro_save_ima(bp_img,raw,sof,(char *) out_name,
00140          PRO_BP_MAP_HP,qclog_tbl,plugin_id,config)) {
00141          cpl_msg_error(_id,"cannot save ima %s", out_name);
00142 
00143          cpl_image_delete(bp_img);
00144          cpl_table_delete(qclog_tbl);
00145          cpl_free(key_value);
00146          destroy_image(mask) ;
00147          destroy_cube (cubeflat) ;
00148          cpl_free(imagelist) ;
00149          detnoise_free(cfg);
00150          cpl_frameset_delete(raw);
00151  
00152          return -1;
00153     }
00154     cpl_image_delete(bp_img);
00155     cpl_table_delete(qclog_tbl);
00156     cpl_free(key_value);
00157     destroy_image(mask) ;
00158     destroy_cube (cubeflat) ;
00159     cpl_free(imagelist) ;
00160     detnoise_free(cfg);
00161     cpl_frameset_delete(raw);
00162     return 0 ;
00163 }
00164 

Generated on Wed Oct 26 13:08:51 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001