File: flames_Optimal.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 (314 lines) | stat: -rw-r--r-- 11,906 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*===========================================================================
 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
 ===========================================================================*/

/*--------------------------------------------------------------------------*/
/**
 * @defgroup flames_optimal   Substep: Flames Optimal Extraction
 *
 */
/*--------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
 Includes
 ---------------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/* MIDAS include files */
#include <flames_midas_def.h>
/* FLAMES-UVES include files */
#include <flames_uves.h>
#include <flames_optimal.h>
#include <flames_Opt_Extract.h>
#include <flames_def_drs_par.h>
#include <flames_newmatrix.h>
#include <flames_opterrors.h>
#include <flames_sigma_clip.h>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**@{*/

/*---------------------------------------------------------------------------
 Implementation
 ---------------------------------------------------------------------------*/

/**
 @name  flames_Optimal()
 @author G. Mulas  -  ITAL_FLAMES Consortium. Ported to CPL by A. Modigliani

 @param ScienceFrame input science frame to be extracted
 @param SingleFF     input all flat field frame base structure
 @param Order        input order traces structure
 @param ordsta       input order start
 @param ordend       input order end
 @param kappa2       square of kappa to be used in kappa-sigma clip
 @param mask         input mask
 @param newmask      output mask
 @param backframe    inter order background frame
 @param normcover    array to hold fibre normalization factors
 @param orderoffset  offset between Slit_FF order tab and fibre-order tabs;
 @param miniters     minimum number of optimal extraction iterations
 @param maxiters     maximum number of optimal extraction iterations
 @param xkillsize    When performing sigma-clipping in the optimal extraction, how many
 other adjacent pixels in the x and/or y direction(s) should be
 discarded along with the one exceeding the threshold? A cosmic or
 cosmetic problem is likely to affect a spot larger than 1 pixel
 @param ykillsize    see above

 Purpose:
 DRS function called:
 Pseudocode:

 @doc       
 -allocates data structures to hold results
 -performs actual optimal extraction of each order-fibre (calling Opt_Extract)
 -if finds some bad slice (numslices>0) flag it
 -if bad slices where flagged updates bad pixel map: bad pixels are flagged with code 4
 -else for a good slice, compute the final pixel values (calling again Opt_Extract) and errors (calling opterrors)

 @note
 */

flames_err optimal(flames_frame *ScienceFrame, allflats *SingleFF,
                   orderpos *Order, int32_t ordsta, int32_t ordend, double kappa2,
                   frame_mask **mask, frame_mask **newmask, frame_data **backframe,
                   frame_data **normcover, int32_t orderoffset, int32_t miniters,
                   int32_t maxiters, int32_t xkillsize, int32_t ykillsize) {

    int32_t nreject = 0, nrejecttot = 0;
    int32_t i = 0, j = 0;
    int32_t ijindex = 0, ijuplimit = 0;
    int32_t *fibrestosolve = 0;
    int32_t *orderstosolve = 0;
    int32_t numslices = 0;
    int32_t numorders = 0, iters = 0, jrejects = 0, arraysize = 0;

    char output[100];
    double **aa = 0, **xx = 0;

    frame_mask *fmvecbuf1 = 0;
    frame_mask *fmvecbuf2 = 0;
    frame_mask *fmvecbuf3 = 0;

    int32_t numordersframe = 0;
    int32_t firstno = 0;
    int32_t lastno = 0;
    int32_t firstnoifibre = 0;
    int32_t lastnoifibre = 0;
    int32_t noifibre = 0;
    int32_t iuplimit = 0;
    int32_t juplimit = 0;

    int32_t ksubcols;

    static frame_mask *repeatflags = 0;
    static frame_mask *badslices = 0;
    static int old_juplimit = -1;

    const char *drs_verbosity = DRS_VERBOSITY;

    fmvecbuf1 = newmask[0];
    fmvecbuf2 = mask[0];
    numorders = 1 + ordend - ordsta;
    numordersframe = ScienceFrame->lastorder - ScienceFrame->firstorder + 1;
    firstno = ordsta - Order->firstorder;
    lastno = ordend - Order->firstorder;
    firstnoifibre = firstno * ScienceFrame->maxfibres;
    lastnoifibre = ((lastno + 1) * ScienceFrame->maxfibres) - 1;
    iuplimit = ScienceFrame->subrows - 1;
    arraysize = numorders * ScienceFrame->maxfibres;
    fibrestosolve = lvector(1, arraysize);
    orderstosolve = lvector(1, arraysize);
    /* allocate aa and xx once and for all, as large as they may be
   possibly needed */
    aa = dmatrix(1, arraysize, 1, arraysize);
    xx = dmatrix(1, arraysize, 1, 1);

    ksubcols = ScienceFrame->subcols;
    juplimit = ksubcols - 1;

    if (juplimit > old_juplimit) {
        if (old_juplimit > 0) {
            free_fmvector(repeatflags, 0, old_juplimit);
            free_fmvector(badslices, 0, old_juplimit);
        }
        repeatflags = fmvector(0, juplimit);
        badslices = fmvector(0, juplimit);
        old_juplimit = juplimit;
    }

    /* if miniters is >1, then we must repeat the extraction at first,
   therefore ...*/
    if (miniters > 1)
        i = 1; /* ... initialise the repeatflags to 1, to begin with */
    else
        i = 0; /* initialise the repeatflags to 0, no iteration by default */

    for (j = 0; j <= juplimit; j++) {
        repeatflags[j] = i;
        badslices[j] = 0; /* initialise the badslices mask */
    }

    /* reset the newmask */
    ijuplimit = (ScienceFrame->subrows * ksubcols) - 1;
    for (ijindex = 0; ijindex <= ijuplimit; ijindex++)
        fmvecbuf1[ijindex] = 0;

    nrejecttot = 1;
    for (iters = 1; (iters <= miniters) || (iters <= maxiters && nrejecttot > 0);
                    iters++) {
        nrejecttot = 0;
        for (j = 0; j <= juplimit; j++) {
            /* should we iterate on this j? */
            if (repeatflags[j] != 0) {
                /* yes, do iterate */
                if (Opt_Extract(ScienceFrame, SingleFF, Order, ordsta, ordend, j, mask,
                                aa, xx, arraysize, fibrestosolve, orderstosolve, &numslices,
                                normcover, orderoffset))
                    return 1;
                if (numslices > 0) {
                    /* Let's do the sigma clipping */
                    if (sigma_clip(ScienceFrame, SingleFF, Order, kappa2, fibrestosolve,
                                    orderstosolve, numslices, j, &nreject, mask, newmask, backframe,
                                    xkillsize, ykillsize))
                        return 2;
                    nrejecttot += nreject;
                } else {
                    badslices[j] = 1;
                    repeatflags[j] = 0;
                    fmvecbuf3 = ScienceFrame->specmask[j][0];
                    for (noifibre = firstnoifibre; noifibre <= lastnoifibre; noifibre++)
                        fmvecbuf3[noifibre] = 0;
                }
            }
        }
        /* if we must not iterate by default on all pixels, reset repeatflags */
        if (iters >= miniters)
            for (j = 0; j <= juplimit; j++)
                repeatflags[j] = 0;

        /* did I reject any pixels with sigma-clipping? */
        if (nrejecttot > 0) {
            /* yes, I did; update the mask from newmask */
            nrejecttot = 0;
            for (j = 0; j <= juplimit; j++) {
                if (badslices[j] == 0) {
                    jrejects = 0;
                    ijindex = j;
                    for (i = 0; i <= iuplimit; i++) {
                        if (fmvecbuf1[ijindex] != 0) {
                            fmvecbuf1[ijindex] = 0;
                            if (fmvecbuf2[ijindex] == 0) {
                                /* this pixel is being newly marked bad now*/
                                jrejects++;
                                fmvecbuf2[ijindex] = 4;
                            }
                        }
                        ijindex += ksubcols;
                    }
                    if (jrejects > 0) {
                        nrejecttot += jrejects;
                        /* mark this x for another iteration */
                        repeatflags[j] = 1;
                    }
                }
            }
            if (strcmp(drs_verbosity, "LOW") == 0) {
            } else {
                sprintf(output, "%d pixels sigma-clipped in iteration %d", nrejecttot,
                                iters);
                SCTPUT(output);
            }
        } else if (iters >= miniters) {
            if (strcmp(drs_verbosity, "LOW") == 0) {
            } else {
                sprintf(output, "Convergence reached in all slices after %d iterations",
                                iters);
                SCTPUT(output);
            }
        }
    } /* end of `iters' loop */

    /* here compute the errors */
    if (strcmp(drs_verbosity, "LOW") == 0) {
    } else {
        SCTPUT("Detailed errors computation...");
    }
    for (j = 0; j <= (ksubcols - 1); j++) {
        /* was this a bad slice? */
        if (badslices[j] != 0) {
            if (strcmp(drs_verbosity, "LOW") == 0) {
            } else {
                sprintf(output, "bad slice at %d-th column", j);
                SCTPUT(output);
            }
        }

        /* did the extraction converge here? */
        else if (repeatflags[j] != 0) {
            /* no, it did not */

            if (strcmp(drs_verbosity, "LOW") == 0) {
            } else {
                sprintf(output, "no convergence after %d iterations at %d-th column",
                                iters - 1, j);
                SCTPUT(output);
            }
            fmvecbuf3 = ScienceFrame->specmask[j][0];
            for (noifibre = firstnoifibre; noifibre <= lastnoifibre; noifibre++)
                fmvecbuf3[noifibre] = 0;
        } else {
            /* this is a good slice, compute the final pixel values and errors */
            if (Opt_Extract(ScienceFrame, SingleFF, Order, ordsta, ordend, j, mask,
                            aa, xx, arraysize, fibrestosolve, orderstosolve, &numslices,
                            normcover, orderoffset))
                return 1;

            /* here put the heavy part of the final error estimate, which we do
       once and for all only when we finished extracting the slice,
       instead of repeating it over and over in the loop */
            if (numslices > 0)
                if (opterrors(ScienceFrame, SingleFF, Order, j, mask, aa, xx,
                                fibrestosolve, orderstosolve, numslices, arraysize) != NOERR
                )
                    return 3;
        }
    }

    free_lvector(fibrestosolve, 1, arraysize);
    free_lvector(orderstosolve, 1, arraysize);
    free_dmatrix(aa, 1, arraysize, 1, arraysize);
    free_dmatrix(xx, 1, arraysize, 1, 1);

    return 0;

}
/**@}*/