summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile13
-rw-r--r--README.md12
-rw-r--r--getgrid.c61
3 files changed, 82 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index fa58aa4..f9d958d 100644
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,11 @@
# gridcal - compute great-circle distance between two grid locators
.POSIX:
-SRC = gridcal.c
+SRC = gridcal.c getgrid.c
OBJ = ${SRC:.c=.o}
+PROG= gridcal getgrid
-all: options gridcal
+all: options gridcal getgrid
options:
@echo "-+- build options -+-"
@@ -15,11 +16,15 @@ options:
@echo "-+-+-+-+-+-+-+-+-+-+-"
gridcal: ${OBJ}
- $(CC) $(LDFLAGS) -o $@ $(OBJ) -lm
+ $(CC) $(LDFLAGS) -o $@ gridcal.o -lm
+
+getgrid: ${OBJ}
+ $(CC) $(LDFLAGS) -o $@ getgrid.o -lm
+
debug: clean
${MAKE} CFLAGS="${DEBUG}" all
clean:
- rm -f gridcal ${OBJ}
+ rm -f ${PROG} ${OBJ}
diff --git a/README.md b/README.md
index f9de941..86a4292 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,18 @@ The latter can be useful to check if a QSO with another ham radio
station qualifies for one of the thousand miles per Watt awards.
+`getgrid` -- compute the grid locator associated to lat-long coordinates
+========================================================================
+
+`getgrid` is a simple tool to compute the grid locator associated to a
+latitude/longitude pair. The coordinates must be in degrees.
+
+Usage: getgrid <lat> <lon>
+
+Example:
+
+ getgrid
+
Dependencies
============
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);
+
+}
+
+