summaryrefslogtreecommitdiffstats
path: root/tksao/wcssubs/distort.c
blob: d903dfe48f269bd562fcb45b9145efe6bd630d0f (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*** File libwcs/distort.c
 *** January 4, 2007
 *** By Jessica Mink, jmink@cfa.harvard.edu, 
 *** Based on code written by Jing Li, IPAC
 *** Harvard-Smithsonian Center for Astrophysics
 *** Copyright (C) 2004-2007
 *** Smithsonian Astrophysical Observatory, Cambridge, MA, USA

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Correspondence concerning WCSTools should be addressed as follows:
           Internet email: jmink@cfa.harvard.edu
           Postal address: Jessica Mink
                           Smithsonian Astrophysical Observatory
                           60 Garden St.
                           Cambridge, MA 02138 USA

 * Module:	distort.c (World Coordinate Systems)
 * Purpose:	Convert focal plane coordinates to pixels and vice versa:
 * Subroutine:  distortinit (wcs, hstring) set distortion coefficients from FITS header
 * Subroutine:  DelDistort (header, verbose) delete distortion coefficients in FITS header
 * Subroutine:	pix2foc (wcs, x, y, u, v) pixel coordinates -> focal plane coordinates
 * Subroutine:	foc2pix (wcs, u, v, x, y) focal plane coordinates -> pixel coordinates
 * Subroutine:  setdistcode (wcs,ctype) sets distortion code from CTYPEi
 * Subroutine:  getdistcode (wcs) returns distortion code string for CTYPEi
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wcs.h"

void
distortinit (wcs, hstring)
struct WorldCoor *wcs;  /* World coordinate system structure */
const char *hstring;	/* character string containing FITS header information
			   in the format <keyword>= <value> [/ <comment>] */
{
    int i, j, m;
    char keyword[12];

    /* Read distortion coefficients, if present */
    if (wcs->distcode == DISTORT_SIRTF) {
	if (wcs->wcsproj == WCS_OLD) {
	    wcs->wcsproj = WCS_NEW;
	    wcs->distort.a_order = 0;
	    wcs->distort.b_order = 0;
	    wcs->distort.ap_order = 0;
	    wcs->distort.bp_order = 0;
	    }
	else {
	    if (!hgeti4 (hstring, "A_ORDER", &wcs->distort.a_order)) {
		setwcserr ("DISTINIT: Missing A_ORDER keyword for Spitzer distortion");
		}
	    else {
		m = wcs->distort.a_order;
		for (i = 0; i <= m; i++) {
		    for (j = 0; j <= m; j++) {
			wcs->distort.a[i][j] = 0.0;
			}
		    }
		for (i = 0; i <= m; i++) {
		    for (j = 0; j <= m-i; j++) {
			sprintf (keyword, "A_%d_%d", i, j);
			hgetr8 (hstring, keyword, &wcs->distort.a[i][j]);
			}
		    }
		}
	    if (!hgeti4 (hstring, "B_ORDER", &wcs->distort.b_order)) {
		setwcserr ("DISTINIT: Missing B_ORDER keyword for Spitzer distortion");
		}
	    else {
		m = wcs->distort.b_order;
		for (i = 0; i <= m; i++) {
		    for (j = 0; j <= m; j++) {
			wcs->distort.b[i][j] = 0.0;
			}
		    }
		for (i = 0; i <= m; i++) {
		    for (j = 0; j <= m-i; j++) {
			sprintf (keyword, "B_%d_%d", i, j);
			hgetr8 (hstring, keyword, &wcs->distort.b[i][j]);
			}
		    }
		}
	    if (!hgeti4 (hstring, "AP_ORDER", &wcs->distort.ap_order)) {
		setwcserr ("DISTINIT: Missing AP_ORDER keyword for Spitzer distortion");
		}
	    else {
		m = wcs->distort.ap_order;
		for (i = 0; i <= m; i++) {
		    for (j = 0; j <= m; j++) {
			wcs->distort.ap[i][j] = 0.0;
			}
		    }
		for (i = 0; i <= m; i++) {
		    for (j = 0; j <= m-i; j++) {
			sprintf (keyword, "AP_%d_%d", i, j);
			hgetr8 (hstring, keyword, &wcs->distort.ap[i][j]);
			}
		    }
		}
	    if (!hgeti4 (hstring, "BP_ORDER", &wcs->distort.bp_order)) {
		setwcserr ("DISTINIT: Missing BP_ORDER keyword for Spitzer distortion");
		}
	    else {
		m = wcs->distort.bp_order;
		for (i = 0; i <= m; i++) {
		    for (j = 0; j <= m; j++) {
			wcs->distort.bp[i][j] = 0.0;
			}
		    }
		for (i = 0; i <= m; i++) {
		    for (j = 0; j <= m-i; j++) {
			sprintf (keyword, "BP_%d_%d", i, j);
			hgetr8 (hstring, keyword, &wcs->distort.bp[i][j]);
			}
		    }
		}
	    }
	}
    return;
}


/* Delete all distortion-related fields.
 * return 0 if at least one such field is found, else -1.  */

int
DelDistort (header, verbose)

char *header;
int verbose;

{
    char keyword[16];
    char str[32];
    int i, j, m;
    int lctype;
    int n;

    n = 0;

    if (hgeti4 (header, "A_ORDER", &m)) {
	for (i = 0; i <= m; i++) {
	    for (j = 0; j <= m-i; j++) {
		sprintf (keyword, "A_%d_%d", i, j);
		hdel (header, keyword);
		n++;
		}
	    }
	hdel (header, "A_ORDER");
	n++;
	}

    if (hgeti4 (header, "AP_ORDER", &m)) {
	for (i = 0; i <= m; i++) {
	    for (j = 0; j <= m-i; j++) {
		sprintf (keyword, "AP_%d_%d", i, j);
		hdel (header, keyword);
		n++;
		}
	    }
	hdel (header, "AP_ORDER");
	n++;
	}

    if (hgeti4 (header, "B_ORDER", &m)) {
	for (i = 0; i <= m; i++) {
	    for (j = 0; j <= m-i; j++) {
		sprintf (keyword, "B_%d_%d", i, j);
		hdel (header, keyword);
		n++;
		}
	    }
	hdel (header, "B_ORDER");
	n++;
	}

    if (hgeti4 (header, "BP_ORDER", &m)) {
	for (i = 0; i <= m; i++) {
	    for (j = 0; j <= m-i; j++) {
		sprintf (keyword, "BP_%d_%d", i, j);
		hdel (header, keyword);
		n++;
		}
	    }
	hdel (header, "BP_ORDER");
	n++;
	}

    if (n > 0 && verbose)
	fprintf (stderr,"%d keywords deleted\n", n);

    /* Remove WCS distortion code from CTYPEi in FITS header */
    if (hgets (header, "CTYPE1", 31, str)) {
	lctype = strlen (str);
	if (lctype > 8) {
	    str[8] = (char) 0;
	    hputs (header, "CTYPE1", str);
	    }
	}
    if (hgets (header, "CTYPE2", 31, str)) {
	lctype = strlen (str);
	if (lctype > 8) {
	    str[8] = (char) 0;
	    hputs (header, "CTYPE2", str);
	    }
	}

    return (n);
}

void
foc2pix (wcs, x, y, u, v)

struct WorldCoor *wcs;  /* World coordinate system structure */
double	x, y;		/* Focal plane coordinates */
double	*u, *v;		/* Image pixel coordinates (returned) */
{
    int m, n, i, j, k;
    double s[DISTMAX], sum;
    double temp_x, temp_y;

    /* Spitzer distortion */
    if (wcs->distcode == DISTORT_SIRTF) {
	m = wcs->distort.ap_order;
	n = wcs->distort.bp_order;

	temp_x = x - wcs->xrefpix;
	temp_y = y - wcs->yrefpix;

	/* compute u */
	for (j = 0; j <= m; j++) {
	    s[j] = wcs->distort.ap[m-j][j];
	    for (k = j-1; k >= 0; k--) {
	   	s[j] = (temp_y * s[j]) + wcs->distort.ap[m-j][k];
		}
	    }
  
	sum = s[0];
	for (i=m; i>=1; i--){
	    sum = (temp_x * sum) + s[m-i+1];
	    }
	*u = sum;

	/* compute v*/
	for (j = 0; j <= n; j++) {
	    s[j] = wcs->distort.bp[n-j][j];
	    for (k = j-1; k >= 0; k--) {
		s[j] = temp_y*s[j] + wcs->distort.bp[n-j][k];
		}
	    }
   
	sum = s[0];
	for (i = n; i >= 1; i--)
	    sum = temp_x * sum + s[n-i+1];

	*v = sum;

	*u = x + *u;
	*v = y + *v;
	}

    /* If no distortion, return pixel positions unchanged */
    else {
	*u = x;
	*v = y;
	}

    return;
}


void
pix2foc (wcs, u, v, x, y)

struct WorldCoor *wcs;  /* World coordinate system structure */
double u, v;		/* Image pixel coordinates */
double *x, *y;		/* Focal plane coordinates (returned) */
{
    int m, n, i, j, k;
    double s[DISTMAX], sum;
    double temp_u, temp_v;

    /* Spitzer distortion */
    if (wcs->distcode == DISTORT_SIRTF) {
	m = wcs->distort.a_order;
	n = wcs->distort.b_order;

	temp_u = u - wcs->xrefpix;
	temp_v = v - wcs->yrefpix;

	/* compute u */
	for (j = 0; j <= m; j++) {
	    s[j] = wcs->distort.a[m-j][j];
	    for (k = j-1; k >= 0; k--) {
		s[j] = (temp_v * s[j]) + wcs->distort.a[m-j][k];
		}
	    }
  
	sum = s[0];
	for (i=m; i>=1; i--){
	    sum = temp_u*sum + s[m-i+1];
	    }
	*x = sum;

	/* compute v*/
	for (j=0; j<=n; j++) {
	    s[j] = wcs->distort.b[n-j][j];
	    for (k=j-1; k>=0; k--) {
		s[j] =temp_v*s[j] + wcs->distort.b[n-j][k];
		}
	    }
   
	sum = s[0];
	for (i=n; i>=1; i--)
	    sum = temp_u*sum + s[n-i+1];

	*y = sum;
  
	*x = u + *x;
	*y = v + *y;

/*	*x = u + *x + coeff.crpix1; */
/*	*y = v + *y + coeff.crpix2; */
	}

    /* If no distortion, return pixel positions unchanged */
    else {
	*x = u;
	*y = v;
	}

    return;
}


/* SETDISTCODE -- Set WCS distortion code from CTYPEi in FITS header */

void
setdistcode (wcs, ctype)

struct WorldCoor *wcs;  /* World coordinate system structure */
char *ctype;		/* Value of CTYPEi from FITS header */

{
    char *extension;
    int lctype;

    lctype = strlen (ctype);
    if (lctype < 9)
	wcs->distcode = DISTORT_NONE;
    else {
	extension = ctype + 8;
	if (!strncmp (extension, "-SIP", 4))
	    wcs->distcode = DISTORT_SIRTF;
	else
	    wcs->distcode = DISTORT_NONE;
	}
    return;
}


/* GETDISTCODE -- Return NULL if no distortion or code from wcs.h */

char *
getdistcode (wcs)

struct WorldCoor *wcs;  /* World coordinate system structure */

{
    char *dcode;	/* Distortion string for CTYPEi */

    if (wcs->distcode == DISTORT_SIRTF) {
	dcode = (char *) calloc (8, sizeof (char));
	strcpy (dcode, "-SIP");
	}
    else
	dcode = NULL;
    return (dcode);
}

/* Apr  2 2003	New subroutines
 * Nov  3 2003	Add getdistcode to return distortion code string
 * Nov 10 2003	Include unistd.h to get definition of NULL
 * Nov 18 2003	Include string.h to get strlen()
 *
 * Jan  9 2004	Add DelDistort() to delete distortion keywords
 *
 * Jan  4 2007	Declare header const char*
 *
 * Feb 25 2011	Change SIRTF to Spitzer (long overdue!)
 */