2022-03-03 15:51:51 +01:00
import argparse
2022-03-04 10:23:24 +01:00
import configparser
2022-03-03 15:51:51 +01:00
import logging
2022-03-04 10:23:24 +01:00
import pathlib
2022-03-03 15:51:51 +01:00
2022-03-04 10:23:24 +01:00
import matplotlib . pyplot as plt
2022-03-03 15:51:51 +01:00
import numpy as np
2022-03-07 10:52:22 +01:00
import scipy . fft as fft
2022-03-16 07:50:44 +01:00
import scipy . signal as sgl
2022-03-03 15:51:51 +01:00
from . read_swash import *
parser = argparse . ArgumentParser ( description = " Post-process swash output " )
parser . add_argument ( " -v " , " --verbose " , action = " count " , default = 0 )
2022-03-29 09:38:11 +02:00
parser . add_argument ( " -c " , " --config " , default = " config.ini " )
2022-03-03 15:51:51 +01:00
args = parser . parse_args ( )
logging . basicConfig ( level = max ( ( 10 , 20 - 10 * args . verbose ) ) )
log = logging . getLogger ( " post " )
log . info ( " Starting post-processing " )
config = configparser . ConfigParser ( )
2022-03-29 09:38:11 +02:00
config . read ( args . config )
2022-03-03 15:51:51 +01:00
2022-03-04 11:30:23 +01:00
inp = pathlib . Path ( config . get ( " post " , " inp " ) )
2022-03-03 15:51:51 +01:00
root = pathlib . Path ( config . get ( " swash " , " out " ) )
2022-03-09 15:23:06 +01:00
log . info ( f " Reading data from ' { inp } ' " )
2022-03-25 10:25:17 +01:00
x = np . load ( inp . joinpath ( " xp.npy " ) )
t = np . load ( inp . joinpath ( " tsec.npy " ) )
botl = np . load ( inp . joinpath ( " botl.npy " ) )
watl = np . load ( inp . joinpath ( " watl.npy " ) )
vel = np . load ( inp . joinpath ( " vel.npy " ) )
2022-03-03 15:51:51 +01:00
# Cospectral calculations
x0 = config . getint ( " post " , " x0 " )
2022-03-04 11:03:06 +01:00
arg_x0 = np . abs ( x - x0 ) . argmin ( )
2022-03-03 16:02:52 +01:00
t0 = config . getfloat ( " post " , " t0 " )
2022-03-04 11:03:06 +01:00
arg_t0 = np . abs ( t - t0 ) . argmin ( )
2022-03-30 10:49:02 +02:00
dt = np . diff ( t ) . mean ( )
2022-03-03 15:51:51 +01:00
f = 1 / dt
2022-03-07 12:25:38 +01:00
nperseg = config . getint ( " post " , " nperseg " , fallback = None )
2022-03-03 15:51:51 +01:00
log . info ( f " Computing reflection coefficient at x= { x0 } " )
2022-03-25 10:25:17 +01:00
eta = watl [ t > t0 , arg_x0 ]
u = vel [ t > t0 , 0 , arg_x0 ]
2022-03-03 15:51:51 +01:00
2022-03-09 15:23:06 +01:00
phi_eta = sgl . welch ( eta , f , nperseg = nperseg )
phi_u = sgl . welch ( u , f , nperseg = nperseg )
phi_eta_u = sgl . csd ( eta , u , f , nperseg = nperseg )
2022-03-03 15:51:51 +01:00
2022-03-09 15:23:06 +01:00
H = np . sqrt ( np . abs ( phi_eta [ 1 ] ) )
U = np . sqrt ( np . abs ( phi_u [ 1 ] ) )
G = H / U
th_eta_u = np . angle ( phi_eta_u [ 1 ] )
2022-03-03 15:51:51 +01:00
R = np . sqrt (
2022-03-29 09:47:45 +02:00
( np . abs ( phi_eta [ 1 ] ) + np . abs ( phi_u [ 1 ] ) - 2 * np . abs ( phi_eta_u [ 1 ] ) )
/ ( np . abs ( phi_eta [ 1 ] ) + np . abs ( phi_u [ 1 ] ) + 2 * np . abs ( phi_eta_u [ 1 ] ) )
2022-03-03 15:51:51 +01:00
)
2022-03-29 09:47:45 +02:00
# R = np.sqrt(
2022-03-28 14:03:29 +02:00
# (1 + G**2 - 2 * G * np.cos(th_eta_u))
# / (1 + G**2 + 2 * G * np.cos(th_eta_u))
2022-03-29 09:47:45 +02:00
# )
2022-03-03 15:51:51 +01:00
2022-03-09 15:23:06 +01:00
if config . has_option ( " post " , " compare " ) :
2022-03-25 13:48:28 +01:00
inp_comp = pathlib . Path ( config . get ( " post " , " compare " ) )
x_ = np . load ( inp_comp . joinpath ( " xp.npy " ) )
t_ = np . load ( inp_comp . joinpath ( " tsec.npy " ) )
botl_ = np . load ( inp_comp . joinpath ( " botl.npy " ) )
watl_ = np . load ( inp_comp . joinpath ( " watl.npy " ) )
vel_ = np . load ( inp_comp . joinpath ( " vel.npy " ) )
2022-03-09 15:23:06 +01:00
arg_x0_ = np . abs ( x_ - x0 ) . argmin ( )
arg_t0_ = np . abs ( t_ - t0 ) . argmin ( )
2022-03-25 13:48:28 +01:00
eta_ = watl_ [ t_ > t0 , arg_x0_ ]
u_ = vel_ [ t_ > t0 , 0 , arg_x0_ ]
2022-03-09 15:23:06 +01:00
phi_eta_ = sgl . welch ( eta_ , f , nperseg = nperseg )
phi_u_ = sgl . welch ( u_ , f , nperseg = nperseg )
phi_eta_u_ = sgl . csd ( eta_ , u_ , f , nperseg = nperseg )
H_ = np . sqrt ( np . abs ( phi_eta_ [ 1 ] ) )
U_ = np . sqrt ( np . abs ( phi_u_ [ 1 ] ) )
G_ = H_ / U_
th_eta_u_ = np . angle ( phi_eta_u_ [ 1 ] )
R_ = np . sqrt (
2022-03-29 09:47:45 +02:00
( np . abs ( phi_eta_ [ 1 ] ) + np . abs ( phi_u_ [ 1 ] ) - 2 * np . abs ( phi_eta_u_ [ 1 ] ) )
/ ( np . abs ( phi_eta_ [ 1 ] ) + np . abs ( phi_u_ [ 1 ] ) + 2 * np . abs ( phi_eta_u_ [ 1 ] ) )
2022-03-09 15:23:06 +01:00
)
2022-03-29 09:47:45 +02:00
# R_ = np.sqrt(
2022-03-28 14:03:29 +02:00
# (1 + G_**2 - 2 * G_ * np.cos(th_eta_u_))
# / (1 + G_**2 + 2 * G_ * np.cos(th_eta_u_))
2022-03-29 09:47:45 +02:00
# )
2022-03-09 15:23:06 +01:00
2022-03-03 15:51:51 +01:00
# Plotting
log . info ( " Plotting results " )
2022-03-04 10:52:28 +01:00
fig , ( ax_watl , ax_vel ) = plt . subplots ( 2 )
2022-03-03 15:51:51 +01:00
2022-03-25 10:25:17 +01:00
ax_watl . plot ( t , watl [ : , arg_x0 ] , lw = 1 , label = " watl " )
2022-03-04 10:52:28 +01:00
ax_watl . set ( xlabel = " t (s) " , ylabel = " z (m) " )
ax_watl . autoscale ( axis = " x " , tight = True )
ax_watl . grid ( )
ax_watl . axvline ( t0 , c = " k " , alpha = 0.2 )
2022-03-03 15:51:51 +01:00
2022-03-25 10:25:17 +01:00
ax_vel . plot ( t , vel [ : , 0 , arg_x0 ] , lw = 1 , label = " vel " )
2022-03-03 15:51:51 +01:00
ax_vel . set ( xlabel = " t (s) " , ylabel = " U (m/s) " )
ax_vel . autoscale ( axis = " x " , tight = True )
ax_vel . grid ( )
ax_vel . axvline ( t0 , c = " k " , alpha = 0.2 )
fig . tight_layout ( )
fig_r , ax_r = plt . subplots ( )
2022-03-07 10:52:22 +01:00
ax_fft = ax_r . twinx ( )
ax_fft . plot (
2022-03-09 15:23:06 +01:00
* sgl . welch ( eta , 1 / dt , nperseg = nperseg ) ,
2022-03-07 10:52:22 +01:00
lw = 1 ,
c = " k " ,
alpha = 0.2 ,
2022-03-09 15:23:06 +01:00
label = " PSD ($ \\ eta$, cas 1) " ,
2022-03-07 10:52:22 +01:00
)
2022-03-09 15:23:06 +01:00
ax_r . plot ( phi_eta [ 0 ] , R , marker = " + " , label = " R (cas 1) " )
2022-03-10 10:55:34 +01:00
if config . has_option ( " post " , " compare " ) :
ax_fft . plot (
* sgl . welch ( eta_ , 1 / dt , nperseg = nperseg ) ,
lw = 1 ,
c = " k " ,
alpha = 0.2 ,
label = " PSD ($ \\ eta$, cas 2) " ,
)
ax_r . plot ( phi_eta [ 0 ] , R_ , marker = " + " , label = " R (cas 2) " )
2022-03-07 10:52:22 +01:00
ax_r . set ( xlim = ( 0 , 0.3 ) , ylim = ( 0 , 1 ) , xlabel = " f (Hz) " , ylabel = " R " )
2022-03-07 12:25:38 +01:00
ax_fft . set ( ylim = 0 , ylabel = " PSD (m²/Hz) " )
2022-03-03 15:51:51 +01:00
ax_r . grid ( )
2022-03-07 12:25:38 +01:00
ax_r . legend ( loc = " upper left " )
ax_fft . legend ( loc = " upper right " )
fig_r . tight_layout ( )
2022-03-03 15:51:51 +01:00
2022-03-07 12:30:50 +01:00
fig_x , ax_x = plt . subplots ( figsize = ( 10 , 1 ) )
2022-03-25 10:25:17 +01:00
ax_x . plot ( x , - botl , color = " k " )
2022-03-09 15:23:06 +01:00
ax_x . plot (
2022-03-25 10:25:17 +01:00
x ,
np . maximum ( watl [ arg_t0 , : ] , - botl ) ,
2022-03-04 11:32:46 +01:00
)
2022-03-10 10:55:34 +01:00
if config . has_option ( " post " , " compare " ) :
2022-03-25 13:48:28 +01:00
ax_x . plot ( x , - botl_ , color = " k " , ls = " -. " )
2022-03-10 10:55:34 +01:00
ax_x . plot (
2022-03-25 10:25:17 +01:00
x ,
2022-03-25 13:48:28 +01:00
np . maximum ( watl_ [ arg_t0 , : ] , - botl_ ) ,
2022-03-10 10:55:34 +01:00
ls = " -. " ,
)
2022-03-03 15:51:51 +01:00
ax_x . axvline ( x0 , c = " k " , alpha = 0.2 )
ax_x . set ( xlabel = " x (m) " , ylabel = " z (m) " )
ax_x . autoscale ( axis = " x " , tight = True )
2022-03-07 12:30:50 +01:00
ax_x . set ( aspect = " equal " )
fig_x . tight_layout ( )
2022-03-03 15:51:51 +01:00
2022-03-04 11:07:43 +01:00
out = pathlib . Path ( config . get ( " post " , " out " ) ) . joinpath ( f " t { t0 } x { x0 } " )
2022-03-03 15:51:51 +01:00
log . info ( f " Saving plots in ' { out } ' " )
2022-03-04 11:08:55 +01:00
out . mkdir ( parents = True , exist_ok = True )
2022-03-03 15:51:51 +01:00
2022-03-07 10:52:22 +01:00
fig . savefig ( out . joinpath ( " t.png " ) )
fig_r . savefig ( out . joinpath ( " R.png " ) )
fig_x . savefig ( out . joinpath ( " x.png " ) )
2022-03-03 15:51:51 +01:00
log . info ( " Finished post-processing " )