From f551791500f133299b2b1448b4ac4863a948c2a5 Mon Sep 17 00:00:00 2001 From: Alvaro Valdebenito Date: Fri, 16 Feb 2024 15:02:07 +0100 Subject: [PATCH 1/3] typo --- tests/fixtures/data_access.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/fixtures/data_access.py b/tests/fixtures/data_access.py index 241169521..7b41af313 100644 --- a/tests/fixtures/data_access.py +++ b/tests/fixtures/data_access.py @@ -19,7 +19,7 @@ logger = logging.getLogger(__name__) #: tarfile to download -TESTATA_FILE = "testdata-minimal.tar.gz.20231116" +TESTDATA_FILE = "testdata-minimal.tar.gz.20231116" minimal_dataset = pooch.create( path=const.OUTPUTDIR, # ~/MyPyaerocom/ @@ -36,7 +36,7 @@ ) -def download(file_name: str = TESTATA_FILE): +def download(file_name: str = TESTDATA_FILE): """download tar file to ~/MyPyaerocom/ unpack contents into ~/MyPyaerocom/testdata-minimal/""" logger.debug(f"fetch {file_name} to {minimal_dataset.path}") minimal_dataset.path.joinpath("tmp").mkdir(parents=True, exist_ok=True) From c440a529e07c2e6347c9fbc9080b0435aa3c6f3c Mon Sep 17 00:00:00 2001 From: Alvaro Valdebenito Date: Fri, 16 Feb 2024 15:06:57 +0100 Subject: [PATCH 2/3] run AeroVal experiment from CLI --- pyaerocom/scripts/cli.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/pyaerocom/scripts/cli.py b/pyaerocom/scripts/cli.py index 305c314b0..48e513c04 100644 --- a/pyaerocom/scripts/cli.py +++ b/pyaerocom/scripts/cli.py @@ -1,3 +1,6 @@ +import json +from enum import Enum +from pathlib import Path from typing import Optional import typer @@ -57,5 +60,41 @@ def ppiaccess(): print("True") if const.has_access_lustre else print("False") +class Verbosity(str, Enum): + ERROR = "ERROR" + WARNING = "WARNING" + INFO = "INFO" + DEBUG = "DEBUG" + + def __str__(self) -> str: + return self.value + + +@main.command() +def aeroval( + config: Path = typer.Argument( + ..., help="experiment configuration (JSON)", exists=True, readable=True + ), + reuse_coldata: bool = typer.Option( + True, "--reuse-coldata/--clean-coldata", help="reuse/clean colocated data before running" + ), + verbosity: Verbosity = typer.Option(Verbosity.ERROR, help="console logger level"), +): + """run AeroVal experiment as descried on config file""" + from pyaerocom import change_verbosity + from pyaerocom.aeroval import EvalSetup, ExperimentProcessor + + if config.suffix != ".json": + typer.echo(f"{config.suffix=} != '.json'") + raise typer.Abort() + + change_verbosity(verbosity) + CFG = json.loads(config.read_text()) + stp = EvalSetup(**CFG) + proc = ExperimentProcessor(stp) + proc.exp_output.delete_experiment_data(also_coldata=not reuse_coldata) + proc.run() + + if __name__ == "__main__": main() From c2de306e726c1a57d8bfa214a02a2ec6537e8ae7 Mon Sep 17 00:00:00 2001 From: Alvaro Valdebenito Date: Fri, 16 Feb 2024 17:56:39 +0100 Subject: [PATCH 3/3] add test --- pyaerocom/scripts/cli.py | 7 +++---- tests/test_cli.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pyaerocom/scripts/cli.py b/pyaerocom/scripts/cli.py index 48e513c04..ea96af23e 100644 --- a/pyaerocom/scripts/cli.py +++ b/pyaerocom/scripts/cli.py @@ -5,7 +5,8 @@ import typer -from pyaerocom import __package__, __version__, const +from pyaerocom import __package__, __version__, change_verbosity, const +from pyaerocom.aeroval import EvalSetup, ExperimentProcessor from pyaerocom.io.cachehandler_ungridded import list_cache_files from pyaerocom.io.utils import browse_database @@ -81,10 +82,8 @@ def aeroval( verbosity: Verbosity = typer.Option(Verbosity.ERROR, help="console logger level"), ): """run AeroVal experiment as descried on config file""" - from pyaerocom import change_verbosity - from pyaerocom.aeroval import EvalSetup, ExperimentProcessor - if config.suffix != ".json": + if config.suffix != ".json": # pragma:no cover typer.echo(f"{config.suffix=} != '.json'") raise typer.Abort() diff --git a/tests/test_cli.py b/tests/test_cli.py index ffb7f70d8..a4c723b3f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,4 @@ +import json from importlib import metadata from pathlib import Path @@ -65,3 +66,25 @@ def test_browse(): def test_ppiaccess(): result = runner.invoke(main, ["ppiaccess"]) assert result.exit_code == 0 + + +@pytest.fixture() +def config_json(monkeypatch, tmp_path: Path, eval_config: dict) -> Path: + def do_not_run(self, model_name=None, obs_name=None, var_list=None, update_interface=True): + assert model_name is None + assert obs_name is None + assert var_list is None + assert update_interface is True + + monkeypatch.setattr("pyaerocom.scripts.cli.ExperimentProcessor.run", do_not_run) + + path = tmp_path / "conf.json" + path.write_text(json.dumps(eval_config)) + return path + + +@pytest.mark.parametrize("cfg", ("cfgexp1",)) +def test_aeroval(config_json: Path): + assert config_json.is_file() + result = runner.invoke(main, ["aeroval", str(config_json)]) + assert result.exit_code == 0