File: flames_mergebadpixels.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 (223 lines) | stat: -rw-r--r-- 9,286 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
/*===========================================================================
  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  : mergebadpixels.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


#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_mergebadpixels.h>


flames_err mergebadpixels(flames_frame *framebuffer, char *badpxfname)
{
    int status=0;
    int badid=0;
    int actvals=0;
    int actsize=0;
    int unit=0;
    int null=0;
    int tnaxis=0;
    double *tstart=0;
    double *tstep=0;
    int *tnpix=0;
    int ibuf[5]={0,0,0,0,0};
    char messagebuffer[160];
    int32_t ix=0;
    frame_mask **newbadpixel=0;

    frame_mask *fmvecbuf1=0;
    frame_mask *fmvecbuf2=0;
    int32_t totpixuplimit=0;

    memset(messagebuffer, '\0', 160);

    /* allocate the local vectors */
    tstart = dvector(0,1);
    tstep = dvector(0,1);
    tnpix = ivector(0,1);
    newbadpixel = fmmatrix(0,framebuffer->subrows-1, 0, framebuffer->subcols-1);

    fmvecbuf1 = newbadpixel[0];
    fmvecbuf2 = framebuffer->badpixel[0];
    totpixuplimit = (framebuffer->subrows*framebuffer->subcols)-1;

    /* is this a good filename? */
    if ((status = SCFINF(badpxfname, 3, ibuf)) != 0) {
        /* I could not open the file for information */
        sprintf(messagebuffer, "File %s could not be opened",
                        badpxfname);
        SCTPUT(messagebuffer);
    }
    else {
        /* yes, the file exists; is it a data frame? */
        if (ibuf[0] != F_IMA_TYPE) {
            /* wrong file type */
            sprintf(messagebuffer, "File %s is not of the type required for a bad \
pixel mask",
badpxfname);
            SCTPUT(messagebuffer);
        }
        else {
            /* yes, it is a data frame; open it */
            if ((status = SCFOPN(badpxfname, D_I1_FORMAT, 0, F_IMA_TYPE,
                            &badid)) != 0) {
                /* I could not open the file: protest... */
                sprintf(messagebuffer, "File %s could not be opened",
                                badpxfname);
                SCTPUT(messagebuffer);
            }
            /* read naxis */
            if ((status=SCDRDI(badid, "NAXIS", 1, 1, &actvals, &tnaxis,
                            &unit, &null)) != 0) {
                /* something went wrong in SCDRDI */
                sprintf(messagebuffer, "Could not read the NAXIS descriptor in \
file %s", 
badpxfname);
                SCTPUT(messagebuffer);
            }
            /* does naxis match the data frame? */
            if (tnaxis != 2) {
                /* naxis does not match */
                sprintf(messagebuffer, "The dimensions of file %s do not match those \
of the data frames", 
badpxfname);
                SCTPUT(messagebuffer);
            }
            else {
                /* naxis matches, proceed to read start, step and npix */
                if ((status=SCDRDD(badid, "START", 1, 2, &actvals, tstart,
                                &unit, &null)) != 0) {
                    /* something went wrong in SCDRDD */
                    sprintf(messagebuffer, "Could not read the START descriptor in \
file %s", 
badpxfname);
                    SCTPUT(messagebuffer);
                }
                if ((status=SCDRDD(badid, "STEP", 1, 2, &actvals, tstep,
                                &unit, &null)) != 0) {
                    /* something went wrong in SCDRDD */
                    sprintf(messagebuffer, "Could not read the STEP descriptor in \
file %s", 
badpxfname);
                    SCTPUT(messagebuffer);
                }
                if ((status=SCDRDI(badid, "NPIX", 1, 2, &actvals, tnpix,
                                &unit, &null)) != 0) {
                    /* something went wrong in SCDRDI */
                    sprintf(messagebuffer, "Could not read the NPIX descriptor in \
file %s", 
badpxfname);
                    SCTPUT(messagebuffer);
                }
                /* do start, step and npix match the data frames? */
                if (fabs(tstart[0]-framebuffer->substartx) >= DEPSILON ||
                                fabs(tstart[1]-framebuffer->substarty) >= DEPSILON ||
                                fabs(tstep[0]-framebuffer->substepx) >= DEPSILON ||
                                fabs(tstep[1]-framebuffer->substepy) >= DEPSILON ||
                                tnpix[0] != framebuffer->subcols ||
                                tnpix[1] != framebuffer->subrows) {
                    /* the dimensions do not match */
                    sprintf(messagebuffer, "The dimensions of file %s do not match \
those of the data frames", 
badpxfname);
                    SCTPUT(messagebuffer);
                }
                else {
                    /* read in the badpixel mask */
                    if ((status = SCFGET(badid, 1,
                                    framebuffer->subrows*framebuffer->subcols,
                                    &actsize, (char *)newbadpixel[0]))
                                    != 0) {
                        /* something went wrong in SCFGET */
                        sprintf(messagebuffer, "Could not read the file %s as a bad \
pixel mask", 
badpxfname);
                        SCTPUT(messagebuffer);
                    }
                    /* did I get all the elements I asked for? */
                    if(actsize != framebuffer->subrows*framebuffer->subcols) {
                        /* some elements were lost: protest... */
                        sprintf(messagebuffer, "Could not completely read file %s as a \
bad pixel mask", 
badpxfname);
                        SCTPUT(messagebuffer);
                    }
                    else {
                        /* merge the new bad pixel mask in the data structure */
                        for(ix=0; ix<=totpixuplimit; ix++) {
                            if(fmvecbuf1[ix] != 0) {
                                /* mark as bad pixel */
                                fmvecbuf2[ix] = 1;
                            }
                        }
                    }
                }
            }
        }
    }

    /* free local temporarily allocated variables */
    free_dvector(tstart, 0, 1);
    free_dvector(tstep, 0, 1);
    free_ivector(tnpix, 0, 1);
    free_fmmatrix(newbadpixel, 0, framebuffer->subrows-1,
                  0, framebuffer->subcols-1);

    return(NOERR);

}