|
| 1 | +"""PyStemmusScope wrapper around Stemmus_Scope.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +from typing import Dict |
| 7 | +from . import config_io |
| 8 | +from . import forcing_io |
| 9 | +from . import soil_io |
| 10 | +from . import utils |
| 11 | + |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class StemmusScope(): |
| 17 | + """PyStemmusScope wrapper around Stemmus_Scope model. |
| 18 | + see https://gmd.copernicus.org/articles/14/1379/2021/ |
| 19 | +
|
| 20 | + It sets the model with a configuration file and executable file. |
| 21 | + It also prepares forcing and soil data for model run. |
| 22 | +
|
| 23 | + Args: |
| 24 | + config_file(str): path to Stemmus_Scope configuration file. An example |
| 25 | + config_file can be found in tests/test_data in `STEMMUS_SCOPE_Processing |
| 26 | + repository <https://github.com/EcoExtreML/STEMMUS_SCOPE_Processing>`_ |
| 27 | + exe_file(str): path to Stemmus_Scope executable file. |
| 28 | +
|
| 29 | + Example: |
| 30 | + See notebooks/run_model_in_notebook.ipynb in `STEMMUS_SCOPE_Processing |
| 31 | + repository <https://github.com/EcoExtreML/STEMMUS_SCOPE_Processing>`_ |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, config_file: str, exe_file: str): |
| 35 | + # make sure paths are abolute and path objects |
| 36 | + config_file = utils.to_absolute_path(config_file) |
| 37 | + self.exe_file = utils.to_absolute_path(exe_file) |
| 38 | + |
| 39 | + # read config template |
| 40 | + self._configs = config_io.read_config(config_file) |
| 41 | + |
| 42 | + def setup( |
| 43 | + self, |
| 44 | + WorkDir: str = None, |
| 45 | + ForcingFileName: str = None, |
| 46 | + NumberOfTimeSteps: str = None, |
| 47 | + ) -> str: |
| 48 | + """Configure model run. |
| 49 | +
|
| 50 | + 1. Creates config file and input/output directories based on the config template. |
| 51 | + 2. Prepare forcing and soil data |
| 52 | +
|
| 53 | + Args: |
| 54 | + WorkDir: path to a directory where input/output directories should be created. |
| 55 | + ForcingFileName: forcing file name. Forcing file should be in netcdf format. |
| 56 | + NumberOfTimeSteps: total number of time steps in which model runs. It can be |
| 57 | + `NA` or a number. Example `10` runs the model for 10 time steps. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + Paths to config file and input/output directories |
| 61 | + """ |
| 62 | + # update config template if needed |
| 63 | + if WorkDir: |
| 64 | + self._configs["WorkDir"] = WorkDir |
| 65 | + |
| 66 | + if ForcingFileName: |
| 67 | + self._configs["ForcingFileName"] = ForcingFileName |
| 68 | + |
| 69 | + if NumberOfTimeSteps: |
| 70 | + self._configs["NumberOfTimeSteps"] = NumberOfTimeSteps |
| 71 | + |
| 72 | + # create customized config file and input/output directories for model run |
| 73 | + _, _, self.cfg_file = config_io.create_io_dir( |
| 74 | + self._configs["ForcingFileName"], self._configs |
| 75 | + ) |
| 76 | + |
| 77 | + # read the run config file |
| 78 | + self._configs = config_io.read_config(self.cfg_file) |
| 79 | + |
| 80 | + # prepare forcing data |
| 81 | + forcing_io.prepare_forcing(self._configs) |
| 82 | + |
| 83 | + # prepare soil data |
| 84 | + soil_io.prepare_soil_data(self._configs) |
| 85 | + |
| 86 | + # set matlab log dir |
| 87 | + os.environ['MATLAB_LOG_DIR'] = str(self._configs["InputPath"]) |
| 88 | + |
| 89 | + return str(self.cfg_file) |
| 90 | + |
| 91 | + def run(self) -> str: |
| 92 | + """Run model using executable. |
| 93 | +
|
| 94 | + Args: |
| 95 | +
|
| 96 | + Returns: |
| 97 | + Tuple with stdout and stderr |
| 98 | + """ |
| 99 | + |
| 100 | + # run the model |
| 101 | + args = [f"{self.exe_file} {self.cfg_file}"] |
| 102 | + result = subprocess.run( |
| 103 | + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, check=True, |
| 104 | + ) |
| 105 | + stdout = result.stdout |
| 106 | + |
| 107 | + # TODO return log info line by line! |
| 108 | + logger.info("%s", stdout) |
| 109 | + |
| 110 | + return stdout |
| 111 | + |
| 112 | + |
| 113 | + @property |
| 114 | + def config(self) -> Dict: |
| 115 | + """Return the configurations for this model.""" |
| 116 | + return self._configs |
0 commit comments