2022-04-05 10:38:05 +02:00
import argparse
import configparser
import logging
import pathlib
import matplotlib . pyplot as plt
import numpy as np
2022-04-07 11:08:11 +02:00
from scipy . interpolate import griddata
2022-04-05 10:38:05 +02:00
parser = argparse . ArgumentParser ( description = " Pre-process time-series " )
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 " )
2022-07-06 08:40:55 +02:00
log . info ( " Starting pre-processing " )
2022-04-05 10:38:05 +02:00
config = configparser . ConfigParser ( )
config . read ( args . config )
inp_root = pathlib . Path ( config . get ( " inp " , " root " ) )
out_root = pathlib . Path ( config . get ( " out " , " root " ) )
inp_spec = inp_root . joinpath ( config . get ( " inp " , " raw_spec " ) )
out_spec = out_root . joinpath ( " spec.dat " )
2022-04-05 11:42:41 +02:00
Sm = np . loadtxt (
inp_spec ,
skiprows = 3 ,
max_rows = 1 ,
)
2022-04-05 10:38:05 +02:00
inp = np . loadtxt (
inp_spec ,
dtype = [ ( " f " , float ) , ( " S " , float ) ] ,
delimiter = " , " ,
skiprows = 12 ,
2022-04-05 11:42:41 +02:00
usecols = ( 0 , 1 ) ,
2022-04-05 10:38:05 +02:00
max_rows = 64 ,
)
2022-04-07 11:08:11 +02:00
cycle = config . getfloat ( " inp " , " cycle " , fallback = None )
if cycle is None :
f = inp [ " f " ]
S = inp [ " S " ] * Sm
else :
2022-06-24 16:50:38 +02:00
f = np . arange ( inp [ " f " ] . min ( ) , inp [ " f " ] . max ( ) + 1 / cycle , 1 / cycle )
2022-04-07 11:08:11 +02:00
S = griddata ( inp [ " f " ] , inp [ " S " ] * Sm , f )
2022-04-05 10:38:05 +02:00
with out_spec . open ( " w " ) as out :
out . write ( " SPEC1D \n " )
2022-04-07 11:08:11 +02:00
np . savetxt ( out , np . stack ( ( f , S ) , axis = 1 ) )
2022-04-05 10:38:05 +02:00
2022-04-07 11:08:11 +02:00
df = np . diff ( f ) . min ( )
2022-04-05 10:38:05 +02:00
log . info ( f " Minimum frequency delta: { df : .4f } Hz " )
log . info ( f " Maximum modelled time: { 1 / df : .0f } s " )
2022-04-05 10:41:13 +02:00
fig , ax = plt . subplots ( )
2022-04-07 11:08:11 +02:00
ax . plot ( f , S , c = " k " , lw = 1 )
2022-04-05 10:41:13 +02:00
ax . autoscale ( True , " x " , tight = True )
ax . grid ( )
ax . set ( xlim = 0 , ylim = 0 , xlabel = " f (Hz) " , ylabel = " S (m^2/Hz) " )
fig . savefig ( out_root . joinpath ( " spec.pdf " ) )
plt . show ( )