aboutsummaryrefslogtreecommitdiff
path: root/src/libcf/cf_astig_farf.c
blob: aabcdaecb632444cc9bd077e5e68a4e2fffa8011 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*****************************************************************************
 *              Johns Hopkins University
 *              Center For Astrophysical Sciences
 *              FUSE
 *****************************************************************************
 *
 * Description: Correct XFARF array for instrumental astigmatism.
 *
 * Returns:    0 upon successful completion.
 *
 * History:       12/12/02   1.1   jch  Begin work
 *                02/24/03   1.2   peb  Using calfusettag.h include file
 *                03/04/03   1.3   peb  Removed math.h - unnecessary
 *                03/11/03   1.4   wvd  Changed channel to type *char
 *                04/04/03   1.5   wvd  Test for overflow of astig array.
 *                05/20/03   1.6   rdr  Add call to cf_proc_check
 *                09/16/03   1.7   wvd  Remove calls to astig_target_aperture
 *					and astig_read_file.  Change sign of
 *					astigmatism correction to match
 *					the new astg**009.fit files.
 *					Make yoff a float.
 *                11/05/03   1.8   wvd  Change channel to unsigned char.
 *                04/07/07   1.9   wvd  Clean up compiler warnings.
 *
 ****************************************************************************/

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

static char CF_PRGM_ID[] = "cf_astig_farf";
static char CF_VER_NUM[] = "1.9";


/* 
 * Add astigmatism correction to XFARF for each photon within target
 * aperture.  Note that spectrum centroid moves with time.
 */
static int
farf_astig_shifts(fitsfile *header, long nevents, float *xfarf, 
	float *yfarf, unsigned char *channel, float *photon_time, 
	long nseconds, float *timeline_time, float *ycentl, float *ycents)
{
    char        astig_file[FLEN_VALUE];
    int   	status=0;
    int   	active_ap[2], ap, ast_cen, extno, overflow=0;
    long	i, j, k;
    long  	npix, nx, ny, ii, jj, kk;
    float 	yoff;
    float       *astig=NULL, *centroid=NULL;
    fitsfile    *astigfits;

    /* Read target apertures from the file header */
    (void) cf_source_aper (header, active_ap);

    /* Open the astigmatism file */
    FITS_read_key(header, TSTRING, "ASTG_CAL", astig_file, NULL, &status);
    FITS_open_file(&astigfits, cf_cal_file(astig_file), READONLY, &status);

    /*   Since we presently have no astig correction for extended
     *   sources, we apply correction only to target aperture.
     */
    for (i = 0; i < 2; i++) {

        ap = active_ap[i];
	if (i == 0) 
	    centroid = ycentl;
	else
	    centroid = ycents;

        /* Read astigmatism correction for this aperture */
        extno = ap+1;
        cf_verbose(3, "Reading extension %d of %s", extno, astig_file);
        FITS_movabs_hdu(astigfits, extno, NULL, &status);
        FITS_read_key(astigfits, TINT, "SLIT_CEN", &ast_cen, NULL, &status);
        FITS_read_key(astigfits, TLONG, "NAXIS1", &nx, NULL, &status);
        FITS_read_key(astigfits, TLONG, "NAXIS2", &ny, NULL, &status);

        npix = nx * ny;
        astig = (float *) cf_malloc(sizeof(float) * npix);
        FITS_read_img(astigfits, TFLOAT, 1L, npix, 0, astig, NULL, &status);

	/* Go through the photon list, find the astigmatism (delta(X)) 
         * corresponding to the (xfarf, yfarf) position and channel,
	 * and add it to xfarf[k].
	 */
	k = 0;
        for (j = 0; j < nevents; j++) {
            if (channel[j] == ap) {
                while ((photon_time[j] > timeline_time[k+1] - FRAME_TOLERANCE)
		    && (k < nseconds-1)) {
                    k++;
                }

        	/* Determine offset between spectrum and astig correction file. */
        	yoff = centroid[k] - ast_cen;

		/* Find the astigmatism correction appropriate 
		   to the photon's (xfarf, yfarf) position. */
                if ((ii = (xfarf[j] + 0.5)) >= nx) {overflow = 1; continue;};
                if ((jj = (yfarf[j] - yoff + 0.5)) >= ny) {overflow = 1; continue;}
                if ((kk = jj * nx + ii) >= npix) {overflow = 1; continue;}
                xfarf[j] += astig[kk];
            }
        }

        /* If overflow flag is set, there is a problem with the ASTG_CAL file. */
	if (overflow)
    	    cf_if_warning("Overflow of ASTG_CAL file in aperture %d", ap);

        /* Space for astig array is allocated in each loop. */
        free(astig);
    }

    FITS_close_file(astigfits, &status);
    return status;
}

int cf_astig_farf(fitsfile *header, long nevents, float *xfarf, float *yfarf, 
		  unsigned char *channel, float *photon_time, long nseconds, 
		  float *timeline_time, float *ycentl, float *ycents)
{
    int 	errflg=0, status=0;

    /* Enter a timestamp into the log. */
    cf_timestamp(CF_PRGM_ID, CF_VER_NUM, "Begin Processing");
    cf_error_init(CF_PRGM_ID, CF_VER_NUM, stderr);

    if ((errflg = cf_proc_check(header, CF_PRGM_ID))) return errflg;

    /* Check that astigmatism correction is appropriate for input image */
    if (astig_check_input_image(header) == 1) {

        /* Read astigmatism corrections for each aperture, add to XFARF */
        farf_astig_shifts(header, nevents, xfarf, yfarf, channel,
		photon_time, nseconds, timeline_time, ycentl, ycents);
    }

    /* Update processing flags. */
    cf_proc_update(header, CF_PRGM_ID, "COMPLETE");
    cf_timestamp(CF_PRGM_ID, CF_VER_NUM, "Done Processing");
    return (status);
}