import sys, os
import numpy as np; np.set_printoptions(threshold=sys.maxsize)
from netCDF4 import Dataset
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

#
# load netcdf file
#
nc_fid = Dataset('TC_information_NWP_jp.nc', 'r')

#
# get variables
#
lontc = nc_fid.variables['lontc'][:]
lattc = nc_fid.variables['lattc'][:]
yr = (nc_fid.variables['yr'][:]).astype(int)
mon = (nc_fid.variables['mon'][:]).astype(int)
day = (nc_fid.variables['day'][:]).astype(int)
hrs = (nc_fid.variables['hrs'][:]).astype(int)
life = (nc_fid.variables['life'][:]).astype(int)
pmin = (nc_fid.variables['pmin'][:]).astype(int)

# Array's dimension:
lft, idx = lontc.shape

# print ( "lon | lat | yyyy | mm | dd | hh" )
# for i in range(idx):
#   for j in range(life[i]):
#     print ( lontc[j,i], lattc[j,i], yr[j,i], mon[j,i], day[j,i], hrs[j,i] )

# Domain chosen:
m = Basemap(projection='merc',llcrnrlat=0,urcrnrlat=30,llcrnrlon=100,urcrnrlon=160,resolution='i')

#
# add grid lines
#
m.drawparallels(np.arange(-80., 81., 10.), labels=[1,0,0,0], fontsize=10, linewidth=0.75, color='gray', textcolor='k')
m.drawmeridians(np.arange(-180., 181., 10.), labels=[0,0,0,1], fontsize=10, linewidth=0.75, color='gray', textcolor='k')

#
# https://stackoverflow.com/questions/14280312/world-map-without-rivers-with-matplotlib-basemap
# https://docs.obspy.org/tutorial/code_snippets/basemap_plot_with_beachballs.html
#
m.drawcoastlines()
m.fillcontinents(color='wheat',lake_color='skyblue',zorder=2)
m.drawcountries(color='black',zorder=3)
m.drawmapboundary(fill_color='skyblue')

#
# change value of YYYY to plot all TCs in the year
#
YYYY = 2010 2013

for i in range(idx):
  if yr[0,i] == YYYY:
    lons = []
    lats = []
    for j in range(life[i]):
      lons.append(lontc[j,i])
      lats.append(lattc[j,i])

    x, y = m(lons,lats)
    m.plot(x,y,linewidth=1.0,color='black')

plt.title('TCs: '+str(YYYY), fontsize=10)

#
# display results on x-window
#
plt.show()

#
# save plot as format .pdf
#
# plt.savefig('TCs_'+str(YYYY)+'.pdf', bbox_inches="tight", pad_inches=.05)
