Skip to content

Commit

Permalink
Merge pull request #998 from metno/pyaeroval-cli
Browse files Browse the repository at this point in the history
run AeroVal experiment from CLI
  • Loading branch information
avaldebe committed Feb 26, 2024
2 parents 8d067d3 + c2de306 commit 43d140d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
40 changes: 39 additions & 1 deletion pyaerocom/scripts/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
from enum import Enum
from pathlib import Path
from typing import Optional

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

Expand Down Expand Up @@ -57,5 +61,39 @@ 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"""

if config.suffix != ".json": # pragma:no cover
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()
4 changes: 2 additions & 2 deletions tests/fixtures/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from importlib import metadata
from pathlib import Path

Expand Down Expand Up @@ -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

0 comments on commit 43d140d

Please sign in to comment.