# coding: utf-8
"""
Name: monsoon_index.py

calcurate Monsoon index(MO指数).
Monsoon index is defined by the pressure difference
between irkutsk(N52.17, E104.16) to nemuro(N43.19, E145.34).

Usage: python3

Author: Ryosuke Tomita
Date: 2021/09/25
"""
import argparse
import numpy as np
from netCDF4 import Dataset


def parse_args():
    """User have to determine ambiguous cyclen center location by weather map and so on."""
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-f", "--file", help="Center of low pressure latitude", type=str)
    p = parser.parse_args()
    args = {"file": p.file}
    return args


class MonsoonIndex:
    """To calcurate monsoon_index,
    get nemuro and irkutsk lat and lon index and sub these pressure."""

    def __init__(self, ncfile):
        self.nc_vars = Dataset(ncfile)
        self.lat = ""
        self.lon = ""

    def get_target_location_index(self, target_lat, target_lon):
        """get [irkutsk] and [nemuro] 's lat lon index number."""
        self.lat = self.nc_vars.variables['latitude'][:]
        self.lon = self.nc_vars.variables['longitude'][:]
        target_lat_index = list(map(int, np.where(self.lat == target_lat)))[0]
        target_lon_index = list(map(int, np.where(self.lon == target_lon)))[0]
        target_prmsl = self.nc_vars.variables['prmsl'][0][target_lat_index][target_lon_index]
        return target_prmsl


def main():
    """
    First, get ncfile path using parse_args().
    Next, find nemuro an irkutsk location's prmsl value.
    Finally, cal monsoon_index.
    """
    ncfile = parse_args()['file']

    mo_index = MonsoonIndex(ncfile)

    irkutsk_prmsl = mo_index.get_target_location_index(52.0, 104.0)
    nemuro_prmsl = mo_index.get_target_location_index(43.0, 145.5)
    print(irkutsk_prmsl - nemuro_prmsl)


if __name__ == '__main__':
    main()
