diff --git a/src/ecofocipy/math/haversine.py b/src/ecofocipy/math/haversine.py new file mode 100644 index 0000000..f007ddc --- /dev/null +++ b/src/ecofocipy/math/haversine.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# Haversine formula example in Python +# Author: Wayne Dyck + +import math +import numpy as np + +def distance(origin, destination): + lat1, lon1 = origin + lat2, lon2 = destination + radius = 6371 # km + + dlat = math.radians(lat2-lat1) + dlon = math.radians(lon2-lon1) + a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \ + * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2) + c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) + d = radius * c + + return d + +def nearest_point(origin, latpoints, lonpoints, grid='1d'): + + if grid == '1d': + dist = np.zeros((np.shape(latpoints)[0], np.shape(lonpoints)[0])) + + for i, lat in enumerate(latpoints): + for j, lon in enumerate(lonpoints): + dist[i,j] = distance(origin,[lat,lon]) + + lati, loni = np.where(dist == dist.min()) + return (dist.min(), latpoints[lati[0]], lonpoints[loni[0]], lati[0], loni[0] ) + + elif grid == '2d': + dist = np.zeros_like(latpoints) + + for i, latrow in enumerate(latpoints): + for j, lonrow in enumerate(latrow): + dist[i,j] = distance(origin,[latpoints[i,j],lonpoints[i,j]]) + + lati, loni = np.where(dist == dist.min()) + + return (dist.min(), latpoints[lati[0]][loni[0]], lonpoints[lati[0]][loni[0]], lati[0], loni[0] ) \ No newline at end of file diff --git a/tools/find_closest_ctd.py b/tools/find_closest_ctd.py index 7f6dc91..5e7a130 100644 --- a/tools/find_closest_ctd.py +++ b/tools/find_closest_ctd.py @@ -29,7 +29,6 @@ import mysql.connector # User defined -import io_utils.ConfigParserLocal as ConfigParserLocal import calc.haversine as sphered __author__ = "Shaun Bell"