Operating environment
Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 3.6.0 on virtualenv
Related geometry> Distance when deviated by 1 degree (latitude direction or longitude direction) at 2 points [km] Related geometry> Find the distance from two latitudes and longitudes> Use Haversine formula
I wanted to make a test code in the process of converting the calculation process to an external file.
I did the following.
UtilGeolocation.py
from math import sin, cos, radians, sqrt, asin
'''
v0.2 Mar 31, 2017
- add calc_distance_center_shift()
v0.1 Mar 31, 2017
- add get_shifted_position()
- add calc_distance_2places()
'''
# codingrule:PEP8
def calc_distance_2places(pos0, pos1, radius=None):
'''
distance based on Haversine formula
Ref: https://en.wikipedia.org/wiki/Haversine_formula
(pos0, pos1[, radius])
where
pos0 is (lat, lon) in [km]
pos1 is (lat, lon) in [km]
radius is in [km]
'''
if radius is None:
radius = 6378.137 # km (Earth's radius)
latang1, lonang1 = pos0
latang2, lonang2 = pos1
phi1, phi2 = radians(latang1), radians(latang2)
lam1, lam2 = radians(lonang1), radians(lonang2)
term1 = sin((phi2 - phi1) / 2.0) ** 2
term2 = sin((lam2 - lam1) / 2.0) ** 2
term2 = cos(phi1) * cos(phi2) * term2
wrk = sqrt(term1 + term2)
wrk = 2.0 * radius * asin(wrk)
return wrk
def get_shifted_position(pos0, shift_lat_deg=0.0, shift_lon_deg=0.0):
lat, lon = pos0
return (lat + shift_lat_deg, lon + shift_lon_deg)
def calc_distance_center_shift(center, shift):
'''
distance with shifted (latitude, longitude)
'''
clat, clon = center
slat, slon = shift
lat_shifted = (clat + slat, clon + 0.0)
dist_lat_km = calc_distance_2places(center, lat_shifted)
lon_shifted = (clat + 0.0, clon + slon)
dist_lon_km = calc_distance_2places(center, lon_shifted)
return (dist_lat_km, dist_lon_km)
#-------------------------------------------------
if __name__ == '__main__':
print("Help on calc_distance_2places:")
print(calc_distance_2places.__doc__)
print("Help on calc_distance_center_shift:")
print(calc_distance_center_shift.__doc__)
The import method doesn't look very good.
testGeolocation.py
from UtilGeolocation import calc_distance_2places
from UtilGeolocation import calc_distance_center_shift
from UtilGeolocation import get_shifted_position
'''
v0.1 Mar. 31, 2017
- add Test_calc_distance_center_shift()
- add Test_calc_distance_2places()
'''
# codingrule:PEP8
def Test_calc_distance_2places():
# at Location1
Osaka1 = 34.702113, 135.494807
print("Osaka:")
Osaka2 = get_shifted_position(Osaka1, shift_lat_deg=1.0)
print(" %.2f km for 1 deg latitude"
% calc_distance_2places(Osaka1, Osaka2))
Osaka2 = get_shifted_position(Osaka1, shift_lon_deg=1.0)
print(" %.2f km for 1 deg longitude"
% calc_distance_2places(Osaka1, Osaka2))
# at Location2
print("London:")
London1 = 51.476853, 0.0
London2 = get_shifted_position(London1, shift_lat_deg=1.0)
print(" %.2f km for 1 deg latitude"
% calc_distance_2places(London1, London2))
London2 = get_shifted_position(London1, shift_lon_deg=1.0)
print(" %.2f km for 1 deg longitude"
% calc_distance_2places(London1, London2))
def Test_calc_distance_center_shift():
Osaka1 = 34.702113, 135.494807
print("Osaka:")
dist = calc_distance_center_shift(Osaka1, shift=(1.0, 1.0))
print(" %.2f km, %.2f km" % dist)
if __name__ == '__main__':
Test_calc_distance_2places()
Test_calc_distance_center_shift()
Run
$ python UtilGeolocation.py
Help on calc_distance_2places:
distance based on Haversine formula
Ref: https://en.wikipedia.org/wiki/Haversine_formula
(pos0, pos1[, radius])
where
pos0 is (lat, lon) in [km]
pos1 is (lat, lon) in [km]
radius is in [km]
Help on calc_distance_center_shift:
distance with shifted (latitude, longitude)
Run
$ python testGeolocation.py
Osaka:
111.32 km for 1 deg latitude
91.52 km for 1 deg longitude
London:
111.32 km for 1 deg latitude
69.33 km for 1 deg longitude
Osaka:
111.32 km, 91.52 km
Recommended Posts