File: fortvimoswcs.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 (537 lines) | stat: -rw-r--r-- 15,120 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*** File saoimage/wcslib/fortwcs.c
 *** February 16, 2001
 *** By Doug Mink, dmink@cfa.harvard.edu
 *** Harvard-Smithsonian Center for Astrophysics

 * Module:	vimoswcsfort.c (World Coordinate Systems)
 * Purpose:	Fortran interface to C WCS subroutines in vimoswcsinit.c and vimoswcs.c
 * Subroutine:	vimoswcsinit_ (hstring, wcs) sets WCS structure from an image header
 * Subroutine:	vimoswcsxinit_ (cra,cdec,secpix,xrpix,yrpix,nxpix,nypix,rotate,
		    equinox,epoch,proj)	sets WCS structure from arguments
 * Subroutine:	vimoswcskinit_ (nxpix,nypix,ctype1,ctype2,crpix1,crpix2,crval1,
 *			   crval2, cd,cdelt1,cdelt2,crota,equinox,epoch)
 *		    sets a WCS structure from keyword-based arguments
 * Subroutine:	vimoswcssize_ (wcs, cra, cdec, dra, ddec)
 *		    returns the image center and size in WCS units
 * Subroutine:	vimoswcsdist_ (x1,y1,x2,y2,diff)
 *		    compute angular distance between ra/dec or lat/long
 * Subroutine:	vimoswcsoutinit (wcs,coor) sets up the output coordinate system
 * Subroutine:	getvimoswcsout_ (iwcs, coorsys)
 *		Return WCS output coordinate system used by pix2vimoswcs
 * Subroutine:	vimoswcsininit (wcs,coor) sets up the input coordinate system
 * Subroutine:	getvimoswcsin_ (iwcs, coorsys)
 *		Return WCS output coordinate system used by pix2vimoswcs
 * Subroutine:	getvimoswcsim_ (iwcs, coorsys) Return WCS coordinate system of image 
 * Subroutine:	setvimoswcslin_ (iwcs, mode)
 *		Set output string mode for LINEAR coordinates
 * Subroutine:	setvimoswcsdeg_ (iwcs, mode)
 *		Set output string mode as decimal degrees if not zero
 * Subroutine:	pix2vimoswcs_ (wcs,xpix,ypix,xpos,ypos)
 *		    pixel coordinates -> sky coordinates
 * Subroutine:	pix2vimoswcst_ (wcs,xpix,ypix,wcstring,lstr)
 *		    pixels -> sky coordinate string
 * Subroutine:	vimoswcs2pix_ (wcs,xpos,ypos,xpix,ypix)
 *		    sky coordinates -> pixel coordinates

 * Copyright:   2000 Smithsonian Astrophysical Observatory
 *              You may do anything you like with this file except remove
 *              this copyright.  The Smithsonian Astrophysical Observatory
 *              makes no representations about the suitability of this
 *              software for any purpose.  It is provided "as is" without
 *              express or implied warranty.
 */

#include <string.h>		/* strstr, NULL */
#include <stdio.h>		/* stderr */
#include <math.h>		/* stderr */
#include "vimoswcs.h"
#ifndef VMS
#include <stdlib.h>
#endif

static struct WorldCoor **pvimoswcs;
static int nvimoswcs = 0;

/* set up a WCS structure from a FITS or IRAF image header */

/* Call VIMOSWCSINIT (HSTRING, IVIMOSWCS)
 * where HSTRING is a Character string and IVIMOSWCS is a returned integer
 * identifying the data structure which is created */

void
vimoswcsinit_ (hstring, ivimoswcs, nc)

char	*hstring;	/* Character string containing FITS header information
			   in the format <keyword>= <value> {/ <comment>} */
int	*ivimoswcs;		/* Pointer to wcs structure (returned) */
int	nc;		/* Number of characters in hstring (supplied by Fortran */
{
    int id;
    struct WorldCoor *tvimoswcs, *vimoswcsinit();

    tvimoswcs = vimoswcsinit (hstring);

    /* Set index to -1 if no WCS is found in header */
    if (tvimoswcs == NULL)
	*ivimoswcs = -1;

    /* Otherwise, use first available index into vector of pointers */
    else if (nvimoswcs == 0) {
	pvimoswcs = (struct WorldCoor **) calloc (10, sizeof (void *));
	nvimoswcs = 10;
	}
    else {
	for (id = 0; id < nvimoswcs; id++) {
	    if (pvimoswcs[id] == NULL) {
		*ivimoswcs = id;
		pvimoswcs[id] = tvimoswcs;
		break;
		}
	    }
	}
    return;
}

/* set up a WCS structure from subroutine arguments*/

void
vimoswcsxinit_ (ivimoswcs,cra,cdec,secpix,xrpix,yrpix,nxpix,nypix,rotate,equinox,
	   epoch,proj,np)

int	*ivimoswcs;		/* Pointer to wcs structure (returned) */
double	*cra;		/* Center right ascension in degrees */
double	*cdec;		/* Center declination in degrees */
double	*secpix;	/* Number of arcseconds per pixel */
double	*xrpix;		/* Reference pixel X coordinate */
double	*yrpix;		/* Reference pixel X coordinate */
int	*nxpix;		/* Number of pixels along x-axis */
int	*nypix;		/* Number of pixels along y-axis */
double	*rotate;	/* Rotation angle (clockwise positive) in degrees */
int	*equinox;	/* Equinox of coordinates, 1950 and 2000 supported */
double	*epoch;		/* Epoch of coordinates, used for FK4/FK5 conversion
			 * no effect if 0 */
char	*proj;		/* Projection */
int	np;		/* Length of projection (supplied by Fortran) */

{
    int id;
    struct WorldCoor *tvimoswcs;
    
    tvimoswcs = vimoswcsxinit (*cra,*cdec,*secpix,*xrpix,*yrpix,*nxpix,*nypix,
		     *rotate,*equinox,*epoch,proj);

    /* Set index to -1 if no WCS is found in header */
    if (tvimoswcs == NULL)
	*ivimoswcs = -1;
    else {
	for (id = 0; id < 10; id++) {
	    if (pvimoswcs[id] == NULL) {
		*ivimoswcs = id;
		pvimoswcs[id] = tvimoswcs;
		break;
		}
	    }
	}
    return;
}

/* set up a WCS structure from subroutine arguments matching FITS keywords*/

void
vimoswcskinit_ (ivimoswcs,naxis1,naxis2,ctype1,ctype2,crpix1,crpix2,crval1,crval2,cd,
	   cdelt1,cdelt2,crota,equinox,epoch, nc1,nc2)

int	*ivimoswcs;		/* Pointer to wcs structure (returned) */
int	*naxis1;	/* Number of pixels along x-axis */
int	*naxis2;	/* Number of pixels along y-axis */
char	*ctype1;	/* FITS WCS projection for axis 1 */
char	*ctype2;	/* FITS WCS projection for axis 2 */
double	*crpix1, *crpix2; /* Reference pixel coordinates */
double	*crval1, *crval2; /* Coordinates at reference pixel in degrees */
double	*cd;		/* Rotation matrix, used if all 4 elements nonzero */
double	*cdelt1, *cdelt2; /* scale in degrees/pixel, ignored if cd is not NULL */
double	*crota;		/* Rotation angle in degrees, ignored if cd is set */
int	*equinox;	/* Equinox of coordinates, 1950 and 2000 supported */
double	*epoch;		/* Epoch of coordinates, used for FK4/FK5 conversion
			 * no effect if 0 */
int	nc1, nc2;	/* Lengths of CTYPEs (supplied by Fortran) */

{
    int id;
    struct WorldCoor *tvimoswcs;

    tvimoswcs = vimoswcskinit (*naxis1,*naxis2,ctype1,ctype2,*crpix1,*crpix2,*crval1,
		     *crval2, cd, *cdelt1, *cdelt2, *crota, *equinox, *epoch);

    /* Set index to -1 if no WCS is found in header */
    if (tvimoswcs == NULL)
	*ivimoswcs = -1;
    else {
	for (id = 0; id < nvimoswcs; id++) {
	    if (pvimoswcs[id] == NULL) {
		*ivimoswcs = id;
		pvimoswcs[id] = tvimoswcs;
		break;
		}
	    }
	}
    return;
}


void
vimoswcsclose_ (ivimoswcs)

int	*ivimoswcs;		/* Index to WCS pointer array */
{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];
    if (vimoswcs != NULL) {
	vimoswcsfree (vimoswcs);
	pvimoswcs[*ivimoswcs] = NULL;
	}
    return;
}


/* Return RA and Dec of image center, plus size in RA and Dec */

void
vimoswcssize_ (ivimoswcs, cra, cdec, dra, ddec, radecsys)

int	*ivimoswcs;		/* Index to WCS pointer array */
double	*cra;		/* Right ascension of image center (rad) (returned) */
double	*cdec;		/* Declination of image center radg) (returned) */
double	*dra;		/* Half-width in radians (returned) */
double	*ddec;		/* Half-height in radians (returned) */
char	*radecsys;	/* Equinox (returned) */

{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */
    double width, height;

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    /* If there is no WCS defined, return all zeroes */
    if (vimoswcs == NULL) {
	(void)fprintf (stderr,"No WCS info available\n");
	*cra = 0.0;
	*cdec = 0.0;
	*dra = 0.0;
	*ddec = 0.0;
	}

    else {
	vimoswcsfull (vimoswcs, cra, cdec, &width, &height);
	*dra = 0.5 * width;
	*ddec = 0.5 * height;

	/* Get coordinate system from structure */
	strcpy (radecsys, vimoswcs->radecsys);
	}
    return;
}


/* Compute distance in degrees between two sky coordinates */

void vimoswcsdist_ (x1,y1,x2,y2,diff)

double	*x1,*y1;	/* (RA,Dec) or (Long,Lat) in degrees */
double	*x2,*y2;	/* (RA,Dec) or (Long,Lat) in degrees */
double	*diff;		/* Distance in degrees */

{
    double xr1, xr2, yr1, yr2;
    double pos1[3], pos2[3], w, cosb;
    int i;

    /* Convert two vectors to direction cosines */
    xr1 = degrad (*x1);
    yr1 = degrad (*y1);
    cosb = cos (yr1);
    pos1[0] = cos (xr1) * cosb;
    pos1[1] = sin (xr1) * cosb;
    pos1[2] = sin (yr1);

    xr2 = degrad (*x2);
    yr2 = degrad (*y2);
    cosb = cos (yr2);
    pos2[0] = cos (xr2) * cosb;
    pos2[1] = sin (xr2) * cosb;
    pos2[2] = sin (yr2);

    /* Modulus squared of half the difference vector */
    w = 0.0;
    for (i = 0; i < 3; i++) {
	w = w + (pos1[i] - pos2[i]) * (pos1[i] - pos2[i]);
	}
    w = w / 4.0;
    if (w > 1.0) w = 1.0;

    /* Angle beween the vectors */
    *diff = 2.0 * atan2 (sqrt (w), sqrt (1.0 - w));
    *diff = raddeg (*diff);
    return;
}


/* Initialize WCS output coordinate system used by pix2vimoswcs */

void
vimoswcsoutinit_ (ivimoswcs, coorsys, nc)

int	*ivimoswcs;		/* Index to WCS structure */
char	*coorsys;	/* Output coordinate system */
int	nc;		/* Length of coorsys (set by Fortran) */

{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	vimoswcsoutinit (vimoswcs, coorsys);

    return;
}


/* Return WCS output coordinate system used by pix2vimoswcs */

void
getvimoswcsout_ (ivimoswcs, coorsys, nc)

int	*ivimoswcs;		/* Index to WCS structure */
char	*coorsys;	/* Output coordinate system (returned) */
int	nc;		/* Length of coorsys (set by Fortran) */

{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	strncpy (coorsys, getvimoswcsout (vimoswcs), nc);

    return;
}


/* Initialize WCS input coordinate system used by vimoswcs2pix */

void
vimoswcsininit_ (ivimoswcs, coorsys, nc)

int	*ivimoswcs;		/* Index to WCS structure */
char	*coorsys;	/* Input coordinate system */
int	nc;		/* Length of coorsys (Set by Fortran) */

{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	vimoswcsininit (vimoswcs, coorsys);

    return;
}


/* Return WCS input coordinate system used by vimoswcs2pix */

void
getvimoswcsin_ (ivimoswcs, coorsys, nc)

int	*ivimoswcs;		/* Index to WCS structure */
char	*coorsys;	/* Input coordinate system (returned) */
int	nc;		/* Length of coorsys (Set by Fortran) */

{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	strncpy (coorsys, getvimoswcsin (vimoswcs), nc);

    return;
}


/* Return WCS coordinate system of image */

void
getvimoswcsim_ (ivimoswcs, coorsys, nc)

int	*ivimoswcs;		/* Index to WCS structure */
char	*coorsys;	/* Image coordinate system (returned) */
int	nc;		/* Length of coorsys (Set by Fortran) */

{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	strncpy (coorsys, getradecsys (vimoswcs), nc);

    return;
}


/* Set WCS output in degrees or hh:mm:ss dd:mm:ss */

void
setvimoswcsdeg_ (ivimoswcs, mode)

int	*ivimoswcs;		/* Index to WCS structure */
int	*mode;		/* mode = 0: h:m:s d:m:s
			   mode = 1: fractional degrees */
{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	vimoswcs->degout = *mode;

    return;
}


/* Set output string mode for LINEAR coordinates */

void
setvimoswcslin_ (ivimoswcs, mode)

int	*ivimoswcs;		/* World coordinate system structure */
int	*mode;		/* mode = 0: x y linear
			   mode = 1: x units x units
			   mode = 2: x y linear units */
{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	vimoswcs->linmode = *mode;

    return;
}


/* Convert pixels to sky coordinate string */

void
pix2vimoswcst_ (ivimoswcs,xpix,ypix,vimoswcstring,lstr)

int	*ivimoswcs;		/* World coordinate system structure */
double	*xpix, *ypix;	/* x and y image coordinates in pixels */
char	*vimoswcstring;	/* World coordinate string (returned) */
int	lstr;		/* Length of world coordinate string (set by Fortran) */

{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	pix2vimoswcst (vimoswcs, *xpix, *ypix, vimoswcstring, lstr);

    return;
}


/* Convert pixel coordinates to World Coordinates */

void
pix2vimoswcs_ (ivimoswcs, xpix, ypix, xpos, ypos)

int	*ivimoswcs;		/* World coordinate system structure */
double	*xpix, *ypix;	/* x and y image coordinates in pixels */
double	*xpos, *ypos;	/* RA and Dec in radians (returned) */
{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	pix2vimoswcs (vimoswcs, *xpix, *ypix, xpos, ypos);

    return;
}


/* Convert World Coordinates to pixel coordinates */

void
vimoswcs2pix_ (ivimoswcs, xpos, ypos, xpix, ypix, offscl)

int	*ivimoswcs;		/* World coordinate system structure */
double	*xpos, *ypos;	/* World coordinates in degrees */
double	*xpix, *ypix;	/* Image coordinates in pixels */
int	*offscl;
{
    struct WorldCoor *vimoswcs;	/* World coordinate system structure */

    if (*ivimoswcs >= 0 && *ivimoswcs < nvimoswcs)
	vimoswcs = pvimoswcs[*ivimoswcs];
    else
	vimoswcs = pvimoswcs[0];

    if (vimoswcs != NULL)
	vimoswcs2pix (vimoswcs, *xpos, *ypos, xpix, ypix, offscl);

    return;
}
/* Jan  4 1996	new program
 * Jan 12 1996	Add WCSSET to set WCS without an image
 *
 * Jun 15 1998	rename vimoswcsf77.c to vimoswcsfort.c and move to libvimoswcs
 * Jun 15 1998	rename WCSSET WCSXINIT and update arguments
 * Jul  7 1998	Change setlinmode to setvimoswcslin_; add setvimoswcsdeg_
 *
 * Oct 15 1999	Free vimoswcs using vimoswcsfree()
 * Oct 21 1999	Add pix2wcst_(); drop unused variables after lint
 * Dec 10 1999	Add error handling for iwcs; document all subroutines
 *
 * Jun  2 2000	Fix WCS structure pointers
 *
 * Feb 16 2001	Change name of file from wcsfort.c to fortwcs.c
 */