summaryrefslogtreecommitdiff
path: root/getgrid.c
diff options
context:
space:
mode:
Diffstat (limited to 'getgrid.c')
-rw-r--r--getgrid.c61
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);
+
+}
+
+