From e2a1fac9d016fb835446135ba1d6f439b6dc0fff Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Fri, 7 Oct 2022 12:31:03 +0100 Subject: add getgrid.c and amend README.md accordingly --- getgrid.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 getgrid.c (limited to 'getgrid.c') 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 +#include +#include + +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 \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); + +} + + -- cgit v1.2.3