File: fixpos.cpp

package info (click to toggle)
xtide 2.6.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,996 kB
  • ctags: 2,617
  • sloc: cpp: 26,266; ansic: 8,105; makefile: 152; yacc: 113; sh: 54; lex: 54
file content (318 lines) | stat: -rw-r--r-- 7,943 bytes parent folder | download
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
/*  fixpos/posfix  Conversion functions for lats and lons  */

/*****************************************************************************\

    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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.

\*****************************************************************************/



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>

#define NINT(a)   ((a)<0.0 ? (int) ((a) - 0.5) : (int) ((a) + 0.5))

/***************************************************************************\

    Module :        fixpos

    Programmer :    Jan C. Depner

    Date :          04/25/94

    Purpose :       This routine converts decimal degrees to degrees, minutes,
                    seconds, and hemisphere in the form specified by 'form'.

    Arguments:      degs        -   decimal degrees
                    type        -   "lat" or "lon"
                    form        -   "hdms", "hdm", "hd", "dms", "dm", or "d"
                    deg         -   degrees
                    min         -   minutes
                    sec         -   seconds
                    hem         -   hemisphere
                    
    Return Value:   char *      -   string containing the position in 'form'
                                    format

\***************************************************************************/

char *fixpos (double degs, double *deg, double *min, double *sec, char *hem,
char *c_type, char *c_form)
{
    double          abs_degs;
    static char     string[132];
    char            type[5], form[5];
    int             i;


    strcpy (type, c_type);
    strcpy (form, c_form);

    for (i = 0 ; i < strlen (type) ; i++) type[i] = tolower (type[i]);
    for (i = 0 ; i < strlen (form) ; i++) form[i] = tolower (form[i]);


    /*  Compute the degrees, minutes, seconds, and hemisphere.  */
    
    abs_degs = fabs (degs) * 3600.0;

    *sec = fmod (abs_degs, 60.0);
    abs_degs -= *sec;
    abs_degs /= 60.0;
    *min = fmod (abs_degs, 60.0);
    abs_degs -= *min;
    abs_degs /= 60.0;
    *deg = abs_degs;

    *deg = (int) (*deg + 0.00001);
    *min = (int) (*min + 0.00001);
    *sec += 0.000000001;

    if (*sec >= 60.0)
    {
        *min += 1.0;
        *sec -= 60.0;
    }

    i = NINT (*sec * 100.0);
    *sec = i / 100.0;

    if (*min >= 60.0)
    {
        *deg += 1.0;
        *min -= 60.0;
    }

    if (!strncmp (type, "lon", 3))
    {
        if (degs < 0.0)
        {
            *hem = 'W';
        }
        else
        {
            *hem = 'E';
        }
    }
    else
    {
        if (degs < 0.0)
        {
            *hem = 'S';
        }
        else
        {
            *hem = 'N';
        }
    }

    /*  Build the output string and reset the min, sec, and hem if needed.  */
    
    if (!strncmp (form, "hdms", 4))
    {
        sprintf (string, "%1c %d %02d %05.2lf", *hem, (int) *deg, (int) *min, 
            *sec);
    }
    else if (!strncmp (form, "hdm", 3))
    {
        *min += *sec / 60.0;
        *sec = 0.0;
        sprintf (string, "%1c %d %lf", *hem, (int) *deg, *min);
    }
    else if (!strncmp (form, "hd", 3))
    {
        *deg += *min / 60.0 + *sec / 3600.0;
        *min = 0.0;
        *sec = 0.0;
        sprintf (string, "%1c %lf", *hem, *deg);
    }
    else if (!strncmp (form, "dms", 3))
    {
        if (degs < 0.0)
        {
            *hem = '-';
        }
        else
        {
            *hem = ' ';
        }
        sprintf (string, "%1c %d %02d %05.2lf", *hem, (int) *deg, (int) *min, *sec);

        if (degs < 0.0)
        {
            if ((int) *deg)
            {
                *deg = -*deg;
            }
            else if ((int) *min)
            {
                *min = -*min;
            }
            else
            {
                *sec = -*sec;
            }
        }
    }
    else if (!strncmp (form, "dm", 3))
    {
        if (degs < 0.0)
        {
            *hem = '-';
        }
        else
        {
            *hem = ' ';
        }
        *min += *sec / 60.0;
        *sec = 0.0;
        sprintf (string, "%1c %d %lf", *hem, (int) *deg, *min + *sec / 60.0);

        if (degs < 0.0)
        {
            if ((int) *deg)
            {
                *deg = -*deg;
            }
            else
            {
                *min = -*min;
            }
        }
    }
    else if (!strncmp (form, "d", 3))
    {
        if (degs < 0.0)
        {
            *hem = '-';
        }
        else
        {
            *hem = ' ';
        }
        sprintf (string, "%1c %lf", *hem, fabs (degs));

        *deg = degs;
        *min = 0.0;
        *sec = 0.0;
    }

    return (string);
}





/***************************************************************************\

    Module :        posfix

    Programmer :    Jan C. Depner

    Date :          04/25/94

    Purpose :       This routine converts a string containing the latitude or
                    longitude in any of the following formats to decimal
                    degrees.

                        Hemisphere Degrees Minutes Seconds.decimal
                        Hemisphere Degrees Minutes.decimal
                        Hemisphere Degrees.decimal
                        SignDegrees Minutes Seconds.decimal
                        SignDegrees Minutes.decimal
                        SignDegrees.decimal

    Arguments:      *string     -   character string
                    *degs       -   degrees decimal
                    type        -   "lat" or "lon"
                    
    Return Value:   None        -   string containing the position in 'form'
                                    format

\***************************************************************************/

void posfix (char *string, double *degs, char *c_type)
{
    int         i, sign;
    double      fdeg, fmin, fsec, f1, f2, f3;
    char        type[5];


    strcpy (type, c_type);

    
    for (i = 0 ; i < strlen (type) ; i++) type[i] = tolower (type[i]);
    
    /*  Check for and clear sign or hemisphere indicators.          */
        
    sign = 0;
    for (i = 0 ; i < strlen (string) ; i++)
    {
        if (!strncmp (type, "lon", 3))
        {
            if (string[i] == 'W' || string[i] == 'w' || string[i] == '-')
            {
                string[i] = ' ';
                sign = 1;
            }
        }
        else
        {
            if (string[i] == 'S' || string[i] == 's' || string[i] == '-')
            {
                string[i] = ' ';
                sign = 1;
            }
        }

        if (string[i] == 'n' || string[i] == 'N' || string[i] == 'e' ||
            string[i] == 'E' || string[i] == '+') string[i] = ' ';
    }

    fdeg = 0.0;
    fmin = 0.0;
    fsec = 0.0;
    f1 = 0.0;
    f2 = 0.0;
    f3 = 0.0;

    /*  Convert the string to degrees, minutes, and seconds.        */
    
    i = sscanf (string, "%lf %lf %lf", &f1, &f2, &f3);

    /*  Based on the number of values scanned, compute the total    */
    /*  degrees.                                                    */
    
    switch (i)
    {
        case 3:
            fsec = f3 / 3600.0;

        case 2:
            fmin = f2 / 60.0;

        case 1:
            fdeg = f1;
    }

    fdeg += fmin + fsec;
    *degs = fdeg;
    if (sign) *degs = - *degs;
}