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