File: flames_sigma_clip.c

package info (click to toggle)
cpl-plugin-uves 6.1.3+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 23,128 kB
  • sloc: ansic: 171,056; sh: 4,359; python: 3,002; makefile: 1,322
file content (256 lines) | stat: -rw-r--r-- 11,315 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*===========================================================================
  Copyright (C) 2001 European Southern Observatory (ESO)

  This program is free software; you can redistribute it and/or 
  modify it under the terms of the GNU General Public License as 
  published by the Free Software Foundation; either version 2 of 
  the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public 
  License along with this program; if not, write to the Free 
  Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, 
  MA 02139, USA.

  Corresponding concerning ESO-MIDAS should be addressed as follows:
    Internet e-mail: midas@eso.org
    Postal address: European Southern Observatory
            Data Management Division 
            Karl-Schwarzschild-Strasse 2
            D 85748 Garching bei Muenchen 
            GERMANY
===========================================================================*/
/* Program  : sigma_clip.c                                                 */
/* Author   : G. Mulas  -  ITAL_FLAMES Consortium                          */
/* Date     :                                                              */
/*                                                                         */
/* Purpose  : Missing                                                      */
/*                                                                         */
/*                                                                         */
/* Input:  see interface                                                   */ 
/*                                                                      */
/* Output:                                                              */
/*                                                                         */
/* DRS Functions called:                                                   */
/* none                                                                    */ 
/*                                                                         */ 
/* Pseudocode:                                                             */
/* Missing                                                                 */ 
/*                                                                         */ 
/* Version  :                                                              */
/* Last modification date: 2002/08/05                                      */
/* Who     When        Why                Where                            */
/* AMo     02-08-05   Add header         header                            */
/*-------------------------------------------------------------------------*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


/* C functions include files */ 
#include <math.h>
/* MIDAS include files */
#include <flames_midas_def.h>
/* FLAMES-UVES include files */ 
#include <flames_sigma_clip.h>
#include <flames_uves.h>
/**
 @name  sigma_clip() clip outliers

  @param ScienceFrame  input science frame
  @param SingleFF      input slit FF frame
  @param Order         input order table
  @param kappa2        square of kappa to xclipo data points
  @param fibrestosolve fibre to be checked
  @param orderstosolve order to be checked
  @param numslices     number of good slices
  @param j             position
  @param nreject       output number of rejected data points
  @param mask          input mask frame
  @param newmask       output mask frame
  @param backframe     background frame   
  @param xkillsize     half X size of box to search for bad (CRH) pixels 
  @param ykillsize     half Y size of box to search for bad (CRH) pixels 

  @return success or error code

  @doc
      -find the portion of frame to be checked for kappa-sigma-clipping 
       this function will never be called if numslices is not greater than zero
      -we only want to check whether I should reject any pixels that were
       previously considered to be good; if they are marked as bad already, 
       it is pointless to check them: once a pixel has been marked as bad
       it will never be used again in this extraction 

      -recompute the variance for this pixel, using the fitted value 
      -comparing chi squared with the maximum allowed kappa value
       do reject the worst pixel 
 */

flames_err sigma_clip(flames_frame *ScienceFrame, allflats *SingleFF, 
                      orderpos *Order, double kappa2, int32_t *fibrestosolve,
                      int32_t *orderstosolve, int32_t numslices, int32_t j,
                      int32_t *nreject, frame_mask **mask,
                      frame_mask **newmask, frame_data **backframe,
                      int32_t xkillsize, int32_t ykillsize)
{
    int32_t i,m,n,imax=0;
    int32_t ilow, ihigh, ifibre, iorder, iframe, ix, iy;
    int32_t xmin, xmax, ymin, ymax;
    double chi2max=0, chi2=0;
    frame_data total=0;
    frame_data origvalue=0, ffsigma=0, pixelvalue=0, pixeldiff=0;

    singleflat *myflat=0;
    int32_t *lvecbuf1=0;
    int32_t *lvecbuf2=0;
    frame_data *fdvecbuf1=0;
    frame_data *fdvecbuf2=0;
    frame_data *fdvecbuf3=0;
    frame_data *fdvecbuf4=0;
    frame_data *fdvecbuf5=0;
    frame_data *fdvecbuf6=0;
    frame_mask *fmvecbuf1=0;
    frame_mask *fmvecbuf2=0;
    int32_t iorderifibreindex=0;
    int32_t iorderifibrejindex=0;
    int32_t ijindex=0;
    int32_t iyixoffset=0;
    int32_t c_singleff_subcols=SingleFF->subcols;
    int32_t c_singleff_maxfibres=SingleFF->maxfibres;


    /* find the portion of frame to be checked for kappa-sigma-clipping */
    /* this function will never be called if numslices is not greater than zero */
    iorder = orderstosolve[1];
    ifibre = fibrestosolve[1];
    iorderifibreindex = (iorder*c_singleff_maxfibres)+ifibre;
    iorderifibrejindex = (iorderifibreindex*c_singleff_subcols)+j;
    lvecbuf1 = SingleFF->lowfibrebounds[0][0];
    lvecbuf2 = SingleFF->highfibrebounds[0][0];
    fdvecbuf1 = ScienceFrame->spectrum[j][0];
    fdvecbuf2 = backframe[0];
    fdvecbuf3 = ScienceFrame->frame_array[0];
    fdvecbuf4 = ScienceFrame->frame_sigma[0];
    fmvecbuf1 = mask[0];
    fmvecbuf2 = newmask[0];
    ilow = lvecbuf1[iorderifibrejindex];
    ihigh = lvecbuf2[iorderifibrejindex];
    for (m=2; m<=numslices; m++) {
        iorder = orderstosolve[m];
        ifibre = fibrestosolve[m];
        iorderifibreindex = (iorder*c_singleff_maxfibres)+ifibre;
        iorderifibrejindex = (iorderifibreindex*c_singleff_subcols)+j;
        if (ilow>lvecbuf1[iorderifibrejindex])
            ilow = lvecbuf1[iorderifibrejindex];
        if (ihigh<lvecbuf2[iorderifibrejindex])
            ihigh = lvecbuf2[iorderifibrejindex];
    }
    for (i=ilow; i<=ihigh; i++) {
        /* I only want to check whether I should reject any pixels that were
      previously considered to be good; if they are marked as bad already, 
      it is pointless to check them: once a pixel has been marked as bad
      it will never be used again in this extraction */
        ijindex = (i*SingleFF->subcols)+j;
        if (fmvecbuf1[ijindex]==0) {
            total=0;
            ffsigma=0;
            for (n=1; n<=numslices; n++) {
                ifibre = fibrestosolve[n];
                iorder = orderstosolve[n];
                iorderifibreindex = (iorder*c_singleff_maxfibres)+ifibre;
                iorderifibrejindex = (iorderifibreindex*c_singleff_subcols)+j;
                iframe = SingleFF->fibre2frame[ifibre];
                myflat = SingleFF->flatdata+iframe;
                fdvecbuf5 = myflat->data[0];
                fdvecbuf6 = myflat->sigma[0];
                /* are we inside this fibre's boundaries? */
                if (i>=lvecbuf1[iorderifibrejindex] && i<=lvecbuf2[iorderifibrejindex]){
                    pixelvalue = fdvecbuf1[iorderifibreindex];
                    total += pixelvalue*fdvecbuf5[ijindex];
                    ffsigma += pixelvalue*pixelvalue*fdvecbuf6[ijindex];
                }
            }
            /* recompute the variance for this pixel, using the fitted value */
            /* actually, since what we need to use is the variance of the difference
    between the Science frame and the fitted value, include also the 
    propagated variance of the latter, although this is not formally
    part of the variance of the Science Frame. There is only one part
    of the variance which is discarded altogether for simplicity and 
    speed, i.e. the covariance between different single fibre FF frames 
    which is due to the previous multiplication by the slit FF. However, 
    a good argument in favor of dropping this term is that the single 
    fibre FF frames were at an earlier stage _divided_ by the slit FF, 
    then shifted and after that multiplied again by the slit FF; for 
    smallish shifts, these two steps mostly cancel out, giving therefore 
    no contribution to the variance either. Since tracking this 
    contribution would be an incredible mess anyway, and require keeping 
    in memory a lot of intermediate frames which can be safely discarded 
    otherwise, we choose to drop this (arguably small anyway) 
    contribution to the variance.
    Following the above line of reasoning to its very end, it could even 
    be argued that the contribution of the slit FF frame to the variance 
    should be dropped altogether, but to play safe we will keep it at 
    the moment. */
            origvalue = total+fdvecbuf2[ijindex];
            if (origvalue > 0) {
                fdvecbuf4[ijindex] = ffsigma+ScienceFrame->gain*
                                (origvalue+ScienceFrame->gain*ScienceFrame->ron);
            }
            else {
                fdvecbuf4[ijindex] = ffsigma+ScienceFrame->gain*
                                ScienceFrame->gain*ScienceFrame->ron;
            }
            /* compute the difference between the predicted and the actual pixel
    value, then square it and divide it by the pixel variance */
            pixeldiff = fdvecbuf3[ijindex]-total;
            chi2= pixeldiff*pixeldiff/fdvecbuf4[ijindex];
            if (chi2>chi2max) {
                chi2max=chi2;
                imax=i;
            }
        }
    }

    /*SCTDIS("Calculating rejected pixels",0);*/
    /* comparing chi squared with the maximum allowed kappa value */
    *nreject=0;
    if (chi2max > kappa2) {
        /* do reject the worst pixel */
        /* set the x and y windows to be rejected */
        ymin = imax-ykillsize;
        if (ymin<0) ymin=0;
        ymax = imax+ykillsize;
        if (ymax>(ScienceFrame->subrows-1)) ymax=ScienceFrame->subrows-1;
        xmin = j-xkillsize;
        if (xmin<0) xmin=0;
        xmax = j+xkillsize;
        if (xmax>(ScienceFrame->subcols-1)) xmax=ScienceFrame->subcols-1;
        for (iy=ymin; iy<=ymax; iy++) {
            iyixoffset = iy*ScienceFrame->subcols;
            if (fmvecbuf1[iyixoffset+j]==0) (*nreject)++;
            for (ix=xmin; ix<=xmax; ix++) fmvecbuf2[iyixoffset+ix]=5;
        }
    }

    /* SCTDIS("Exiting sigma_clip\n",0);*/
    return 0;

}