In a previous post I showed how to call matlab's t_tide using oct2py. But that is in the past now! Thanks to Mitchell O'Flaherty-Sproul we do not need to wrap the matlab code with oct2py! Now we have a full python version: ttide_py. Here is a quick example, using the same data from the other post post.
In [2]:
from datetime import datetime
from pandas import read_table, DataFrame
def date_parser(year, day, month, hour):
year, day, month, hour = map(int, (year, day, month, hour))
return datetime(year, day, month, hour)
parse_dates = dict(datetime=['year', 'month', 'day','hour'])
names = ['year', 'month', 'day', 'hour', 'elev', 'flag']
obs = read_table('./data/can1998.dtf',
names=names,
skipinitialspace=True,
delim_whitespace=True,
index_col='datetime',
usecols=range(1, 7),
na_fvalues='9.990',
parse_dates=parse_dates,
date_parser=date_parser)
obs.head(6)
Out[2]:
The next cell just temove the mean and interpolates the flagged data points.
In [3]:
import numpy as np
import matplotlib.pyplot as plt
bad = obs['flag'] == 2
corrected = obs['flag'] == 1
obs[bad] = np.NaN
obs['anomaly'] = obs['elev'] - obs['elev'].mean()
obs['anomaly'] = obs['anomaly'].interpolate()
Note that ttide_py needs the input to be NumPy arrays.
In [4]:
from ttide.t_tide import t_tide
nameu, fu, tideconout, xout = t_tide(obs['anomaly'], dt=1, lat=np.array(-25))
In [5]:
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3, sharey=True, sharex=True, figsize=(13, 5))
ax0.plot(obs.index, obs['anomaly'], label=u'Observations')
ax0.legend(numpoints=1, loc='lower right')
ax1.plot(obs.index, xout.squeeze(), alpha=0.5, label=u'Prediction')
ax1.legend(numpoints=1, loc='lower right')
ax2.plot(obs.index, obs['anomaly']-xout.squeeze(), alpha=0.5, label=u'Residue')
_ = ax2.legend(numpoints=1, loc='lower right')
The module is in its early stages and needs a lot of love (docstrings, code formatting, handling the inputs and etc). I am already sending some.
In [6]:
HTML(html)
Out[6]: