aboutsummaryrefslogtreecommitdiff
path: root/src/libcf/cf_screen_bad_pixels.c
blob: 99884a5b4e24703d608230eca091a2d97d7ad7f8 (plain) (blame)
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
/*****************************************************************************
 *              Johns Hopkins University
 *              Center For Astrophysical Sciences
 *              FUSE
 *****************************************************************************
 *
 * Synopsis:    cf_screen_bad_pixels (fitsfile *header, long nevents,
 *			      float *x, float *y, unsigned char *location)
 *
 * Description: Flag photon events in bad-pixel regions.
 *
 * Arguments:   fitsfile  *header	Input FITS file pointer
 *              long      nevents	Number of photon events
 *              float     *x		X coordinate array
 *              float     *y		Y coordinate array
 *              unsigned char *location Photon location flag array
 *
 * Calls:	cf_get_potholes
 *
 * Returns:	0 on success
 *
 * History:	11/22/05   1.1   wvd    Initial coding
 *
 ****************************************************************************/

#include <stdlib.h>
#include "calfuse.h"

/***********************************************************************
 *
 * Procedure to read pothole locations from the QUAL_CAL file
 *
 ***********************************************************************/

int
cf_get_potholes(fitsfile *header, float **xbad, float **ybad,
        float **rxbad, float **rybad)
{
    char qualfile[FLEN_VALUE];
    int nbad, status=0;
    fitsfile *qualfits;

    cf_verbose(3, "Entering cf_get_potholes.");

    /*
     *  Read limits of bad-pixel regions from the first extension 
     *  of the QUAL_CAL file.
     */
    FITS_movabs_hdu(header, 1, NULL, &status);
    FITS_read_key(header, TSTRING, "QUAL_CAL", qualfile, NULL, &status);
    cf_verbose(3, "QUAL_CAL = %s", qualfile);

    FITS_open_file(&qualfits, cf_cal_file(qualfile), READONLY, &status);
    FITS_movabs_hdu(qualfits, 2, NULL, &status);
    nbad = cf_read_col(qualfits, TFLOAT, "X",  (void **) xbad);
    nbad = cf_read_col(qualfits, TFLOAT, "Y",  (void **) ybad);
    nbad = cf_read_col(qualfits, TFLOAT, "RX", (void **) rxbad);
    nbad = cf_read_col(qualfits, TFLOAT, "RY", (void **) rybad);
    FITS_close_file(qualfits, &status);

    cf_verbose(3, "Exiting cf_get_potholes.");
    return nbad;
}


int
cf_screen_bad_pixels (fitsfile *header, long nevents, float *x, float *y,
	unsigned char *location)
{
    char CF_PRGM_ID[] = "cf_screen_bad_pixels";
    char CF_VER_NUM[] = "1.1";

    int   errflg=0, nbad;
    long  counter=0, i, j;
    float *xbad, *ybad, *rxbad, *rybad, *rx2, *ry2;

    cf_error_init(CF_PRGM_ID, CF_VER_NUM, stderr);
    cf_timestamp(CF_PRGM_ID, CF_VER_NUM, "Begin Processing");
    if ((errflg = cf_proc_check(header, CF_PRGM_ID))) return errflg;

    /*
     *  Read limits of bad-pixel regions from QUAL_CAL file.
     */
    nbad = cf_get_potholes(header, &xbad, &ybad, &rxbad, &rybad);
    cf_verbose(2, "For first pothole: x = %.0f, y = %.0f, rx = %.0f, ry = %.0f",
	xbad[0], ybad[0], rxbad[0], rybad[0]);

    /*
     *  Square the semi-major axes.
     */
    for (j = 0; j < nbad; j++) {
	rxbad[j] *= rxbad[j];
	rybad[j] *= rybad[j];
    }
    rx2 = rxbad;
    ry2 = rybad;
    cf_verbose(2, "For first pothole: rx2 = %.0f, ry2 = %.0f", rx2[0], ry2[0]);

    /*
     *  Flag photons in the bad-pixel regions.
     */
    for (i = 0; i < nevents; i++) {
	for (j = 0; j < nbad; j++) {
            if ((x[i] - xbad[j]) * (x[i] - xbad[j]) / rx2[j] +
		(y[i] - ybad[j]) * (y[i] - ybad[j]) / ry2[j] < 1.0) {
		location[i] = location[i] | LOCATION_BADPX;
		counter++;
		break;
	    }
        }
    }
    cf_verbose(2, "Flagged %d pixels as bad", counter);

    free(xbad);
    free(ybad);
    free(rxbad);
    free(rybad);

    cf_proc_update(header, CF_PRGM_ID, "COMPLETE");
    cf_timestamp(CF_PRGM_ID, CF_VER_NUM, "Done processing");
    return 0;
}