Skip to content

Commit

Permalink
Add "gribmagic unity list" subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Oct 12, 2021
1 parent 856ffaa commit 4502f98
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -69,8 +69,12 @@ make test

### Ad hoc usage
```
# List available labels.
gribmagic unity list
# Acquire data.
export GM_DATA_PATH=.gribmagic-data
gribmagic unity --model=dwd-icon-eu --timestamp=2021-10-03T00:00:00Z
gribmagic unity acquire --model=dwd-icon-eu --timestamp=2021-10-03T00:00:00Z
```

### Configuration
Expand All @@ -96,7 +100,7 @@ docker build --tag gribmagic .

and then invoke it like
```
docker run -it --volume=$PWD/.gribmagic-data:/var/spool/gribmagic gribmagic:latest gribmagic unity --model=dwd-icon-eu --timestamp=2021-10-03T00:00:00Z
docker run -it --volume=$PWD/.gribmagic-data:/var/spool/gribmagic gribmagic:latest gribmagic unity acquire --model=dwd-icon-eu --timestamp=2021-10-03T00:00:00Z
```

---
Expand Down
21 changes: 18 additions & 3 deletions gribmagic/commands.py
@@ -1,4 +1,5 @@
"""Command line entry points for NWP data download"""
import json
import logging
from datetime import datetime
from typing import Union
Expand All @@ -7,6 +8,7 @@

from gribmagic.unity.core import run_model_download
from gribmagic.unity.enumerations.weather_models import WeatherModels
from gribmagic.unity.modules.config.parse_configurations import parse_model_config
from gribmagic.util import setup_logging

logger = logging.getLogger(__name__)
Expand All @@ -20,14 +22,27 @@

@click.group()
@click.pass_context
def main(ctx):
def cli(ctx):
pass


@main.command()
@cli.group(help="Unified NWP data downloader")
@click.pass_context
def unity(ctx):
pass


@unity.command(name="list", help="List available NWP data labels")
def unity_list():
model_config = parse_model_config()
labels = [label for label in model_config.keys() if not label.endswith("-base")]
print(json.dumps(labels, indent=4))


@unity.command(name="acquire", help="Acquire NWP data")
@click.option("--model", required=True, help="The weather model name.")
@click.option("--timestamp", required=True, help="The initialization timestamp.")
def unity(model: Union[str, WeatherModels], timestamp: Union[str, datetime]):
def unity_acquire(model: Union[str, WeatherModels], timestamp: Union[str, datetime]):
logger.info("Starting GribMagic")
results = run_model_download(
weather_model=model, initialization_timestamp=timestamp
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -73,7 +73,7 @@
},
entry_points={
'console_scripts': [
'gribmagic = gribmagic.commands:main',
'gribmagic = gribmagic.commands:cli',
],
},
)
34 changes: 30 additions & 4 deletions tests/unity/test_command.py → tests/unity/test_commands.py
@@ -1,3 +1,4 @@
import json
import logging
import os
import re
Expand All @@ -10,7 +11,7 @@
import responses
from click.testing import CliRunner

from gribmagic.commands import main
from gribmagic.commands import cli
from gribmagic.unity.enumerations.weather_models import WeatherModels
from gribmagic.unity.models import MODEL_CONFIG
from gribmagic.unity.modules.config.constants import KEY_VARIABLES
Expand All @@ -19,12 +20,37 @@
model_config.update({KEY_VARIABLES: ["air_temperature_2m", "relative_humidity_2m"]})


def test_command_gribmagic_unity_list(capsys):
runner = CliRunner()
result = runner.invoke(
cli=cli,
args=[
"unity",
"list",
],
# catch_exceptions=False,
)
labels = json.loads(result.output)
assert labels == [
"ncep-gfs-025",
"ncep-gfs-050",
"ncep-gfs-100",
"dwd-icon-global",
"dwd-icon-eu",
"dwd-icon-eu-eps",
"dwd-cosmo-d2",
"dwd-cosmo-d2-eps",
"meteo-france-arome",
"knmi-harmonie",
]


@responses.activate
@patch(
"gribmagic.unity.models.MODEL_CONFIG",
{WeatherModels.DWD_ICON_EU.value: model_config},
)
def test_command_gribmagic_unity(caplog):
def test_command_gribmagic_unity_acquire(caplog):

responses.add(
method=responses.GET,
Expand All @@ -40,9 +66,10 @@ def test_command_gribmagic_unity(caplog):
with caplog.at_level(logging.DEBUG):
runner = CliRunner()
result = runner.invoke(
cli=main,
cli=cli,
args=[
"unity",
"acquire",
"--model=dwd-icon-eu",
"--timestamp=2021-10-03T00:00:00Z",
],
Expand All @@ -52,7 +79,6 @@ def test_command_gribmagic_unity(caplog):
# result.exit_code == 1 -- why!?
# assert result.exit_code == 0

# assert result.output == 'Hello Peter!\n'
assert "Starting GribMagic" in caplog.text
assert (
"WeatherModels.DWD_ICON_EU: Accessing parameter 'air_temperature_2m'"
Expand Down

0 comments on commit 4502f98

Please sign in to comment.