From 7bf1716ed0a3597c48bd02d0cb542c527685d0ea Mon Sep 17 00:00:00 2001 From: joelridden Date: Fri, 10 May 2024 11:21:59 +1200 Subject: [PATCH 1/3] custom filename --- qcore/timeseries.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/qcore/timeseries.py b/qcore/timeseries.py index 236fb3e1..37fd7805 100644 --- a/qcore/timeseries.py +++ b/qcore/timeseries.py @@ -237,6 +237,7 @@ def seis2txt( prefix, stat, comp, + filename=None, start_hr=0, start_min=0, start_sec=0.0, @@ -253,6 +254,7 @@ def seis2txt( prefix: filename excluding station name and extention, None to return contents as byte array. stat: station name comp: same as file extention ('090', '000', 'ver') + filename: None to use stat and comp, otherwise use this start_hr: start time (hours, generally not used) start_min: start time (minutes, generally not used) start_sec: start time (seconds) @@ -266,7 +268,10 @@ def seis2txt( if prefix is None: txt = BytesIO() else: - txt = open("%s%s.%s" % (prefix, stat, comp), "wb") + if filename is not None: + txt = open(f"{prefix}/{filename}", "wb") + else: + txt = open("%s%s.%s" % (prefix, stat, comp), "wb") # same format strings as fdbin2wcc txt.write(("%-10s %3s %s\n" % (stat, comp, title)).encode()) From 020a243824b927dff76d6d73f38e682d4c565758 Mon Sep 17 00:00:00 2001 From: joelridden Date: Fri, 10 May 2024 12:32:56 +1200 Subject: [PATCH 2/3] timeseries_to_text --- qcore/timeseries.py | 98 +++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/qcore/timeseries.py b/qcore/timeseries.py index 37fd7805..286b5cbe 100644 --- a/qcore/timeseries.py +++ b/qcore/timeseries.py @@ -231,65 +231,67 @@ def pgv2MMI(pgv): ) -def seis2txt( - seis, - dt, - prefix, - stat, - comp, - filename=None, - start_hr=0, - start_min=0, - start_sec=0.0, - edist=0.0, - az=0.0, - baz=0.0, - title="", - vpl=6, +def timeseries_to_text( + timeseries: np.ndarray, + filename: Path, + dt: float, + stat: str, + comp: str, + values_per_line: int = 6, + start_hr: int = 0, + start_min: int = 0, + start_sec: float = 0.0, + edist: float = 0.0, + az: float = 0.0, + baz: float = 0.0, ): """ - Store timeseries data as standard EMOD3D text file {prefix}{stat}.{comp}. - seis: timeseries - dt: timestep - prefix: filename excluding station name and extention, None to return contents as byte array. - stat: station name - comp: same as file extention ('090', '000', 'ver') - filename: None to use stat and comp, otherwise use this - start_hr: start time (hours, generally not used) - start_min: start time (minutes, generally not used) - start_sec: start time (seconds) - edist: epicentre distance - az: generally not used - baz: generally not used - title: used in header - vpl: values per line, more is faster + Store timeseries data into a text file. + + Parameters + ---------- + timeseries : np.ndarray + The timeseries data to store + filename : Path + The full file path to store the file + dt : float + The time step of the data + stat : str + The station name + comp : str + The component name + values_per_line : int, optional + The number of values per line, by default 6 + start_hr : int, optional + The start hour of the data, by default 0 + start_min : int, optional + The start minute of the data, by default 0 + start_sec : float, optional + The start second of the data, by default 0.0 + edist : float, optional + The epicentral distance, by default 0.0 + az : float, optional + The azimuth forward A->B in degrees, by default 0.0 + baz : float, optional + The azimuth backwards B->A in degrees, by default 0.0 """ - nt = seis.shape[0] - if prefix is None: - txt = BytesIO() - else: - if filename is not None: - txt = open(f"{prefix}/{filename}", "wb") - else: - txt = open("%s%s.%s" % (prefix, stat, comp), "wb") + nt = timeseries.shape[0] + txt = open(filename, "wb") # same format strings as fdbin2wcc - txt.write(("%-10s %3s %s\n" % (stat, comp, title)).encode()) + txt.write(("%-10s %3s\n" % (stat, comp)).encode()) txt.write( ( "%d %12.5e %d %d %12.5e %12.5e %12.5e %12.5e\n" % (nt, dt, start_hr, start_min, start_sec, edist, az, baz) ).encode() ) - # values below header lines, vpl per line - divisible = nt - nt % vpl - np.savetxt(txt, seis[:divisible].reshape(-1, vpl), fmt="%13.5e") - np.savetxt(txt, np.atleast_2d(seis[divisible:]), fmt="%13.5e") - - if prefix is None: - return txt.getvalue() - else: - txt.close() + # values below header lines, split into lines + divisible = nt - nt % values_per_line + np.savetxt(txt, timeseries[:divisible].reshape(-1, values_per_line), fmt="%13.5e") + np.savetxt(txt, np.atleast_2d(timeseries[divisible:]), fmt="%13.5e") + + txt.close() ### From eb1231ec3e0813924c8c1ff8ca4ab9e71e5cc37c Mon Sep 17 00:00:00 2001 From: joelridden Date: Fri, 10 May 2024 13:40:10 +1200 Subject: [PATCH 3/3] using with --- qcore/timeseries.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/qcore/timeseries.py b/qcore/timeseries.py index 286b5cbe..caa8cd44 100644 --- a/qcore/timeseries.py +++ b/qcore/timeseries.py @@ -276,22 +276,21 @@ def timeseries_to_text( The azimuth backwards B->A in degrees, by default 0.0 """ nt = timeseries.shape[0] - txt = open(filename, "wb") - - # same format strings as fdbin2wcc - txt.write(("%-10s %3s\n" % (stat, comp)).encode()) - txt.write( - ( - "%d %12.5e %d %d %12.5e %12.5e %12.5e %12.5e\n" - % (nt, dt, start_hr, start_min, start_sec, edist, az, baz) - ).encode() - ) - # values below header lines, split into lines - divisible = nt - nt % values_per_line - np.savetxt(txt, timeseries[:divisible].reshape(-1, values_per_line), fmt="%13.5e") - np.savetxt(txt, np.atleast_2d(timeseries[divisible:]), fmt="%13.5e") - - txt.close() + with open(filename, "wb") as txt: + # same format strings as fdbin2wcc + txt.write(("%-10s %3s\n" % (stat, comp)).encode()) + txt.write( + ( + "%d %12.5e %d %d %12.5e %12.5e %12.5e %12.5e\n" + % (nt, dt, start_hr, start_min, start_sec, edist, az, baz) + ).encode() + ) + # values below header lines, split into lines + divisible = nt - nt % values_per_line + np.savetxt( + txt, timeseries[:divisible].reshape(-1, values_per_line), fmt="%13.5e" + ) + np.savetxt(txt, np.atleast_2d(timeseries[divisible:]), fmt="%13.5e") ###