Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run AeroVal experiment from CLI #998

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 71 in pyaerocom/scripts/cli.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/scripts/cli.py#L71

Added line #L71 was not covered by tests


@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()

Check warning on line 88 in pyaerocom/scripts/cli.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/scripts/cli.py#L87-L88

Added lines #L87 - L88 were not covered by tests

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
Loading