File: wcsutil.c

package info (click to toggle)
cfitsio 3.470-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 14,484 kB
  • sloc: ansic: 106,145; yacc: 4,883; sh: 3,259; fortran: 2,613; lex: 504; makefile: 162
file content (502 lines) | stat: -rw-r--r-- 16,769 bytes parent folder | download | duplicates (16)
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
#include <math.h>
#include "fitsio2.h"
#define D2R 0.01745329252
#define TWOPI 6.28318530717959

/*--------------------------------------------------------------------------*/
int ffwldp(double xpix, double ypix, double xref, double yref,
      double xrefpix, double yrefpix, double xinc, double yinc, double rot,
      char *type, double *xpos, double *ypos, int *status)

/* This routine is based on the classic AIPS WCS routine. 

   It converts from pixel location to RA,Dec for 9 projective geometries:
   "-CAR", "-SIN", "-TAN", "-ARC", "-NCP", "-GLS", "-MER", "-AIT" and "-STG".
*/

/*-----------------------------------------------------------------------*/
/* routine to determine accurate position for pixel coordinates          */
/* returns 0 if successful otherwise:                                    */
/* 501 = angle too large for projection;                                 */
/* does: -CAR, -SIN, -TAN, -ARC, -NCP, -GLS, -MER, -AIT  -STG projections*/
/* Input:                                                                */
/*   f   xpix    x pixel number  (RA or long without rotation)           */
/*   f   ypiy    y pixel number  (dec or lat without rotation)           */
/*   d   xref    x reference coordinate value (deg)                      */
/*   d   yref    y reference coordinate value (deg)                      */
/*   f   xrefpix x reference pixel                                       */
/*   f   yrefpix y reference pixel                                       */
/*   f   xinc    x coordinate increment (deg)                            */
/*   f   yinc    y coordinate increment (deg)                            */
/*   f   rot     rotation (deg)  (from N through E)                      */
/*   c  *type    projection type code e.g. "-SIN";                       */
/* Output:                                                               */
/*   d   *xpos   x (RA) coordinate (deg)                                 */
/*   d   *ypos   y (dec) coordinate (deg)                                */
/*-----------------------------------------------------------------------*/
 {double cosr, sinr, dx, dy, dz, temp, x, y, z;
  double sins, coss, dect, rat, dt, l, m, mg, da, dd, cos0, sin0;
  double dec0, ra0;
  double geo1, geo2, geo3;
  double deps = 1.0e-5;
  char *cptr;
  
  if (*status > 0)
     return(*status);

/*   Offset from ref pixel  */
  dx = (xpix-xrefpix) * xinc;
  dy = (ypix-yrefpix) * yinc;

/*   Take out rotation  */
  cosr = cos(rot * D2R);
  sinr = sin(rot * D2R);
  if (rot != 0.0) {
     temp = dx * cosr - dy * sinr;
     dy = dy * cosr + dx * sinr;
     dx = temp;
  }

/* convert to radians  */
  ra0 = xref * D2R;
  dec0 = yref * D2R;

  l = dx * D2R;
  m = dy * D2R;
  sins = l*l + m*m;
  cos0 = cos(dec0);
  sin0 = sin(dec0);

  if (*type != '-') {  /* unrecognized projection code */
     return(*status = 504);
  }

    cptr = type + 1;

    if (*cptr == 'C') { /* linear -CAR */
      if (*(cptr + 1) != 'A' ||  *(cptr + 2) != 'R') {
         return(*status = 504);
      }
      rat =  ra0 + l;
      dect = dec0 + m;

    } else if (*cptr == 'T') {  /* -TAN */
      if (*(cptr + 1) != 'A' ||  *(cptr + 2) != 'N') {
         return(*status = 504);
      }
      x = cos0*cos(ra0) - l*sin(ra0) - m*cos(ra0)*sin0;
      y = cos0*sin(ra0) + l*cos(ra0) - m*sin(ra0)*sin0;
      z = sin0                       + m*         cos0;
      rat  = atan2( y, x );
      dect = atan ( z / sqrt(x*x+y*y) );

    } else if (*cptr == 'S') {

      if (*(cptr + 1) == 'I' &&  *(cptr + 2) == 'N') { /* -SIN */
          if (sins>1.0)
	    return(*status = 501);
          coss = sqrt (1.0 - sins);
          dt = sin0 * coss + cos0 * m;
          if ((dt>1.0) || (dt<-1.0))
	    return(*status = 501);
          dect = asin (dt);
          rat = cos0 * coss - sin0 * m;
          if ((rat==0.0) && (l==0.0))
	    return(*status = 501);
          rat = atan2 (l, rat) + ra0;

       } else if (*(cptr + 1) == 'T' &&  *(cptr + 2) == 'G') {  /* -STG Sterographic*/
          dz = (4.0 - sins) / (4.0 + sins);
          if (fabs(dz)>1.0)
	    return(*status = 501);
          dect = dz * sin0 + m * cos0 * (1.0+dz) / 2.0;
          if (fabs(dect)>1.0)
	    return(*status = 501);
          dect = asin (dect);
          rat = cos(dect);
          if (fabs(rat)<deps)
	    return(*status = 501);
          rat = l * (1.0+dz) / (2.0 * rat);
          if (fabs(rat)>1.0)
	    return(*status = 501);
          rat = asin (rat);
          mg = 1.0 + sin(dect) * sin0 + cos(dect) * cos0 * cos(rat);
          if (fabs(mg)<deps)
	    return(*status = 501);
          mg = 2.0 * (sin(dect) * cos0 - cos(dect) * sin0 * cos(rat)) / mg;
          if (fabs(mg-m)>deps)
	    rat = TWOPI /2.0 - rat;
          rat = ra0 + rat;
        } else  {
          return(*status = 504);
        }
 
    } else if (*cptr == 'A') {

      if (*(cptr + 1) == 'R' &&  *(cptr + 2) == 'C') { /* ARC */
          if (sins>=TWOPI*TWOPI/4.0)
	    return(*status = 501);
          sins = sqrt(sins);
          coss = cos (sins);
          if (sins!=0.0)
	    sins = sin (sins) / sins;
          else
	    sins = 1.0;
          dt = m * cos0 * sins + sin0 * coss;
          if ((dt>1.0) || (dt<-1.0))
	    return(*status = 501);
          dect = asin (dt);
          da = coss - dt * sin0;
          dt = l * sins * cos0;
          if ((da==0.0) && (dt==0.0))
	    return(*status = 501);
          rat = ra0 + atan2 (dt, da);

      } else if (*(cptr + 1) == 'I' &&  *(cptr + 2) == 'T') {  /* -AIT Aitoff */
          dt = yinc*cosr + xinc*sinr;
          if (dt==0.0)
	    dt = 1.0;
          dt = dt * D2R;
          dy = yref * D2R;
          dx = sin(dy+dt)/sqrt((1.0+cos(dy+dt))/2.0) -
	      sin(dy)/sqrt((1.0+cos(dy))/2.0);
          if (dx==0.0)
	    dx = 1.0;
          geo2 = dt / dx;
          dt = xinc*cosr - yinc* sinr;
          if (dt==0.0)
	    dt = 1.0;
          dt = dt * D2R;
          dx = 2.0 * cos(dy) * sin(dt/2.0);
          if (dx==0.0) dx = 1.0;
          geo1 = dt * sqrt((1.0+cos(dy)*cos(dt/2.0))/2.0) / dx;
          geo3 = geo2 * sin(dy) / sqrt((1.0+cos(dy))/2.0);
          rat = ra0;
          dect = dec0;
          if ((l != 0.0) || (m != 0.0)) {
            dz = 4.0 - l*l/(4.0*geo1*geo1) - ((m+geo3)/geo2)*((m+geo3)/geo2) ;
            if ((dz>4.0) || (dz<2.0)) return(*status = 501);
            dz = 0.5 * sqrt (dz);
            dd = (m+geo3) * dz / geo2;
            if (fabs(dd)>1.0) return(*status = 501);
            dd = asin (dd);
            if (fabs(cos(dd))<deps) return(*status = 501);
            da = l * dz / (2.0 * geo1 * cos(dd));
            if (fabs(da)>1.0) return(*status = 501);
            da = asin (da);
            rat = ra0 + 2.0 * da;
            dect = dd;
          }
        } else  {
          return(*status = 504);
        }
 
    } else if (*cptr == 'N') { /* -NCP North celestial pole*/
      if (*(cptr + 1) != 'C' ||  *(cptr + 2) != 'P') {
         return(*status = 504);
      }
      dect = cos0 - m * sin0;
      if (dect==0.0)
        return(*status = 501);
      rat = ra0 + atan2 (l, dect);
      dt = cos (rat-ra0);
      if (dt==0.0)
        return(*status = 501);
      dect = dect / dt;
      if ((dect>1.0) || (dect<-1.0))
        return(*status = 501);
      dect = acos (dect);
      if (dec0<0.0) dect = -dect;

    } else if (*cptr == 'G') {   /* -GLS global sinusoid */
      if (*(cptr + 1) != 'L' ||  *(cptr + 2) != 'S') {
         return(*status = 504);
      }
      dect = dec0 + m;
      if (fabs(dect)>TWOPI/4.0)
        return(*status = 501);
      coss = cos (dect);
      if (fabs(l)>TWOPI*coss/2.0)
        return(*status = 501);
      rat = ra0;
      if (coss>deps) rat = rat + l / coss;

    } else if (*cptr == 'M') {  /* -MER mercator*/
      if (*(cptr + 1) != 'E' ||  *(cptr + 2) != 'R') {
         return(*status = 504);
      }
      dt = yinc * cosr + xinc * sinr;
      if (dt==0.0) dt = 1.0;
      dy = (yref/2.0 + 45.0) * D2R;
      dx = dy + dt / 2.0 * D2R;
      dy = log (tan (dy));
      dx = log (tan (dx));
      geo2 = dt * D2R / (dx - dy);
      geo3 = geo2 * dy;
      geo1 = cos (yref*D2R);
      if (geo1<=0.0) geo1 = 1.0;
      rat = l / geo1 + ra0;
      if (fabs(rat - ra0) > TWOPI)
        return(*status = 501);
      dt = 0.0;
      if (geo2!=0.0) dt = (m + geo3) / geo2;
      dt = exp (dt);
      dect = 2.0 * atan (dt) - TWOPI / 4.0;

    } else  {
      return(*status = 504);
    }

  /*  correct for RA rollover  */
  if (rat-ra0>TWOPI/2.0) rat = rat - TWOPI;
  if (rat-ra0<-TWOPI/2.0) rat = rat + TWOPI;
  if (rat < 0.0) rat += TWOPI;

  /*  convert to degrees  */
  *xpos  = rat  / D2R;
  *ypos  = dect  / D2R;
  return(*status);
} 
/*--------------------------------------------------------------------------*/
int ffxypx(double xpos, double ypos, double xref, double yref, 
      double xrefpix, double yrefpix, double xinc, double yinc, double rot,
      char *type, double *xpix, double *ypix, int *status)

/* This routine is based on the classic AIPS WCS routine. 

   It converts from RA,Dec to pixel location to for 9 projective geometries:
   "-CAR", "-SIN", "-TAN", "-ARC", "-NCP", "-GLS", "-MER", "-AIT" and "-STG".
*/
/*-----------------------------------------------------------------------*/
/* routine to determine accurate pixel coordinates for an RA and Dec     */
/* returns 0 if successful otherwise:                                    */
/* 501 = angle too large for projection;                                 */
/* 502 = bad values                                                      */
/* does: -SIN, -TAN, -ARC, -NCP, -GLS, -MER, -AIT projections            */
/* anything else is linear                                               */
/* Input:                                                                */
/*   d   xpos    x (RA) coordinate (deg)                                 */
/*   d   ypos    y (dec) coordinate (deg)                                */
/*   d   xref    x reference coordinate value (deg)                      */
/*   d   yref    y reference coordinate value (deg)                      */
/*   f   xrefpix x reference pixel                                       */
/*   f   yrefpix y reference pixel                                       */
/*   f   xinc    x coordinate increment (deg)                            */
/*   f   yinc    y coordinate increment (deg)                            */
/*   f   rot     rotation (deg)  (from N through E)                      */
/*   c  *type    projection type code e.g. "-SIN";                       */
/* Output:                                                               */
/*   f  *xpix    x pixel number  (RA or long without rotation)           */
/*   f  *ypiy    y pixel number  (dec or lat without rotation)           */
/*-----------------------------------------------------------------------*/
 {
  double dx, dy, dz, r, ra0, dec0, ra, dec, coss, sins, dt, da, dd, sint;
  double l, m, geo1, geo2, geo3, sinr, cosr, cos0, sin0;
  double deps=1.0e-5;
  char *cptr;

  if (*type != '-') {  /* unrecognized projection code */
     return(*status = 504);
  }

  cptr = type + 1;

  dt = (xpos - xref);
  if (dt >  180) xpos -= 360;
  if (dt < -180) xpos += 360;
  /* NOTE: changing input argument xpos is OK (call-by-value in C!) */

  /* default values - linear */
  dx = xpos - xref;
  dy = ypos - yref;

  /*  Correct for rotation */
  r = rot * D2R;
  cosr = cos (r);
  sinr = sin (r);
  dz = dx*cosr + dy*sinr;
  dy = dy*cosr - dx*sinr;
  dx = dz;

  /*     check axis increments - bail out if either 0 */
  if ((xinc==0.0) || (yinc==0.0)) {*xpix=0.0; *ypix=0.0;
    return(*status = 502);}

  /*     convert to pixels  */
  *xpix = dx / xinc + xrefpix;
  *ypix = dy / yinc + yrefpix;

  if (*cptr == 'C') { /* linear -CAR */
      if (*(cptr + 1) != 'A' ||  *(cptr + 2) != 'R') {
         return(*status = 504);
      }

      return(*status);  /* done if linear */
  }

  /* Non linear position */
  ra0 = xref * D2R;
  dec0 = yref * D2R;
  ra = xpos * D2R;
  dec = ypos * D2R;

  /* compute direction cosine */
  coss = cos (dec);
  sins = sin (dec);
  cos0 = cos (dec0);
  sin0 = sin (dec0);
  l = sin(ra-ra0) * coss;
  sint = sins * sin0 + coss * cos0 * cos(ra-ra0);

    /* process by case  */
    if (*cptr == 'T') {  /* -TAN tan */
         if (*(cptr + 1) != 'A' ||  *(cptr + 2) != 'N') {
           return(*status = 504);
         }

         if (sint<=0.0)
	   return(*status = 501);
         if( cos0<0.001 ) {
            /* Do a first order expansion around pole */
            m = (coss * cos(ra-ra0)) / (sins * sin0);
            m = (-m + cos0 * (1.0 + m*m)) / sin0;
         } else {
            m = ( sins/sint - sin0 ) / cos0;
         }
	 if( fabs(sin(ra0)) < 0.3 ) {
	    l  = coss*sin(ra)/sint - cos0*sin(ra0) + m*sin(ra0)*sin0;
	    l /= cos(ra0);
	 } else {
	    l  = coss*cos(ra)/sint - cos0*cos(ra0) + m*cos(ra0)*sin0;
	    l /= -sin(ra0);
	 }

    } else if (*cptr == 'S') {

      if (*(cptr + 1) == 'I' &&  *(cptr + 2) == 'N') { /* -SIN */
         if (sint<0.0)
	   return(*status = 501);
         m = sins * cos(dec0) - coss * sin(dec0) * cos(ra-ra0);

      } else if (*(cptr + 1) == 'T' &&  *(cptr + 2) == 'G') {  /* -STG Sterographic*/
         da = ra - ra0;
         if (fabs(dec)>TWOPI/4.0)
	   return(*status = 501);
         dd = 1.0 + sins * sin(dec0) + coss * cos(dec0) * cos(da);
         if (fabs(dd)<deps)
	   return(*status = 501);
         dd = 2.0 / dd;
         l = l * dd;
         m = dd * (sins * cos(dec0) - coss * sin(dec0) * cos(da));

        } else  {
          return(*status = 504);
        }
 
    } else if (*cptr == 'A') {

      if (*(cptr + 1) == 'R' &&  *(cptr + 2) == 'C') { /* ARC */
         m = sins * sin(dec0) + coss * cos(dec0) * cos(ra-ra0);
         if (m<-1.0) m = -1.0;
         if (m>1.0) m = 1.0;
         m = acos (m);
         if (m!=0) 
            m = m / sin(m);
         else
            m = 1.0;
         l = l * m;
         m = (sins * cos(dec0) - coss * sin(dec0) * cos(ra-ra0)) * m;

      } else if (*(cptr + 1) == 'I' &&  *(cptr + 2) == 'T') {  /* -AIT Aitoff */
         da = (ra - ra0) / 2.0;
         if (fabs(da)>TWOPI/4.0)
	     return(*status = 501);
         dt = yinc*cosr + xinc*sinr;
         if (dt==0.0) dt = 1.0;
         dt = dt * D2R;
         dy = yref * D2R;
         dx = sin(dy+dt)/sqrt((1.0+cos(dy+dt))/2.0) -
             sin(dy)/sqrt((1.0+cos(dy))/2.0);
         if (dx==0.0) dx = 1.0;
         geo2 = dt / dx;
         dt = xinc*cosr - yinc* sinr;
         if (dt==0.0) dt = 1.0;
         dt = dt * D2R;
         dx = 2.0 * cos(dy) * sin(dt/2.0);
         if (dx==0.0) dx = 1.0;
         geo1 = dt * sqrt((1.0+cos(dy)*cos(dt/2.0))/2.0) / dx;
         geo3 = geo2 * sin(dy) / sqrt((1.0+cos(dy))/2.0);
         dt = sqrt ((1.0 + cos(dec) * cos(da))/2.0);
         if (fabs(dt)<deps)
	     return(*status = 503);
         l = 2.0 * geo1 * cos(dec) * sin(da) / dt;
         m = geo2 * sin(dec) / dt - geo3;

        } else  {
          return(*status = 504);
        }
 
    } else if (*cptr == 'N') { /* -NCP North celestial pole*/
         if (*(cptr + 1) != 'C' ||  *(cptr + 2) != 'P') {
             return(*status = 504);
         }

         if (dec0==0.0) 
	     return(*status = 501);  /* can't stand the equator */
         else
	   m = (cos(dec0) - coss * cos(ra-ra0)) / sin(dec0);

    } else if (*cptr == 'G') {   /* -GLS global sinusoid */
         if (*(cptr + 1) != 'L' ||  *(cptr + 2) != 'S') {
             return(*status = 504);
         }

         dt = ra - ra0;
         if (fabs(dec)>TWOPI/4.0)
	   return(*status = 501);
         if (fabs(dec0)>TWOPI/4.0)
	   return(*status = 501);
         m = dec - dec0;
         l = dt * coss;

    } else if (*cptr == 'M') {  /* -MER mercator*/
         if (*(cptr + 1) != 'E' ||  *(cptr + 2) != 'R') {
             return(*status = 504);
         }

         dt = yinc * cosr + xinc * sinr;
         if (dt==0.0) dt = 1.0;
         dy = (yref/2.0 + 45.0) * D2R;
         dx = dy + dt / 2.0 * D2R;
         dy = log (tan (dy));
         dx = log (tan (dx));
         geo2 = dt * D2R / (dx - dy);
         geo3 = geo2 * dy;
         geo1 = cos (yref*D2R);
         if (geo1<=0.0) geo1 = 1.0;
         dt = ra - ra0;
         l = geo1 * dt;
         dt = dec / 2.0 + TWOPI / 8.0;
         dt = tan (dt);
         if (dt<deps)
	   return(*status = 502);
         m = geo2 * log (dt) - geo3;

    } else  {
      return(*status = 504);
    }

    /*   convert to degrees  */
    dx = l / D2R;
    dy = m / D2R;

    /*  Correct for rotation */
    dz = dx*cosr + dy*sinr;
    dy = dy*cosr - dx*sinr;
    dx = dz;

    /*     convert to pixels  */
    *xpix = dx / xinc + xrefpix;
    *ypix = dy / yinc + yrefpix;
    return(*status);
}