# Plotting tidal ellipse with Utide

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)

Out[2]:
u v temp sal
1993-07-18 03:00:00 11.7 -1.3 28.3 29.2
1993-07-18 04:00:00 12.3 -4.5 28.1 29.4
1993-07-18 05:00:00 9.6 -5.3 27.6 31.0
1993-07-18 06:00:00 7.6 -2.3 27.2 32.4
1993-07-18 07:00:00 10.6 -2.0 27.2 32.5

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)

solve:
matrix prep ...
Solution ...
conf. int vls...
Done.



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)


Out[5]:
Lsmaj Lsmaj_ci Lsmin Lsmin_ci g g_ci theta theta_ci frq
name
MM 10.025156 1.330969 -1.472702 1.598556 357.242244 8.045043 28.903073 9.227543 0.001512
O1 4.120147 1.011807 -3.293878 0.175315 149.042548 39.399034 170.177282 31.926241 0.038731
MSF 3.638211 1.087993 -1.875688 0.567645 348.262650 23.515309 151.782141 17.031935 0.002822
K1 2.570776 1.049644 -1.988055 0.177977 87.852104 58.807203 9.605010 46.156704 0.041781
J1 2.115125 0.334725 -1.994584 0.944709 32.351296 232.821085 70.504177 243.696261 0.043293

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]:

This post was written as an IPython notebook. It is available for download or as a static html.