VIRCAM Pipeline  1.3.4
vircam/vircam_destripe.c
1 /* $Id: vircam_destripe.c,v 1.14 2009-12-11 06:54:21 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: 2009-12-11 06:54:21 $
24  * $Revision: 1.14 $
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 #include <cxtypes.h>
36 #include <math.h>
37 
38 #include "vircam_mods.h"
39 #include "vircam_utils.h"
40 #include "vircam_stats.h"
41 #include "vircam_mask.h"
42 #include "vircam_filt.h"
43 
46 /*---------------------------------------------------------------------------*/
83 /*---------------------------------------------------------------------------*/
84 
85 extern int vircam_destripe(vir_fits *in, vir_mask *inbpm, int *status) {
86  float *data,*d,medprofile,profilerms,*wptr,val,rms,lcut,hcut;
87  float skymed,skynoise,*copy;
88  unsigned char *bpm,*b,*rb,*ob;
89  long i,j,nx,ny;
90  cpl_propertylist *plist;
91 
92  /* Inherited status */
93 
94  if (*status != VIR_OK)
95  return(*status);
96 
97  /* Get the data array for the input image and the bad pixel mask */
98 
99  data = cpl_image_get_data(vircam_fits_get_image(in));
100  if (inbpm != NULL)
101  bpm = vircam_mask_get_data(inbpm);
102  else
103  bpm = NULL;
104 
105  /* Get the data array size */
106 
107  nx = (long)cpl_image_get_size_x(vircam_fits_get_image(in));
108  ny = (long)cpl_image_get_size_y(vircam_fits_get_image(in));
109 
110  /* Work out the background sigma */
111 
112  vircam_qmedsig(data,bpm,nx*ny,3.0,3,-65535.0,65535.0,&skymed,&skynoise);
113 
114  /* Get some workspace to hold the 1d profile */
115 
116  wptr = cpl_malloc(ny*sizeof(*wptr));
117  copy = cpl_malloc(ny*sizeof(*copy));
118  rb = cpl_calloc(ny,sizeof(*rb));
119 
120  /* Loop for each row */
121 
122  d = data;
123  b = bpm;
124  for (i = 0; i < ny; i++) {
125 
126  /* Get the median for that row, ignoring any bad pixels. Iterate
127  to clip out any objects or funny pixels */
128 
129  lcut = -1.0e10;
130  hcut = 1.0e10;
131  for (j = 0; j < 3; j++) {
132  vircam_medmadcut(d,b,nx,lcut,hcut,&val,&rms);
133  if (val == CX_MAXFLOAT) {
134  break;
135  } else {
136  lcut = val - 3.0*skynoise;
137  hcut = val + 3.0*skynoise;
138  }
139  }
140  rb[i] = (val == CX_MAXFLOAT);
141  wptr[i] = (rb[i] ? 0.0 : val);
142  copy[i] = wptr[i];
143  d += nx;
144  if (b != NULL)
145  b += nx;
146  }
147 
148  /* Get the median of the profile and normalise the profile
149  to zero median */
150 
151  vircam_medmad(wptr,rb,ny,&medprofile,&profilerms);
152  profilerms *= 1.48;
153  if (profilerms > 5.0) {
154  ob = cpl_calloc(ny,sizeof(*ob));
155  vircam_dostat(copy,rb,ob,ny,25,MEDIANCALC);
156  vircam_dostat(copy,rb,ob,ny,5,MEANCALC);
157  for (i = 0; i < ny; i++) {
158  if (! ob[i])
159  wptr[i] -= copy[i];
160  else
161  wptr[i] = 0.0;
162  }
163  freespace(ob);
164  } else {
165  for (i = 0; i < ny; i++) {
166  if (rb[i]) {
167  wptr[i] = 0.0;
168  } else {
169  wptr[i] -= medprofile;
170  }
171  }
172  }
173  freespace(rb);
174  freespace(copy);
175 
176  /* Now do the correction */
177 
178  d = data;
179  for (i = 0; i < ny; i++) {
180  for (j = 0; j < nx; j++)
181  d[j] -= wptr[i];
182  d += nx;
183  }
184 
185  /* Store the RMS of the profile away */
186 
187  plist = vircam_fits_get_ehu(in);
188  cpl_propertylist_update_bool(plist,"ESO DRS STRIPECOR",TRUE);
189  cpl_propertylist_set_comment(plist,"ESO DRS STRIPECOR",
190  "Stripe correction done");
191  cpl_propertylist_update_float(plist,"ESO DRS STRIPERMS",profilerms);
192  cpl_propertylist_set_comment(plist,"ESO DRS STRIPERMS",
193  "RMS of the removed stripe profile");
194 
195  /* Ditch the workspace and get out of here */
196 
197  freespace(wptr);
198  GOOD_STATUS
199 }
200 
201 
205 /*
206 
207 $Log: not supported by cvs2svn $
208 Revision 1.13 2009/11/17 10:31:05 jim
209 If the rms of the stripe profile is above a threshold value then it does
210 a median/linear smooth to remove any large scale variation
211 
212 Revision 1.12 2009/05/21 10:58:08 jim
213 Little boo-boo in the workspace allocation
214 
215 Revision 1.11 2009/02/03 18:38:40 jim
216 Cut is now done with respect to full image background sigma
217 
218 Revision 1.10 2009/01/28 13:30:36 jim
219 fixed another typo
220 
221 Revision 1.9 2009/01/28 12:27:38 jim
222 Fixed typo
223 
224 Revision 1.8 2009/01/28 12:26:13 jim
225 Correction is done iteratively
226 
227 Revision 1.7 2007/10/25 17:34:00 jim
228 Modified to remove lint warnings
229 
230 Revision 1.6 2007/09/07 10:45:10 jrl
231 Fixed bug which arises if an entire row is flagged bad
232 
233 Revision 1.5 2007/03/29 12:19:39 jim
234 Little changes to improve documentation
235 
236 Revision 1.4 2007/03/01 12:42:41 jim
237 Modified slightly after code checking
238 
239 Revision 1.3 2006/11/27 12:09:35 jim
240 Tidied up some docs. Also modified definition of DRS STRIPECOR so that it's
241 now a boolean
242 
243 Revision 1.2 2006/11/10 10:27:56 jim
244 Added STRIPECOR header parameter
245 
246 Revision 1.1 2006/10/02 13:43:31 jim
247 new file
248 
249 
250 */
void vircam_qmedsig(float *data, unsigned char *bpm, long npts, float thresh, int niter, float lowv, float highv, float *median, float *sigma)
Definition: vircam_stats.c:542
void vircam_medmadcut(float *data, unsigned char *bpm, long np, float lcut, float hcut, float *med, float *mad)
Definition: vircam_stats.c:684
int vircam_destripe(vir_fits *in, vir_mask *inbpm, int *status)
Remove stripes from the background of an image.
unsigned char * vircam_mask_get_data(vir_mask *m)
Definition: vircam_mask.c:535
cpl_image * vircam_fits_get_image(vir_fits *p)
Definition: vircam_fits.c:349
void vircam_medmad(float *data, unsigned char *bpm, long np, float *med, float *mad)
Definition: vircam_stats.c:625
void vircam_dostat(float *data, unsigned char *bpm, unsigned char *goodval, int npts, int nfilt, int whichstat)
Definition: vircam_filt.c:330
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
Definition: vircam_fits.c:457