1 
2 /*
3  * Very simple test program to check locator conversion against some other --SF
4  * This is mainly to test longlat2locator and locator2longlat functions.
5  *
6  * Takes at least two arguments, which is a locator and desired locater
7  * precision in pairs, e.g. EM19ov is three pairs.  precision is limited
8  * to >= 1 or <= 6.  If two locators are given, then the qrb is also
9  * calculated.
10  *
11  *
12  */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <hamlib/rig.h>
18 #include <hamlib/rotator.h>
19 
20 
main(int argc,char * argv[])21 int main(int argc, char *argv[])
22 {
23     char recodedloc[13], *loc1, *loc2, sign;
24     double lon1, lat1, lon2, lat2;
25     double distance, az, mmm, sec;
26     int  deg, min, retcode, loc_len, nesw = 0;
27 
28     if (argc < 2)
29     {
30         fprintf(stderr,
31                 "Usage: %s <locator1> <precision> [<locator2>]\n",
32                 argv[0]);
33         exit(1);
34     }
35 
36     loc1 = argv[1];
37     loc_len = argc > 2 ? atoi(argv[2]) : strlen(loc1) / 2;
38     loc2 = argc > 3 ? argv[3] : NULL;
39 
40     printf("Locator1:\t%s\n", loc1);
41 
42     /* hamlib function to convert maidenhead to decimal degrees */
43     retcode = locator2longlat(&lon1, &lat1, loc1);
44 
45     if (retcode != RIG_OK)
46     {
47         fprintf(stderr, "locator2longlat() failed with malformed input.\n");
48         exit(2);
49     }
50 
51     /* hamlib function to convert decimal degrees to deg, min, sec */
52     retcode = dec2dms(lon1, &deg, &min, &sec, &nesw);
53 
54     if (retcode != RIG_OK)
55     {
56         fprintf(stderr, "dec2dms() failed, invalid parameter address.\n");
57         exit(2);
58     }
59 
60     if (nesw == 1)
61     {
62         sign = '-';
63     }
64     else
65     {
66         sign = '\0';
67     }
68 
69     printf("  Longitude:\t%f\t%c%d %d' %.2f\"\n", lon1, sign, deg, min, sec);
70 
71     /* hamlib function to convert deg, min, sec to decimal degrees */
72     lon1 = dms2dec(deg, min, sec, nesw);
73     printf("  Recoded lon:\t%f\n", lon1);
74 
75     /* hamlib function to convert decimal degrees to deg decimal minutes */
76     retcode = dec2dmmm(lon1, &deg, &mmm, &nesw);
77 
78     if (retcode != RIG_OK)
79     {
80         fprintf(stderr, "dec2dmmm() failed, invalid parameter address.\n");
81         exit(2);
82     }
83 
84     if (nesw == 1)
85     {
86         sign = '-';
87     }
88     else
89     {
90         sign = '\0';
91     }
92 
93     printf("  GPS lon:\t%f\t%c%d %.3f'\n", lon1, sign, deg, mmm);
94 
95     /* hamlib function to convert deg, decimal min to decimal degrees */
96     lon1 = dmmm2dec(deg, mmm, nesw, 0.0);
97     printf("  Recoded GPS:\t%f\n", lon1);
98 
99     /* hamlib function to convert decimal degrees to deg, min, sec */
100     retcode = dec2dms(lat1, &deg, &min, &sec, &nesw);
101 
102     if (retcode != RIG_OK)
103     {
104         fprintf(stderr, "dec2dms() failed, invalid parameter address.\n");
105         exit(2);
106     }
107 
108     if (nesw == 1)
109     {
110         sign = '-';
111     }
112     else
113     {
114         sign = '\0';
115     }
116 
117     printf("  Latitude:\t%f\t%c%d %d' %.2f\"\n", lat1, sign, deg, min, sec);
118 
119     /* hamlib function to convert deg, min, sec to decimal degrees */
120     lat1 = dms2dec(deg, min, sec, nesw);
121     printf("  Recoded lat:\t%f\n", lat1);
122 
123     /* hamlib function to convert decimal degrees to deg decimal minutes */
124     retcode = dec2dmmm(lat1, &deg, &mmm, &nesw);
125 
126     if (retcode != RIG_OK)
127     {
128         fprintf(stderr, "dec2dmmm() failed, invalid parameter address.\n");
129         exit(2);
130     }
131 
132     if (nesw == 1)
133     {
134         sign = '-';
135     }
136     else
137     {
138         sign = '\0';
139     }
140 
141     printf("  GPS lat:\t%f\t%c%d %.3f'\n", lat1, sign, deg, mmm);
142 
143     /* hamlib function to convert deg, decimal min to decimal degrees */
144     lat1 = dmmm2dec(deg, mmm, nesw, 0.0);
145     printf("  Recoded GPS:\t%f\n", lat1);
146 
147     /* hamlib function to convert decimal degrees to maidenhead */
148     retcode = longlat2locator(lon1, lat1, recodedloc, loc_len);
149 
150     if (retcode != RIG_OK)
151     {
152         fprintf(stderr, "longlat2locator() failed, precision out of range.\n");
153         exit(2);
154     }
155 
156     printf("  Recoded:\t%s\n", recodedloc);
157 
158     if (loc2 == NULL)
159     {
160         exit(0);
161     }
162 
163     /* Now work on the second locator */
164     printf("\nLocator2:\t%s\n", loc2);
165 
166     retcode = locator2longlat(&lon2, &lat2, loc2);
167 
168     if (retcode != RIG_OK)
169     {
170         fprintf(stderr, "locator2longlat() failed with malformed input.\n");
171         exit(2);
172     }
173 
174     /* hamlib function to convert decimal degrees to deg, min, sec */
175     retcode = dec2dms(lon2, &deg, &min, &sec, &nesw);
176 
177     if (retcode != RIG_OK)
178     {
179         fprintf(stderr, "dec2dms() failed, invalid parameter address.\n");
180         exit(2);
181     }
182 
183     if (nesw == 1)
184     {
185         sign = '-';
186     }
187     else
188     {
189         sign = '\0';
190     }
191 
192     printf("  Longitude:\t%f\t%c%d %d' %.2f\"\n", lon2, sign, deg, min, sec);
193 
194     /* hamlib function to convert deg, min, sec to decimal degrees */
195     lon2 = dms2dec(deg, min, sec, nesw);
196     printf("  Recoded lon:\t%f\n", lon2);
197 
198     /* hamlib function to convert decimal degrees to deg decimal minutes */
199     retcode = dec2dmmm(lon2, &deg, &mmm, &nesw);
200 
201     if (retcode != RIG_OK)
202     {
203         fprintf(stderr, "dec2dmmm() failed, invalid parameter address.\n");
204         exit(2);
205     }
206 
207     if (nesw == 1)
208     {
209         sign = '-';
210     }
211     else
212     {
213         sign = '\0';
214     }
215 
216     printf("  GPS lon:\t%f\t%c%d %.3f'\n", lon2, sign, deg, mmm);
217 
218     /* hamlib function to convert deg, decimal min to decimal degrees */
219     lon2 = dmmm2dec(deg, mmm, nesw, 0.0);
220     printf("  Recoded GPS:\t%f\n", lon2);
221 
222     /* hamlib function to convert decimal degrees to deg, min, sec */
223     retcode = dec2dms(lat2, &deg, &min, &sec, &nesw);
224 
225     if (retcode != RIG_OK)
226     {
227         fprintf(stderr, "dec2dms() failed, invalid parameter address.\n");
228         exit(2);
229     }
230 
231     if (nesw == 1)
232     {
233         sign = '-';
234     }
235     else
236     {
237         sign = '\0';
238     }
239 
240     printf("  Latitude:\t%f\t%c%d %d' %.2f\"\n", lat2, sign, deg, min, sec);
241 
242     /* hamlib function to convert deg, min, sec to decimal degrees */
243     lat2 = dms2dec(deg, min, sec, nesw);
244     printf("  Recoded lat:\t%f\n", lat2);
245 
246     /* hamlib function to convert decimal degrees to deg decimal minutes */
247     retcode = dec2dmmm(lat2, &deg, &mmm, &nesw);
248 
249     if (retcode != RIG_OK)
250     {
251         fprintf(stderr, "dec2dmmm() failed, invalid parameter address.\n");
252         exit(2);
253     }
254 
255     if (nesw == 1)
256     {
257         sign = '-';
258     }
259     else
260     {
261         sign = '\0';
262     }
263 
264     printf("  GPS lat:\t%f\t%c%d %.3f'\n", lat2, sign, deg, mmm);
265 
266     /* hamlib function to convert deg, decimal min to decimal degrees */
267     lat2 = dmmm2dec(deg, mmm, nesw, 0.0);
268     printf("  Recoded GPS:\t%f\n", lat2);
269 
270     /* hamlib function to convert decimal degrees to maidenhead */
271     retcode = longlat2locator(lon2, lat2, recodedloc, loc_len);
272 
273     if (retcode != RIG_OK)
274     {
275         fprintf(stderr, "longlat2locator() failed, precision out of range.\n");
276         exit(2);
277     }
278 
279     printf("  Recoded:\t%s\n", recodedloc);
280 
281     retcode = qrb(lon1, lat1, lon2, lat2, &distance, &az);
282 
283     if (retcode != RIG_OK)
284     {
285         fprintf(stderr, "QRB error: %d\n", retcode);
286         exit(2);
287     }
288 
289     dec2dms(az, &deg, &min, &sec, &nesw);
290     printf("\nDistance: %.6fkm\n", distance);
291 
292     if (nesw == 1)
293     {
294         sign = '-';
295     }
296     else
297     {
298         sign = '\0';
299     }
300 
301     /* Beware printf() rounding error! */
302     printf("Bearing: %.2f, %c%d %d' %.2f\"\n", az, sign, deg, min, sec);
303 
304     exit(0);
305 }
306