2022-03-02 14:25:53 +01:00
import configparser
import pathlib
2022-03-03 11:27:22 +01:00
import argparse
import logging
2022-03-02 14:25:53 +01:00
import numpy as np
import pandas as pd
import matplotlib . pyplot as plt
2022-03-03 10:54:09 +01:00
import scipy . signal as sgl
2022-03-02 14:25:53 +01:00
from . read_swash import *
2022-03-03 11:27:22 +01:00
parser = argparse . ArgumentParser ( description = " Pre-process bathymetry " )
parser . add_argument ( " -v " , " --verbose " , action = " count " , default = 0 )
args = parser . parse_args ( )
logging . basicConfig ( level = max ( ( 10 , 20 - 10 * args . verbose ) ) )
log = logging . getLogger ( " post " )
log . info ( " Starting post-processing " )
2022-03-02 14:25:53 +01:00
config = configparser . ConfigParser ( )
config . read ( " config.ini " )
2022-03-03 10:58:49 +01:00
cache = pathlib . Path ( config . get ( " data " , " out " ) )
2022-03-02 14:25:53 +01:00
root = pathlib . Path ( config . get ( " swash " , " out " ) )
2022-03-03 11:27:22 +01:00
log . info ( f " Reading bathymetry from ' { cache } ' " )
2022-03-02 14:25:53 +01:00
bathy = pd . read_hdf ( cache . joinpath ( " bathy.h5 " ) , " bathy " )
n = bathy . index . size
2022-03-03 11:27:22 +01:00
log . info ( f " Reading swash output from ' { root } ' " )
2022-03-02 14:25:53 +01:00
botl = read_nohead_scalar ( root . joinpath ( " botl.dat " ) , n )
2022-03-02 15:52:46 +01:00
dep = np . maximum ( 0 , read_nohead_scalar ( root . joinpath ( " dep.dat " ) , n ) )
2022-03-03 10:38:32 +01:00
vel = read_nohead_vect ( root . joinpath ( " vel.dat " ) , n )
2022-03-02 15:52:46 +01:00
2022-03-03 10:54:09 +01:00
n_t = botl . shape [ 0 ]
# Cospectral calculations
2022-03-03 11:27:22 +01:00
pos_x = config . getint ( " post " , " x0 " )
f = 1 / config . getfloat ( " post " , " dt " )
log . info ( f " Computing reflection coefficient at x= { pos_x } " )
2022-03-03 10:54:09 +01:00
2022-03-03 11:16:40 +01:00
eta = ( dep - botl ) [ n_t / / 2 : , pos_x ]
u = vel [ n_t / / 2 : , 0 , pos_x ]
2022-03-03 10:54:09 +01:00
2022-03-03 11:16:40 +01:00
phi_eta = np . abs ( sgl . csd ( eta , eta , f ) )
phi_u = np . abs ( sgl . csd ( u , u , f ) )
phi_eta_u = np . abs ( sgl . csd ( eta , u , f ) )
2022-03-03 10:54:09 +01:00
2022-03-03 11:16:40 +01:00
R = np . sqrt (
( phi_eta [ 1 ] + phi_u [ 1 ] - 2 * phi_eta_u [ 1 ] )
/ ( phi_eta [ 1 ] + phi_u [ 1 ] + 2 * phi_eta_u [ 1 ] )
)
2022-03-03 10:54:09 +01:00
# Plotting
2022-03-03 11:27:22 +01:00
log . info ( " Plotting results " )
2022-03-03 10:38:32 +01:00
fig , ( ax_dep , ax_vel ) = plt . subplots ( 2 )
2022-03-03 10:54:09 +01:00
ax_dep . plot ( ( dep - botl ) [ : , pos_x ] , label = " dep " , color = " #0066ff " )
2022-03-03 10:38:32 +01:00
ax_dep . set ( xlabel = " t (s) " , ylabel = " z (m) " )
2022-03-03 11:16:40 +01:00
ax_dep . autoscale ( axis = " x " , tight = True )
ax_dep . grid ( )
2022-03-03 10:38:32 +01:00
2022-03-03 10:54:09 +01:00
ax_vel . plot ( vel [ : , 0 , pos_x ] , label = " vel " )
2022-03-03 10:38:32 +01:00
ax_vel . set ( xlabel = " t (s) " , ylabel = " U (m/s) " )
2022-03-03 11:16:40 +01:00
ax_vel . autoscale ( axis = " x " , tight = True )
ax_vel . grid ( )
2022-03-03 10:38:32 +01:00
fig . tight_layout ( )
2022-03-03 10:54:09 +01:00
fig_r , ax_r = plt . subplots ( )
2022-03-03 11:16:40 +01:00
ax_r . plot ( phi_eta [ 0 ] , R )
ax_r . autoscale ( axis = " x " , tight = True )
ax_r . set ( ylim = ( 0 , 1 ) , xlabel = " f (Hz) " , ylabel = " R " )
2022-03-03 10:54:09 +01:00
ax_r . grid ( )
2022-03-03 11:16:40 +01:00
out = pathlib . Path ( config . get ( " post " , " out " ) )
2022-03-03 11:27:22 +01:00
log . info ( f " Saving plots in ' { out } ' " )
2022-03-03 11:16:40 +01:00
out . mkdir ( exist_ok = True )
fig . savefig ( out . joinpath ( f " { pos_x } .png " ) )
fig_r . savefig ( out . joinpath ( f " R { pos_x } .png " ) )
2022-03-03 11:27:22 +01:00
log . info ( " Finished post-processing " )