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
|
/**
*
* Compute the grid locator associated to a pair of latitude/longitude
* coordinates, given in degrees
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void latlon_to_grid(double lat, double lon, char *loc){
double q;
q = (int)((lon + 180)/20);
lon = (q==0? lon: fmod(lon+180, 20));
loc[0] = 'A' + q;
q = (int)((lat + 90)/10);
lat = (q==0? lat: fmod(lat+90, 10));
loc[1] = 'A' + q;
q = (int)(lon/2);
lon = (q==0? lon: fmod(lon, 2));
loc[2] = '0' + q;
q = (int)(lat);
lat = (q==0? lat: lat - q);
loc[3] = '0' + q;
q = (int)(lon*12);
loc[4] = 'A' + q;
q = (int)(lat*24);
loc[5] = 'A' + q;
loc[6] = '\0';
}
void usage(char s[]){
printf("Usage: get_grid <lat> <lon>\n");
exit(1);
}
int main (int argc, char *argv[]){
double lat, lon;
char loc[7];
if (argc < 3){
usage(argv[0]);
}
lat = atof(argv[1]);
lon = atof(argv[2]);
while (lat < -90) lat +=180;
while (lat > 90) lat -=180;
while (lon < -180) lon += 360;
while (lon > 180) lon -= 360;
//fprintf(stderr, "lat: %g lon: %g\n", lat, lon);
latlon_to_grid(lat, lon, loc);
printf("%s\n", loc);
}
|