File: palDafin.c

package info (click to toggle)
starlink-pal 0.9.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,808 kB
  • sloc: ansic: 6,689; makefile: 128; sh: 81
file content (194 lines) | stat: -rw-r--r-- 6,494 bytes parent folder | download | duplicates (3)
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
/*
*+
*  Name:
*     palDafin

*  Purpose:
*     Sexagesimal character string to angle

*  Language:
*     Starlink ANSI C

*  Type of Module:
*     Library routine

*  Invocation:
*     void palDafin ( const char *string, int *ipos, double *a, int *j );

*  Arguments:
*     string = const char * (Given)
*        String containing deg, arcmin, arcsec fields
*     ipos = int * (Given & Returned)
*        Position to start decoding "string". First character
*        is position 1 for compatibility with SLA. After
*        calling this routine "iptr" will be positioned after
*        the sexagesimal string.
*     a = double * (Returned)
*        Angle in radians.
*     j = int * (Returned)
*        status:  0 = OK
*                +1 = default, A unchanged
*                -1 = bad degrees      )
*                -2 = bad arcminutes   )  (note 3)
*                -3 = bad arcseconds   )

*  Description:
*     Extracts an angle from a sexagesimal string with degrees, arcmin,
*     arcsec fields using space or comma delimiters.

*  Authors:
*     TIMJ: Tim Jenness (JAC, Hawaii)
*     PTW: Patrick T. Wallace
*     {enter_new_authors_here}

*  Example:
*     argument    before                           after
*
*     STRING      '-57 17 44.806  12 34 56.7'      unchanged
*     IPTR        1                                16 (points to 12...)
*     A           ?                                -1.00000D0
*     J           ?                                0

*  Notes:
*     - The first three "fields" in STRING are degrees, arcminutes,
*       arcseconds, separated by spaces or commas.  The degrees field
*       may be signed, but not the others.  The decoding is carried
*       out by the palDfltin routine and is free-format.
*     - Successive fields may be absent, defaulting to zero.  For
*       zero status, the only combinations allowed are degrees alone,
*       degrees and arcminutes, and all three fields present.  If all
*       three fields are omitted, a status of +1 is returned and A is
*       unchanged.  In all other cases A is changed.
*     - Range checking:
*
*           The degrees field is not range checked.  However, it is
*           expected to be integral unless the other two fields are absent.
*
*           The arcminutes field is expected to be 0-59, and integral if
*           the arcseconds field is present.  If the arcseconds field
*           is absent, the arcminutes is expected to be 0-59.9999...
*
*           The arcseconds field is expected to be 0-59.9999...
*
*     - Decoding continues even when a check has failed.  Under these
*       circumstances the field takes the supplied value, defaulting
*       to zero, and the result A is computed and returned.
*     - Further fields after the three expected ones are not treated
*       as an error.  The pointer IPOS is left in the correct state
*       for further decoding with the present routine or with palDfltin
*       etc. See the example, above.
*     - If STRING contains hours, minutes, seconds instead of degrees
*       etc, or if the required units are turns (or days) instead of
*       radians, the result A should be multiplied as follows:
*
*           for        to obtain    multiply
*           STRING     A in         A by
*
*           d ' "      radians      1       =  1.0
*           d ' "      turns        1/2pi   =  0.1591549430918953358
*           h m s      radians      15      =  15.0
*           h m s      days         15/2pi  =  2.3873241463784300365

*  History:
*     2012-03-08 (TIMJ):
*        Initial version from SLA/F using Fortran documentation
*        Adapted with permission from the Fortran SLALIB library.
*     2012-10-17 (TIMJ):
*        Fix range check on arcminute value.
*     {enter_further_changes_here}

*  Copyright:
*     Copyright (C) 1996 Rutherford Appleton Laboratory
*     Copyright (C) 2012 Science and Technology Facilities Council.
*     All Rights Reserved.

*  Licence:
*     This program is free software; you can redistribute it and/or
*     modify it under the terms of the GNU General Public License as
*     published by the Free Software Foundation; either version 3 of
*     the License, or (at your option) any later version.
*
*     This program is distributed in the hope that it will be
*     useful, but WITHOUT ANY WARRANTY; without even the implied
*     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
*     PURPOSE. See the GNU General Public License for more details.
*
*     You should have received a copy of the GNU General Public License
*     along with this program; if not, write to the Free Software
*     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
*     MA 02110-1301, USA.

*  Bugs:
*     {note_any_bugs_here}
*-
*/

#include "pal.h"
#include "palmac.h"
#include "pal1sofa.h"

#include <math.h>

void palDafin ( const char *string, int *ipos, double *a, int *j ) {

  int jd = 0;    /* Status for degree parsing */
  int jm = 0;    /* Status for arcmin parsing */
  int js = 0;    /* Status for arcsec parsing */
  int jf = 0;    /* Internal copy of status */
  double deg = 0.0;
  double arcmin = 0.0;
  double arcsec = 0.0;

  /* Decode degrees, arcminutes, arcseconds */
  palDfltin( string, ipos, &deg, &jd );
  if (jd > 1) {
    jf = -1;
  } else {

    palDfltin( string, ipos, &arcmin, &jm );
    if ( jm < 0 || jm > 1 ) {
      jf = -2;
    } else {

      palDfltin( string, ipos, &arcsec, &js );
      if (js < 0 || js > 1) {
        jf = -3;

      } else if (jd > 0) { /* See if combination of fields is credible */
        /* No degrees: arcmin, arcsec ought also to be absent */
        if (jm == 0) {
          /* Suspect arcmin */
          jf = -2;
        } else if (js == 0) {
          /* Suspect arcsec */
          jf = -3;
        } else {
          /* All three fields absent */
          jf = 1;
        }

      } else if (jm != 0 && js == 0) { /* Deg present: if arcsec present should have arcmin */
        jf = -3;

      /* Tests for range and integrality */
      } else if (jm == 0 && DINT(deg) != deg) { /* Degrees */
        jf = -1;

      } else if ( (js == 0 && DINT(arcmin) != arcmin) || arcmin >= 60.0 ) { /* Arcmin */
        jf = -2;

      } else if (arcsec >= 60.0) { /* Arcsec */
        jf = -3;
      }
    }
  }

  /* Unless all three fields absent, compute angle value */
  if (jf <= 0) {
    *a = PAL__DAS2R * ( 60.0 * ( 60.0 * fabs(deg) + arcmin) + arcsec );
    if ( jd < 0 ) *a *= -1.;
  }

  *j = jf;

}