2022-04-07 14:47:42 +02:00
import argparse
import configparser
import logging
import pathlib
import matplotlib . pyplot as plt
import numpy as np
parser = argparse . ArgumentParser ( description = " Post-process swash output " )
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 ( " post " )
log . info ( " Starting post-processing " )
config = configparser . ConfigParser ( )
config . read ( args . config )
inp = pathlib . Path ( config . get ( " post " , " inp " ) )
2022-05-25 10:27:36 +02:00
inp_comp = pathlib . Path ( config . get ( " post " , " compare " ) )
2022-04-07 14:47:42 +02:00
root = pathlib . Path ( config . get ( " swash " , " out " ) )
log . info ( f " Reading data from ' { inp } ' " )
x = np . load ( inp . joinpath ( " x.npy " ) )
t = np . load ( inp . joinpath ( " t.npy " ) )
watl = np . load ( inp . joinpath ( " watl.npy " ) )
2022-05-25 10:27:36 +02:00
watl_comp = np . load ( inp_comp . joinpath ( " watl.npy " ) )
2022-04-07 14:47:42 +02:00
# Cospectral calculations
x0 = config . getint ( " post " , " x0 " )
arg_x0 = np . abs ( x - x0 ) . argmin ( )
w0 = watl [ : , arg_x0 ]
2022-05-25 10:27:36 +02:00
w0_comp = watl_comp [ : , arg_x0 ]
2022-04-07 14:47:42 +02:00
cr0 = np . where ( np . diff ( np . sign ( w0 ) ) ) [ 0 ]
2022-05-25 10:27:36 +02:00
cr0_comp = np . where ( np . diff ( np . sign ( w0_comp ) ) ) [ 0 ]
2022-04-07 14:47:42 +02:00
2022-06-24 16:50:38 +02:00
log . info ( f " 1: { cr0 . size } waves " )
log . info ( f " 2: { cr0_comp . size } waves " )
2022-04-07 14:47:42 +02:00
wave = np . fromiter (
(
np . abs (
np . max ( np . abs ( w0 [ cr0 [ i - 1 ] : cr0 [ i ] ] ) )
+ np . max ( np . abs ( w0 [ cr0 [ i ] : cr0 [ i + 1 ] ] ) )
)
for i in range ( 1 , len ( cr0 ) - 1 )
) ,
dtype = np . single ,
)
2022-05-25 10:27:36 +02:00
wave_comp = np . fromiter (
(
np . abs (
np . max ( np . abs ( w0_comp [ cr0_comp [ i - 1 ] : cr0_comp [ i ] ] ) )
+ np . max ( np . abs ( w0_comp [ cr0_comp [ i ] : cr0_comp [ i + 1 ] ] ) )
)
for i in range ( 1 , len ( cr0 ) - 1 )
) ,
dtype = np . single ,
)
2022-04-07 14:47:42 +02:00
i0 = np . argmax ( wave )
out = pathlib . Path ( config . get ( " post " , " out " ) ) . joinpath ( f " x { x0 } " )
log . info ( f " Saving plots in ' { out } ' " )
out . mkdir ( parents = True , exist_ok = True )
fig , ax = plt . subplots ( )
ax . plot ( t [ cr0 [ 1 : - 1 ] ] * 1e-3 , wave )
2022-05-25 10:27:36 +02:00
ax . set ( xlabel = " t (s) " , ylabel = " z (m) " )
ax . autoscale ( True , " x " , True )
ax . grid ( )
2022-04-07 14:47:42 +02:00
fig . savefig ( out . joinpath ( " wsize.pdf " ) )
2022-06-24 16:50:38 +02:00
fig2 , ax2 = plt . subplots (
figsize = ( 10 / 2.54 , 2 / 3 * 10 / 2.54 ) , constrained_layout = True
)
ax2 . plot (
t [ cr0 [ i0 - 5 ] : cr0 [ i0 + 7 ] ] * 1e-3 ,
w0 [ cr0 [ i0 - 5 ] : cr0 [ i0 + 7 ] ] ,
color = " k " ,
label = " Cas 1 " ,
)
ax2 . plot (
t [ cr0 [ i0 - 5 ] : cr0 [ i0 + 7 ] ] * 1e-3 ,
w0_comp [ cr0 [ i0 - 5 ] : cr0 [ i0 + 7 ] ] ,
color = " k " ,
ls = " -. " ,
label = " Cas 2 " ,
)
2022-05-25 10:27:36 +02:00
ax2 . set ( xlabel = " t (s) " , ylabel = " z (m) " )
ax2 . autoscale ( True , " x " , True )
ax2 . grid ( )
ax2 . legend ( )
2022-04-07 14:47:42 +02:00
fig2 . savefig ( out . joinpath ( " maxw.pdf " ) )
2022-05-25 10:27:36 +02:00
fig2 . savefig ( out . joinpath ( " maxw.jpg " ) , dpi = 200 )
2022-06-24 16:50:38 +02:00
log . info (
f " RMS difference: { np . sqrt ( np . mean ( ( w0_comp - w0 ) * * 2 ) ) } m ; { np . sqrt ( np . mean ( ( w0_comp - w0 ) * * 2 ) ) / ( w0 . max ( ) - w0 . min ( ) ) : % } "
)
2022-05-25 10:27:36 +02:00
log . info ( f " Bias: { np . mean ( w0_comp - w0 ) } m " )
log . info ( f " Maximum wave size: { wave . max ( ) } m ; { wave_comp . max ( ) } m " )
2022-06-24 16:50:38 +02:00
log . info (
f " Maximum wave size difference: { abs ( wave_comp . max ( ) - wave . max ( ) ) / wave . max ( ) : % } "
)