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

Make upper level weather chart.

Usage: python3 upper_air_humidity.py --file <ncfile>

Author: Ryosuke Tomita
Date: 2022/01/07
"""
import argparse
from ncmagics import fetchtime, japanmap, meteotool


def parse_args() -> dict:
    """parse_args.
    set file path.

    Args:

    Returns:
        dict:
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--file", help="set ncfile.", type=str)
    p = parser.parse_args()
    args = {"file": p.file}
    return args


def output_name(ncfile: str, isobaric_surface: int) -> str:
    """output_name.

    Args:
        ncfile (str): ncfile
        isobaric_surface (int): isobaric_surface

    Returns:
        str:
    """
    date_time = fetchtime.fetch_time(ncfile)
    outname = (date_time + "_" + str(isobaric_surface))
    return outname


def main():
    """main.
    """
    args = parse_args()

    meteo_tool = meteotool.MeteoTools(args["file"])
    lat, lon = meteo_tool.get_lat_lon()
    isobaric_surface = (850, 500, 300)
    #label_upper = (30, 0)
    #lebel_min = (-30, -60)

    for i, pressure in enumerate(isobaric_surface):
        # get parameter
        temp_c = meteo_tool.get_parameter('t', isobaric_surface=pressure) - 273.15
        rh = meteo_tool.get_parameter('r', isobaric_surface=pressure)
        height_gpm = meteo_tool.get_parameter('gh', isobaric_surface=pressure)
        u_wind = meteo_tool.get_parameter('u', isobaric_surface=pressure)
        v_wind = meteo_tool.get_parameter('v', isobaric_surface=pressure)

        jp_map = japanmap.JpMap()
        jp_map.contour_plot(lon, lat, height_gpm)
        #jp_map.shade_plot(lon, lat, temp_c,
        #                  label="2m temperature ($^\circ$C)",
        #                  color_bar_label_max=label_upper[i],
        #                  color_bar_label_min=lebel_min[i],
        #                  color_map_type="temperature",
        #                  double_color_bar=True,)
        jp_map.shade_plot(lon, lat, rh,
                          label="relative humidity (%)",
                          color_bar_label_max=100,
                          color_bar_label_min=0,
                          color_map_type="gray",
                          double_color_bar=False,)
        jp_map.vector_plot(lon, lat, u_wind, v_wind,
                           vector_interval=5, vector_scale=10, mode="wind")
        #jp_map.gray_shade(lon, lat, rh,
        #                  label="relative humidity (%)",
        #                  color_bar_label_max=100,
        #                  color_bar_label_min=0,
        #                  )
        if pressure == 850:
            jp_map.color_line(lon, lat, temp_c, line_value=-6, color='#0000ff')
        if pressure == 500:
            jp_map.color_line(lon, lat, temp_c, line_value=-36, color='#b22222')
        outname = output_name(args["file"], pressure)
        print(outname)
        jp_map.save_fig(outname, str(pressure) + "hPa")


if __name__ == "__main__":
    main()

[((25, 13, 25, 38), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ({}, {}), '()', False, 'import argparse\n'), ((42, 16, 42, 44), 'ncmagics.fetchtime.fetch_time', 'fetchtime.fetch_time', ({(42, 37, 42, 43): 'ncfile'}, {}), '(ncfile)', False, 'from ncmagics import fetchtime, japanmap, meteotool\n'), ((52, 17, 52, 51), 'ncmagics.meteotool.MeteoTools', 'meteotool.MeteoTools', ({(52, 38, 52, 50): "args['file']"}, {}), "(args['file'])", False, 'from ncmagics import fetchtime, japanmap, meteotool\n'), ((66, 17, 66, 33), 'ncmagics.japanmap.JpMap', 'japanmap.JpMap', ({}, {}), '()', False, 'from ncmagics import fetchtime, japanmap, meteotool\n')]