# OTN Gliders (more advanced plotting)

Gliders are Autonomous Underwater Vehicles that carry several oceanographic instruments and allow unmanned ocean sampling. Below we use data from the OTN Ocean Glider Programme. Here we present “more advanced” code. We also showed a very basic plotting code HERE.

• Note that this particular glider adds a “zero” every time it starts a up-cast.
• Also note that the bathymetry will plot “funky” when the glider turns around and starts going back to shore (i.e. the only “black” region will be the area between the bathymetries of the “going-out” and “coming -in” portions of the transect).
```import csv
import numpy as np
import matplotlib.pyplot as plt
import datetime
import urllib2
import StringIO
import pickle

# Open file

response = urllib2.urlopen('http://glider.ceotr.ca/data/live/sci_water_temp_live.csv')

data = StringIO.StringIO(data)

# Initialize empty variables
date, lat, lon, depth, temp = [],[],[],[],[]

# Loop to parse data into our variables
for row in r:
date.append(float(row['unixtime']))
lat.append(float(row['lat']))
lon.append(float(row['lon']))
depth.append(float(row['depth']))
temp.append(float(row['sci_water_temp']))

# Change unix-time into a date object (for easier plotting)
DATE = []
for row in date:
DATE.append(datetime.datetime.fromtimestamp(row))

# Estimate "distance to shore" and "distance along transect ********************

# First, design a function to estimate great-circle distance
def distance(origin, destination):
#Source: http://www.platoscave.net/blog/2009/oct/5/calculate-distance-latitude-longitude-python/
import math
lat1, lon1 = origin
lat2, lon2 = destination

a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))

return d

# Compute distance to shore
DuncansCove = [44.501397222222224,-63.53186111111111]
dist2shore = np.zeros((np.size(lon)))
for i in range(np.size(lon)):
dist2shore[i] = distance(DuncansCove, [lat[i], lon[i]])

# Compute distance along transect
dist = np.zeros((np.size(lon)))
for i in range(2,np.size(lon)):
dist[i] = dist[i-1] + distance([lat[i-1], lon[i-1]], [lat[i], lon[i]])

# Estimate bathymetry **********************************************************
# Load bathymetry file created here: https://oceanpython.org/2013/03/21/bathymetry-topography-srtm30/

# Compute bathymetry
bathy = np.zeros((np.size(lon)))
for i in range(np.size(lon)):
cost_func = ((T['lons']-lon[i])**2) + ((T['lats']-lat[i])**2)
xmin, ymin = np.unravel_index(cost_func.argmin(), cost_func.shape)
bathy[i] = -T['topo'][xmin, ymin]

# ---------------------------------------------------
# Make plot 1
fig1, ax1 = plt.subplots(1)
plt.fill_between(dist2shore,bathy,1000,color='k')
plt.scatter(dist2shore,depth,s=20,c=temp,marker='o', edgecolor='none')
plt.ylim((-0.5,max(depth)+5))
ax1.set_ylim(ax1.get_ylim()[::-1])
ax1.set_xlim(min(dist2shore),max(dist2shore))
cbar = plt.colorbar(orientation='horizontal', extend='both')
cbar.ax.set_xlabel('Temperature (\$^\circ\$C)')
plt.title('OTN Glider transect')
plt.ylabel('Depth (m)')
plt.xlabel('Distance form shore (km)')
plt.show()

# Save figure (without 'white' borders)
plt.savefig('glider_Dist2Shore.png', bbox_inches='tight')

# ---------------------------------------------------
# Make plot 2
fig2, ax2 = plt.subplots(1)
plt.fill_between(dist,bathy,1000,color='k')
plt.scatter(dist,depth,s=20,c=temp,marker='o', edgecolor='none')
plt.ylim((-0.5,max(depth)+5))
ax2.set_ylim(ax2.get_ylim()[::-1])
ax2.set_xlim(min(dist),max(dist))
cbar = plt.colorbar(orientation='horizontal', extend='both')
cbar.ax.set_xlabel('Temperature (\$^\circ\$C)')
plt.title('OTN Glider transect')
plt.ylabel('Depth (m)')
plt.xlabel('Distance along transect (km)')
plt.show()

# Save figure (without 'white' borders)
plt.savefig('glider_DistTransect.png', bbox_inches='tight')
```