-
Notifications
You must be signed in to change notification settings - Fork 1
/
Oban_demo.py
148 lines (117 loc) · 4.43 KB
/
Oban_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Oban_demo.py
Harmonic reconstruction.
Reconstructs for a vector of times.
Usage: python Oban_demo.py
Author: [email protected]
Date: 1 Oct 2019
"""
# For point reconstruction
import numpy as np
import math # atan2
import pandas as pd
import datetime
from os import path # fix the path
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from NOCtidepred import UtcNow
from NOCtidepred import date2mjd
from NOCtidepred import phamp0fast
from NOCtidepred import set_names_phases
##########################################################################
def get_port():
"""
Obtain a reduced number of Gladstone harmonics from a file
Returns: lat, lon [floats]
z0 (ref height) [float]
data (amp, pha, doo, labels) [pandas dataframe]
$ head glad.txt
Port Name: ENGLAND, WEST COAST $ LIVERPOOL (GLADSTONE DOCK)
53 27.0 N 03 01.1 W
z0= 5.249 OD= -4.930
3.03800 320.72000 31 M2
0.97800 4.70000 36 S2
...
"""
fname = "Oban.txt"
data = pd.read_csv(fname, header=3, delimiter=r"\s+")
data.columns = ['amp','pha', 'doo']
lat = +(56+25/60)
lon = -( 5+29/60)
z0 = +2.402
return lat,lon, z0, data
##########################################################################
def test_port(mjd):
"""
Demonstration to load, reconstruct and plot port data.
"""
rad = np.pi / 180.
# Obtain data
lat, lon, z0, data = get_port()
ha = data['amp'][:]
ga = data['pha'][:]
doo = data['doo'][:]
nh = len(ha)
# Initialise output variable
npred = np.shape(mjd)[0] # vector: [npred]
pred = np.zeros(npred)
# Get the nodal corrections
mjdns = np.floor(mjd) # vector: [npred]
hrs = 24 * (mjd - mjdns) # hrs are the time variable required for the reconstruction. vector: [npred]
[f,v] = phamp0fast(mjdns) # [npred,120]
print('shape of f,v {}'.format(np.shape(f)))
print('shape of hrs time series {}'.format(np.shape(hrs)))
# load the phase speeds and harmonic names for 120 harmonics
names, sig0 = set_names_phases()
#
# Sum constant + constituents to give prediction
#
for j in range(nh):
"""
Need to match up input harmonics with tabulated harmonic. Can pair by names,
or by doodson number (i.e. index). The former is preferable but there are
issues with spelling conventions.
"""
#k = names.index(lab[j].upper()) # This is the index of the harmonic from vector sig0, of speeds
k = doo[j]-1 # offset since indexing starts from zero
## Port
pred = pred + ha[j] * f[:,k] * np.cos(rad * ( sig0[k]*hrs + v[:,k] - ga[j] ))
## Map
# pred = pred + ha(j+1) * f[:,k] * np.cos(rad * ( sig0[k]*hrs + v[:,k] - ga[j] ))
print('shape of prediction {}'.format(np.shape(pred)))
ssh = pred + z0
print('prediction values: {}'.format(pred))
return ssh
##################################################################################
##################################################################################
## Now do the main routine stuff
##################################################################################
##################################################################################
if __name__ == '__main__':
# Settings
rad = np.pi/180
deg = 1.0 / rad
startTime = datetime.datetime.now() # For a timing test
# Set the dates
# Create a vector of predictions times. E.g. 24 hourly instants.
startdate = UtcNow()
npred = 24
dates = [startdate + datetime.timedelta(hours=hh) for hh in range(0, npred)]
mjd = date2mjd( dates ) # convert to modified julian dates
## Compute reconstuction on port data.
#####################################
ssh = test_port(mjd) # reconstuct ssh for the time vector
print('plot time series reconstruction of port data')
ssh = np.ma.masked_where( ssh > 1E6, ssh) # get rid of nasties
# Plot sea level time series
fig, ax = plt.subplots()
ax.plot(np.array(dates),[ssh[i] for i in range(len(dates))],'+-')
ax.set_ylabel('Height (m)')
ax.set_xlabel('Hours since '+dates[0].strftime("%Y-%m-%d"))
ax.set_title('Oban tide prediction')
# Pain plotting time on the x-axis
myFmt = mdates.DateFormatter('%H')
ax.xaxis.set_major_formatter(myFmt)
plt.show()