python4oceanographers

Turning ripples into waves

t_tide for python

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]:
elev flag
datetime
1998-01-01 00:00:00 1.20 0
1998-01-01 01:00:00 1.43 0
1998-01-01 02:00:00 1.73 0
1998-01-01 03:00:00 2.03 0
1998-01-01 04:00:00 2.38 0
1998-01-01 05:00:00 2.54 0

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))
This would be a t_predic call.
-----------------------------------
date: Placeholder
nobs = 8760 
ngood = 8759 
record length (days) = 365.00
rayleigh criterion = 1.0

Phases at central time
x0= 0.000362, x trend= 0
var(x)=  0.155081612164    var(xp)=  0.122346542672    var(xres)=  0.0327350694918 

percent var predicted/var original= 78.9 

     tidal amplitude and phase with 95 % CI estimates
   tide      freq         amp     amp_err     pha     pha_err    snr
  SSA   0.0002282     0.0198     0.035    120.85    116.88      0.32
  MSM   0.0013098     0.0135     0.033    274.17    147.93      0.17
  MM    0.0015122     0.0209     0.035    229.01    113.08      0.36
  MSF   0.0028219     0.0366     0.040    196.42     65.06      0.85
  MF    0.0030501     0.0330     0.039    232.29     74.28      0.71
  ALP1  0.0343966     0.0010     0.003    319.35    199.34     0.077
  2Q1   0.0357064     0.0008     0.004    265.36    191.67     0.047
  SIG1  0.0359087     0.0002     0.003    188.80    256.27    0.0033
* Q1    0.0372185     0.0222     0.005     92.73     12.83        18
  RHO1  0.0374209     0.0057     0.005    296.04     54.80       1.5
* O1    0.0387307     0.0905     0.005    298.55      3.06   3.8e+02
  TAU1  0.0389588     0.0009     0.004    200.69    188.22     0.069
  BET1  0.0400404     0.0024     0.004    111.29    122.29      0.42
* NO1   0.0402686     0.0075     0.005    225.02     38.31       2.1
  CHI1  0.0404710     0.0041     0.004    113.67     77.12      0.93
* P1    0.0415526     0.0264     0.005    178.69     10.10        26
* K1    0.0417807     0.0584     0.005    157.06      5.04   1.6e+02
  PHI1  0.0420089     0.0010     0.003    198.04    190.33     0.094
* THE1  0.0430905     0.0069     0.005     23.52     44.28       2.1
  J1    0.0432929     0.0025     0.004     92.39    113.05      0.38
  SO1   0.0446027     0.0039     0.005     42.62     71.66      0.71
  OO1   0.0448308     0.0021     0.004    341.42    118.20      0.28
  UPS1  0.0463430     0.0017     0.004     92.80    136.01      0.23
  OQ2   0.0759749     0.0014     0.004    212.38    173.90      0.14
  EPS2  0.0761773     0.0035     0.005    337.74     76.06      0.53
* 2N2   0.0774871     0.0162     0.005      2.80     19.61       9.1
* MU2   0.0776895     0.0266     0.005    215.01     11.75        26
* N2    0.0789992     0.0660     0.005    201.18      4.43   1.5e+02
  NU2   0.0792016     0.0070     0.005     53.01     46.02         2
* M2    0.0805114     0.3840     0.006    322.18      0.74   4.5e+03
  MKS2  0.0807396     0.0075     0.005    205.63     45.25       1.9
  LDA2  0.0818212     0.0025     0.004     44.58    116.86      0.38
* L2    0.0820236     0.0289     0.005    296.10      9.79        33
* S2    0.0833333     0.2429     0.006    133.18      1.33   1.7e+03
* K2    0.0835615     0.0597     0.005    289.24      4.89   1.3e+02
  MSN2  0.0848455     0.0027     0.005    122.66    102.19      0.36
  ETA2  0.0850736     0.0033     0.005     82.01     85.23       0.5
* MO3   0.1192421     0.0217     0.007     82.76     17.86        11
* M3    0.1207671     0.0712     0.007    209.13      5.49        98
* SO3   0.1220640     0.0226     0.007     41.70     15.25        10
* MK3   0.1222921     0.0246     0.006      3.19     15.44        17
* SK3   0.1251141     0.0201     0.006    342.61     17.07        12
* MN4   0.1595106     0.0333     0.004     65.56      6.01        83
* M4    0.1610228     0.0607     0.003    302.02      3.70   3.2e+02
  SN4   0.1623326     0.0009     0.003     25.05    174.19      0.12
* MS4   0.1638447     0.0284     0.003    228.16      6.85        99
* MK4   0.1640729     0.0077     0.003     29.34     25.48       4.9
  S4    0.1666667     0.0032     0.003    162.83     66.33         1
  SK4   0.1668948     0.0014     0.003    228.34    146.30      0.25
* 2MK5  0.2028035     0.0030     0.002    267.40     38.07       2.7
  2SK5  0.2084474     0.0005     0.001    258.07    165.88      0.13
  2MN6  0.2400221     0.0015     0.001     76.59     46.26       1.8
* M6    0.2415342     0.0033     0.001    186.92     21.20       6.9
* 2MS6  0.2443561     0.0065     0.001     13.04     11.04        31
  2MK6  0.2445843     0.0013     0.001    190.48     54.98       1.4
* 2SM6  0.2471781     0.0030     0.001    220.15     24.80       6.4
* MSK6  0.2474062     0.0019     0.001     16.54     40.58       2.4
  3MK7  0.2833149     0.0014     0.001    332.98     56.94       1.2
* M8    0.3220456     0.0019     0.001     47.31     40.66       2.9

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

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

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://ocefpaf.github.io/.

Comments