2022-03-31 10:24:38 +02:00
import argparse
import configparser
import logging
import pathlib
2022-03-31 10:31:45 +02:00
import matplotlib . pyplot as plt
2022-06-24 16:50:38 +02:00
from matplotlib . ticker import MultipleLocator
2022-03-31 10:24:38 +02:00
import numpy as np
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 ( level = max ( ( 10 , 20 - 10 * args . verbose ) ) )
log = logging . getLogger ( " bathy " )
log . info ( " Starting time-series pre-processing " )
config = configparser . ConfigParser ( )
config . read ( args . config )
inp_root = pathlib . Path ( config . get ( " inp " , " root " ) )
out_root = pathlib . Path ( config . get ( " out " , " root " ) )
out_ts = out_root . joinpath ( " ts.dat " )
2022-03-31 10:59:30 +02:00
raw_ts = [ ]
for tsi in config . get ( " inp " , " raw_ts " ) . split ( " , " ) :
2022-06-24 16:50:38 +02:00
raw_ts . append (
np . loadtxt (
inp_root . joinpath ( tsi ) ,
dtype = [ ( " state " , int ) , ( " z " , float ) , ( " y " , float ) , ( " x " , float ) ] ,
delimiter = " , " ,
max_rows = 2304 ,
)
)
2022-03-31 10:59:30 +02:00
n = len ( raw_ts )
raw_ts = np . concatenate ( raw_ts )
2022-03-31 10:24:38 +02:00
log . debug ( f " { raw_ts =} " )
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 ) ) ) } " )
2022-06-24 16:50:38 +02:00
t = np . linspace ( 0 , 30 * 60 * n , 2304 * n + 1 ) [ : - 1 ]
2022-03-31 10:24:38 +02:00
log . debug ( f " { t =} " )
log . info ( f " Saving timeseries to ' { out_ts } ' " )
2022-06-24 16:50:38 +02:00
np . savetxt ( out_ts , np . stack ( ( t , raw_ts [ " z " ] / 100 ) , axis = 1 ) )
2022-03-31 10:31:45 +02:00
2022-06-24 16:50:38 +02:00
fig , ax = plt . subplots ( figsize = ( 8 / 2.54 , 2 / 3 * 10 / 2.54 ) , constrained_layout = True )
tp = np . datetime64 ( " 2017-02-28T17:00:00 " ) + t . astype ( np . timedelta64 ) [ - ( t . size / / 3 ) : ]
ax . plot (
tp ,
raw_ts [ " z " ] [ - ( t . size / / 3 ) : ] * 1e-2 ,
color = " k " ,
lw = 1 ,
)
ax . axvline (
np . datetime64 ( " 2017-02-28T17:00:00 " ) + np . timedelta64 ( 23 * 60 + 8 ) ,
color = " k " ,
alpha = 0.1 ,
lw = 20 ,
)
ax . autoscale ( True , " x " , True )
ax . set ( xlabel = " t (s) " , ylabel = " z (m) " )
yabs_max = abs ( max ( ax . get_ylim ( ) , key = abs ) )
ax . set ( ylim = ( - 10 , 10 ) )
ax . set (
xticks = (
np . datetime64 ( " 2017-02-28T17:20:00 " ) ,
np . datetime64 ( " 2017-02-28T17:25:00 " ) ,
np . datetime64 ( " 2017-02-28T17:30:00 " ) ,
) ,
xticklabels = (
" 17:20 " ,
" 17:25 " ,
" 17:30 " ,
) ,
)
ax . yaxis . set_minor_locator ( MultipleLocator ( 1 ) )
ax . grid ( color = " k " , alpha = 0.2 )
2022-03-31 10:31:45 +02:00
fig . savefig ( out_root . joinpath ( " ts.pdf " ) )
2022-06-24 16:50:38 +02:00
fig . savefig ( out_root . joinpath ( " ts.jpg " ) , dpi = 200 )