vircam_destripe.c

00001 /* $Id: vircam_destripe.c,v 1.14 2009/12/11 06:54:21 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jim $
00023  * $Date: 2009/12/11 06:54:21 $
00024  * $Revision: 1.14 $
00025  * $Name: vcam-1_3_2 $
00026  */
00027 
00028 /* Includes */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <cpl.h>
00035 #include <cxtypes.h>
00036 #include <math.h>
00037 
00038 #include "vircam_mods.h"
00039 #include "vircam_utils.h"
00040 #include "vircam_stats.h"
00041 #include "vircam_mask.h"
00042 #include "vircam_filt.h"
00043 
00046 /*---------------------------------------------------------------------------*/
00083 /*---------------------------------------------------------------------------*/
00084 
00085 extern int vircam_destripe(vir_fits *in, vir_mask *inbpm, int *status) {
00086     float *data,*d,medprofile,profilerms,*wptr,val,rms,lcut,hcut;
00087     float skymed,skynoise,*copy;
00088     unsigned char *bpm,*b,*rb,*ob;
00089     long i,j,nx,ny;
00090     cpl_propertylist *plist;
00091 
00092     /* Inherited status */
00093 
00094     if (*status != VIR_OK)
00095         return(*status);
00096 
00097     /* Get the data array for the input image and the bad pixel mask */
00098 
00099     data = cpl_image_get_data(vircam_fits_get_image(in));
00100     if (inbpm != NULL) 
00101         bpm = vircam_mask_get_data(inbpm);
00102     else
00103         bpm = NULL;
00104 
00105     /* Get the data array size */
00106 
00107     nx = (long)cpl_image_get_size_x(vircam_fits_get_image(in));
00108     ny = (long)cpl_image_get_size_y(vircam_fits_get_image(in));
00109 
00110     /* Work out the background sigma */
00111 
00112     vircam_qmedsig(data,bpm,nx*ny,3.0,3,-65535.0,65535.0,&skymed,&skynoise);
00113 
00114     /* Get some workspace to hold the 1d profile */
00115 
00116     wptr = cpl_malloc(ny*sizeof(*wptr));
00117     copy = cpl_malloc(ny*sizeof(*copy));
00118     rb = cpl_calloc(ny,sizeof(*rb));
00119 
00120     /* Loop for each row */
00121 
00122     d = data;
00123     b = bpm;
00124     for (i = 0; i < ny; i++) {
00125 
00126         /* Get the median for that row, ignoring any bad pixels. Iterate
00127            to clip out any objects or funny pixels */
00128 
00129         lcut = -1.0e10;
00130         hcut = 1.0e10;
00131         for (j = 0; j < 3; j++) {
00132             vircam_medmadcut(d,b,nx,lcut,hcut,&val,&rms);
00133             if (val == CX_MAXFLOAT) {
00134                 break;
00135             } else {
00136                 lcut = val - 3.0*skynoise;
00137                 hcut = val + 3.0*skynoise;
00138             }
00139         }
00140         rb[i] = (val == CX_MAXFLOAT);
00141         wptr[i] = (rb[i] ? 0.0 : val);
00142         copy[i] = wptr[i];
00143         d += nx;
00144         if (b != NULL)
00145             b += nx;
00146     }
00147 
00148     /* Get the median of the profile and normalise the profile 
00149        to zero median */
00150 
00151     vircam_medmad(wptr,rb,ny,&medprofile,&profilerms);
00152     profilerms *= 1.48;
00153     if (profilerms > 5.0) {
00154         ob = cpl_calloc(ny,sizeof(*ob));
00155         vircam_dostat(copy,rb,ob,ny,25,MEDIANCALC);
00156         vircam_dostat(copy,rb,ob,ny,5,MEANCALC);
00157         for (i = 0; i < ny; i++) {
00158             if (! ob[i]) 
00159                 wptr[i] -= copy[i];
00160             else
00161                 wptr[i] = 0.0;
00162         }
00163         freespace(ob);
00164     } else {
00165         for (i = 0; i < ny; i++) {
00166             if (rb[i]) {
00167                 wptr[i] = 0.0;
00168             } else {
00169                 wptr[i] -= medprofile;
00170             }
00171         }
00172     }
00173     freespace(rb);
00174     freespace(copy);
00175 
00176     /* Now do the correction */
00177 
00178     d = data;
00179     for (i = 0; i < ny; i++) {
00180         for (j = 0; j < nx; j++) 
00181             d[j] -= wptr[i];
00182         d += nx;
00183     }
00184 
00185     /* Store the RMS of the profile away */
00186 
00187     plist = vircam_fits_get_ehu(in);
00188     cpl_propertylist_update_bool(plist,"ESO DRS STRIPECOR",TRUE);
00189     cpl_propertylist_set_comment(plist,"ESO DRS STRIPECOR",
00190                                  "Stripe correction done");
00191     cpl_propertylist_update_float(plist,"ESO DRS STRIPERMS",profilerms);
00192     cpl_propertylist_set_comment(plist,"ESO DRS STRIPERMS",
00193                                  "RMS of the removed stripe profile");
00194 
00195     /* Ditch the workspace and get out of here */
00196 
00197     freespace(wptr);
00198     GOOD_STATUS
00199 }
00200         
00201 
00205 /*
00206 
00207 $Log: vircam_destripe.c,v $
00208 Revision 1.14  2009/12/11 06:54:21  jim
00209 Fixed memory allocation bug
00210 
00211 Revision 1.13  2009/11/17 10:31:05  jim
00212 If the rms of the stripe profile is above a threshold value then it does
00213 a median/linear smooth to remove any large scale variation
00214 
00215 Revision 1.12  2009/05/21 10:58:08  jim
00216 Little boo-boo in the workspace allocation
00217 
00218 Revision 1.11  2009/02/03 18:38:40  jim
00219 Cut is now done with respect to full image background sigma
00220 
00221 Revision 1.10  2009/01/28 13:30:36  jim
00222 fixed another typo
00223 
00224 Revision 1.9  2009/01/28 12:27:38  jim
00225 Fixed typo
00226 
00227 Revision 1.8  2009/01/28 12:26:13  jim
00228 Correction is done iteratively
00229 
00230 Revision 1.7  2007/10/25 17:34:00  jim
00231 Modified to remove lint warnings
00232 
00233 Revision 1.6  2007/09/07 10:45:10  jrl
00234 Fixed bug which arises if an entire row is flagged bad
00235 
00236 Revision 1.5  2007/03/29 12:19:39  jim
00237 Little changes to improve documentation
00238 
00239 Revision 1.4  2007/03/01 12:42:41  jim
00240 Modified slightly after code checking
00241 
00242 Revision 1.3  2006/11/27 12:09:35  jim
00243 Tidied up some docs. Also modified definition of DRS STRIPECOR so that it's
00244 now a boolean
00245 
00246 Revision 1.2  2006/11/10 10:27:56  jim
00247 Added STRIPECOR header parameter
00248 
00249 Revision 1.1  2006/10/02 13:43:31  jim
00250 new file
00251 
00252 
00253 */

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1