1
Fork 0
internship/data/processing/wavelet.py

61 lines
1.8 KiB
Python

import argparse
import configparser
import logging
import pathlib
import matplotlib.pyplot as plt
import numpy as np
import scipy.signal as sgl
parser = argparse.ArgumentParser(description="Pre-process time-series")
parser.add_argument("-v", "--verbose", action="count", default=0)
parser.add_argument("-c", "--config", default="config.ini")
args = parser.parse_args()
logging.basicConfig()
log = logging.getLogger("bathy")
log.setLevel(max((10, 20 - 10 * args.verbose)))
log.info("Starting time-series pre-processing")
config = configparser.ConfigParser()
config.read(args.config)
inp_root = pathlib.Path(config.get("inp", "root"), "cerema/raw")
out_root = pathlib.Path(config.get("out", "root"))
raw_ts = []
for tsi in sorted(inp_root.glob("2017022817*.raw")):
#for tsi in sorted(inp_root.glob("*.raw")):
raw_ts.append(
np.loadtxt(
tsi,
dtype=[("state", int), ("z", float), ("y", float), ("x", float)],
delimiter=",",
max_rows=2304,
)
)
log.debug(f"Loading <{tsi}>")
n = len(raw_ts)
raw_ts = np.concatenate(raw_ts)
log.debug(f"{raw_ts=}")
# t = np.linspace(0, 30 * 60 * n * 1e3, 2304 * n + 1)[:-1].astype("timedelta64[ms]") + np.datetime64("2017-02-28T00:00")
t = np.linspace(0, 30 * 60 * n, 2304 * n, endpoint=False)
if (errs := np.count_nonzero(raw_ts["state"])) != 0:
log.warning(f"{errs} transmission errors!")
log.debug(f"{dict(zip(*np.unique(raw_ts['state'], return_counts=True)))}")
# log.debug(f"{t[raw_ts['state'] != 0]}")
z = raw_ts["z"]
# z = np.cos(2 * np.pi * 7 * t) + sgl.gausspulse(t - 0.4, fc=2)
M = sgl.cwt(z, sgl.morlet, np.arange(1, 30 / (30 * 60 / 2304)))
print(M)
fig, ax = plt.subplots()
c = ax.imshow(M, aspect="auto", cmap="spring", vmin=0)
ax2 = ax.twinx()
ax2.plot(z, c="k")
fig.colorbar(c)
plt.show()