diff options
author | KatolaZ <katolaz@freaknet.org> | 2022-10-07 12:31:03 +0100 |
---|---|---|
committer | KatolaZ <katolaz@freaknet.org> | 2022-10-07 12:31:03 +0100 |
commit | e2a1fac9d016fb835446135ba1d6f439b6dc0fff (patch) | |
tree | b5f14bc0c51e6a38d38ffba376b2b7749ed4a542 /getgrid.c | |
parent | 3d7b4bbb3362533f1506b0948364b35eeb1b305b (diff) |
Diffstat (limited to 'getgrid.c')
-rw-r--r-- | getgrid.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/getgrid.c b/getgrid.c new file mode 100644 index 0000000..84ac26d --- /dev/null +++ b/getgrid.c @@ -0,0 +1,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); + +} + + |