In [2]:
import netCDF4
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import datetime as dt
import ipywidgets as widgets

In [3]:
url='http://geoport.whoi.edu/thredds/dodsC/usgs/data2/emontgomery/stellwagen/Data/FI14/10001whr-cal.nc'

In [4]:
nc = netCDF4.Dataset(url)
ncv = nc.variables

In [5]:
def convert_epic_time(ncv):
    """ convert EPIC time and time2 variables to datenum64 """
    t1 = np.array(ncv['time'][:] - 2440588, dtype='int64')*3600*24*1000
    t2 = np.array(ncv['time2'][:], dtype='int64')
    return pd.to_datetime(t1+t2,unit='ms')

In [6]:
# if we find a time2 variable, convert EPIC time and time2 variables to datetime64 object
if 'time2' in ncv.keys():
    dt = convert_epic_time(ncv)

In [7]:
dt[0]


Out[7]:
Timestamp('2014-02-07 14:51:29.999000')

In [8]:
dt.shape


Out[8]:
(2134L,)

In [9]:
ncv['press']


Out[9]:
<type 'netCDF4._netCDF4.Variable'>
int16 press(time, sample, lat, lon)
unlimited dimensions: 
current shape = (2134, 2048, 1, 1)
filling off

In [10]:
itime = 200

In [11]:
print(dt[itime])


2014-02-15 22:51:30

In [12]:
def my_plot(var_name='press',itime=0):
    var = ncv[var_name]
    plt.plot(var[itime,:,:,:].ravel())
    plt.title(dt[itime])

In [13]:
my_plot(var_name='press',itime=itime)



In [ ]:
int(dt.shape[0])

In [ ]:
widgets.interact(my_plot, itime=(0,int(dt.shape[0]),1), var_name=ncv.keys() )

In [ ]:
def button_plus(counter, w):
    counter.increment(+1)  
    
def button_minus(counter, w):
    counter.increment(-1)

In [ ]:
counter = Counter()

wplus = widgets.Button(description='>')
wplus.on_click(partial(button_plus, counter))
display(wplus)

wminus = widgets.Button(description='<')
wminus.on_click(partial(button_minus, counter))
display(wminus)

In [ ]:
counter.value

In [ ]: