Skip to content

Commit

Permalink
Edits and bugfixes to cmd module
Browse files Browse the repository at this point in the history
  • Loading branch information
jreniel committed Jan 27, 2020
1 parent 9f5d41c commit 90f8a58
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 82 deletions.
122 changes: 46 additions & 76 deletions pyschism/cmd/basecmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pathlib
from functools import lru_cache
from datetime import datetime, timedelta
from pyschism.mesh import Mesh
from pyschism.driver import SchismRun
Expand Down Expand Up @@ -51,53 +52,32 @@ def _init_output_stations(self, driver):
pass

@property
@lru_cache
def driver(self):
try:
return self.__driver
except AttributeError:
driver = SchismRun(
self.mesh,
self.start_date,
self.end_date,
self.spinup_time,
self.tidal_forcing,
self.wind_forcing,
self.wave_forcing,
# self.args.netcdf,
)
# self._enable_outputs(driver)
self.__driver = driver
return self.__driver
driver = SchismRun(
self.mesh,
self.start_date,
self.end_date,
self.spinup_time,
self.tidal_forcing,
self.wind_forcing,
self.wave_forcing,
# self.args.netcdf,
)
# self._enable_outputs(driver)
return driver

@property
def start_date(self):
try:
return self.__start_date
except AttributeError:
self.__start_date = datetime.strptime(
self.args.start_date,
"%Y-%m-%dT%H:%M"
)
return self.__start_date
return datetime.strptime(self.args.start_date, "%Y-%m-%dT%H:%M")

@property
def end_date(self):
try:
return self.__end_date
except AttributeError:
self.__end_date = datetime.strptime(
self.args.end_date,
"%Y-%m-%dT%H:%M"
)
return self.__end_date
return datetime.strptime(self.args.end_date, "%Y-%m-%dT%H:%M")

@property
def spinup_time(self):
try:
return self.__spinup_time
except AttributeError:
self.__spinup_time = timedelta(days=self.args.spinup_days)
return self.__spinup_time
return timedelta(days=self.args.spinup_days)

@property
def args(self):
Expand All @@ -108,15 +88,12 @@ def mesh(self):
return self._mesh

@property
@lru_cache
def tidal_forcing(self):
try:
return self.__tidal_forcing
except AttributeError:
tidal_forcing = TidalForcing()
for constituent in self.constituents:
tidal_forcing.use_constituent(constituent)
self.__tidal_forcing = tidal_forcing
return self.__tidal_forcing
tidal_forcing = TidalForcing()
for constituent in self.constituents:
tidal_forcing.use_constituent(constituent)
return tidal_forcing

@property
def wind_forcing(self):
Expand All @@ -132,32 +109,30 @@ def output_directory(self):
return pathlib.Path(self.args.output_directory).absolute()

@property
@lru_cache
def constituents(self):
try:
return self.__constituents
except AttributeError:
# might be better to get these from TidalForcing()
_major = ('Q1', 'O1', 'P1', 'K1', 'N2', 'M2', 'S2', 'K2')
_all = (*_major, 'Mm', 'Mf', 'M4', 'MN4', 'MS4', '2N2', 'S1')
if ('all' in self.args.constituents
and len(self.args.constituents) > 1):
msg = 'When using all, must only pass one'
raise IOError(msg)

elif ('major' in self.args.constituents
and len(self.args.constituents) > 1):
msg = 'When using major, must only pass one'
raise IOError(msg)
if 'all' in self.args.constituents:
constituents = _all
elif 'major' in self.args.constituents:
constituents = _major
else:
constituents = self.args.constituents
self.__constituents = constituents
return self.__constituents
# might be better to get these from TidalForcing()
_major = ('Q1', 'O1', 'P1', 'K1', 'N2', 'M2', 'S2', 'K2')
_all = (*_major, 'Mm', 'Mf', 'M4', 'MN4', 'MS4', '2N2', 'S1')
if ('all' in self.args.constituents
and len(self.args.constituents) > 1):
msg = 'When using all, must only pass one'
raise IOError(msg)

elif ('major' in self.args.constituents
and len(self.args.constituents) > 1):
msg = 'When using major, must only pass one'
raise IOError(msg)
if 'all' in self.args.constituents:
constituents = _all
elif 'major' in self.args.constituents:
constituents = _major
else:
constituents = self.args.constituents
return constituents

@property
@lru_cache
def server_config(self):
if self.args.hostname:
if (not self.args.use_slurm or
Expand All @@ -182,25 +157,20 @@ def server_config(self):

else:
server_config = None

self.__server_config = server_config
return self.__server_config
return server_config

@property
def _args(self):
return self.__args

@property
@lru_cache
def _mesh(self):
try:
return self.__mesh
except AttributeError:
self.__mesh = Mesh.open(
return Mesh.open(
hgrid=self.args.hgrid,
vgrid=self.args.vgrid,
crs=self.args.crs
)
return self.__mesh

@_args.setter
def _args(self, args):
Expand Down
4 changes: 2 additions & 2 deletions pyschism/driver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ def _end_date(self, end_date):

@_spinup_time.setter
def _spinup_time(self, spinup_time):
msg = f"spinup_time must be a {datetime} instance."
assert isinstance(spinup_time, datetime), msg
msg = f"spinup_time must be a {timedelta} instance."
assert isinstance(spinup_time, timedelta), msg
self.__spinup_time = spinup_time

@_tidal_forcing.setter
Expand Down
1 change: 0 additions & 1 deletion pyschism/mesh/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ def _quads(self, quads):
self._certify_input_geom("quads", quads)
self.__quads = quads


@_crs.setter
def _crs(self, crs):
if crs is not None:
Expand Down
8 changes: 5 additions & 3 deletions tests/cmd/test_tidal_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

class TidalRunCmdTestCase(unittest.TestCase):

def _test_tidal_pr_mesh(self):
parent = pathlib.Path(__name__).parent.absolute()
def _test_tidal_run_mesh(self):
parent = pathlib.Path(__name__).parent
hgrid = parent.resolve() / "examples/example_1/hgrid.gr3"
cmd = ["tidal_run"]
cmd += [str(parent / '../hgrid.gr3')]
cmd += [str(hgrid.resolve())]
cmd += ["2017-9-18T12:00"]
cmd += ["2017-9-23T12:00"]
cmd += ["--spinup-days=2"]
print(" ".join(cmd))
with patch.object(sys, 'argv', cmd):
self.assertEqual(tidal_run.main(), 0)

Expand Down

0 comments on commit 90f8a58

Please sign in to comment.