Skip to content

Commit

Permalink
Increase timeout (#250)
Browse files Browse the repository at this point in the history
* Set timeout to 300 second

* Fix some linter issues
  • Loading branch information
Peter9192 committed Sep 21, 2021
1 parent c794ef7 commit ee43e4c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 39 deletions.
17 changes: 11 additions & 6 deletions src/ewatercycle/models/lisflood.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module with Lisflood model."""
"""eWaterCycle wrapper around Lisflood BMI."""

import datetime
import logging
import xml.etree.ElementTree as ET
Expand Down Expand Up @@ -37,13 +38,12 @@ class Lisflood(AbstractModel[LisfloodForcing]):
available_versions = ("20.10",)
"""Versions for which ewatercycle grpc4bmi docker images are available."""

def __init__(
def __init__( # noqa: D107
self,
version: str,
parameter_set: ParameterSet,
forcing: LisfloodForcing,
):
"""Construct Lisflood model with initial values."""
super().__init__(version, parameter_set, forcing)
self._check_forcing(forcing)
self.cfg = XmlConfig(parameter_set.config)
Expand Down Expand Up @@ -96,7 +96,10 @@ def setup( # type: ignore
Path to config file and path to config directory
"""
# TODO forcing can be a part of parameter_set
cfg_dir_as_path = to_absolute_path(cfg_dir) if cfg_dir else None
cfg_dir_as_path = None
if cfg_dir:
cfg_dir_as_path = to_absolute_path(cfg_dir)

cfg_dir_as_path = _generate_workdir(cfg_dir_as_path)
config_file = self._create_lisflood_config(
cfg_dir_as_path,
Expand All @@ -122,6 +125,7 @@ def setup( # type: ignore
image=str(self.singularity_image),
input_dirs=input_dirs,
work_dir=str(cfg_dir_as_path),
timeout=300,
)
elif CFG["container_engine"].lower() == "docker":
self._set_docker_image()
Expand All @@ -130,6 +134,7 @@ def setup( # type: ignore
image_port=55555,
input_dirs=input_dirs,
work_dir=str(cfg_dir_as_path),
timeout=300,
)
else:
raise ValueError(
Expand All @@ -138,7 +143,7 @@ def setup( # type: ignore
return str(config_file), str(cfg_dir_as_path)

def _check_forcing(self, forcing):
"""Checks forcing argument and get path, start/end time of forcing data."""
"""Check forcing argument and get path, start/end time of forcing data."""
# TODO check if mask has same grid as forcing files,
# if not warn users to run reindex_forcings
if isinstance(forcing, LisfloodForcing):
Expand Down Expand Up @@ -341,7 +346,7 @@ def parameters(self) -> Iterable[Tuple[str, Any]]:


def _generate_workdir(cfg_dir: Path = None) -> Path:
"""Creates or makes sure workdir exists.
"""Create or make sure workdir exists.
Args:
cfg_dir: If cfg dir is None then create sub-directory in CFG['output_dir']
Expand Down
34 changes: 23 additions & 11 deletions src/ewatercycle/models/marrmot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""eWaterCycle wrapper around Marrmot BMI."""

import datetime
import logging
from dataclasses import asdict, dataclass
Expand All @@ -21,7 +23,9 @@

@dataclass
class Solver:
"""Solver, for current implementations see `here
"""Container for properties of the solver.
For current implementations see `here
<https://github.com/wknoben/MARRMoT/tree/master/MARRMoT/Functions/Time%20stepping>`_.
"""

Expand All @@ -31,7 +35,8 @@ class Solver:


def _generate_cfg_dir(cfg_dir: Path = None) -> Path:
"""
"""Make sure there is a working directory.
Args:
cfg_dir: If cfg dir is None or does not exist then create sub-directory
in CFG['output_dir']
Expand Down Expand Up @@ -70,8 +75,7 @@ class MarrmotM01(AbstractModel[MarrmotForcing]):
available_versions = ("2020.11",)
"""Versions for which ewatercycle grpc4bmi docker images are available."""

def __init__(self, version: str, forcing: MarrmotForcing):
"""Construct MarrmotM01 with initial values."""
def __init__(self, version: str, forcing: MarrmotForcing): # noqa: D107
super().__init__(version, forcing=forcing)
self._parameters = [1000.0]
self.store_ini = [900.0]
Expand Down Expand Up @@ -128,7 +132,10 @@ def setup( # type: ignore
self.store_ini = [initial_soil_moisture_storage]
if solver:
self.solver = solver
cfg_dir_as_path = to_absolute_path(cfg_dir) if cfg_dir else None

cfg_dir_as_path = None
if cfg_dir:
cfg_dir_as_path = to_absolute_path(cfg_dir)

cfg_dir_as_path = _generate_cfg_dir(cfg_dir_as_path)
config_file = self._create_marrmot_config(cfg_dir_as_path, start_time, end_time)
Expand All @@ -139,12 +146,14 @@ def setup( # type: ignore
self.bmi = BmiClientSingularity(
image=str(self.singularity_image),
work_dir=str(cfg_dir_as_path),
timeout=300,
)
elif CFG["container_engine"].lower() == "docker":
self.bmi = BmiClientDocker(
image=self.docker_image,
image_port=55555,
work_dir=str(cfg_dir_as_path),
timeout=300,
)
else:
raise ValueError(
Expand All @@ -153,7 +162,7 @@ def setup( # type: ignore
return str(config_file), str(cfg_dir_as_path)

def _check_forcing(self, forcing):
""" "Check forcing argument and get path, start and end time of forcing data."""
"""Check forcing argument and get path, start and end time of forcing data."""
if isinstance(forcing, MarrmotForcing):
forcing_dir = to_absolute_path(forcing.directory)
self.forcing_file = str(forcing_dir / forcing.forcing_file)
Expand Down Expand Up @@ -270,14 +279,13 @@ def get_value_as_xarray(self, name: str) -> xr.DataArray:
@property
def parameters(self) -> Iterable[Tuple[str, Any]]:
"""List the parameters for this model."""
p = [
return [
("maximum_soil_moisture_storage", self._parameters[0]),
("initial_soil_moisture_storage", self.store_ini[0]),
("solver", self.solver),
("start time", self.forcing_start_time.strftime("%Y-%m-%dT%H:%M:%SZ")),
("end time", self.forcing_end_time.strftime("%Y-%m-%dT%H:%M:%SZ")),
]
return p


M14_PARAMS = (
Expand Down Expand Up @@ -315,8 +323,7 @@ class MarrmotM14(AbstractModel[MarrmotForcing]):
available_versions = ("2020.11",)
"""Versions for which ewatercycle grpc4bmi docker images are available."""

def __init__(self, version: str, forcing: MarrmotForcing):
"""Construct MarrmotM14 with initial values."""
def __init__(self, version: str, forcing: MarrmotForcing): # noqa: D107
super().__init__(version, forcing=forcing)
self._parameters = [1000.0, 0.5, 0.5, 100.0, 0.5, 4.25, 2.5]
self.store_ini = [900.0, 900.0]
Expand Down Expand Up @@ -392,7 +399,10 @@ def setup( # type: ignore
self.store_ini[1] = initial_saturated_zone_storage
if solver:
self.solver = solver
cfg_dir_as_path = to_absolute_path(cfg_dir) if cfg_dir else None

cfg_dir_as_path = None
if cfg_dir:
cfg_dir_as_path = to_absolute_path(cfg_dir)

cfg_dir_as_path = _generate_cfg_dir(cfg_dir_as_path)
config_file = self._create_marrmot_config(cfg_dir_as_path, start_time, end_time)
Expand All @@ -403,12 +413,14 @@ def setup( # type: ignore
self.bmi = BmiClientSingularity(
image=str(self.singularity_image),
work_dir=str(cfg_dir_as_path),
timeout=300,
)
elif CFG["container_engine"].lower() == "docker":
self.bmi = BmiClientDocker(
image=self.docker_image,
image_port=55555,
work_dir=str(cfg_dir_as_path),
timeout=300,
)
else:
raise ValueError(
Expand Down
23 changes: 13 additions & 10 deletions src/ewatercycle/models/pcrglobwb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""eWaterCycle wrapper around PCRGlobWB BMI."""

import datetime
import logging
from os import PathLike
Expand All @@ -24,7 +26,6 @@ class PCRGlobWB(AbstractModel[PCRGlobWBForcing]):
"""eWaterCycle implementation of PCRGlobWB hydrological model.
Args:
version: pick a version from :py:attr:`~available_versions`
parameter_set: instance of
:py:class:`~ewatercycle.parameter_sets.default.ParameterSet`.
Expand All @@ -35,7 +36,7 @@ class PCRGlobWB(AbstractModel[PCRGlobWBForcing]):

available_versions = ("setters",)

def __init__(
def __init__( # noqa: D107
self,
version: str,
parameter_set: ParameterSet,
Expand Down Expand Up @@ -94,7 +95,8 @@ def _setup_default_config(self):
"temperatureNC",
str(
to_absolute_path(
self.forcing.temperatureNC, parent=self.forcing.directory
self.forcing.temperatureNC,
parent=self.forcing.directory,
)
),
)
Expand All @@ -103,7 +105,8 @@ def _setup_default_config(self):
"precipitationNC",
str(
to_absolute_path(
self.forcing.precipitationNC, parent=self.forcing.directory
self.forcing.precipitationNC,
parent=self.forcing.directory,
)
),
)
Expand All @@ -129,17 +132,17 @@ def setup(self, cfg_dir: str = None, **kwargs) -> Tuple[str, str]: # type: igno

try:
self._start_container()
except FutureTimeoutError:
except FutureTimeoutError as exc:
# https://github.com/eWaterCycle/grpc4bmi/issues/95
# https://github.com/eWaterCycle/grpc4bmi/issues/100
raise ValueError(
"Couldn't spawn container within allocated time limit "
"(15 seconds). You may try pulling the docker image with"
"(300 seconds). You may try pulling the docker image with"
f" `docker pull {self.docker_image}` or call `singularity "
f"build {self._singularity_image(CFG['singularity_dir'])} "
f"docker://{self.docker_image}` if you're using singularity,"
" and then try again."
)
) from exc

return str(cfg_file), str(work_dir)

Expand Down Expand Up @@ -199,14 +202,14 @@ def _start_container(self):
image_port=55555,
work_dir=str(self.work_dir),
input_dirs=additional_input_dirs,
timeout=15,
timeout=300,
)
elif CFG["container_engine"] == "singularity":
self.bmi = BmiClientSingularity(
image=self._singularity_image(CFG["singularity_dir"]),
work_dir=str(self.work_dir),
input_dirs=additional_input_dirs,
timeout=15,
timeout=300,
)
else:
raise ValueError(
Expand All @@ -216,7 +219,7 @@ def _start_container(self):
def _coords_to_indices(
self, name: str, lat: Iterable[float], lon: Iterable[float]
) -> Iterable[int]:
"""Converts lat/lon values to index.
"""Convert lat/lon values to index.
Args:
lat: Latitudinal value
Expand Down
25 changes: 13 additions & 12 deletions src/ewatercycle/models/wflow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""eWaterCycle wrapper around WFlow BMI."""

import datetime
import logging
import shutil
Expand Down Expand Up @@ -35,13 +37,12 @@ class Wflow(AbstractModel[WflowForcing]):
available_versions = ("2020.1.1",)
"""Show supported WFlow versions in eWaterCycle"""

def __init__(
def __init__( # noqa: D107
self,
version: str,
parameter_set: ParameterSet,
forcing: Optional[WflowForcing] = None,
):

super().__init__(version, parameter_set, forcing)
self._set_docker_image()
self._setup_default_config()
Expand Down Expand Up @@ -121,17 +122,17 @@ def setup(self, cfg_dir: str = None, **kwargs) -> Tuple[str, str]: # type: igno

try:
self._start_container()
except FutureTimeoutError:
except FutureTimeoutError as exc:
# https://github.com/eWaterCycle/grpc4bmi/issues/95
# https://github.com/eWaterCycle/grpc4bmi/issues/100
raise ValueError(
"Couldn't spawn container within allocated time limit "
"(15 seconds). You may try pulling the docker image with"
"(300 seconds). You may try pulling the docker image with"
f" `docker pull {self.docker_image}` or call `singularity "
f"build {self._singularity_image(CFG['singularity_dir'])} "
f"docker://{self.docker_image}` if you're using singularity,"
" and then try again."
)
) from exc

return (
str(updated_cfg_file),
Expand Down Expand Up @@ -165,21 +166,21 @@ def _start_container(self):
image=self.docker_image,
image_port=55555,
work_dir=str(self.work_dir),
timeout=10,
timeout=300,
)
elif CFG["container_engine"] == "singularity":
self.bmi = BmiClientSingularity(
image=self._singularity_image(CFG["singularity_dir"]),
work_dir=str(self.work_dir),
timeout=15,
timeout=300,
)
else:
raise ValueError(f"Unknown container technology: {CFG['container_engine']}")

def _coords_to_indices(
self, name: str, lat: Iterable[float], lon: Iterable[float]
) -> Iterable[int]:
"""Converts lat/lon values to index.
"""Convert lat/lon values to index.
Args:
lat: Latitudinal value
Expand Down Expand Up @@ -240,11 +241,11 @@ def parameters(self) -> Iterable[Tuple[str, Any]]:
]


def _wflow_to_iso(t):
dt = datetime.datetime.fromisoformat(t)
def _wflow_to_iso(time):
dt = datetime.datetime.fromisoformat(time)
return dt.strftime("%Y-%m-%dT%H:%M:%SZ")


def _iso_to_wflow(t):
dt = get_time(t)
def _iso_to_wflow(time):
dt = get_time(time)
return dt.strftime("%Y-%m-%d %H:%M:%S")
2 changes: 2 additions & 0 deletions tests/models/test_lisflood.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def test_setup(self, model_with_setup, tmp_path):
f"{tmp_path}/forcing",
],
work_dir=f"{tmp_path}/lisflood_20210102_030405",
timeout=300,
)

# Check content config file
Expand Down Expand Up @@ -207,6 +208,7 @@ def test_setup(self, model_with_setup, tmp_path):
f"{tmp_path}/custommask",
],
work_dir=f"{tmp_path}/lisflood_20210102_030405",
timeout=300,
)

# Check content config file
Expand Down

0 comments on commit ee43e4c

Please sign in to comment.