#!/usr/bin/env python
"""
`tethys-CANON-ESP-76.py`
========================

Deployment data script -- intended to provide summary data over an entire
deployment.

For this particular deployment, the summary script also serves as a draft
script for the investigation of the sharkbite event.

"""

import numpy as np
import scipy as sp
import matplotlib
import matplotlib.pyplot as plt

import okeanidanalysis as oa

### define the logfiles for this deployment
fns = [ '/mbari/LRAUV/tethys/missionlogs/2013/20130920_20131007/20130920T204656/201309202047_201309210606.mat',
        '/mbari/LRAUV/tethys/missionlogs/2013/20130920_20131007/20130921T060621/201309210606_201309252034.mat',
        '/mbari/LRAUV/tethys/missionlogs/2013/20130920_20131007/20130925T203433/201309252034_201309282307.mat',
        '/mbari/LRAUV/tethys/missionlogs/2013/20130920_20131007/20130928T230746/201309282307_201309301141.mat',
        '/mbari/LRAUV/tethys/missionlogs/2013/20130920_20131007/20130930T114115/201309301141_201310070703.mat',
        '/mbari/LRAUV/tethys/missionlogs/2013/20130920_20131007/20131007T070400/201310070704_201310071643.mat']
# TODO # programmatic way to achieve the goal above

### load all of the slates
slates = [oa.logs.OkeanidLog(fn) for fn in fns]

### set up a figure for the summary plots

fig = plt.figure(figsize = (6.5, 9))
axkw = dict(frameon = True)
#left, width = 0.075, 0.6
left, width = 0.3, 0.675
bottom = 0.1
pad = 0.005
bh = 0.09
### build list of axes from top down
depth_ax = fig.add_axes((left, bottom + 7*(pad + bh), width, bh*2), **axkw)
axkw.update(dict(sharex = depth_ax))
heading_ax = fig.add_axes((left, bottom + 6*(pad + bh), width, bh), **axkw)
pitch_ax = fig.add_axes((left, bottom + 5*(pad + bh), width, bh), **axkw)
roll_ax = fig.add_axes((left, bottom + 4*(pad + bh), width, bh), **axkw)
buoyancy_ax = fig.add_axes((left, bottom + 3*(pad + bh), width, bh), **axkw)
mass_ax = fig.add_axes((left, bottom + 2*(pad + bh), width, bh), **axkw)
control_surface_ax = fig.add_axes((left, bottom + pad + bh, width, bh), **axkw)
propeller_ax = fig.add_axes((left, bottom, width, bh), **axkw)

for s in slates: 
    s.plot_timeseries('depth', axes=depth_ax)
    s.plot_timeseries('platform_orientation', axes=heading_ax,
            convert=np.rad2deg)
    s.plot_timeseries('platform_pitch_angle', axes=pitch_ax, 
            convert=np.rad2deg)
    s.plot_timeseries('platform_roll_angle', axes=roll_ax,
            convert=np.rad2deg)
    s.plot_timeseries('platform_buoyancy_position', axes=buoyancy_ax)
    s.plot_timeseries('platform_mass_position', axes=mass_ax, 
            convert=oa.lib.make_multiplier(1e3))
    s.plot_timeseries('platform_elevator_angle', axes=control_surface_ax,
            convert=np.rad2deg)
    s.plot_timeseries('platform_propeller_rotation_rate', axes=propeller_ax)

ylkw = dict(fontsize='small', rotation='horizontal', horizontalalignment='right')
depth_ax.set_ylabel('depth [m]', **ylkw)
heading_ax.set_ylabel('heading [deg]', **ylkw)
pitch_ax.set_ylabel('pitch [deg]', **ylkw)
roll_ax.set_ylabel('roll [deg]', **ylkw)
buoyancy_ax.set_ylabel('buoyancy [?]', **ylkw)
mass_ax.set_ylabel('mass position [mm]', **ylkw)
control_surface_ax.set_ylabel('elevator angle [deg]', **ylkw)
propeller_ax.set_ylabel('prop rotation [rad/s]', **ylkw)


depth_ax.set_title('Tethys Deployment: CANON ESP 76', fontsize='small')
# depth_ax.invert_yaxis()
depth_ax.set_ylim([80, 0])
propeller_ax.set_ylim([0, 40]) # XXX # there's a spike 2013-10-03

for ax in fig.get_axes():
    ax.grid(True)
    plt.setp(ax.yaxis.get_majorticklabels(), fontsize='small')
#    try: ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize='small')
#    except: print 'uncaught exception for legend...'
for ax in fig.get_axes()[:-1]:
    plt.setp(ax.get_xticklabels(), visible=False)
plt.setp(propeller_ax.xaxis.get_majorticklabels(), rotation=60)
plt.setp(propeller_ax.xaxis.get_majorticklabels(), fontsize='small')
propeller_ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%Y-%m-%d")) # start w/ ISO8601 date only
### finally, don't forget to show the plot ###
plt.show()
