Skip to content

Commit

Permalink
Improve Tessif's CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
tZE authored and tZE committed Feb 14, 2023
1 parent e8bc7b6 commit 70fa2c1
Showing 1 changed file with 67 additions and 38 deletions.
105 changes: 67 additions & 38 deletions src/tessif/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
"""Module providing the Tessif Command Line Interface (CLI)."""
import importlib
import os
import venv
import pickle
import subprocess
import venv

import click

PLUGIN = "tessif-oemof-4-4"
import tessif.logging as tessif_logging # nopep8
from tessif.frused.paths import tessif_dir

tessif_dir = os.path.join(os.path.expanduser("~"), ".tessif.d")
venv_dir = os.path.join(tessif_dir, "plugin-venvs", PLUGIN)
python_bin = os.path.join(venv_dir, "bin", "python")
logger = tessif_logging.logger


@click.group()
Expand All @@ -20,21 +21,42 @@ def main_cli_entry():


@main_cli_entry.command()
def greet():
"""Greet the world."""
click.echo("Hello World!")
@click.option(
"-d",
"--tessif_directory",
help="(Optional) Venv directory the plugin will be installed into.",
default="~/.emacs.d/",
show_default=True,
)
def init(tessif_directory=None):
"""Initialize Tessif's working directory."""
if not tessif_directory:
tessif_directory = os.path.join(os.path.expanduser("~"), ".tessif.d")
click.echo(f"Initializing Tessif's main directory at {tessif_directory}")


@main_cli_entry.command()
@click.argument("plugin")
def install_plugin(plugin):
@click.option(
"-vd",
"--venv_dir",
help="(Optional) Venv directory the plugin will be installed into.",
default=None,
show_default=True,
)
def install(plugin, venv_dir=None):
"""Install ESSMOS-Plugin."""
if not venv_dir:
venv_dir = os.path.join(tessif_dir, "plugin-venvs", plugin)
python_bin = os.path.join(venv_dir, "bin", "python")
venv.create(venv_dir, upgrade_deps=True, with_pip=True)
subprocess.run(
[
python_bin,
"-m",
"pip",
"install",
"-U", # update plugin
plugin,
]
)
Expand All @@ -44,62 +66,69 @@ def install_plugin(plugin):
@click.option(
"--directory",
help="Directory the system_model.tsf and the results are/will be stored in",
default=tessif_dir,
show_default=True,
)
@click.option(
"-q",
"--quiet",
help="Silences the stdout info level logging",
is_flag=True,
)
@click.argument("plugin")
def tropp(directory, plugin):
"""TRansform Optimize and Post-Process."""
def tropp(directory, plugin, quiet):
"""Transform Optimize and Post-Process."""
if quiet:
tessif_logging.set_logger_level(logger, tessif_logging.logging.WARNING)

system_model_location = os.path.join(directory, "tessif_system_model.tsf")
all_resultier_location = os.path.join(directory, "all_resutlier.alr")
igr_resultier_location = os.path.join(directory, "igr_resultier.igr")
opt_sysmod_location = os.path.join(directory, "optimized_system_model.osm")

click.echo("TRansform Optimize and Post-Process!\n")
logger.info("TRansform Optimize and Post-Process!\n")

module_name = plugin.replace("-", "_")
plugin_module = importlib.import_module(module_name)

import oemof.solph

click.echo("Using Plugin:")
click.echo(f"{plugin} version {plugin_module.__version__}\n")
click.echo("Using the modelling suit:")
click.echo(f"oemof.solph version {oemof.solph.__version__}\n")
logger.info("Using Plugin:")
logger.info(f"{plugin} version {plugin_module.__version__}\n")
logger.info("Using the modelling suit:")
logger.info(f"oemof.solph version {oemof.solph.__version__}\n")

click.echo("On System Model Stored In:")
click.echo(system_model_location)
click.echo()
logger.info("On System Model Stored In:")
logger.info(system_model_location)
logger.info("")

from tessif.model.energy_system import AbstractEnergySystem
from tessif.system_model import AbstractEnergySystem

restored_es = AbstractEnergySystem(uid="This Instance Is Restored")
msg = restored_es.unpickle(system_model_location)
click.echo("Succesfully deserialized tessif system model!")
click.echo("Nodes present in the tessif system model:")
click.echo([node.uid.name for node in restored_es.nodes])
click.echo()
restored_es.unpickle(system_model_location)
logger.info("Succesfully deserialized tessif system model!")
logger.info("Nodes present in the tessif system model:")
logger.info([node.uid.name for node in restored_es.nodes])
logger.info("")

# t2o = importlib.import_module(f"{module_name}.tsf2omf")
tool_system_model = plugin_module.transform(restored_es)

click.echo("Nodes present in the oemof system model:")
click.echo([str(node) for node in tool_system_model.nodes])
logger.info("Nodes present in the oemof system model:")
logger.info([str(node) for node in tool_system_model.nodes])

optimized_system_model = plugin_module.optimize(tool_system_model)

click.echo("Optimization Succesfull!")
click.echo(f"Global Costs: {optimized_system_model.results['global']['costs']}")
logger.info("Optimization Succesfull!")
pickle.dump(optimized_system_model, open(opt_sysmod_location, "wb"))
logger.info(f"Stored optmized system model to {opt_sysmod_location}")

click.echo("Post-Process Tool System Model")
logger.info("Post-Process Tool System Model")
post_process = importlib.import_module(f"{module_name}.post_process")
global_resultier = post_process.IntegratedGlobalResultier(
optimized_system_model)
global_resultier = post_process.IntegratedGlobalResultier(optimized_system_model)
global_resultier.pickle(igr_resultier_location)
click.echo(f"Stored global results to {igr_resultier_location}")
logger.info(f"Stored global results to {igr_resultier_location}")

all_resultier = post_process.AllResultier(optimized_system_model)
all_resultier.pickle(all_resultier_location)
click.echo(f"Stored all other results to {all_resultier_location}")


# if __name__ == '__main__':
# main_cli_entry()
logger.info(f"Stored all other results to {all_resultier_location}")

0 comments on commit 70fa2c1

Please sign in to comment.