aboutsummaryrefslogtreecommitdiff
path: root/src/cal/geom/make_geom_file.c
blob: 52789e89df77b143add5c7ab5ab204bfbe759b1e (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
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
/*******************************************************************************
 *              Johns Hopkins University
 *              Center For Astrophysical Sciences
 *              FUSE
 *******************************************************************************
 *
 * Synopsis:    make_geom_file
 *
 * Description: Make the geom(Y distortion) file:  copy the distortion
 *		image from a FITS file produced by Rich Robinson, and
 *		write it out in the format required by CALFUSE.
 *
 * Usage:	make_geom_file version
 *
 *		The program will read the files ydist_1a_2d.fit, 
 *		ydist_1b_2d.fit, etc and write the files geom1axxx.fit
 *		(where xxx is the version number specified on the command line).
 *
 *		The output file has an empty primary extension (containing
 *		just header keywords), an empty extension for X distortions
 *		(not presently used as such), and a full-size image containing
 *		the Y distortion as a function of position (FLOAT's).
 *
 * History:     10/19/00        jwk     Begin work.
 ******************************************************************************/

#include <stdio.h>
#include "calfuse.h"
#define EFFMJD 50000.0
#define NXMAX 16384
#define NYMAX 1024

static char CF_PRGM_ID[] = "make_geom_file";
static char CF_VER_NUM[] = "1.1";


int main(int argc, char *argv[])
{
    char *segment[]={"1A","1B","2A","2B"};
    char *segments[]={"1a","1b","2a","2b"};
    char extname[]="GEOMETRIC DISTORTION";
    char infile[80];
    char outfile[80];
    int	version;
    int   i, j, hdutype, status=0, nullval=0, anynull=0;
    int   ix, iy;
    int	ival;
    int   naxis;
    long  naxes[2];
    int   nxpix, nypix;
    float effmjd;
    float bscale, bzero, datamin, datamax;
    float *inbuf;
    fitsfile *geomfits, *infits;

    /* Initialize error checking */
    cf_error_init(CF_PRGM_ID, CF_VER_NUM, stderr);

    if (argc != 2)
	{
	fprintf (stderr, "Usage: make_geom_file version\n");
	exit (-1);
	}

    version = (int) strtol (argv[1], (char **) NULL, 10);
    if ((version <0) || (version > 999))
	{
	fprintf (stderr, "Illegal version value: %s\n", argv[1]);
	exit (-1);
	}


for (i=0; i<4; i++) {  /* 1A,1B,2A,2B */
    /* open the y-distortion file, move to first extension (the one with
       the Y distortion image in it) */
    sprintf(infile, "ydist_%2.2s_2d.fit", segments[i]);
    FITS_open_file (&infits, infile, READONLY, &status);
    FITS_movabs_hdu (infits, 2, &hdutype, &status);
    FITS_read_key (infits, TINT, "NAXIS1", &nxpix, NULL, &status);
    FITS_read_key (infits, TINT, "NAXIS2", &nypix, NULL, &status);
    if (nxpix != NXMAX)
	{
	fprintf (stderr, "NAXIS1 = %d, not %d\n", nxpix, NXMAX);
	exit (-1);
	}

    if (nypix != NYMAX)
	{
	fprintf (stderr, "NAXIS2 = %d, not %d\n", nypix, NYMAX);
	exit (-1);
	}

    /* buffers to hold one row of data at a time */
    inbuf = (float *) calloc (nxpix, sizeof(float));

    /* create the output file */
    naxis = 0;
    naxes[0] = 0;
    naxes[1] = 0;

    sprintf(outfile,"geom%2.2s%03d.fit", segments[i], version);
    printf("%20.20s\n",outfile); 

    FITS_create_file (&geomfits,outfile,&status);
    FITS_create_img (geomfits, SHORT_IMG, naxis, naxes, &status);

    /* populate basic keywords */
    FITS_write_key (geomfits, TSTRING, "CALFTYPE", "GEOM", 
		   "Calibration file type", &status);

    FITS_write_key (geomfits,TINT,"CALFVERS",&version,
		   "Calibration file version", &status);

    FITS_write_key (geomfits,TSTRING,"DETECTOR",segment[i],
                   "detector (1A, 1B, 2A, 2B", &status);

    effmjd=EFFMJD;
    FITS_write_key (geomfits,TFLOAT,"EFFMJD",&effmjd,
		   "Date on which file should be applied (MJD)", &status);

    ival = 1;
    FITS_write_key (geomfits, TINT, "SPECBINX", &ival,
		"Binning in Detector X coordinate for HIST mode", &status);

    FITS_write_key (geomfits, TINT, "SPECBINY", &ival,
		"Binning in Detector Y coordinate for HIST mode", &status);

    FITS_write_date (geomfits, &status);

    FITS_write_key (geomfits,TSTRING,"AUTHOR","RICH ROBINSON",
		   "Author of file", &status);

    /* create empty first extension (HDU 2) for X distortions;
     * not used presently, but is kept for possible later use.
     */
    FITS_create_img (geomfits, SHORT_IMG, naxis, naxes, &status);
    FITS_write_comment (geomfits, "No x correction for geometric distortion",
	&status);

    /* now create extension to hold Y distortions */
    naxis = 2;
    naxes[0] = nxpix;
    naxes[1] = nypix;
    bzero = 0.;
    bscale = 0.01;
    datamin = 0.;
    datamax = 0.;

    /* use SHORT_IMG to force bitpix=16 */
    FITS_create_img (geomfits, SHORT_IMG, naxis, naxes, &status);
    FITS_write_key (geomfits, TFLOAT, "BSCALE", &bscale, "Scale factor",
	&status);
    FITS_write_key (geomfits, TFLOAT, "BZERO", &bzero, "Zero level",
	&status);

    /* now loop over rows of distortion image */
    for (iy=0; iy<nypix; iy++)
	{
	FITS_read_img (infits, TFLOAT, nxpix*iy+1, nxpix, &nullval,
		inbuf, &anynull, &status);
	for (ix=0; ix<nxpix; ix++)
		{
		if (inbuf[ix] < datamin)
			datamin = inbuf[ix];
		if (inbuf[ix] > datamax)
			datamax = inbuf[ix];
		}
	/* cfitsio will do conversion to SHORT using bscale, bzero */
	FITS_write_img (geomfits, TFLOAT, nxpix*iy+1, nxpix, inbuf, &status);
	}

    FITS_write_key (geomfits, TFLOAT, "DATAMIN", &datamin, "Minimum value",
	&status);
    FITS_write_key (geomfits, TFLOAT, "DATAMAX", &datamax, "Maximum value",
	&status);


    /* done with this segment */
    FITS_close_file(geomfits, &status);
    FITS_close_file(infits, &status);

} /* end of loop over segments */

    return 0;
}