-
Notifications
You must be signed in to change notification settings - Fork 31
/
tidalfit.py
347 lines (291 loc) · 11.5 KB
/
tidalfit.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'''
A class that offers a fit on a time series.
Written by R. Jolivet, June 2014.
'''
import numpy as np
import pyproj as pp
import datetime as dt
import matplotlib.pyplot as plt
import scipy.interpolate as sciint
import sys
class tidalfit(object):
def __init__(self, constituents='all', linear=False, steps=None, cossin=False, verbose=True):
'''
Initialize a tidalfit object.
Args:
* constituents : List of constituents to use. Can be 'all'.
* linear : Include a linear trend (default is False).
* steps : List of datetime instances to add step functions in the fit.
* cossin : Just add a cos and sin term to estimate
'''
# print
if verbose:
print ("---------------------------------")
print ("---------------------------------")
print ("Initialize a tidal fit object")
# Tidal Periods in hours (source: wikipedia)
self.tidePeriodDict = {'M2' : 12.4206012, # Principal Lunar
'S2' : 12.0, # Principal Solar
'N2' : 12.65834751, # Lunar elliptic
'v2' : 12.62600509, # Larger Lunar evectional
'MU2': 12.8717576, # Variational
'2N2': 12.90537297, # Lunar elliptical semidiurnal 2nd order
'Lambda2': 12.22177348, # Smaller lunar evectional
'T2' : 12.01644934, # Larger Solar elliptic
'R2' : 11.98359564, # Smaller Solar elliptic
'L2' : 12.19162085, # Smalle lunar elliptic semidiurnal
'K2' : 11.96723606, # Lunisolar
'K1' : 23.93447213, # Lunisolar
'O1' : 25.81933871, # Principal Lunar
'OO1': 22.30608083, # Lunar diurnal
'S1' : 24.00000000, # Solar diurnal
'M1' : 24.84120241, # Smaller lunar elliptic diurnal
'J1' : 23.09848146, # Smaller lunar elliptic diurnal
'Rho': 26.72305326, # Larger lunar evectional diurnal
'P1' : 24.06588766, # Principal Solar
'Q1' : 26.868350, # Elliptic Lunar
'2Q1': 28.00621204, # Larger elliptic diurnal
'Mf' : 327.8599387, # Fortnightly
'Msf': 354.3670666, # Lunisolar synodic fortnightly
'Mm' : 661.3111655, # Monthly
'Ssa': 4383.076325, # Solar semiannual
'Sa' : 8766.15265 } # Solar annual
# What periods do we want
if type(constituents) is str:
if constituents in ('All', 'all', 'ALL'):
constituents = self.tidePeriodDict.keys()
self.constituents = constituents
# Constituents
if cossin:
self.constituents.append('cossin')
self.tidePeriodDict['cossin'] = 1.0
# Store things
self.steps = steps
self.linear = linear
# All done
return
def doFit(self, timeseries, tZero=dt.datetime(2001, 1, 1), chunks=None):
'''
Performs the fit on the chunks of data specified in chunks.
Args:
* timeseries: Timeseries instance.
* tZero : Sets the origin time (datetime instance).
* chunks : if not None, provide a list: [[start1, end1], [start2, end2], ...[startn, endn]].
if None, takes all the data.
'''
# Sets tZero
self.tZero = tZero
# Build G and data
self.buildG(timeseries, chunks=chunks, linear=self.linear, steps=self.steps, constituents=self.constituents)
# Solve
m, res, rank, s = np.linalg.lstsq(self.G, self.data, rcond=1e-8)
self.m = m
self.res = res
self.rank = rank
self.s = s
# Save Linear and Steps
self.Offset = m[0]
iP = 1
if self.linear:
self.Linear = m[1]
iP += 1
if self.steps is not None:
self.Steps = []
for step in self.steps:
self.Steps.append(m[iP])
iP += 1
# Save Constituents
self.Constituents = {}
for constituent in self.constituents:
self.Constituents[constituent] = m[iP:iP+2]
iP += 2
# All done
return
def buildG(self, timeseries, chunks=None, linear=False, steps=None, constituents='all', derivative=False):
'''
Builds the G matrix we will invert.
Args:
* timeseries : Timeseries instance.
* chunks : List of chunks of dates [[start1, end1], [start2, end2], ...[startn, endn]].
* linear : True/False.
* steps : List of datetime to add steps.
* constituents : List of consituents.
'''
# Get things
time = timeseries.time
value = timeseries.value
tZero = self.tZero
# What periods do we want
if type(constituents) is str:
if constituents in ('All', 'all', 'ALL'):
constituents = self.tidePeriodDict.keys()
# Check
if derivative:
steps = None
# Parameters
nTide = 2*len(constituents)
nStep = 0
if steps is not None:
nStep = len(steps)
nLin = 0
if linear:
nLin = 1
if derivative:
add = 0
else:
add = 1
self.nParam = add + nTide + nStep + nLin
# Get the data indexes
if chunks is None:
u = range(len(time))
else:
u = []
for chunk in chunks:
u1 = np.flatnonzero(np.array(time)>=chunk[0])
u2 = np.flatnonzero(np.array(time)<=chunk[1])
uu = np.intersect1d(u1, u2)
u.append(uu)
u = np.array(u).flatten().tolist()
# How many data
nData = len(u)
self.nData = nData
# Initialize G
G = np.zeros((self.nData, self.nParam))
# Build time and data vectors
time = time[u]
Tvec = np.array([(time[i]-tZero).total_seconds()/(60.*60.*24.) for i in range(time.shape[0])]) # In Days
self.data = value[u]
# Constant term
if not derivative:
G[:,0] = 1.0
iP = 1
else:
iP = 0
# Linear?
if linear:
if not derivative:
G[:,iP] = Tvec
else:
G[:,iP] = 1.0
iP += 1
# Steps?
if steps is not None:
self.steps = steps
for step in steps:
sline = np.zeros((self.data.shape[0],))
p = np.flatnonzero(np.flatnonzero(time)>=step)
sline[p] = 1.0
G[:,iP] = sline
iP += 1
# Constituents
periods = []
for constituent in constituents:
period = self.tidePeriodDict[constituent]/24.
if not derivative:
G[:,iP] = np.cos(2*np.pi*Tvec/period)
G[:,iP+1] = np.sin(2*np.pi*Tvec/period)
else:
G[:,iP] = -1.0*(2*np.pi/period)*np.sin(2*np.pi*Tvec/period)
G[:,iP+1] = (2*np.pi/period)*np.cos(2*np.pi*Tvec/period)
periods.append(period)
iP += 2
self.periods = periods
# Save G
if derivative:
self.Gderiv = G
else:
self.G = G
# All done
return
def predict(self, timeseries, constituents='all', linear=False, steps=True, cossin=False,
derivative=False):
'''
Given the results of the fit, this routine predicts the time series.
Args:
* timeseries : timeseries instance.
* constituents : List of constituents (default: 'all')
* linear : Include the linear trend (default: False).
* steps : Include the steps (default: False).
* cossin : Add a simple cos+sin term
* derivative : If True, stores results in timeseries.deriv,
else, stores results in timeseries.synth
'''
# If cossin
if cossin:
if type(constituents) is list:
if 'cossin' not in constituents:
constituents += ['cossin']
else:
constituents = [constituents, 'cossin']
# Build G
if steps:
steps = self.steps
else:
steps = None
self.buildG(timeseries, chunks=None, linear=linear, steps=steps,
constituents=constituents, derivative=derivative)
# Get the model vector
if derivative:
m = np.zeros((self.Gderiv.shape[1],))
else:
m = np.zeros((self.G.shape[1],))
# Offsets
if not derivative:
m[0] = self.Offset
iP = 1
else:
iP = 0
# Linear
if linear:
m[iP] = self.Linear
iP += 1
# Steps
if derivative:
steps = None
if steps is not None:
for step in self.Steps:
m[iP] = step
iP += 1
# Constituents
if type(constituents) is str:
if constituents in ('All', 'all', 'ALL'):
constituents = self.tidePeriodDict.keys()
for constituent in constituents:
m[iP:iP+2] = self.Constituents[constituent]
iP += 2
# Predict
if derivative:
timeseries.derivative = np.dot(self.Gderiv, m)
else:
timeseries.synth = np.dot(self.G, m)
# All done
return
def findPeaksTidalModel(self, timeseries, maximum=True, minimum=True):
'''
Returns all the local maximums (if True) and minimums (if True) in the modeled tidal signal.
'''
# Assert a few things
assert hasattr(self, 'Constituents'), 'Need a dictionary of constituents amplitudes'
# Get the constituents
constituents = self.constituents
# build G and its derivative
self.buildG(timeseries, constituents=constituents, linear=False, steps=None, derivative=True)
self.buildG(timeseries, constituents=constituents, linear=False, steps=None, derivative=False)
# Predict the signal and its derivative
self.predict(timeseries, constituents=constituents, linear=False, steps=False, derivative=True)
self.predict(timeseries, constituents=constituents, linear=False, steps=False, derivative=False)
# Find the zero crossing points
u = np.array(timeseries.findZeroIntersect(data='derivative'))
# Get the mean
mean = np.mean(timeseries.synth)
# Maximum are those above the mean (resp. Minimum, under)
M = np.flatnonzero(timeseries.synth[u]>mean)
m = np.flatnonzero(timeseries.synth[u]<mean)
# return
R = []
if maximum:
R.append(u[M])
if minimum:
R.append(u[m])
# All done
return R