File: vimoswcspoly.c

package info (click to toggle)
cpl-plugin-vimos 4.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 28,228 kB
  • sloc: ansic: 169,271; cpp: 16,177; sh: 4,344; python: 3,678; makefile: 1,138; perl: 10
file content (377 lines) | stat: -rw-r--r-- 9,125 bytes parent folder | download | duplicates (5)
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
/* File wcstools/libwcs/wcspoly.c
 * February 24, 1999
 * By Doug Mink, Harvard-Smithsonian Center for Astrophysics

 * Module:	wcspoly.c
 * Purpose:	Polynomial correction to pixel coordinates
 * Subroutine:	polypos() converts from pixel location to corrected pixel
 * Subroutine:	polypix() converts from corrected pixel to pixel location   
 */

#include <string.h>
#include <stdio.h>
#include "vimoswcs.h"
#include "fitshead.h"

#define VIMOSWCS_NCOEFF	15

static double vimoswcspolycomp();
static double vimoswcspolydx();
static double vimoswcspolydy();

int
vimoswcspolypos (xpix, ypix, vimoswcs, xpix1, ypix1)

/* Routine to determine accurate position for pixel coordinates */
/* returns 0 if successful otherwise 1 = angle too large for projection; */
/* based on amdpos() from getimage */

/* Input: */
double	xpix;		/* x pixel number  (RA or long without rotation) */
double	ypix;		/* y pixel number  (dec or lat without rotation) */
struct WorldCoor *vimoswcs;	/* WCS parameter structure */

/* Output: */
double	*xpix1;		/* Corrected x pixel number */
double	*ypix1;		/* Corrected y pixel number */

{
    double x, y;

    /* Make pixels relative to reference pixels */
    x = xpix - vimoswcs->crpix[0];
    y = ypix - vimoswcs->crpix[1];

    /*  Compute new x and y from polynomial model */
    *xpix1 = vimoswcspolycomp (x, y, vimoswcs->x_coeff);
    *ypix1 = vimoswcspolycomp (x,y, vimoswcs->y_coeff);

    return (0);
}

int
vimoswcspolypix (xpix1, ypix1, vimoswcs, xpix, ypix)

/* Routine to determine pixel coordinates for sky position */
/* returns 0 if successful otherwise 1 = angle too large for projection; */
/* based on amdinv() from getimage */

/* Input: */
double	xpix1;		/* Right ascension or longitude in degrees */
double	ypix1;		/* Declination or latitude in degrees */
struct WorldCoor *vimoswcs;	/* WCS parameter structure */

/* Output: */
double	*xpix;		/* x pixel number  (RA or long without rotation) */
double	*ypix;		/* y pixel number  (dec or lat without rotation) */

{
    double x, y, dx, dy;
    double f,fx,fy,g,gx,gy;
    double tolerance = 0.0000005;
    int    max_iterations = 50;
    int    i;

    /* Set initial value for x,y */
    if (vimoswcs->x_coeff[1] == 0.0)
	x = xpix1 - vimoswcs->x_coeff[0];
    else
	x = (xpix1 - vimoswcs->x_coeff[0]) / vimoswcs->x_coeff[1];
    if (vimoswcs->y_coeff[2] == 0.0)
	y = ypix1 - vimoswcs->y_coeff[0];
    else
	y = (ypix1 - vimoswcs->y_coeff[0]) / vimoswcs->y_coeff[2];

    /* Iterate by Newton's method */
    for (i = 0; i < max_iterations; i++) {

	f = vimoswcspolycomp (x, y, vimoswcs->x_coeff);

	/*  Derivative of X model wrt x */
	fx = vimoswcspolydx (x, y, vimoswcs->x_coeff);

	/* Derivative of X model wrt y */
	fy = vimoswcspolydy (x, y, vimoswcs->x_coeff);

	/* Y plate model */
	g = vimoswcspolycomp (x, y, vimoswcs->y_coeff);

	/* Derivative of Y model wrt x */
	gx = vimoswcspolydx (x, y, vimoswcs->y_coeff);

	/* Derivative of Y model wrt y */
	gy = vimoswcspolydy (x, y, vimoswcs->y_coeff);

	f = f - xpix1;
	g = g - ypix1;
	dx = ((-f * gy) + (g * fy)) / ((fx * gy) - (fy * gx));
	dy = ((-g * fx) + (f * gx)) / ((fx * gy) - (fy * gx));
	x = x + dx;
	y = y + dy;
	if ((fabs(dx) < tolerance) && (fabs(dy) < tolerance)) break;
	}

    /* Convert from plate pixels to image pixels */
    *xpix = x;
    *ypix = y;

    /* If position is off of the image, return offscale code */
    if (*xpix < 0.5 || *xpix > vimoswcs->nxpix+0.5)
	return -1;
    if (*ypix < 0.5 || *ypix > vimoswcs->nypix+0.5)
	return -1;

    return 0;
}


/* Set plate fit coefficients in structure from arguments */
int
vimoswcspolyset (vimoswcs, xcoeff, ycoeff, coeff)

struct WorldCoor *vimoswcs;  /* World coordinate system structure */
int	*xcoeff;	/* 1 if corresponding x coefficient used */
int	*ycoeff;	/* 1 if corresponding y coefficient used */
double	*coeff;		/* Plate fit coefficients, only non-zero ones */

{
    int i, j;

    if (novimoswcs (vimoswcs))
	return 1;

    j = 0;
    for (i = 0; i < VIMOSWCS_NCOEFF; i++) {
	if (xcoeff[i])
	    vimoswcs->x_coeff[i] = coeff[j++];
	else
	    vimoswcs->x_coeff[i] = 0.0;
	}

    for (i = 0; i < VIMOSWCS_NCOEFF; i++) {
	if (ycoeff[i])
	    vimoswcs->y_coeff[i] = coeff[j++];
	else
	    vimoswcs->y_coeff[i] = 0.0;
	}
    return 0;
}


/* Return plate fit coefficients from structure in argument */

int
vimoswcspolyget (vimoswcs, xcoeff, ycoeff, coeff)

struct WorldCoor *vimoswcs;  /* World coordinate system structure */
int	*xcoeff;	/* 1 if corresponding x coefficient used */
int	*ycoeff;	/* 1 if corresponding y coefficient used */
double	*coeff;		/* Plate fit coefficients, only non-zero ones */

{
    int i, j;

    if (novimoswcs (vimoswcs))
	return 1;

    j = 0;
    for (i = 0; i < VIMOSWCS_NCOEFF; i++) {
	if (xcoeff[i])
	    coeff[i] = vimoswcs->x_coeff[j++];
	}

    for (i = 0; i < VIMOSWCS_NCOEFF; i++) {
	if (ycoeff[i])
	    coeff[i] = vimoswcs->y_coeff[j++];
	}

    return 0;
}


/* Set FITS header plate fit coefficients from structure */
void
vimoswcspolyfset (header, vimoswcs)

char    *header;        /* Image FITS header */
struct WorldCoor *vimoswcs;  /* WCS structure */

{
    char keyword[16];
    int i;

    for (i = 0; i < VIMOSWCS_NCOEFF; i++) {
	if (vimoswcs->x_coeff[i] != 0.0) {
	    sprintf (keyword,"PX1_%d",i+1);
	    hputnr8 (header, keyword, -15, vimoswcs->x_coeff[i]);
	    }
	}
    for (i = 0; i < VIMOSWCS_NCOEFF; i++) {
	if (vimoswcs->y_coeff[i] != 0.0) {
	    sprintf (keyword,"PX2_%d",i+1);
	    hputnr8 (header, keyword, -15, vimoswcs->y_coeff[i]);
	    }
	}
    return;
}


/* Read FITS header plate fit coefficients into structure */
void
vimoswcspolyfget (header, vimoswcs)

char    *header;        /* Image FITS header */
struct WorldCoor *vimoswcs;  /* WCS structure */

{
    char keyword[16];
    int i;

    for (i = 0; i < VIMOSWCS_NCOEFF; i++) {
	sprintf (keyword,"PX1_%d",i+1);
	if (!hgetr8 (header, keyword, vimoswcs->x_coeff[i]))
	    vimoswcs->x_coeff[i] = 0.0;
	}
    for (i = 0; i < VIMOSWCS_NCOEFF; i++) {
	sprintf (keyword,"PX2_%d",i+1);
	if (!hgetr8 (header, keyword, vimoswcs->y_coeff[i]))
	    vimoswcs->y_coeff[i] = 0.0;
	}
    return;
}

/* WCSPOLYCOMP: Compute polynomial with cross terms */

static double wx0 = 0.0;
static double wy0 = 0.0;
static double x2 = 0.0;
static double y2 = 0.0;
static double xy = 0.0;
static double x3 = 0.0;
static double y3 = 0.0;
static double x2y = 0.0;
static double xy2 = 0.0;
static double r2 = 0.0;
static double xr2 = 0.0;
static double yr2 = 0.0;
static double r4 = 0.0;
static double xr4 = 0.0;
static double yr4 = 0.0;

static double
vimoswcspolycomp (x, y, coeff)

double	x, y;		/* Coordinates to be used in transform */
double	*coeff;		/* Polynomial coefficients */
{
    double v;

    /* If new x or y, compute new terms */
    if (x != wx0 || y != wy0) {
	wx0 = x;
	wy0 = y;
	x2 = x * x;
	y2 = y * y;
	xy = x * y;
	x3 = x * x2;
	y3 = y * y2;
	x2y = x2 * y;
	xy2 = x * y2;
	r2 = x2 + y2;
	r4 = r2 * r2;
	xr4 = x * r4;
	yr4 = y * r4;
	}

    v = coeff[ 0]	+ coeff[ 1]*x	+ coeff[ 2]*y	+ coeff[ 3]*x2 +
	coeff[ 4]*y2	+ coeff[ 5]*xy	+ coeff[ 6]*x3	+ coeff[ 7]*y3 +
	coeff[ 8]*x2y	+ coeff[ 9]*xy2	+ coeff[10]*r2	+ coeff[11]*xr2 +
	coeff[12]*yr2	+ coeff[13]*r4	+ coeff[14]*xr4	+ coeff[15]*yr4;

    return (v);
}


static double dx0 = 0.0;
static double dy0 = 0.0;
static double tx = 0.0;
static double ty = 0.0;
static double tx2 = 0.0;
static double ty2 = 0.0;
static double txy = 0.0;
static double fr2 = 0.0;
static double fx3 = 0.0;
static double fy3 = 0.0;
static double x4 = 0.0;
static double y4 = 0.0;

/*  Compute derivative of one coordinate with respect to x */

static double
vimoswcspolydx (x, y, coeff)

double	x, y;		/* Coordinates to be used in transform */
double	*coeff;		/* Polynomial coefficients */
{
    double dx;

    /* If new x or y, compute new terms */
    if (x != dx0 || y != dy0) {
	dx0 = x;
	dy0 = y;
	tx = 2.0 * x;
	tx2 = 3.0 * x2;
	ty = 2.0 * y;
	ty2 = 3.0 * y2;
	txy = 2.0 * xy;
	fr2 = 4.0 * r2;
	fx3 = 4.0 * x3;
	fy3 = 4.0 * y3;
	x4 = x2 * x2;
	y4 = y2 * y2;
	}

    dx = coeff[1]	+ coeff[3]*tx	+ coeff[5]*y	+ coeff[6]*tx2 +
	 coeff[8]*txy	+ coeff[9]*y2	+ coeff[10]*tx	+ coeff[11]*(tx2+y2) +
	 coeff[12]*txy	+ coeff[13]*fr2*wx0 + coeff[14]*(5.0*x4 + 6.0*x2*y2) +
	 coeff[15]*(fy3*wx0 + fx3);

    return (dx);
}


/*  Compute derivative of one coordinate with respect to y */
static double
vimoswcspolydy (x, y, coeff)

double	x, y;		/* Coordinates to be used in transform */
double	*coeff;		/* Polynomial coefficients */
{
    double dy;

    /* If new x or y, compute new terms */
    if (x != dx0 || y != dy0) {
	dx0 = x;
	dy0 = y;
	tx = 2.0 * x;
	tx2 = 3.0 * x2;
	ty = 2.0 * y;
	ty2 = 3.0 * y2;
	txy = 2.0 * xy;
	fr2 = 4.0 * r2;
	fx3 = 4.0 * x3;
	fy3 = 4.0 * y3;
	x4 = x2 * x2;
	y4 = y2 * y2;
	}

    dy = coeff[2]	+ coeff[4]*ty	+ coeff[5]*x	+ coeff[7]*ty2 +
	 coeff[8]*x2	+ coeff[9]*txy	+ coeff[10]*ty	+ coeff[11]*txy +
	 coeff[12]*(ty2+x2) + coeff[13]*fr2*wy0 + coeff[14]*(fx3*wy0 + fy3) +
	 coeff[15]*(5.0*y4 + 6.0*x2*y2);

    return (dy);
}

/* Feb 24 1999	New subroutines for direct image pixel <-> corrected image pixel
 */