VIRCAM Pipeline  1.3.3
vircam_genbpm.c
1 /* $Id: vircam_genbpm.c,v 1.3 2012-01-15 17:40:09 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jim $
23  * $Date: 2012-01-15 17:40:09 $
24  * $Revision: 1.3 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <cpl.h>
35 
36 #include <math.h>
37 
38 #include "vircam_mods.h"
39 #include "vircam_utils.h"
40 #include "vircam_stats.h"
41 
44 /*---------------------------------------------------------------------------*/
90 /*---------------------------------------------------------------------------*/
91 
92 extern int vircam_genbpm(vir_fits **flatlist, int nflatlist, float lthr,
93  float hthr, cpl_array **bpm_array, int *nbad,
94  float *badfrac, int *status) {
95  cpl_image *master_img,*im;
96  unsigned char *rejmask,*rejplus;
97  cpl_propertylist *drs;
98  int npi,i,status2,*bpm,k,nbmax;
99  float *idata,med,sig,low,high;
100  const char *fctid = "vircam_genbpm";
101 
102  /* Inherited status */
103 
104  *nbad = 0;
105  *badfrac = 0.0;
106  *bpm_array = NULL;
107  if (*status != VIR_OK)
108  return(*status);
109 
110  /* Combine all of the dome flats into a master */
111 
112  status2 = VIR_OK;
113  (void)vircam_imcombine(flatlist,nflatlist,1,3,1,5.0,&master_img,&rejmask,
114  &rejplus,&drs,&status2);
115  freespace(rejmask);
116  freespace(rejplus);
117  freepropertylist(drs);
118  if (status2 != VIR_OK) {
119  cpl_msg_error(fctid,"Flat combination failed");
120  *status = VIR_FATAL;
121  return(*status);
122  }
123 
124  /* Divide the resulting image by its median */
125 
126  idata = cpl_image_get_data_float(master_img);
127  npi = vircam_getnpts(master_img);
128  vircam_medsig(idata,NULL,npi,&med,&sig);
129  cpl_image_divide_scalar(master_img,(double)med);
130  for (i = 0; i < npi; i++)
131  if (idata[i] == 0.0)
132  idata[i] = 1.0;
133 
134  /* Create and zero the rejection mask */
135 
136  *bpm_array = cpl_array_new((cpl_size)npi,CPL_TYPE_INT);
137  bpm = cpl_array_get_data_int(*bpm_array);
138  for (i = 0; i < npi; i++)
139  bpm[i] = 0;
140 
141  /* Now loop for all input images and divide each by the master */
142 
143  for (i = 0; i < nflatlist; i++) {
144  im = cpl_image_duplicate(vircam_fits_get_image(flatlist[i]));
145  cpl_image_divide(im,master_img);
146  idata = cpl_image_get_data_float(im);
147  vircam_medmad(idata,NULL,npi,&med,&sig);
148  sig *= 1.48;
149 
150  /* Divide the resulting image by its median */
151 
152  cpl_image_divide_scalar(im,med);
153 
154  /* Get the standard deviation of the image */
155 
156  low = 1.0 - lthr*sig/med;
157  high = 1.0 + hthr*sig/med;
158 
159  /* Now define the bad pixels */
160 
161  for (k = 0; k < npi; k++)
162  if (idata[k] < low || idata[k] > high)
163  bpm[k] += 1;
164  cpl_image_delete(im);
165  }
166  cpl_image_delete(master_img);
167 
168  /* Go through the rejection mask and if a pixel has been marked bad
169  more than a set number of times, then it is defined as bad */
170 
171  nbmax = max(2,nflatlist/4);
172  for (i = 0; i < npi; i++) {
173  if (bpm[i] >= nbmax) {
174  bpm[i] = 1;
175  (*nbad)++;
176  } else
177  bpm[i] = 0;
178  }
179  *badfrac = (float)(*nbad)/(float)npi;
180 
181  /* Get out of here */
182 
183  return(VIR_OK);
184 }
185 
189 /*
190 
191 $Log: not supported by cvs2svn $
192 Revision 1.2 2010/06/03 12:15:31 jim
193 A few mods to get rid of compiler warnings
194 
195 Revision 1.1 2007/11/14 10:43:35 jim
196 Initial version
197 
198 
199 */