Python > 自作外部ファイルのテストコードを作ってみた

```txt:動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 3.6.0 on virtualenv
```

関連 [geometry > 2地点でそれぞれ1度(緯度方向もしくは経度方向)ずれた時の距離 [km]](http://qiita.com/7of9/items/13a4716c86a0fffce664)
関連 [geometry > 2つの緯度経度から距離を求める > Haversine formula使用](http://qiita.com/7of9/items/4ea7356a8806eaa08d5e)

計算処理を外部ファイル化する過程でテストコードを作ろうと思った。

以下のようにした。

### テスト対象ファイル v0.2

```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__)

```

### テストファイル v0.1 

importの仕方があまり見栄え良くない。

```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()
```

### 使用例

```txt:実行
$ 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)
    
```

```txt:実行
$ 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

```