2022-03-28 10:15:36 +02:00
import argparse
import configparser
import logging
import pathlib
2022-03-29 09:47:45 +02:00
import matplotlib . pyplot as plt
2022-03-28 10:15:36 +02:00
import numpy as np
from scipy import interpolate
from . olaflow import OFModel
2022-03-29 09:47:45 +02:00
parser = argparse . ArgumentParser ( description = " Convert swash output to olaFlow input " )
2022-03-28 10:15:36 +02:00
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-04-11 16:06:36 +02:00
parser . add_argument ( " -o " , " --output " , type = pathlib . Path )
2022-03-28 10:15:36 +02:00
args = parser . parse_args ( )
logging . basicConfig ( level = max ( ( 10 , 20 - 10 * args . verbose ) ) )
log = logging . getLogger ( " sws_ola " )
log . info ( " Starting sws -> olaFlow converter " )
config = configparser . ConfigParser ( )
2022-03-29 09:38:11 +02:00
config . read ( args . config )
2022-03-28 10:15:36 +02:00
sws_out = pathlib . Path ( config . get ( " swash " , " np_out " ) )
2022-03-29 09:47:45 +02:00
2022-03-28 10:59:35 +02:00
def data ( var ) :
return np . load ( sws_out . joinpath ( f " { var } .npy " ) )
2022-03-29 09:47:45 +02:00
2022-04-11 16:06:36 +02:00
t0 = config . getfloat ( " olaflow " , " t0 " )
2022-04-08 12:43:26 +02:00
x = data ( " x " )
t = data ( " t " )
2022-03-28 10:59:35 +02:00
2022-04-11 16:06:36 +02:00
arg_t0 = np . argmin ( np . abs ( t - t0 * 1e3 ) )
2022-03-28 10:59:35 +02:00
watl = data ( " watl " )
zk = data ( " zk " )
2022-04-08 12:43:26 +02:00
velk , _ = data ( " velk " )
2022-03-28 10:59:35 +02:00
vz = data ( " vz " )
2022-03-28 10:15:36 +02:00
2022-04-11 16:06:36 +02:00
olaflow_root = args . output
2022-03-28 10:15:36 +02:00
model = OFModel ( olaflow_root )
model . read_mesh ( )
2022-04-11 16:06:36 +02:00
watl_t = interpolate . interp1d ( x , watl [ arg_t0 ] + config . getfloat ( " bathy " , " level " , fallback = 0. ) )
2022-03-28 10:15:36 +02:00
alpha_water = np . where ( model . z < watl_t ( model . x ) , 1 , 0 )
2022-04-11 16:06:36 +02:00
zk_t = interpolate . interp1d ( x , zk [ arg_t0 ] )
velk_t = interpolate . interp1d ( x , velk [ arg_t0 , : , : ] ) ( model . x )
vz_t = interpolate . interp1d ( x , vz [ arg_t0 ] ) ( model . x )
2022-03-28 10:59:35 +02:00
zk_tl = zk_t ( model . x )
ux = np . zeros ( model . x . shape )
uy = np . zeros ( model . x . shape )
uz = np . zeros ( model . x . shape )
for zk_l , velk_l , vz_l in zip ( zk_tl , velk_t , vz_t ) :
ux = np . where ( model . z < zk_l , velk_l , ux )
uz = np . where ( model . z < zk_l , vz_l , uz )
2022-03-28 10:15:36 +02:00
model . write_field ( " alpha.water " , alpha_water )
2022-03-28 10:59:35 +02:00
model . write_vector_field ( " U " , np . stack ( ( ux , uy , uz ) ) . T )