Matplotlib basemap

Import necessary packages


In [1]:
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

Extract data from the weather logbook


In [2]:
logbook_data = extract_logbook_data('CLIWOC15.csv')


/Applications/miniconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py:2825: DtypeWarning: Columns (5,6,7,8,11,13,18,19,23,24,25,26,28,29,30,34,35,38,43,44,46,73,77,81,82,84,85,87,88,94,96,97,98,99,111,114,116,119,120,122,124,125,127,129,131,133,135,137,140) have mixed types. Specify dtype option on import or set low_memory=False.
  if self.run_code(code, result):

Convert longitude units


In [3]:
logbook_data['LongitudeUnits'].value_counts()


Out[3]:
180 degrees    143690
360 degrees     73986
Unknown         40121
360 GRADOS      18605
180 GRADOS       2332
180 graden        849
unknown           166
DESCONOCIDO        77
Name: LongitudeUnits, dtype: int64

In [4]:
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

In [5]:
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

In [8]:
logbook_data = convert_longitude_to_180(logbook_data)
#logbook_data = convert_longitude_to_360(logbook_data)

Find logs that contain latitude and longitude positions


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

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.

In [10]:
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()



In [ ]: