Skip to content

Commit

Permalink
Merge pull request #31 from ericbarefoot/iss22
Browse files Browse the repository at this point in the history
Adding command line interface to model
  • Loading branch information
ericbarefoot committed May 19, 2020
2 parents fd0be0d + 7265a3d commit d089909
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pyDeltaRCM/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pyDeltaRCM import command_line

if __name__ == '__main__':
command_line.run_model()
33 changes: 33 additions & 0 deletions pyDeltaRCM/command_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import argparse

import pyDeltaRCM
from . import deltaRCM_driver


def run_model():

parser = argparse.ArgumentParser(description='Options for running pyDeltaRCM from command line')

parser.add_argument('--config', help='Path to a config file that you would like to use.')
parser.add_argument('--version', action='version', version='pyDeltaRCM ' + pyDeltaRCM.__version__, help='Prints the version of pyDeltaRCM.')

args = parser.parse_args()

cmdlineargs = vars(args)

if cmdlineargs['config'] is not None:
delta = deltaRCM_driver.pyDeltaRCM(input_file=cmdlineargs['config'])
else:
delta = deltaRCM_driver.pyDeltaRCM()

for time in range(0, delta.timesteps):

delta.update()

delta.finalize()


if __name__ == '__main__':

run_model()
3 changes: 3 additions & 0 deletions pyDeltaRCM/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,6 @@ coeff_U_ero_sand:
alpha:
type: ['float', 'int']
default: 0.1
timesteps:
type: ['float', 'int']
default: 10
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
url='https://github.com/DeltaRCM/pyDeltaRCM',
install_requires=['matplotlib', 'netCDF4',
'basic-modeling-interface', 'scipy', 'numpy', 'pyyaml'],
entry_points={
'console_scripts': ['run_pyDeltaRCM=pyDeltaRCM.command_line:run_model'],
}
)
1 change: 1 addition & 0 deletions tests/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ save_discharge_grids: False
save_velocity_grids: False
save_dt: 50
save_strata: False
timesteps: 1
40 changes: 40 additions & 0 deletions tests/test_command_line_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import pytest

import os
import shutil
import locale
import numpy as np
import subprocess

import pyDeltaRCM as deltaModule
from pyDeltaRCM import command_line


def test_call():
"""
test calling the command line feature with a config file.
"""
subprocess.run(['run_pyDeltaRCM', '--config', os.path.join(os.getcwd(), 'tests', 'test_output.yaml')])
assert os.path.isfile(os.path.join(os.getcwd(), 'test', 'eta_0.0.png'))
shutil.rmtree(os.path.join(os.getcwd(), 'test'))


def test_version_call():
"""
test calling the command line feature to query the version.
"""
encoding = locale.getpreferredencoding()
printed1 = subprocess.run(['run_pyDeltaRCM', '--version'], stdout=subprocess.PIPE, encoding=encoding)
assert printed1.stdout == 'pyDeltaRCM ' + deltaModule.__version__ + '\n'
printed2 = subprocess.run(['python', '-m', 'pyDeltaRCM', '--version'], stdout=subprocess.PIPE, encoding=encoding)
assert printed2.stdout == 'pyDeltaRCM ' + deltaModule.__version__ + '\n'


def test_python_call():
"""
test calling the python hook command line feature with a config file.
"""
subprocess.run(['python', '-m', 'pyDeltaRCM', '--config', os.path.join(os.getcwd(), 'tests', 'test_output.yaml')])
assert os.path.isfile(os.path.join(os.getcwd(), 'test', 'eta_0.0.png'))
shutil.rmtree(os.path.join(os.getcwd(), 'test'))
35 changes: 35 additions & 0 deletions tests/test_output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
site_prefix: ''
case_prefix: ''
out_dir: test
Length: 10.0
Width: 10.0
seed: 0
dx: 1.0
L0_meters: 1.0
S0: 0.0002
itermax: 1
Np_water: 10
u0: 1.0
N0_meters: 2.0
h0: 1.0
H_SL: 0.0
SLR: 0.001
Np_sed: 10
f_bedload: 0.5
C0_percent: 0.1
toggle_subsidence: False
sigma_max: 0.0
start_subsidence: 50.
save_eta_figs: True
save_stage_figs: False
save_depth_figs: False
save_discharge_figs: False
save_velocity_figs: False
save_eta_grids: False
save_stage_grids: False
save_depth_grids: False
save_discharge_grids: False
save_velocity_grids: False
save_dt: 50
save_strata: False
timesteps: 1

0 comments on commit d089909

Please sign in to comment.