Commit 52afd9a1 authored by Pierre Cazenave's avatar Pierre Cazenave
Browse files

Merge branch 'dev'

parents af003ec2 3c05aaf8
......@@ -1662,6 +1662,209 @@ class tappy(Util):
def print_node_factor_table(self):
pass
def TAPPY(data, noisy=False):
"""
Uses a slightly modified version of TAPPY which can be imported as a module
to perform the harmonic analysis. The returned arrays are identical in
format to those from runTAPPY (which calls tappy.py externally and reads in
the XML file generated by TAPPY).
Parameters
----------
data : ndarray
Array of [YYYY, MM, DD, hh, mm, ss, ZZ], where ZZ is time series data
(e.g. surface elevation, velocity components etc.).
Returns
-------
cName : list
Tidal constituent names.
cSpeed : list
Tidal constituent speeds (in degrees per hour).
cPhase : list
Tidal constituent phases (in degrees).
cAmplitude : list
Tidal constituent amplitudes (in metres).
cInference : list
Flag of whether the tidal constituent was inferred due to a
short time series for the given constituent (True/False).
"""
# Set up the bits needed for TAPPY. This is mostly lifted from
# tappy.py in the baker function "analysis" (around line 1721).
quiet = noisy
debug = False
outputts = False
outputxml = False
ephemeris = False
rayleigh = 1.0
print_vau_table = False
missing_data = 'ignore'
linear_trend = False
remove_extreme = False
zero_ts = None
filter = None
pad_filters = None
include_inferred = True
# Create a tappy object.
x = tappy.tappy(outputts=outputts,
outputxml=outputxml,
quiet=quiet,
debug=debug,
ephemeris=ephemeris,
rayleigh=rayleigh,
print_vau_table=print_vau_table,
missing_data=missing_data,
linear_trend=linear_trend,
remove_extreme=remove_extreme,
zero_ts=zero_ts,
filter=filter,
pad_filters=pad_filters,
include_inferred=include_inferred)
# Add the time series to the TAPPY object
x.dates = []
x.elevation = []
for v in data:
ttt = ([int(i) for i in v[:-1]])
x.dates.append(datetime.datetime(*ttt))
x.elevation.append(v[-1])
x.dates = np.asarray(x.dates)
x.elevation = np.asarray(x.elevation)
package = x.astronomic(x.dates)
(x.zeta, x.nu, x.nup, x.nupp, x.kap_p, x.ii, x.R, x.Q, x.T, x.jd, x.s, x.h, x.N, x.p, x.p1) = package
if rayleigh:
ray = float(rayleigh)
else:
ray = 1.0
(x.speed_dict, x.key_list) = x.which_constituents(len(x.dates),
package,
rayleigh_comp=ray)
x.constituents() # the analysis
# Format output to match that returned by run_TAPPY().
cName = []
cSpeed = []
cInference = []
cPhase = []
cAmplitude = []
# Fully resolved constituents
ndict = {}
for k in x.key_list:
ndict[k] = x.speed_dict[k]['speed']
klist = [i[0] for i in x.sortbyvalue(ndict)]
for i in klist:
cName.append(i)
cSpeed.append(x.speed_dict[i]['speed'] * tappy.rad2deg)
cPhase.append(x.phase[i])
cAmplitude.append(x.r[i])
cInference.append(False)
# Inferred constituents
ndict = {}
for k in x.inferred_key_list:
ndict[k] = x.tidal_dict[k]['speed']
klist = [i[0] for i in x.sortbyvalue(ndict)]
for i in klist:
cName.append(i)
cSpeed.append(x.tidal_dict[i]['speed'] * tappy.rad2deg)
cPhase.append(x.inferred_phase[i])
cAmplitude.append(x.inferred_r[i])
cInference.append(True)
return cName, cSpeed, cPhase, cAmplitude, cInference
def run_TAPPY(data, sparseDef=False, noisy=False, deleteFile=True, tappy='/usr/bin/tappy.py'):
"""
A simple wrapper to perform a harmonic analysis on the supplied data.
Input data format is YYYY, MM, DD, hh, mm, ss, ZZ as a numpy array.
TAPPY is called as follows:
tappy.py analysis --def_filename=sparse.def --outputxml=tempfile.xml --quiet tempinput.txt
The output XML file is parsed with parse_TAPPY_XML to return a series of
variables containing the analysis output. The input file tempinput.txt is
deleted once the analysis is complete, unless deleteFile is set to False,
in which case it is left where it is. To find it, pass noisy=True to be
given more verbose output.
By default, tappy.py is expected in /usr/bin. If yours lives elsewhere,
pass tappy='/path/to/tappy.py'.
The default sparse definition file is:
/users/modellers/pica/Data/proc/tides/sparse.def
Pass an alternate value to sparseDef to use a different one.
Parameters
----------
data : ndarray
Array of [YYYY, MM, DD, hh, mm, ss, ZZ], where ZZ is surface elevation.
sparseDef : str, optional
Path to a TAPPY definition file. The default is
/users/modellers/pica/Data/proc/tides/sparse.def.
noisy : bool, optional
Set to True to enable verbose output.
deleteFile : bool, optional
By default the output file created by TAPPY is deleted
automatically. Set to False to keep the file.
tappy : str, optional
Specify an alternate path to the TAPPY script.
Returns
-------
cName : list
Tidal constituent names.
cSpeed : list
Tidal constituent speeds (in degrees per hour).
cPhase : list
Tidal constituent phases (in degrees).
cAmplitude : list
Tidal constituent amplitudes (in metres).
cInference : list
Flag of whether the tidal constituent was inferred due to a
short time series for the given constituent.
"""
if not sparseDef:
sparseDef = '/users/modellers/pica/Data/proc/tides/sparse.def'
tFile = tempfile.NamedTemporaryFile(delete=deleteFile)
if noisy:
if not deleteFile:
print('Saving to temporary file {}...'.format(tFile.name))
else:
print('Saving to temporary file...', end=' ')
np.savetxt(tFile.name, data, fmt='%4i/%02i/%02i %02i:%02i:%02i %.3f')
if noisy:
print('done.')
print('Running TAPPY on the current station...', end=' ')
xFile = tempfile.NamedTemporaryFile()
subprocess.call([tappy, 'analysis', '--def_filename=' + sparseDef, '--outputxml=' + xFile.name, '--quiet', tFile.name])
[cName, cSpeed, cPhase, cAmplitude, cInference] = parse_TAPPY_XML(xFile.name)
if noisy:
print('done.')
return cName, cSpeed, cPhase, cAmplitude, cInference
if __name__ == '__main__':
@baker.command()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment