File: flames_quickoptimal.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 (303 lines) | stat: -rw-r--r-- 11,631 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
/*===========================================================================
  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  : Optimal.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                            */
/*-------------------------------------------------------------------------*/
/* C functions include files */ 
/* 020716    KB */
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

flames_err quickoptimal(flames_frame *ScienceFrame, allflats *SingleFF, 
                        orderpos *Order, int32_t ordsta, int32_t ordend,
                        double kappa2, frame_mask **mask,
                        frame_mask **newmask, frame_data **backframe,
                        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;


    int actvals=0;
    char drs_verbosity[10];
    int status=0;
    memset(drs_verbosity, 0, 10);
    if ((status=SCKGETC(DRS_VERBOSITY, 1, 3, &actvals, drs_verbosity))
                    != 0) {
        /* the keyword seems undefined, protest... */
        return(MAREMMA);
    }


    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 (quickoptextract(ScienceFrame, SingleFF, Order, ordsta, ordend, j,
                                mask, aa, xx, arraysize, fibrestosolve,
                                orderstosolve, &numslices))
                    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 (quickoptextract(ScienceFrame, SingleFF, Order, ordsta, ordend, j,
                            mask, aa, xx, arraysize, fibrestosolve,
                            orderstosolve, &numslices))
                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;

}