public software.sextractor

[/] [trunk/] [src/] [wcs/] [cel.c] - Blame information for rev 235

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 233 bertin
/*
2
*                               cel.c
3
*
4
* Lower level spherical coordinate transformation and projection routines.
5
*
6
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7
*
8
*       This file part of:      AstrOmatic WCS library
9
*
10 235 bertin
*       Copyright:              (C) 2000-2010 Emmanuel Bertin -- IAP/CNRS/UPMC
11
*                               (C) 1995-1999 Mark Calabretta (original version)
12 233 bertin
*
13
*       Licenses:               GNU General Public License
14
*
15
*       AstrOmatic software is free software: you can redistribute it and/or
16
*       modify it under the terms of the GNU General Public License as
17
*       published by the Free Software Foundation, either version 3 of the
18
*       License, or (at your option) any later version.
19
*       AstrOmatic software is distributed in the hope that it will be useful,
20
*       but WITHOUT ANY WARRANTY; without even the implied warranty of
21
*       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
*       GNU General Public License for more details.
23
*       You should have received a copy of the GNU General Public License
24
*       along with AstrOmatic software.
25
*       If not, see <http://www.gnu.org/licenses/>.
26
*
27
*       Last modified:          10/10/2010
28
*
29
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
30 2 bertin
/*=============================================================================
31
*
32
*   WCSLIB - an implementation of the FITS WCS proposal.
33
*   Copyright (C) 1995-1999, Mark Calabretta
34
*
35
*   This library is free software; you can redistribute it and/or modify it
36
*   under the terms of the GNU Library General Public License as published
37
*   by the Free Software Foundation; either version 2 of the License, or (at
38
*   your option) any later version.
39
*
40
*   This library is distributed in the hope that it will be useful, but
41
*   WITHOUT ANY WARRANTY; without even the implied warranty of
42
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
43
*   General Public License for more details.
44
*
45
*   You should have received a copy of the GNU Library General Public License
46
*   along with this library; if not, write to the Free Software Foundation,
47
*   Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48
*
49
*   Correspondence concerning WCSLIB may be directed to:
50
*      Internet email: mcalabre@atnf.csiro.au
51
*      Postal address: Dr. Mark Calabretta,
52
*                      Australia Telescope National Facility,
53
*                      P.O. Box 76,
54
*                      Epping, NSW, 2121,
55
*                      AUSTRALIA
56
*
57
*=============================================================================
58
*
59
*   C routines which implement the FITS World Coordinate System (WCS)
60
*   convention.
61
*
62
*   Summary of routines
63
*   -------------------
64
*   These routines are provided as drivers for the lower level spherical
65
*   coordinate transformation and projection routines.  There are separate
66
*   driver routines for the forward, celfwd(), and reverse, celrev(),
67
*   transformations.
68
*
69
*   An initialization routine, celset(), computes intermediate values from
70
*   the transformation parameters but need not be called explicitly - see the
71
*   explanation of cel.flag below.
72
*
73
*
74
*   Initialization routine; celset()
75
*   --------------------------------
76
*   Initializes members of a celprm data structure which hold intermediate
77
*   values.  Note that this routine need not be called directly; it will be
78
*   invoked by celfwd() and celrev() if the "flag" structure member is
79
*   anything other than a predefined magic value.
80
*
81
*   Given:
82
*      pcode[4] const char
83
*                        WCS projection code (see below).
84
*
85
*   Given and returned:
86
*      cel      celprm*  Spherical coordinate transformation parameters
87
*                        (see below).
88
*      prj      prjprm*  Projection parameters (usage is described in the
89
*                        prologue to "proj.c").
90
*
91
*   Function return value:
92
*               int      Error status
93
*                           0: Success.
94
*                           1: Invalid coordinate transformation parameters.
95
*                           2: Ill-conditioned coordinate transformation
96
*                              parameters.
97
*
98
*   Forward transformation; celfwd()
99
*   --------------------------------
100
*   Compute (x,y) coordinates in the plane of projection from celestial
101
*   coordinates (lng,lat).
102
*
103
*   Given:
104
*      pcode[4] const char
105
*                        WCS projection code (see below).
106
*      lng,lat  const double
107
*                        Celestial longitude and latitude of the projected
108
*                        point, in degrees.
109
*
110
*   Given and returned:
111
*      cel      celprm*  Spherical coordinate transformation parameters
112
*                        (see below).
113
*
114
*   Returned:
115
*      phi,     double*  Longitude and latitude in the native coordinate
116
*      theta             system of the projection, in degrees.
117
*
118
*   Given and returned:
119
*      prj      prjprm*  Projection parameters (usage is described in the
120
*                        prologue to "proj.c").
121
*
122
*   Returned:
123
*      x,y      double*  Projected coordinates, "degrees".
124
*
125
*   Function return value:
126
*               int      Error status
127
*                           0: Success.
128
*                           1: Invalid coordinate transformation parameters.
129
*                           2: Invalid projection parameters.
130
*                           3: Invalid value of (lng,lat).
131
*
132
*   Reverse transformation; celrev()
133
*   --------------------------------
134
*   Compute the celestial coordinates (lng,lat) of the point with projected
135
*   coordinates (x,y).
136
*
137
*   Given:
138
*      pcode[4] const char
139
*                        WCS projection code (see below).
140
*      x,y      const double
141
*                        Projected coordinates, "degrees".
142
*
143
*   Given and returned:
144
*      prj      prjprm*  Projection parameters (usage is described in the
145
*                        prologue to "proj.c").
146
*
147
*   Returned:
148
*      phi,     double*  Longitude and latitude in the native coordinate
149
*      theta             system of the projection, in degrees.
150
*
151
*   Given and returned:
152
*      cel      celprm*  Spherical coordinate transformation parameters
153
*                        (see below).
154
*
155
*   Returned:
156
*      lng,lat  double*  Celestial longitude and latitude of the projected
157
*                        point, in degrees.
158
*
159
*   Function return value:
160
*               int      Error status
161
*                           0: Success.
162
*                           1: Invalid coordinate transformation parameters.
163
*                           2: Invalid projection parameters.
164
*                           3: Invalid value of (x,y).
165
*
166
*   Coordinate transformation parameters
167
*   ------------------------------------
168
*   The celprm struct consists of the following:
169
*
170
*      int flag
171
*         The celprm struct contains pointers to the forward and reverse
172
*         projection routines as well as intermediaries computed from the
173
*         reference coordinates (see below).  Whenever the projection code
174
*         (pcode) or any of ref[4] are set or changed then this flag must be
175
*         set to zero to signal the initialization routine, celset(), to
176
*         redetermine the function pointers and recompute intermediaries.
177
*         Once this has been done pcode itself is ignored.
178
*
179
*      double ref[4]
180
*         The first pair of values should be set to the celestial longitude
181
*         and latitude (usually right ascension and declination) of the
182
*         reference point of the projection.
183
*
184
*         The second pair of values are the native longitude and latitude of
185
*         the pole of the celestial coordinate system and correspond to the
186
*         FITS keywords LONGPOLE and LATPOLE.
187
*
188
*         LONGPOLE defaults to 0 degrees if the celestial latitude of the
189
*         reference point of the projection is greater than the native
190
*         latitude, otherwise 180 degrees.  (This is the condition for the
191
*         celestial latitude to increase in the same direction as the native
192
*         latitude at the reference point.)  ref[2] may be set to 999.0 to
193
*         indicate that the correct default should be substituted.
194
*
195
*         In some circumstances the latitude of the native pole may be
196
*         determined by the first three values only to within a sign and
197
*         LATPOLE is used to choose between the two solutions.  LATPOLE is
198
*         set in ref[3] and the solution closest to this value is used to
199
*         reset ref[3].  It is therefore legitimate, for example, to set
200
*         ref[3] to 999.0 to choose the more northerly solution - the default
201
*         if the LATPOLE card is omitted from the FITS header.  For the
202
*         special case where the reference point of the projection is at
203
*         native latitude zero, its celestial latitude is zero, and
204
*         LONGPOLE = +/- 90 then the native latitude of the pole is not
205
*         determined by the first three reference values and LATPOLE
206
*         specifies it completely.
207
*
208
*   The remaining members of the celprm struct are maintained by the
209
*   initialization routines and should not be modified.  This is done for the
210
*   sake of efficiency and to allow an arbitrary number of contexts to be
211
*   maintained simultaneously.
212
*
213
*      double euler[5]
214
*         Euler angles and associated intermediaries derived from the
215
*         coordinate reference values.
216
*      int (*prjfwd)()
217
*      int (*prjrev)()
218
*         Pointers to the forward and reverse projection routines.
219
*
220
*
221
*   WCS projection codes
222
*   --------------------
223
*   Zenithals/azimuthals:
224
*      AZP: zenithal/azimuthal perspective
225
*      TAN: gnomonic
226
*      SIN: synthesis (generalized orthographic)
227
*      STG: stereographic
228
*      ARC: zenithal/azimuthal equidistant
229
*      ZPN: zenithal/azimuthal polynomial
230
*      ZEA: zenithal/azimuthal equal area
231
*      AIR: Airy
232
*      TNX: IRAF's polynomial correction to TAN
233
*
234
*   Cylindricals:
235
*      CYP: cylindrical perspective
236
*      CAR: Cartesian
237
*      MER: Mercator
238
*      CEA: cylindrical equal area
239
*
240
*   Conics:
241
*      COP: conic perspective
242
*      COD: conic equidistant
243
*      COE: conic equal area
244
*      COO: conic orthomorphic
245
*
246
*   Polyconics:
247
*      BON: Bonne
248
*      PCO: polyconic
249
*
250
*   Pseudo-cylindricals:
251
*      GLS: Sanson-Flamsteed (global sinusoidal)
252
*      PAR: parabolic
253
*      MOL: Mollweide
254
*
255
*   Conventional:
256
*      AIT: Hammer-Aitoff
257
*
258
*   Quad-cubes:
259
*      CSC: COBE quadrilateralized spherical cube
260
*      QSC: quadrilateralized spherical cube
261
*      TSC: tangential spherical cube
262
*
263
*   Author: Mark Calabretta, Australia Telescope National Facility
264
*   IRAF's TNX added by E.Bertin 2000/03/28
265
*   Filtering of abs(phi)>180 and abs(theta)>90 added by E.Bertin 2000/11/11
266
*   $Id: cel.c,v 1.1.1.1 2002/03/15 16:33:26 bertin Exp $
267
*===========================================================================*/
268
 
269
#ifdef HAVE_CONFIG_H
270
#include        "config.h"
271
#endif
272
 
273
#ifdef HAVE_MATHIMF_H
274
#include <mathimf.h>
275
#else
276
#include <math.h>
277
#endif
278
#include <string.h>
279
#include "wcstrig.h"
280
#include "cel.h"
281
#include "sph.h"
282
#include "tnx.h"
283
 
284
int  npcode = 26;
285
char pcodes[26][4] =
286
      {"AZP", "TAN", "SIN", "STG", "ARC", "ZPN", "ZEA", "AIR", "CYP", "CAR",
287
       "MER", "CEA", "COP", "COD", "COE", "COO", "BON", "PCO", "GLS", "PAR",
288
       "AIT", "MOL", "CSC", "QSC", "TSC", "TNX"};
289
 
290
/* Map error number to error message for each function. */
291
const char *celset_errmsg[] = {
292
   0,
293
   "Invalid coordinate transformation parameters",
294
   "Ill-conditioned coordinate transformation parameters"};
295
 
296
const char *celfwd_errmsg[] = {
297
   0,
298
   "Invalid coordinate transformation parameters",
299
   "Invalid projection parameters",
300
   "Invalid value of (lng,lat)"};
301
 
302
const char *celrev_errmsg[] = {
303
   0,
304
   "Invalid coordinate transformation parameters",
305
   "Invalid projection parameters",
306
   "Invalid value of (x,y)"};
307
 
308
 
309
int celset(pcode, cel, prj)
310
 
311
const char pcode[4];
312
struct celprm *cel;
313
struct prjprm *prj;
314
 
315
{
316
   int dophip;
317
   const double tol = 1.0e-10;
318
   double clat0, cphip, cthe0, theta0, slat0, sphip, sthe0;
319
   double latp, latp1, latp2;
320
   double u, v, x, y, z;
321
 
322
   /* Set pointers to the forward and reverse projection routines. */
323
   if (strcmp(pcode, "AZP") == 0) {
324
      cel->prjfwd = azpfwd;
325
      cel->prjrev = azprev;
326
      theta0 = 90.0;
327
   } else if (strcmp(pcode, "TAN") == 0) {
328
      cel->prjfwd = tanfwd;
329
      cel->prjrev = tanrev;
330
      theta0 = 90.0;
331
   } else if (strcmp(pcode, "SIN") == 0) {
332
      cel->prjfwd = sinfwd;
333
      cel->prjrev = sinrev;
334
      theta0 = 90.0;
335
   } else if (strcmp(pcode, "STG") == 0) {
336
      cel->prjfwd = stgfwd;
337
      cel->prjrev = stgrev;
338
      theta0 = 90.0;
339
   } else if (strcmp(pcode, "ARC") == 0) {
340
      cel->prjfwd = arcfwd;
341
      cel->prjrev = arcrev;
342
      theta0 = 90.0;
343
   } else if (strcmp(pcode, "ZPN") == 0) {
344
      cel->prjfwd = zpnfwd;
345
      cel->prjrev = zpnrev;
346
      theta0 = 90.0;
347
   } else if (strcmp(pcode, "ZEA") == 0) {
348
      cel->prjfwd = zeafwd;
349
      cel->prjrev = zearev;
350
      theta0 = 90.0;
351
   } else if (strcmp(pcode, "AIR") == 0) {
352
      cel->prjfwd = airfwd;
353
      cel->prjrev = airrev;
354
      theta0 = 90.0;
355
   } else if (strcmp(pcode, "CYP") == 0) {
356
      cel->prjfwd = cypfwd;
357
      cel->prjrev = cyprev;
358
      theta0 = 0.0;
359
   } else if (strcmp(pcode, "CAR") == 0) {
360
      cel->prjfwd = carfwd;
361
      cel->prjrev = carrev;
362
      theta0 = 0.0;
363
   } else if (strcmp(pcode, "MER") == 0) {
364
      cel->prjfwd = merfwd;
365
      cel->prjrev = merrev;
366
      theta0 = 0.0;
367
   } else if (strcmp(pcode, "CEA") == 0) {
368
      cel->prjfwd = ceafwd;
369
      cel->prjrev = cearev;
370
      theta0 = 0.0;
371
   } else if (strcmp(pcode, "COP") == 0) {
372
      cel->prjfwd = copfwd;
373
      cel->prjrev = coprev;
374
      theta0 = prj->p[1];
375
   } else if (strcmp(pcode, "COD") == 0) {
376
      cel->prjfwd = codfwd;
377
      cel->prjrev = codrev;
378
      theta0 = prj->p[1];
379
   } else if (strcmp(pcode, "COE") == 0) {
380
      cel->prjfwd = coefwd;
381
      cel->prjrev = coerev;
382
      theta0 = prj->p[1];
383
   } else if (strcmp(pcode, "COO") == 0) {
384
      cel->prjfwd = coofwd;
385
      cel->prjrev = coorev;
386
      theta0 = prj->p[1];
387
   } else if (strcmp(pcode, "BON") == 0) {
388
      cel->prjfwd = bonfwd;
389
      cel->prjrev = bonrev;
390
      theta0 = 0.0;
391
   } else if (strcmp(pcode, "PCO") == 0) {
392
      cel->prjfwd = pcofwd;
393
      cel->prjrev = pcorev;
394
      theta0 = 0.0;
395
   } else if (strcmp(pcode, "GLS") == 0) {
396
      cel->prjfwd = glsfwd;
397
      cel->prjrev = glsrev;
398
      theta0 = 0.0;
399
   } else if (strcmp(pcode, "PAR") == 0) {
400
      cel->prjfwd = parfwd;
401
      cel->prjrev = parrev;
402
      theta0 = 0.0;
403
   } else if (strcmp(pcode, "AIT") == 0) {
404
      cel->prjfwd = aitfwd;
405
      cel->prjrev = aitrev;
406
      theta0 = 0.0;
407
   } else if (strcmp(pcode, "MOL") == 0) {
408
      cel->prjfwd = molfwd;
409
      cel->prjrev = molrev;
410
      theta0 = 0.0;
411
   } else if (strcmp(pcode, "CSC") == 0) {
412
      cel->prjfwd = cscfwd;
413
      cel->prjrev = cscrev;
414
      theta0 = 0.0;
415
   } else if (strcmp(pcode, "QSC") == 0) {
416
      cel->prjfwd = qscfwd;
417
      cel->prjrev = qscrev;
418
      theta0 = 0.0;
419
   } else if (strcmp(pcode, "TSC") == 0) {
420
      cel->prjfwd = tscfwd;
421
      cel->prjrev = tscrev;
422
      theta0 = 0.0;
423
   } else if (strcmp(pcode, "TNX") == 0) {
424
      cel->prjfwd = tnxfwd;
425
      cel->prjrev = tnxrev;
426
      theta0 = 90.0;
427
   } else {
428
      /* Unrecognized projection code. */
429
      return 1;
430
   }
431
 
432
   /* Set default for native longitude of the celestial pole? */
433
   dophip = (cel->ref[2] == 999.0);
434
 
435
   /* Compute celestial coordinates of the native pole. */
436
   if (theta0 == 90.0) {
437
      /* Reference point is at the native pole. */
438
 
439
      if (dophip) {
440
         /* Set default for longitude of the celestial pole. */
441
         cel->ref[2] = 180.0;
442
      }
443
 
444
      latp = cel->ref[1];
445
      cel->ref[3] = latp;
446
 
447
      cel->euler[0] = cel->ref[0];
448
      cel->euler[1] = 90.0 - latp;
449
   } else {
450
      /* Reference point away from the native pole. */
451
 
452
      /* Set default for longitude of the celestial pole. */
453
      if (dophip) {
454
         cel->ref[2] = (cel->ref[1] < theta0) ? 180.0 : 0.0;
455
      }
456
 
457
      clat0 = wcs_cosd(cel->ref[1]);
458
      slat0 = wcs_sind(cel->ref[1]);
459
      cphip = wcs_cosd(cel->ref[2]);
460
      sphip = wcs_sind(cel->ref[2]);
461
      cthe0 = wcs_cosd(theta0);
462
      sthe0 = wcs_sind(theta0);
463
 
464
      x = cthe0*cphip;
465
      y = sthe0;
466
      z = sqrt(x*x + y*y);
467
      if (z == 0.0) {
468
         if (slat0 != 0.0) {
469
            return 1;
470
         }
471
 
472
         /* latp determined by LATPOLE in this case. */
473
         latp = cel->ref[3];
474
      } else {
475
         if (fabs(slat0/z) > 1.0) {
476
            return 1;
477
         }
478
 
479
         u = wcs_atan2d(y,x);
480
         v = wcs_acosd(slat0/z);
481
 
482
         latp1 = u + v;
483
         if (latp1 > 180.0) {
484
            latp1 -= 360.0;
485
         } else if (latp1 < -180.0) {
486
            latp1 += 360.0;
487
         }
488
 
489
         latp2 = u - v;
490
         if (latp2 > 180.0) {
491
            latp2 -= 360.0;
492
         } else if (latp2 < -180.0) {
493
            latp2 += 360.0;
494
         }
495
 
496
         if (fabs(cel->ref[3]-latp1) < fabs(cel->ref[3]-latp2)) {
497
            if (fabs(latp1) < 90.0+tol) {
498
               latp = latp1;
499
            } else {
500
               latp = latp2;
501
            }
502
         } else {
503
            if (fabs(latp2) < 90.0+tol) {
504
               latp = latp2;
505
            } else {
506
               latp = latp1;
507
            }
508
         }
509
 
510
         cel->ref[3] = latp;
511
      }
512
 
513
      cel->euler[1] = 90.0 - latp;
514
 
515
      z = wcs_cosd(latp)*clat0;
516
      if (fabs(z) < tol) {
517
         if (fabs(clat0) < tol) {
518
            /* Celestial pole at the reference point. */
519
            cel->euler[0] = cel->ref[0];
520
            cel->euler[1] = 90.0 - theta0;
521
         } else if (latp > 0.0) {
522
            /* Celestial pole at the native north pole.*/
523
            cel->euler[0] = cel->ref[0] + cel->ref[2] - 180.0;
524
            cel->euler[1] = 0.0;
525
         } else if (latp < 0.0) {
526
            /* Celestial pole at the native south pole. */
527
            cel->euler[0] = cel->ref[0] - cel->ref[2];
528
            cel->euler[1] = 180.0;
529
         }
530
      } else {
531
         x = (sthe0 - wcs_sind(latp)*slat0)/z;
532
         y =  sphip*cthe0/clat0;
533
         if (x == 0.0 && y == 0.0) {
534
            return 1;
535
         }
536
         cel->euler[0] = cel->ref[0] - wcs_atan2d(y,x);
537
      }
538
 
539
      /* Make euler[0] the same sign as ref[0]. */
540
      if (cel->ref[0] >= 0.0) {
541
         if (cel->euler[0] < 0.0) cel->euler[0] += 360.0;
542
      } else {
543
         if (cel->euler[0] > 0.0) cel->euler[0] -= 360.0;
544
      }
545
   }
546
 
547
   cel->euler[2] = cel->ref[2];
548
   cel->euler[3] = wcs_cosd(cel->euler[1]);
549
   cel->euler[4] = wcs_sind(cel->euler[1]);
550
   cel->flag = CELSET;
551
 
552
   /* Check for ill-conditioned parameters. */
553
   if (fabs(latp) > 90.0+tol) {
554
      return 2;
555
   }
556
 
557
   return 0;
558
}
559
 
560
/*--------------------------------------------------------------------------*/
561
 
562
int celfwd(pcode, lng, lat, cel, phi, theta, prj, x, y)
563
 
564
const char pcode[4];
565
const double lng, lat;
566
struct celprm *cel;
567
double *phi, *theta;
568
struct prjprm *prj;
569
double *x, *y;
570
 
571
{
572
   int    err;
573
 
574
   if (cel->flag != CELSET) {
575
      if (celset(pcode, cel, prj)) return 1;
576
   }
577
 
578
   /* Compute native coordinates. */
579
   sphfwd(lng, lat, cel->euler, phi, theta);
580
 
581
   /* Apply forward projection. */
582
   if ((err = cel->prjfwd(*phi, *theta, prj, x, y))) {
583
      return err == 1 ? 2 : 3;
584
   }
585
 
586
   return 0;
587
}
588
 
589
/*--------------------------------------------------------------------------*/
590
 
591
int celrev(pcode, x, y, prj, phi, theta, cel, lng, lat)
592
 
593
const char pcode[4];
594
const double x, y;
595
struct prjprm *prj;
596
double *phi, *theta;
597
struct celprm *cel;
598
double *lng, *lat;
599
 
600
{
601
   int    err;
602
 
603
   if (cel->flag != CELSET) {
604
      if(celset(pcode, cel, prj)) return 1;
605
   }
606
 
607
   /* Apply reverse projection. */
608
   if ((err = cel->prjrev(x, y, prj, phi, theta))) {
609
      return err == 1 ? 2 : 3;
610
   }
611
   if (fabs(*phi)>180.0 || fabs(*theta)>90.0)
612
     return 2;
613
 
614
   /* Compute native coordinates. */
615
   sphrev(*phi, *theta, cel->euler, lng, lat);
616
 
617
   return 0;
618
}