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 = response.read()
data = StringIO.StringIO(data)
# Read file
r = csv.DictReader(data)

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

# Loop to parse data into our variables
for row in r:

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

# 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
    radius = 6371 # km

    dlat = math.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    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))
    d = radius * c

    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/
T = pickle.load(open('/home/diego/PythonTutorials/SRTM30/topo.p','rb'))

# 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.scatter(dist2shore,depth,s=20,c=temp,marker='o', edgecolor='none')
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)')

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

# ---------------------------------------------------
# Make plot 2
fig2, ax2 = plt.subplots(1)
plt.scatter(dist,depth,s=20,c=temp,marker='o', edgecolor='none')
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)')

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s