public software.sextractor

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

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 284 bertin
*       Copyright:              (C) 2000-2012 Emmanuel Bertin -- IAP/CNRS/UPMC
11 235 bertin
*                               (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 284 bertin
*       Last modified:          18/04/2012
28 233 bertin
*
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 284 bertin
*      TPV: AstrOmatic's polynomial correction to TAN
234 2 bertin
*
235
*   Cylindricals:
236
*      CYP: cylindrical perspective
237
*      CAR: Cartesian
238
*      MER: Mercator
239
*      CEA: cylindrical equal area
240
*
241
*   Conics:
242
*      COP: conic perspective
243
*      COD: conic equidistant
244
*      COE: conic equal area
245
*      COO: conic orthomorphic
246
*
247
*   Polyconics:
248
*      BON: Bonne
249
*      PCO: polyconic
250
*
251
*   Pseudo-cylindricals:
252
*      GLS: Sanson-Flamsteed (global sinusoidal)
253
*      PAR: parabolic
254
*      MOL: Mollweide
255
*
256
*   Conventional:
257
*      AIT: Hammer-Aitoff
258
*
259
*   Quad-cubes:
260
*      CSC: COBE quadrilateralized spherical cube
261
*      QSC: quadrilateralized spherical cube
262
*      TSC: tangential spherical cube
263
*
264
*   Author: Mark Calabretta, Australia Telescope National Facility
265
*   IRAF's TNX added by E.Bertin 2000/03/28
266 284 bertin
*   TPV added by E.Bertin 2012/04/11
267 2 bertin
*   Filtering of abs(phi)>180 and abs(theta)>90 added by E.Bertin 2000/11/11
268 284 bertin
*   $Id: cel.c,v 1.1.1.1 2012/04/18 16:33:26 bertin Exp $
269 2 bertin
*===========================================================================*/
270
 
271
#ifdef HAVE_CONFIG_H
272
#include        "config.h"
273
#endif
274
 
275
#ifdef HAVE_MATHIMF_H
276
#include <mathimf.h>
277
#else
278
#include <math.h>
279
#endif
280
#include <string.h>
281
#include "wcstrig.h"
282
#include "cel.h"
283
#include "sph.h"
284
#include "tnx.h"
285
 
286 284 bertin
int  npcode = 27;
287
char pcodes[27][4] =
288 2 bertin
      {"AZP", "TAN", "SIN", "STG", "ARC", "ZPN", "ZEA", "AIR", "CYP", "CAR",
289
       "MER", "CEA", "COP", "COD", "COE", "COO", "BON", "PCO", "GLS", "PAR",
290 284 bertin
       "AIT", "MOL", "CSC", "QSC", "TSC", "TNX", "TPV"};
291 2 bertin
 
292
/* Map error number to error message for each function. */
293
const char *celset_errmsg[] = {
294
   0,
295
   "Invalid coordinate transformation parameters",
296
   "Ill-conditioned coordinate transformation parameters"};
297
 
298
const char *celfwd_errmsg[] = {
299
   0,
300
   "Invalid coordinate transformation parameters",
301
   "Invalid projection parameters",
302
   "Invalid value of (lng,lat)"};
303
 
304
const char *celrev_errmsg[] = {
305
   0,
306
   "Invalid coordinate transformation parameters",
307
   "Invalid projection parameters",
308
   "Invalid value of (x,y)"};
309
 
310
 
311
int celset(pcode, cel, prj)
312
 
313
const char pcode[4];
314
struct celprm *cel;
315
struct prjprm *prj;
316
 
317
{
318
   int dophip;
319
   const double tol = 1.0e-10;
320
   double clat0, cphip, cthe0, theta0, slat0, sphip, sthe0;
321
   double latp, latp1, latp2;
322
   double u, v, x, y, z;
323
 
324
   /* Set pointers to the forward and reverse projection routines. */
325 284 bertin
   if (strcmp(pcode, "TAN") == 0) {
326
      cel->prjfwd = tanfwd;
327
      cel->prjrev = tanrev;
328 2 bertin
      theta0 = 90.0;
329 284 bertin
   } else if (strcmp(pcode, "TPV") == 0) {
330 2 bertin
      cel->prjfwd = tanfwd;
331
      cel->prjrev = tanrev;
332
      theta0 = 90.0;
333 284 bertin
   } else if (strcmp(pcode, "AZP") == 0) {
334
      cel->prjfwd = azpfwd;
335
      cel->prjrev = azprev;
336
      theta0 = 90.0;
337 2 bertin
   } else if (strcmp(pcode, "SIN") == 0) {
338
      cel->prjfwd = sinfwd;
339
      cel->prjrev = sinrev;
340
      theta0 = 90.0;
341
   } else if (strcmp(pcode, "STG") == 0) {
342
      cel->prjfwd = stgfwd;
343
      cel->prjrev = stgrev;
344
      theta0 = 90.0;
345
   } else if (strcmp(pcode, "ARC") == 0) {
346
      cel->prjfwd = arcfwd;
347
      cel->prjrev = arcrev;
348
      theta0 = 90.0;
349
   } else if (strcmp(pcode, "ZPN") == 0) {
350
      cel->prjfwd = zpnfwd;
351
      cel->prjrev = zpnrev;
352
      theta0 = 90.0;
353
   } else if (strcmp(pcode, "ZEA") == 0) {
354
      cel->prjfwd = zeafwd;
355
      cel->prjrev = zearev;
356
      theta0 = 90.0;
357
   } else if (strcmp(pcode, "AIR") == 0) {
358
      cel->prjfwd = airfwd;
359
      cel->prjrev = airrev;
360
      theta0 = 90.0;
361
   } else if (strcmp(pcode, "CYP") == 0) {
362
      cel->prjfwd = cypfwd;
363
      cel->prjrev = cyprev;
364
      theta0 = 0.0;
365
   } else if (strcmp(pcode, "CAR") == 0) {
366
      cel->prjfwd = carfwd;
367
      cel->prjrev = carrev;
368
      theta0 = 0.0;
369
   } else if (strcmp(pcode, "MER") == 0) {
370
      cel->prjfwd = merfwd;
371
      cel->prjrev = merrev;
372
      theta0 = 0.0;
373
   } else if (strcmp(pcode, "CEA") == 0) {
374
      cel->prjfwd = ceafwd;
375
      cel->prjrev = cearev;
376
      theta0 = 0.0;
377
   } else if (strcmp(pcode, "COP") == 0) {
378
      cel->prjfwd = copfwd;
379
      cel->prjrev = coprev;
380
      theta0 = prj->p[1];
381
   } else if (strcmp(pcode, "COD") == 0) {
382
      cel->prjfwd = codfwd;
383
      cel->prjrev = codrev;
384
      theta0 = prj->p[1];
385
   } else if (strcmp(pcode, "COE") == 0) {
386
      cel->prjfwd = coefwd;
387
      cel->prjrev = coerev;
388
      theta0 = prj->p[1];
389
   } else if (strcmp(pcode, "COO") == 0) {
390
      cel->prjfwd = coofwd;
391
      cel->prjrev = coorev;
392
      theta0 = prj->p[1];
393
   } else if (strcmp(pcode, "BON") == 0) {
394
      cel->prjfwd = bonfwd;
395
      cel->prjrev = bonrev;
396
      theta0 = 0.0;
397
   } else if (strcmp(pcode, "PCO") == 0) {
398
      cel->prjfwd = pcofwd;
399
      cel->prjrev = pcorev;
400
      theta0 = 0.0;
401
   } else if (strcmp(pcode, "GLS") == 0) {
402
      cel->prjfwd = glsfwd;
403
      cel->prjrev = glsrev;
404
      theta0 = 0.0;
405
   } else if (strcmp(pcode, "PAR") == 0) {
406
      cel->prjfwd = parfwd;
407
      cel->prjrev = parrev;
408
      theta0 = 0.0;
409
   } else if (strcmp(pcode, "AIT") == 0) {
410
      cel->prjfwd = aitfwd;
411
      cel->prjrev = aitrev;
412
      theta0 = 0.0;
413
   } else if (strcmp(pcode, "MOL") == 0) {
414
      cel->prjfwd = molfwd;
415
      cel->prjrev = molrev;
416
      theta0 = 0.0;
417
   } else if (strcmp(pcode, "CSC") == 0) {
418
      cel->prjfwd = cscfwd;
419
      cel->prjrev = cscrev;
420
      theta0 = 0.0;
421
   } else if (strcmp(pcode, "QSC") == 0) {
422
      cel->prjfwd = qscfwd;
423
      cel->prjrev = qscrev;
424
      theta0 = 0.0;
425
   } else if (strcmp(pcode, "TSC") == 0) {
426
      cel->prjfwd = tscfwd;
427
      cel->prjrev = tscrev;
428
      theta0 = 0.0;
429
   } else if (strcmp(pcode, "TNX") == 0) {
430
      cel->prjfwd = tnxfwd;
431
      cel->prjrev = tnxrev;
432
      theta0 = 90.0;
433
   } else {
434
      /* Unrecognized projection code. */
435
      return 1;
436
   }
437
 
438
   /* Set default for native longitude of the celestial pole? */
439
   dophip = (cel->ref[2] == 999.0);
440
 
441
   /* Compute celestial coordinates of the native pole. */
442
   if (theta0 == 90.0) {
443
      /* Reference point is at the native pole. */
444
 
445
      if (dophip) {
446
         /* Set default for longitude of the celestial pole. */
447
         cel->ref[2] = 180.0;
448
      }
449
 
450
      latp = cel->ref[1];
451
      cel->ref[3] = latp;
452
 
453
      cel->euler[0] = cel->ref[0];
454
      cel->euler[1] = 90.0 - latp;
455
   } else {
456
      /* Reference point away from the native pole. */
457
 
458
      /* Set default for longitude of the celestial pole. */
459
      if (dophip) {
460
         cel->ref[2] = (cel->ref[1] < theta0) ? 180.0 : 0.0;
461
      }
462
 
463
      clat0 = wcs_cosd(cel->ref[1]);
464
      slat0 = wcs_sind(cel->ref[1]);
465
      cphip = wcs_cosd(cel->ref[2]);
466
      sphip = wcs_sind(cel->ref[2]);
467
      cthe0 = wcs_cosd(theta0);
468
      sthe0 = wcs_sind(theta0);
469
 
470
      x = cthe0*cphip;
471
      y = sthe0;
472
      z = sqrt(x*x + y*y);
473
      if (z == 0.0) {
474
         if (slat0 != 0.0) {
475
            return 1;
476
         }
477
 
478
         /* latp determined by LATPOLE in this case. */
479
         latp = cel->ref[3];
480
      } else {
481
         if (fabs(slat0/z) > 1.0) {
482
            return 1;
483
         }
484
 
485
         u = wcs_atan2d(y,x);
486
         v = wcs_acosd(slat0/z);
487
 
488
         latp1 = u + v;
489
         if (latp1 > 180.0) {
490
            latp1 -= 360.0;
491
         } else if (latp1 < -180.0) {
492
            latp1 += 360.0;
493
         }
494
 
495
         latp2 = u - v;
496
         if (latp2 > 180.0) {
497
            latp2 -= 360.0;
498
         } else if (latp2 < -180.0) {
499
            latp2 += 360.0;
500
         }
501
 
502
         if (fabs(cel->ref[3]-latp1) < fabs(cel->ref[3]-latp2)) {
503
            if (fabs(latp1) < 90.0+tol) {
504
               latp = latp1;
505
            } else {
506
               latp = latp2;
507
            }
508
         } else {
509
            if (fabs(latp2) < 90.0+tol) {
510
               latp = latp2;
511
            } else {
512
               latp = latp1;
513
            }
514
         }
515
 
516
         cel->ref[3] = latp;
517
      }
518
 
519
      cel->euler[1] = 90.0 - latp;
520
 
521
      z = wcs_cosd(latp)*clat0;
522
      if (fabs(z) < tol) {
523
         if (fabs(clat0) < tol) {
524
            /* Celestial pole at the reference point. */
525
            cel->euler[0] = cel->ref[0];
526
            cel->euler[1] = 90.0 - theta0;
527
         } else if (latp > 0.0) {
528
            /* Celestial pole at the native north pole.*/
529
            cel->euler[0] = cel->ref[0] + cel->ref[2] - 180.0;
530
            cel->euler[1] = 0.0;
531
         } else if (latp < 0.0) {
532
            /* Celestial pole at the native south pole. */
533
            cel->euler[0] = cel->ref[0] - cel->ref[2];
534
            cel->euler[1] = 180.0;
535
         }
536
      } else {
537
         x = (sthe0 - wcs_sind(latp)*slat0)/z;
538
         y =  sphip*cthe0/clat0;
539
         if (x == 0.0 && y == 0.0) {
540
            return 1;
541
         }
542
         cel->euler[0] = cel->ref[0] - wcs_atan2d(y,x);
543
      }
544
 
545
      /* Make euler[0] the same sign as ref[0]. */
546
      if (cel->ref[0] >= 0.0) {
547
         if (cel->euler[0] < 0.0) cel->euler[0] += 360.0;
548
      } else {
549
         if (cel->euler[0] > 0.0) cel->euler[0] -= 360.0;
550
      }
551
   }
552
 
553
   cel->euler[2] = cel->ref[2];
554
   cel->euler[3] = wcs_cosd(cel->euler[1]);
555
   cel->euler[4] = wcs_sind(cel->euler[1]);
556
   cel->flag = CELSET;
557
 
558
   /* Check for ill-conditioned parameters. */
559
   if (fabs(latp) > 90.0+tol) {
560
      return 2;
561
   }
562
 
563
   return 0;
564
}
565
 
566
/*--------------------------------------------------------------------------*/
567
 
568
int celfwd(pcode, lng, lat, cel, phi, theta, prj, x, y)
569
 
570
const char pcode[4];
571
const double lng, lat;
572
struct celprm *cel;
573
double *phi, *theta;
574
struct prjprm *prj;
575
double *x, *y;
576
 
577
{
578
   int    err;
579
 
580
   if (cel->flag != CELSET) {
581
      if (celset(pcode, cel, prj)) return 1;
582
   }
583
 
584
   /* Compute native coordinates. */
585
   sphfwd(lng, lat, cel->euler, phi, theta);
586
 
587
   /* Apply forward projection. */
588
   if ((err = cel->prjfwd(*phi, *theta, prj, x, y))) {
589
      return err == 1 ? 2 : 3;
590
   }
591
 
592
   return 0;
593
}
594
 
595
/*--------------------------------------------------------------------------*/
596
 
597
int celrev(pcode, x, y, prj, phi, theta, cel, lng, lat)
598
 
599
const char pcode[4];
600
const double x, y;
601
struct prjprm *prj;
602
double *phi, *theta;
603
struct celprm *cel;
604
double *lng, *lat;
605
 
606
{
607
   int    err;
608
 
609
   if (cel->flag != CELSET) {
610
      if(celset(pcode, cel, prj)) return 1;
611
   }
612
 
613
   /* Apply reverse projection. */
614
   if ((err = cel->prjrev(x, y, prj, phi, theta))) {
615
      return err == 1 ? 2 : 3;
616
   }
617
   if (fabs(*phi)>180.0 || fabs(*theta)>90.0)
618
     return 2;
619
 
620
   /* Compute native coordinates. */
621
   sphrev(*phi, *theta, cel->euler, lng, lat);
622
 
623
   return 0;
624
}