This post is the result of an online conversation about tidal ellipses and how to compute them in Python. We will be using the same data from previous posts.
In [2]:
import pandas as pd
from datetime import datetime
fname = './data/15t30717.3f1'
cols = ['j', 'u', 'v', 'temp', 'sal', 'y', 'mn', 'd', 'h', 'mi']
df = pd.read_table(fname , delim_whitespace=True, names=cols)
dates = [datetime(*x) for x in
zip(df['y'], df['mn'], df['d'], df['h'], df['mi'])]
df.index = dates
df.drop(['y', 'mn', 'd', 'h', 'mi', 'j'], axis=1, inplace=True)
df.head()
Out[2]:
And now we can compute the tidal analysis using utide.
In [3]:
from utide._solve import solve
from matplotlib.dates import date2num
time = date2num(df.index.to_pydatetime())
kw = dict(lat=32, cnstit='auto', notrend=True, rmin=0.95, method='ols',
nodiagn=True, linci=True, conf_int=True)
coef = solve(time, df['u'].values, df['v'].values, **kw)
The two cells below just clean the results a little bit and show a table with the ellipse parameters.
In [4]:
import numpy as np
frq = coef['aux']['frq']
for k, v in coef.items():
if not isinstance(v, np.ndarray) or v.shape < frq.shape:
del coef[k]
In [5]:
df = pd.DataFrame.from_dict(coef)
df['frq'] = frq
index = [s.strip() for s in df['name']]
df['name'] = index
df = df.set_index('name').sort(['Lsmaj', 'Lsmin'], ascending=False)
df.head()
Out[5]:
For the figure we can use the Python translation of Zhigang Xu's tidal_ellipse MATLAB tools.
In [6]:
MM = df.ix['MM']
SEMA, SEMI = MM['Lsmaj'], MM['Lsmin']
PHA, INC = MM['g'], MM['theta']
ECC = SEMI / SEMA
And here is the plot!
In [7]:
import tidal_ellipse
tidal_ellipse.do_the_plot(SEMA, ECC, INC, PHA)
In [8]:
HTML(html)
Out[8]: