import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from exploringShipLogbooks.basic_utils import extract_logbook_data
from mpl_toolkits.basemap import Basemap

%matplotlib inline

"""
Explanation: Matplotlib basemap

http://matplotlib.org/basemap/

Import necessary packages
End of explanation
"""


logbook_data = extract_logbook_data('CLIWOC15.csv')

"""
Explanation: Extract data from the weather logbook
End of explanation
"""


logbook_data['LongitudeUnits'].value_counts()

def convert_longitude_to_360(logbook_data):
    degrees_180 = logbook_data['LongitudeUnits'] == '180 degrees' 
    grados_180 = logbook_data['LongitudeUnits'] == '180 GRADOS' 
    graden_180 = logbook_data['LongitudeUnits'] == '180 graden' 

    mask = [any(tup) for tup in zip(degrees_180, grados_180, graden_180)]
    
    logbook_data.loc[mask, 'Lon3'] = logbook_data.loc[mask, 'Lon3'] + 180
    logbook_data.loc[mask, 'LongitudeUnits']  = '360 degrees'
    
    return logbook_data

def convert_longitude_to_180(logbook_data):
    degrees_360 = logbook_data['LongitudeUnits'] == '360 degrees' 
    grados_360 = logbook_data['LongitudeUnits'] == '360 GRADOS' 

    mask = [any(tup) for tup in zip(degrees_360, grados_360)]
    
    logbook_data.loc[mask, 'Lon3'] = logbook_data.loc[mask, 'Lon3'] - 180
    logbook_data.loc[mask, 'LongitudeUnits']  = '180 degrees'
    
    return logbook_data

logbook_data = convert_longitude_to_180(logbook_data)
#logbook_data = convert_longitude_to_360(logbook_data)

"""
Explanation: Convert longitude units
End of explanation
"""


valid_locations = pd.notnull(logbook_data['Lat3']) & pd.notnull(logbook_data['Lon3'])

"""
Explanation: Find logs that contain latitude and longitude positions
End of explanation
"""


plt.figure(figsize=(20,10))
# setup Lambert Conformal basemap.
#m = Basemap(width=12000000,height=9000000,projection='lcc',
#            resolution='c',lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
m = Basemap(llcrnrlon=0,llcrnrlat=-80,urcrnrlon=360,urcrnrlat=80,projection='mill')
# draw a boundary around the map, fill the background.
# this background will end up being the ocean color, since
# the continents will be drawn on top.
m.drawmapboundary(fill_color='aqua')
# fill continents, set lake color same as ocean color.
m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
# label parallels on right and top
# meridians on bottom and left
parallels = np.arange(-81,81,10.)
# labels = [left,right,top,bottom]
m.drawparallels(parallels,labels=[False,True,True,False])
meridians = np.arange(10.,351.,20.)
m.drawmeridians(meridians,labels=[True,False,False,True])
# plot blue dot on Boulder, colorado and label it as such.
#lon, lat = -104.237, 40.125 # Location of Boulder
#logbook_data['Lon3'][logbook_data['LongitudeUnits'] == '180 degrees'] = logbook_data['Lon3'][logbook_data['LongitudeUnits'] == '180 degrees'] + 180
lon = np.array(logbook_data['Lon3'][valid_locations])
lat = np.array(logbook_data['Lat3'][valid_locations])
# convert to map projection coords.
# Note that lon,lat can be scalars, lists or numpy arrays.
xpt,ypt = m(lon,lat)
m.plot(xpt,ypt,'bo')  # plot a blue dot there
plt.show()


"""
Explanation: Plot these locations on a map

 The following results are not quite right. The input to matplotlib basemap is supposed to be -180 to 180 degrees
  latitude; however, this only shows up on half the map below. Since we are not using this method, we decided not to explore this problem further.
End of explanation
"""


