File: wwl.c

package info (click to toggle)
wwl 1.1-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36 kB
  • ctags: 2
  • sloc: ansic: 82; makefile: 33
file content (112 lines) | stat: -rw-r--r-- 2,739 bytes parent folder | download | duplicates (2)
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
/*
 * $Id: wwl.c,v 1.1 1998/05/19 14:20:49 root Exp $
 * Given two Maidenhead locators, calculates distance and azimuth.
 * (C) 1998 by IK0ZSN Mirko Caserta <ik0zsn@amsat.org>
 * This software is placed under the terms of the GNU General Public Licence.
 *
 * Compile with: gcc -lm -o wwl wwl.c
 *
 * $Log: wwl.c,v $
 * Revision 1.1  1998/05/19 14:20:49  root
 * Initial revision
 *
 */

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

int main (int argc, char **argv) {
	int l, p;
	double z, y, n, h, x, w, f, t, s, v, u, c, d, e, lx;
	char my_wwl[6], dx_wwl[6];

	if (argc != 3) {
		printf("Usage: wwl my_ww_locator dx_ww_locator\n");
		exit(1);
	}

	if (strlen(argv[1]) != 6) {
		printf("%s: not a valid locator\n", argv[1]);
		exit(1);
	}
	
	if (strlen(argv[2]) != 6) {
		printf("%s: not a valid locator\n", argv[2]);
		exit(1);
	}	
	
	strcpy(my_wwl, argv[1]);
	strcpy(dx_wwl, argv[2]);

	my_wwl[0] = toupper(my_wwl[0]);
	z = my_wwl[0] - 65;
	my_wwl[1] = toupper(my_wwl[1]);
	y = my_wwl[1] - 65;
	n = my_wwl[2] - 48;
	h = my_wwl[3] - 48;
	my_wwl[4] = toupper(my_wwl[4]);
	x = my_wwl[4] - 65;
	my_wwl[5] = toupper(my_wwl[5]);
	w = my_wwl[5] - 65;

	if (my_wwl[0] < 65 || my_wwl[0] > 88 ||
		my_wwl[1] < 65 || my_wwl[1] > 88 ||
		my_wwl[2] < 48 || my_wwl[2] > 57 ||
		my_wwl[3] < 48 || my_wwl[3] > 57 ||
		my_wwl[4] < 65 || my_wwl[4] > 88 ||
		my_wwl[5] < 65 || my_wwl[5] > 88 ) {
		printf("%s: not a valid locator\n", my_wwl);
		exit(1);
	}

	t = z * 20 - 180 + n * 2 + x / 12 + 1.0 / 24;
	t = t * 3.1415926 / 180; 
	s = y * 10 - 90 + h + w / 24 + 1.0 / 48;
	s = s * 3.1415926 / 180;

	dx_wwl[0] = toupper(dx_wwl[0]);
	z = dx_wwl[0] - 65;
	dx_wwl[1] = toupper(dx_wwl[1]);
	y = dx_wwl[1] - 65;
	n = dx_wwl[2] - 48;
	h = dx_wwl[3] - 48;
	dx_wwl[4] = toupper(dx_wwl[4]);
	x = dx_wwl[4] - 65;
	dx_wwl[5] = toupper(dx_wwl[5]);
	w = dx_wwl[5] - 65;

	if (dx_wwl[0] < 65 || dx_wwl[0] > 88 ||
		dx_wwl[1] < 65 || dx_wwl[1] > 88 ||
		dx_wwl[2] < 48 || dx_wwl[2] > 57 ||
		dx_wwl[3] < 48 || dx_wwl[3] > 57 ||
		dx_wwl[4] < 65 || dx_wwl[4] > 88 ||
		dx_wwl[5] < 65 || dx_wwl[5] > 88 ) {
		printf("%s: not a valid locator\n", dx_wwl);
		exit(1);
	}

	v = z * 20 - 180 + n * 2 + x / 12 + 1.0 / 24;
	v = v * 3.1415926 / 180;
	u = y * 10 - 90 + h + w / 24 + 1.0 / 48;
	u = u * 3.1415926 / 180;
	
	c = cos(s) * cos(u) * cos(v - t) + sin(s) * sin(u);
	d = 1 - c * c;
	if (sqrt(d) != 0) {
		e = (sin(u) - sin(s) * c) / (cos(s) * sqrt(d));
		if (e>1.0) e=1.0;
		if (e<-1.0) e=-1.0;
		lx = acos(e) * 180 / 3.1415926;
		l = (int) (lx + 0.5);
	} else { l = 0; }
	
	if (sin(v - t) < 0) { l = 360 - l; }
	
	p = (int) ((atan(sqrt(d) / c) * 6371.33) + .5);
	printf("qrb: %d kilometers, azimuth: %d degrees\n", p, l);

	return 0;
}