|
15 | 15 | import numpy as np
|
16 | 16 | from scipy import interpolate
|
17 | 17 |
|
18 |
| -from datetime import datetime |
| 18 | +import collections |
| 19 | +import datetime |
| 20 | +import numbers |
19 | 21 |
|
20 | 22 | import pdb
|
21 | 23 | import warnings
|
|
30 | 32 | 'Q1':{'index':8,'omega':6.495854e-05,'v0u':5.877717569}}
|
31 | 33 |
|
32 | 34 |
|
| 35 | +def _preprocess_time(time): |
| 36 | + """ |
| 37 | + See if we can massage time to be something useful |
| 38 | + """ |
| 39 | + print(time, type(time)) |
| 40 | + if not isinstance(time, collections.Iterable): |
| 41 | + time = [time] |
| 42 | + if isinstance(time[0], np.datetime64): |
| 43 | + return time |
| 44 | + if isinstance(time[0], datetime.datetime): |
| 45 | + return np.array([np.datetime64(d) for d in time]) |
| 46 | + if isinstance(time[0], numbers.Number): |
| 47 | + return np.array([np.datetime64(datetime.datetime.fromordinal(d)) |
| 48 | + for d in time]) |
| 49 | + raise TypeError('time must be np.datetime64, ' |
| 50 | + 'list of datetime objects, or ordinal floats') |
| 51 | + |
33 | 52 | def tide_pred(modfile, lon, lat, time, z=None,conlist=None):
|
34 | 53 | """
|
35 |
| - Performs a tidal prediction at all points in [lon,lat] at times in vector [time] |
36 |
| - time is numpy datetime64. |
| 54 | + Performs a tidal prediction at all points in [lon,lat] at times. |
| 55 | +
|
| 56 | +
|
| 57 | + time is numpy datetime64 or a list of datetime objects. |
37 | 58 | """
|
38 | 59 |
|
| 60 | + time = _preprocess_time(time) |
| 61 | + |
39 | 62 | # Read and interpolate the constituents
|
40 |
| - u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(modfile,lon,lat,z=z,conlist=conlist) |
| 63 | + u_re, u_im, v_re, v_im, h_re, h_im, omega, conlist = extract_HC(modfile, |
| 64 | + lon, lat, z=z, conlist=conlist) |
41 | 65 |
|
42 | 66 | # Initialise the output arrays
|
43 | 67 | sz = lon.shape
|
@@ -70,7 +94,7 @@ def tide_pred(modfile, lon, lat, time, z=None,conlist=None):
|
70 | 94 | # Calculate the time series
|
71 | 95 | tsec = (time.astype('datetime64[s]') -
|
72 | 96 | np.datetime64('1992-01-01', 's')).astype(float)
|
73 |
| - |
| 97 | + |
74 | 98 | h=np.zeros((nt,nx))
|
75 | 99 | u=np.zeros((nt,nx))
|
76 | 100 | v=np.zeros((nt,nx))
|
|
0 commit comments