VIRCAM Pipeline  2.3.10
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) 2015 European Southern Observatory
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 <casu_utils.h>
39 #include <casu_mods.h>
40 #include <casu_stats.h>
41 #include "vircam_mods.h"
42 
45 /*---------------------------------------------------------------------------*/
91 /*---------------------------------------------------------------------------*/
92 
93 extern int vircam_genbpm(casu_fits **flatlist, int nflatlist, float lthr,
94  float hthr, cpl_array **bpm_array, int *nbad,
95  float *badfrac, int *status) {
96  cpl_image *master_img,*im;
97  unsigned char *rejmask,*rejplus;
98  cpl_propertylist *drs;
99  int npi,i,status2,*bpm,k,nbmax;
100  float *idata,med,sig,low,high;
101  const char *fctid = "vircam_genbpm";
102 
103  /* Inherited status */
104 
105  *nbad = 0;
106  *badfrac = 0.0;
107  *bpm_array = NULL;
108  if (*status != CASU_OK)
109  return(*status);
110 
111  /* Combine all of the dome flats into a master */
112 
113  status2 = CASU_OK;
114  (void)casu_imcombine(flatlist,NULL,nflatlist,1,3,1,5.0,"EXPTIME",
115  &master_img,NULL,&rejmask,&rejplus,&drs,&status2);
116  freespace(rejmask);
117  freespace(rejplus);
118  freepropertylist(drs);
119  if (status2 != CASU_OK) {
120  cpl_msg_error(fctid,"Flat combination failed");
121  *status = CASU_FATAL;
122  return(*status);
123  }
124 
125  /* Divide the resulting image by its median */
126 
127  idata = cpl_image_get_data_float(master_img);
128  npi = casu_getnpts(master_img);
129  casu_medsig(idata,NULL,npi,&med,&sig);
130  cpl_image_divide_scalar(master_img,(double)med);
131  for (i = 0; i < npi; i++)
132  if (idata[i] == 0.0)
133  idata[i] = 1.0;
134 
135  /* Create and zero the rejection mask */
136 
137  *bpm_array = cpl_array_new((cpl_size)npi,CPL_TYPE_INT);
138  bpm = cpl_array_get_data_int(*bpm_array);
139  for (i = 0; i < npi; i++)
140  bpm[i] = 0;
141 
142  /* Now loop for all input images and divide each by the master */
143 
144  for (i = 0; i < nflatlist; i++) {
145  im = cpl_image_duplicate(casu_fits_get_image(flatlist[i]));
146  cpl_image_divide(im,master_img);
147  idata = cpl_image_get_data_float(im);
148  casu_medmad(idata,NULL,npi,&med,&sig);
149  sig *= 1.48;
150 
151  /* Divide the resulting image by its median */
152 
153  cpl_image_divide_scalar(im,med);
154 
155  /* Get the standard deviation of the image */
156 
157  low = 1.0 - lthr*sig/med;
158  high = 1.0 + hthr*sig/med;
159 
160  /* Now define the bad pixels */
161 
162  for (k = 0; k < npi; k++)
163  if (idata[k] < low || idata[k] > high)
164  bpm[k] += 1;
165  cpl_image_delete(im);
166  }
167  cpl_image_delete(master_img);
168 
169  /* Go through the rejection mask and if a pixel has been marked bad
170  more than a set number of times, then it is defined as bad */
171 
172  nbmax = max(2,nflatlist/4);
173  for (i = 0; i < npi; i++) {
174  if (bpm[i] >= nbmax) {
175  bpm[i] = 1;
176  (*nbad)++;
177  } else
178  bpm[i] = 0;
179  }
180  *badfrac = (float)(*nbad)/(float)npi;
181 
182  /* Get out of here */
183 
184  return(CASU_OK);
185 }
186 
190 /*
191 
192 $Log: not supported by cvs2svn $
193 Revision 1.2 2010/06/03 12:15:31 jim
194 A few mods to get rid of compiler warnings
195 
196 Revision 1.1 2007/11/14 10:43:35 jim
197 Initial version
198 
199 
200 */
cpl_image * casu_fits_get_image(casu_fits *p)
Definition: casu_fits.c:436
int casu_imcombine(casu_fits **fset, casu_fits **fsetv, int nfits, int combtype, int scaletype, int xrej, float thresh, const char *expkey, cpl_image **outimage, cpl_image **outvimage, unsigned char **rejmask, unsigned char **rejplus, cpl_propertylist **drs, int *status)
Stack images into a mean or median image with rejection.
void casu_medmad(float *data, unsigned char *bpm, long np, float *med, float *mad)
Definition: casu_stats.c:347
void casu_medsig(float *data, unsigned char *bpm, long np, float *med, float *sig)
Definition: casu_stats.c:672
long casu_getnpts(cpl_image *in)
Get the number of pixels in a 2d image.
Definition: casu_utils.c:243
int vircam_genbpm(casu_fits **flatlist, int nflatlist, float lthr, float hthr, cpl_array **bpm_array, int *nbad, float *badfrac, int *status)
Generate a bad pixel mask from a list of dome flats.
Definition: vircam_genbpm.c:93