VIRCAM Pipeline  2.3.10
casu_genbpm.c
1 /* $Id: casu_genbpm.c,v 1.2 2015/08/07 13:06:54 jim Exp $
2  *
3  * This file is part of the CASU Pipeline utilities
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: 2015/08/07 13:06:54 $
24  * $Revision: 1.2 $
25  * $Name: $
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_mods.h"
39 #include "catalogue/casu_utils.h"
40 #include "casu_stats.h"
41 
44 /*---------------------------------------------------------------------------*/
95 /*---------------------------------------------------------------------------*/
96 
97 extern int casu_genbpm(casu_fits **flatlist, int nflatlist, cpl_image *master,
98  float lthr, float hthr, const char *expkey,
99  cpl_array **bpm_array, int *nbad, float *badfrac,
100  int *status) {
101  cpl_image *master_img,*im;
102  unsigned char *rejmask,*rejplus;
103  cpl_propertylist *drs;
104  int npi,i,status2,*bpm,k,nbmax;
105  float *idata,med,sig,low,high;
106  const char *fctid = "casu_genbpm";
107 
108  /* Inherited status */
109 
110  *nbad = 0;
111  *badfrac = 0.0;
112  *bpm_array = NULL;
113  if (*status != CASU_OK)
114  return(*status);
115 
116  /* Combine all of the flats into a master if one isn't already provided */
117 
118  if (master == NULL) {
119  status2 = CASU_OK;
120  (void)casu_imcombine(flatlist,NULL,nflatlist,1,3,1,5.0,expkey,
121  &master_img,NULL,&rejmask,&rejplus,&drs,&status2);
122  freespace(rejmask);
123  freespace(rejplus);
124  freepropertylist(drs);
125  if (status2 != CASU_OK) {
126  cpl_msg_error(fctid,"Flat combination failed");
127  *status = CASU_FATAL;
128  return(*status);
129  }
130  } else {
131  master_img = cpl_image_duplicate(master);
132  }
133 
134  /* Divide the resulting image by its median */
135 
136  idata = cpl_image_get_data_float(master_img);
137  npi = casu_getnpts(master_img);
138  casu_medsig(idata,NULL,npi,&med,&sig);
139  cpl_image_divide_scalar(master_img,(double)med);
140  for (i = 0; i < npi; i++)
141  if (idata[i] == 0.0)
142  idata[i] = 1.0;
143 
144  /* Create and zero the rejection mask */
145 
146  bpm = cpl_calloc(npi,sizeof(int));
147 
148  /* Now loop for all input images and divide each by the master */
149 
150  for (i = 0; i < nflatlist; i++) {
151  im = cpl_image_duplicate(casu_fits_get_image(flatlist[i]));
152  cpl_image_divide(im,master_img);
153  idata = cpl_image_get_data_float(im);
154  casu_medmad(idata,NULL,npi,&med,&sig);
155  sig *= 1.48;
156 
157  /* Divide the resulting image by its median */
158 
159  cpl_image_divide_scalar(im,med);
160 
161  /* Get the standard deviation of the image */
162 
163  low = 1.0 - lthr*sig/med;
164  high = 1.0 + hthr*sig/med;
165 
166  /* Now define the bad pixels */
167 
168  for (k = 0; k < npi; k++)
169  if (idata[k] < low || idata[k] > high)
170  bpm[k] += 1;
171  cpl_image_delete(im);
172  }
173  cpl_image_delete(master_img);
174 
175  /* Go through the rejection mask and if a pixel has been marked bad
176  more than a set number of times, then it is defined as bad */
177 
178  nbmax = max(2,nflatlist/4);
179  for (i = 0; i < npi; i++) {
180  if (bpm[i] >= nbmax) {
181  bpm[i] = 1;
182  (*nbad)++;
183  } else
184  bpm[i] = 0;
185  }
186  *badfrac = (float)(*nbad)/(float)npi;
187  *bpm_array = cpl_array_wrap_int(bpm,npi);
188 
189  /* Get out of here */
190 
191  return(CASU_OK);
192 }
193 
196 /*
197 
198 $Log: casu_genbpm.c,v $
199 Revision 1.2 2015/08/07 13:06:54 jim
200 Fixed copyright to ESO
201 
202 Revision 1.1.1.1 2015/06/12 10:44:32 jim
203 Initial import
204 
205 Revision 1.5 2015/01/29 11:48:15 jim
206 modified comments
207 
208 Revision 1.4 2015/01/09 12:13:15 jim
209 *** empty log message ***
210 
211 Revision 1.3 2014/12/11 12:23:33 jim
212 new version
213 
214 Revision 1.2 2013/11/21 09:38:13 jim
215 detabbed
216 
217 Revision 1.1.1.1 2013-08-27 12:07:48 jim
218 Imported
219 
220 
221 */
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.
int casu_genbpm(casu_fits **flatlist, int nflatlist, cpl_image *master, float lthr, float hthr, const char *expkey, cpl_array **bpm_array, int *nbad, float *badfrac, int *status)
Generate a bad pixel mask from a list of dome flats.
Definition: casu_genbpm.c:97
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