vircam_genbpm.c

00001 /* $Id: vircam_genbpm.c,v 1.1 2007/11/14 10:43:35 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jim $
00023  * $Date: 2007/11/14 10:43:35 $
00024  * $Revision: 1.1 $
00025  * $Name:  $
00026  */
00027 
00028 /* Includes */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <cpl.h>
00035 
00036 #include <math.h>
00037 
00038 #include "vircam_mods.h"
00039 #include "vircam_utils.h"
00040 #include "vircam_stats.h"
00041 
00044 /*---------------------------------------------------------------------------*/
00090 /*---------------------------------------------------------------------------*/
00091 
00092 extern int vircam_genbpm(vir_fits **flatlist, int nflatlist, float lthr,
00093                          float hthr, cpl_array **bpm_array, int *nbad,
00094                          float *badfrac, int *status) {
00095     cpl_image *master_img,*im;
00096     unsigned char *rejmask,*rejplus;
00097     cpl_propertylist *drs;
00098     int npi,i,status2,*bpm,k,nbmax;
00099     float *idata,med,sig,low,high;
00100     char *fctid = "vircam_genbpm";
00101 
00102     /* Inherited status */
00103 
00104     *nbad = 0;
00105     *badfrac = 0.0;
00106     *bpm_array = NULL;
00107     if (*status != VIR_OK)
00108         return(*status);
00109 
00110     /* Combine all of the dome flats into a master */
00111 
00112     status2 = VIR_OK;
00113     (void)vircam_imcombine(flatlist,nflatlist,1,3,1,5.0,&master_img,&rejmask,
00114                            &rejplus,&drs,&status2);
00115     freespace(rejmask);
00116     freespace(rejplus);
00117     freepropertylist(drs);
00118     if (status2 != VIR_OK) {
00119         cpl_msg_error(fctid,"Flat combination failed");
00120         *status = VIR_FATAL;
00121         return(*status);
00122     }
00123 
00124     /* Divide the resulting image by its median */
00125 
00126     idata = cpl_image_get_data_float(master_img);
00127     npi = vircam_getnpts(master_img);
00128     vircam_medsig(idata,NULL,npi,&med,&sig);
00129     cpl_image_divide_scalar(master_img,(double)med);
00130     for (i = 0; i < npi; i++)
00131         if (idata[i] == 0.0)
00132             idata[i] = 1.0;
00133 
00134     /* Create and zero the rejection mask */
00135 
00136     *bpm_array = cpl_array_new(npi,CPL_TYPE_INT);
00137     bpm = cpl_array_get_data_int(*bpm_array);
00138     for (i = 0; i < npi; i++)
00139         bpm[i] = 0;
00140 
00141     /* Now loop for all input images and divide each by the master */
00142 
00143     for (i = 0; i < nflatlist; i++) {
00144         im = cpl_image_duplicate(vircam_fits_get_image(flatlist[i]));
00145         cpl_image_divide(im,master_img);
00146         idata = cpl_image_get_data_float(im);
00147         vircam_medmad(idata,NULL,npi,&med,&sig);
00148         sig *= 1.48;
00149 
00150         /* Divide the resulting image by its median */
00151 
00152         cpl_image_divide_scalar(im,med);
00153 
00154         /* Get the standard deviation of the image */
00155 
00156         low = 1.0 - lthr*sig/med;
00157         high = 1.0 + hthr*sig/med;
00158 
00159         /* Now define the bad pixels */
00160 
00161         for (k = 0; k < npi; k++)
00162             if (idata[k] < low || idata[k] > high)
00163                 bpm[k] += 1;
00164         cpl_image_delete(im);
00165     }
00166     cpl_image_delete(master_img);
00167 
00168     /* Go through the rejection mask and if a pixel has been marked bad 
00169        more than a set number of times, then it is defined as bad */
00170 
00171     nbmax = max(2,nflatlist/4);
00172     for (i = 0; i < npi; i++) {
00173         if (bpm[i] >= nbmax) {
00174             bpm[i] = 1;
00175             (*nbad)++;
00176         } else
00177             bpm[i] = 0;
00178     }
00179      *badfrac = (float)(*nbad)/(float)npi;
00180 
00181     /* Get out of here */
00182 
00183     return(VIR_OK);
00184 }
00185 
00189 /*
00190 
00191 $Log: vircam_genbpm.c,v $
00192 Revision 1.1  2007/11/14 10:43:35  jim
00193 Initial version
00194 
00195 
00196 */

Generated on Wed Apr 10 04:01:55 2013 for VIRCAM Pipeline by  doxygen 1.5.1