File: flames_ffslitmultiply.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 (342 lines) | stat: -rw-r--r-- 14,288 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*===========================================================================
  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
  ===========================================================================*/



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

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <flames_midas_def.h>
#include <flames_uves.h>
#include <flames_ffslitmultiply.h>
#include <flames_newmatrix.h>


/*-------------------------------------------------------------------------*/
/**
 * @defgroup flames_ffslitmultiply
 *
 */
/*---------------------------------------------------------------------------
  Includes
  --------------------------------------------------------------------------*/
/**
   @name  flames_fastprepfibreff()  
   @short  This function prepare aframe 
   @author G. Mulas  -  ITAL_FLAMES Consortium. Ported to CPL by A. Modigliani


   @param             allslitflats *slitflats, 
   @param             orderpos *ordpos, 
   @param             allflats *inflats, 
   @param             allflats *outflats)

   @return success or failure code, 


   DRS Functions called:          
   none


   Pseudocode:                                                             


   @note
 */

flames_err 
ffslitmultiply(
                allslitflats *slitflats,
                orderpos *ordpos,
                allflats *inflats,
                allflats *outflats)
{
    int32_t **uplimbuf=0;
    int32_t **lowlimbuf=0;
    frame_mask **goodfibbuf=0;
    int32_t *fibresbuf=0;
    frame_mask **badbuffer=0;
    frame_mask *badbufi1=0;
    frame_mask *badbufi2=0;
    frame_data *datai=0;
    frame_data *sigmai=0;
    frame_data slitpixel=0;
    frame_data flatpixel=0;
    int32_t ix=0;
    int32_t iy=0;
    int32_t iorder=0;
    int32_t iframe=0;
    int32_t iframe2=0;
    int32_t lfibre=0;
    int32_t ifibre=0;
    int32_t nfibres=0;
    int32_t starty=0;
    int32_t endy=0;
    int32_t offsetiorder=0;
    int32_t offsetiorderixindex=0;
    int32_t ifibreixindex=0;
    int32_t iyixindex=0;
    int32_t *slitlowbuf=0;
    int32_t *slithighbuf=0;
    char found=0;
    int32_t orderoffset=0;
    int32_t realfirstorder=0;
    int32_t reallastorder=0;
    char output[200];

    frame_data *fdvecbuf1=0;
    frame_data *fdvecbuf2=0;
    frame_data *fdvecbuf3=0;
    frame_data *fdvecbuf5=0;


    frame_mask *fmvecbuf2=0;
    frame_mask *fmvecbuf3=0;
    frame_mask *fmvecbuf5=0;
    int32_t *lvecbuf1=0;
    int32_t *lvecbuf2=0;
    int32_t *lvecbuf3=0;
    int32_t *lvecbuf4=0;

    memset(output, 0, 200);

    orderoffset = slitflats->tab_io_oshift-ordpos->tab_io_oshift;
    realfirstorder = ordpos->firstorder;
    if (orderoffset > 0) {
        sprintf(output, "Warning: the first %d order(s) is/are not covered in \
slit flats and will be dropped in the subsequent reduction.", orderoffset);
        SCTPUT(output);
        strcpy(output, "Consider recreating the slit flats structure using the \
current order definition table");
        SCTPUT(output);
        realfirstorder += orderoffset;
    }
    else if (orderoffset < 0) {
        sprintf(output, "Warning: the first %d order(s) is/are present in the \
slit flats, but were not detected in the current order table.", orderoffset);
        SCTPUT(output);
        strcpy(output, "Consider repeating the order/fibre positioning step to \
detect those lost orders");
        SCTPUT(output);
    }

    reallastorder = slitflats->lastorder+orderoffset;
    if (reallastorder < ordpos->lastorder) {
        sprintf(output, "Warning: the last %d order(s) is/are not covered in \
slit flats and will be dropped in the subsequent reduction.", 
ordpos->lastorder-reallastorder);
        SCTPUT(output);
        strcpy(output, "Consider recreating the slit flats structure using the \
current order definition table");
        SCTPUT(output);
    }
    else if (reallastorder > ordpos->lastorder) {
        sprintf(output, "Warning: the last %d order(s) is/are present in the \
slit flats, but were not detected in the current order table.", 
reallastorder-ordpos->lastorder);
        SCTPUT(output);
        strcpy(output, "Consider repeating the order/fibre positioning step to \
detect those lost orders");
        SCTPUT(output);
        reallastorder = ordpos->lastorder;
    }

    if (inflats->nflats == 0) {
        SCTPUT("Error: no slit flats defined (nflats==0), aborting...\n");
        SCSEPI();
        return flames_midas_fail();
    }

    if (outflats == inflats) {
        badbuffer = fmmatrix(0, outflats->subrows-1, 0, outflats->subcols-1);
    }

    for (iframe=0; iframe<=inflats->nflats-1; iframe++) {

        fdvecbuf2 = inflats->flatdata[iframe].data[0];
        frame_data* fdvecbuf4 = outflats->flatdata[iframe].sigma[0];
        fdvecbuf5 = inflats->flatdata[iframe].sigma[0];
        frame_data* fdvecbuf6 = outflats->flatdata[iframe].data[0];
        frame_mask* fmvecbuf4 = inflats->flatdata[iframe].badpixel[0];

        /* do the frames in inflats one by one */

        if (outflats != inflats) {
            badbuffer = outflats->flatdata[iframe].badpixel;
        }

        nfibres = inflats->flatdata[iframe].numfibres;
        /* printf("nfibres=%d\n",nfibres); */

        /* to begin with, mark all pixels in outframe as bad, good ones will
       be marked as such when they are computed, later in the function */
        frame_mask* fmvecbuf1 = badbuffer[0];
        for (ix=0; ix<=((inflats->subrows*inflats->subcols)-1); ix++) {
            fmvecbuf1[ix] = 1;
        }
        /* is there at least one lit fibre in this frame? */
        if (nfibres>0) {

            /* now proceed with the loop over orders */
            for(iorder=realfirstorder-ordpos->firstorder;
                            iorder<=(reallastorder-ordpos->firstorder);
                            iorder++) {
                offsetiorder = iorder-orderoffset;
                /* now run over the pixels belonging to each order, provided that the
	   slit FF is defined on them, and do the multiplication */
                lowlimbuf = inflats->lowfibrebounds[iorder];
                uplimbuf = inflats->highfibrebounds[iorder];
                goodfibbuf = inflats->goodfibres[iorder];
                slitlowbuf = slitflats->lowbound[offsetiorder];
                slithighbuf = slitflats->highbound[offsetiorder];
                fibresbuf = inflats->flatdata[iframe].fibres;
                fmvecbuf2 = slitflats->goodx[offsetiorder];
                fmvecbuf3 = goodfibbuf[0];
                lvecbuf1 = lowlimbuf[0];
                lvecbuf2 = uplimbuf[0];
                for (ix=0; ix<=(inflats->subcols-1); ix++) {
                    offsetiorderixindex = (offsetiorder*inflats->subcols)+ix;
                    if (fmvecbuf2[ix] == 0) {
                        /* this slice can be multiplied by the slit FF */
                        /* now run a loop over the fibres which are in this frame: they
	       should be non-overlapping by definition and by construction */
                        /* find the interval which we must multiply by the slit FF */
                        /* first look for the first fibre which is good at this ix */
                        for (lfibre=0;
                                        (lfibre<=(nfibres-1))&&
                                                        (fmvecbuf3[ifibreixindex=
                                                                        (((ifibre=fibresbuf[lfibre])*inflats->subcols)
                                                                                        +ix)]==BADSLICE);
                                        lfibre++){
                        };
                        if (lfibre<=(nfibres-1)) {
                            starty = lvecbuf1[ifibreixindex];
                            endy = lvecbuf2[ifibreixindex];
                            /* continue the loop over remaining fibres */
                            for (lfibre++; lfibre<=(nfibres-1); lfibre++) {
                                ifibre = fibresbuf[lfibre];
                                ifibreixindex = (ifibre*inflats->subcols)+ix;
                                if (fmvecbuf3[ifibreixindex] != BADSLICE) {
                                    if ((lvecbuf1[ifibreixindex])<starty)
                                        starty = lvecbuf1[ifibreixindex];
                                    if (lvecbuf2[ifibreixindex]>endy)
                                        endy = lvecbuf2[ifibreixindex];
                                }
                            }
                            if (slitlowbuf[ix]>starty) starty=slitlowbuf[ix];
                            if (slithighbuf[ix]<endy) endy=slithighbuf[ix];
                            /* now the y loop */
                            for (iy=starty; iy<=endy; iy++) {
                                iyixindex = (iy*inflats->subcols)+ix;
                                /* only bother to flatfield if the pixel is good */
                                if(fmvecbuf4[iyixindex] == 0) {
                                    /* now we must decide which of the int32_t slit FF frames must
		     be used for the division: take the first one which 
		     includes the pixel at hand */
                                    found=0;
                                    for (iframe2=0; (iframe2<=slitflats->nflats-1)&&(found==0);
                                                    iframe2++) {
                                        lvecbuf3 = slitflats->slit[iframe2].lowbound[0];
                                        lvecbuf4 = slitflats->slit[iframe2].highbound[0];
                                        fmvecbuf5 = slitflats->slit[iframe2].badpixel[0];
                                        fdvecbuf1 = slitflats->slit[iframe2].data[0];
                                        fdvecbuf3 = slitflats->slit[iframe2].sigma[0];
                                        if((iy>=lvecbuf3[offsetiorderixindex]) &&
                                                        (iy<=lvecbuf4[offsetiorderixindex])) {
                                            /* ok, it is in this frame */
                                            /* only divide if the slit FF is good here */
                                            if (fmvecbuf5[iyixindex] == 0) {
                                                found=1;
                                                /* this pixel is good in the outframe */
                                                fmvecbuf1[iyixindex] = 0;
                                                slitpixel = fdvecbuf1[iyixindex];
                                                flatpixel = fdvecbuf2[iyixindex];
                                                /* set the sigma of the normalised pixel value */
                                                fdvecbuf4[iyixindex] = fdvecbuf5[iyixindex]*
                                                                (slitpixel*slitpixel)+fdvecbuf3[iyixindex]*
                                                                (flatpixel*flatpixel);
                                                /* set the normalised pixel value */
                                                fdvecbuf6[iyixindex] = flatpixel*slitpixel;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else {
                        /* this whole slice has no corresponding slit FF,
	       mark it bad for all fibres */
                        for (ifibre=0; ifibre<inflats->maxfibres; ifibre++)
                            goodfibbuf[ifibre][ix] = BADSLICE;
                    }
                }
            }

            /* although this is strictly unneeded, explicitly zero all frame_array
	 and frame_sigma values for bad pixels (it helps purify checks) */
            for (iy=0; iy<=(outflats->subrows-1); iy++) {
                badbufi1 = outflats->flatdata[iframe].badpixel[iy];
                badbufi2 = badbuffer[iy];
                datai = outflats->flatdata[iframe].data[iy];
                sigmai = outflats->flatdata[iframe].sigma[iy];
                for (ix=0; ix<=(outflats->subcols-1); ix++) {
                    badbufi1[ix] = badbufi2[ix];
                    if (badbufi2[ix] != 0) {
                        datai[ix] = 0;
                        sigmai[ix] = 0;
                    }
                }
            }
            if (outflats == inflats) {
                /* I used a temporary badpixel buffer, copy it to outflats */
                for (iy=0; iy<=(outflats->subrows-1); iy++) {
                    badbufi1 = outflats->flatdata[iframe].badpixel[iy];
                    badbufi2 = badbuffer[iy];
                    for (ix=0; ix<=(outflats->subcols-1); ix++) {
                        badbufi1[ix] = badbufi2[ix];
                    }
                }
            }
        }
    }

    /* free temporary memory allocations */
    if (outflats == inflats) {
        /* I used a temporary badpixel buffer, free it */
        free_fmmatrix(badbuffer, 0, outflats->subrows-1, 0, outflats->subcols-1);
    }

    return(NOERR);

}
/**@}*/