File: flames_fillnormfactors.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 (241 lines) | stat: -rw-r--r-- 9,051 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
/*===========================================================================
  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_fillnormfactors
 *
 * 
 *
 */

/**
   @name  flames_fillnormfactors()  
   @short  Computes normalization factors and their sigmas
   @author G. Mulas  -  ITAL_FLAMES Consortium. Ported to CPL by A. Modigliani


   @param allflatsin 
   @param shiftdata 
   @param badifibre 
   @param iorder 
   @param iframe 
   @param ifibre 
   @param ix 
   @param ixindex 
   @param normdata

   @return success or failure code, 


   DRS Functions called:          
   none


   Pseudocode:                                                             


   @note
   @doc this structure contains the temporary data used to compute 
   normalisation 
   factors and their sigmas, for bad pixel filling in the fibre FF frames;
   one issue of this structure will be used for each offset to be used for 
   filling, hence an array of these structures will be necessary 

 */


#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_newmatrix.h>
#include <flames_shiftcommon.h>
#include <flames_fillholes.h>
#include <flames_fillnormfactors.h>


flames_err 
fillnormfactors(
                allflats *allflatsin, 
                shiftstruct *shiftdata, 
                badifibrestruct *badifibre, 
                int32_t iorder, 
                int32_t iframe, 
                int32_t ifibre, 
                int32_t ix, 
                int32_t ixindex, 
                normstruct* normdata)
{
    int32_t i=0, j=0;
    int32_t iy=0;
    tnormfactorstruct *tnormdata=0;
    tnormfactorstruct *tnormdatai=0;

    shiftstruct *shiftdataix=0;
    badifibrestruct *mybadifibre=0;
    badixstruct *mybadixs=0;
    int32_t *lvecbuf1=0;
    int32_t *lvecbuf2=0;
    frame_data *fdvecbuf1=0;
    frame_data *fdvecbuf2=0;
    frame_mask *fmvecbuf1=0;
    int32_t iorderifibreindex=0;
    int32_t iorderifibreixindex=0;
    int32_t iorderifibreshiftixindex=0;
    int32_t iyixindex=0;
    int32_t shiftiy=0;
    int32_t shiftix=0;
    int32_t shiftiyshiftixindex=0;

    shiftdataix = shiftdata+ix;
    lvecbuf1 = allflatsin->lowfibrebounds[0][0];
    lvecbuf2 = allflatsin->highfibrebounds[0][0];
    mybadifibre = badifibre+ifibre;
    mybadixs = mybadifibre->badixs+ixindex;
    fdvecbuf1 = allflatsin->flatdata[iframe].data[0];
    fdvecbuf2 = allflatsin->flatdata[iframe].sigma[0];
    fmvecbuf1 = allflatsin->flatdata[iframe].badpixel[0];

    tnormdata = (tnormfactorstruct *) calloc((size_t)(shiftdataix->numoffsets),
                    sizeof(tnormfactorstruct));

    iorderifibreindex = (iorder*allflatsin->maxfibres)+ifibre;
    iorderifibreixindex = (iorderifibreindex*allflatsin->subcols)+ix;

    /* initialise to zero numerators and denominators */
    for (i=0; i<=shiftdataix->numoffsets-1;i++) {
        tnormdatai = tnormdata+i;
        tnormdatai->numerator = 0;
        tnormdatai->numsigma = 0;
        tnormdatai->denominator = 0;
        tnormdatai->densigma = 0;
        tnormdatai->yshift = calloc((size_t) 2, sizeof(int32_t));
        tnormdatai->yshift[0] = (int32_t) floor(shiftdataix->yfracoffsets[i])
                      - shiftdataix->yintoffsets[i];
        tnormdatai->yshift[1] = (int32_t) ceil(shiftdataix->yfracoffsets[i])
                      - shiftdataix->yintoffsets[i];
        tnormdatai->yshiftnum =
                        (tnormdata[i].yshift[1] - tnormdata[i].yshift[0])>DEPSILON? 1 : 0;
        tnormdatai->yshiftfrac = calloc((size_t) 2, sizeof(double));
        tnormdatai->yshiftfrac[0] = 1-fabs(shiftdataix->yfracoffsets[i]-
                        floor(shiftdataix->yfracoffsets[i]));
        tnormdatai->yshiftfrac[1] = 1-fabs(shiftdataix->yfracoffsets[i]-
                        ceil(shiftdataix->yfracoffsets[i]));
        tnormdatai->overlapfrac = 0;
    }
    mybadixs->badiycount = 0;
    if (lvecbuf2[iorderifibreixindex] >= lvecbuf1[iorderifibreixindex]) {
        mybadixs->badiy =
                        (int32_t *) calloc((size_t)(lvecbuf2[iorderifibreixindex]-
                                        lvecbuf1[iorderifibreixindex]+1),
                                        sizeof(int32_t));
    }
    for (iy=lvecbuf1[iorderifibreixindex];
                    iy<=lvecbuf2[iorderifibreixindex];
                    iy++) {
        iyixindex = (iy*allflatsin->subcols)+ix;
        /* is this pixel good? */
        if (fmvecbuf1[iyixindex] == 0) {
            /* yes, it is good, try to add its contribution to numerator,
	 denominator, errors */
            for (i=0; i<=shiftdataix->numoffsets-1;i++) {
                tnormdatai = tnormdata+i;
                shiftix = shiftdataix->ixoffsets[i];
                iorderifibreshiftixindex =
                                (iorderifibreindex*allflatsin->subcols)+shiftix;
                for (j=0;j<=tnormdatai->yshiftnum;j++) {
                    /* is the shifted iy within fibre boundaries? */
                    shiftiy = iy+tnormdatai->yshift[j];
                    if ((shiftiy >= lvecbuf1[iorderifibreshiftixindex])
                                    && (shiftiy <= lvecbuf2[iorderifibreshiftixindex])) {
                        /* yes, within boundaries, but good shifted pixel? */
                        shiftiyshiftixindex = (shiftiy*allflatsin->subcols)+shiftix;
                        if (fmvecbuf1[shiftiyshiftixindex] == 0) {
                            /* yes, good pixel, do add it to normalisation factors */
                            tnormdatai->overlapfrac += tnormdatai->yshiftfrac[j];
                            tnormdatai->numerator += fdvecbuf1[iyixindex]*
                                            tnormdatai->yshiftfrac[j];
                            tnormdatai->numsigma += fdvecbuf2[iyixindex]*
                                            tnormdatai->yshiftfrac[j];
                            tnormdatai->denominator += fdvecbuf1[shiftiyshiftixindex]*
                                            tnormdatai->yshiftfrac[j];
                            tnormdatai->densigma += fdvecbuf2[shiftiyshiftixindex]*
                                            tnormdatai->yshiftfrac[j];
                        }
                    }
                }
            }
        }
        else {
            /* this pixel is not good, add it to the list of pixels to be
	 corrected */
            mybadixs->badiy[mybadixs->badiycount]=iy;
            mybadixs->badiycount++;
        }
    }
    /* ok, numerators, denominators and errors computed, calculate normalisation
     factors */
    for (i=0; i<=shiftdataix->numoffsets-1;i++){
        tnormdatai = tnormdata+i;
        normstruct* normdatai = normdata+i;
        if (((tnormdatai->overlapfrac*allflatsin->substepy)/
                        (2*allflatsin->halfibrewidth) >=
                        (allflatsin->minfibrefrac)) &&
                        (tnormdatai->denominator > DEPSILON) &&
                        (tnormdatai->numerator > DEPSILON)) {
            /* enough overlap between these two ixs */
            normdatai->normfactor = tnormdatai->numerator/tnormdatai->denominator;
            normdatai->normsigma = (tnormdatai->numsigma/
                            (tnormdatai->numerator*tnormdatai->numerator)+
                            tnormdatai->densigma/
                            (tnormdatai->denominator*
                                            tnormdatai->denominator))*
                                                            normdatai->normfactor;
            normdatai->goodoverlap = 0; /* use this as a marker for good overlap */
        }
        else {
            /* not enough overlap */
            normdatai->goodoverlap = 1;
        }
        free(tnormdatai->yshift);
        free(tnormdatai->yshiftfrac);
    }


    free(tnormdata);
    return(NOERR);

}
/**@}*/