Skip to content

Commit

Permalink
Merge fae9afc into 228e471
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarlokhorst committed Feb 9, 2021
2 parents 228e471 + fae9afc commit dd38b96
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Changelog of fews-3di
1.10 (unreleased)
-----------------

- Nothing changed yet.
- Added the functionality to provide a cold state file.
Place next to original state file with the name:
3di-cold-state-id.txt.


1.9 (2021-01-27)
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ The expected information in run_info.xml is::
``save_state_expiry_days``, without a "d". The example radar uuid
is the Dutch rainfall radar (NRR).

**Using saved states:** To use a warm state provide a text file with
id in the states folder using the name ``states/3di-saved-state-id.txt``.
A cold state is supplied in a similar way with the name:
``states/3di-cold-state-id.txt``.

**Rain_type:** multipe rain-types can be used in the configuration:

Expand Down
55 changes: 29 additions & 26 deletions fews_3di/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
API_HOST = "https://api.3di.live/v3.0"
CHUNK_SIZE = 1024 * 1024 # 1MB
SAVED_STATE_ID_FILENAME = "3di-saved-state-id.txt"
COLD_STATE_ID_FILENAME = "3di-cold-state-id.txt"
SIMULATION_STATUS_CHECK_INTERVAL = 30
USER_AGENT = "fews-3di (https://github.com/nens/fews-3di/)"

Expand Down Expand Up @@ -163,8 +164,9 @@ def run(self):
saved_state_id_file = (
self.settings.base_dir / "states" / SAVED_STATE_ID_FILENAME
)
cold_state_id_file = self.settings.base_dir / "states" / COLD_STATE_ID_FILENAME
if self.settings.save_state:
self._add_initial_state(saved_state_id_file)
self._add_initial_state(saved_state_id_file, cold_state_id_file)
self.saved_state_id = self._prepare_initial_state()
else:
logger.info("Saved state not enabled in the configuration, skipping.")
Expand Down Expand Up @@ -300,36 +302,37 @@ def _add_laterals(self, laterals):
if not still_to_process:
return

def _add_initial_state(self, saved_state_id_file: Path):
def _add_initial_state(self, saved_state_id_file: Path, cold_state_id_file: Path):
# TODO explain rationale. (likewise for the other methods).
if not saved_state_id_file.exists():
msg = f"Saved state id file {saved_state_id_file} not found"
if self.allow_missing_saved_state:
logger.warn(msg)
return
else:
raise utils.MissingFileException(msg)
saved_state_id: str = saved_state_id_file.read_text().strip()
logger.info("Simulation will use initial state %s", saved_state_id)
try:
self.simulations_api.simulations_initial_saved_state_create(
self.simulation_id, data={"saved_state": saved_state_id}
)
except openapi_client.exceptions.ApiException as e:
if e.status == 400:
logger.debug("Saved state setting error: %s", str(e))
msg = (
f"Setting initial state to saved state id={saved_state_id} failed. "
f"The error response was {e.body}, perhaps use "
f"--allow-missing-saved-state initially?"
)
for state_file in [saved_state_id_file, cold_state_id_file]:
if not state_file.exists():
msg = f"Saved state id file {state_file} not found"
if self.allow_missing_saved_state:
logger.warn(msg)
return
else:
raise MissingSavedStateError(msg) from e
logger.debug("Error isn't a 400, so we re-raise it.")
raise
raise utils.MissingFileException(msg)
saved_state_id: str = state_file.read_text().strip()
logger.info("Simulation will use initial state %s", saved_state_id)
try:
self.simulations_api.simulations_initial_saved_state_create(
self.simulation_id, data={"saved_state": saved_state_id}
)
return
except openapi_client.exceptions.ApiException as e:
if e.status == 400:
logger.debug("Saved state setting error: %s", str(e))
msg = (
f"Setting initial state to saved state id={saved_state_id} failed. "
f"The error response was {e.body}, perhaps use "
f"--allow-missing-saved-state initially?"
)
if self.allow_missing_saved_state:
logger.warn(msg)
return
else:
logger.debug("Error isn't a 400, so we re-raise it.")
raise

def _prepare_initial_state(self) -> int:
"""Instruct 3di to save the state afterwards and return its ID."""
Expand Down

0 comments on commit dd38b96

Please sign in to comment.