diff --git a/seaice_ecdr/cli/nrt.py b/seaice_ecdr/cli/nrt.py index 2060cfe5..6e8cb062 100644 --- a/seaice_ecdr/cli/nrt.py +++ b/seaice_ecdr/cli/nrt.py @@ -7,12 +7,13 @@ import copy import datetime as dt from pathlib import Path -from typing import get_args +from typing import Literal, get_args import click +from loguru import logger from pm_tb_data._types import Hemisphere -from seaice_ecdr.cli.util import CLI_EXE_PATH, datetime_to_date, run_cmd +from seaice_ecdr.cli.util import CLI_EXE_PATH, run_cmd from seaice_ecdr.constants import DEFAULT_BASE_NRT_OUTPUT_DIR from seaice_ecdr.platforms.config import NRT_PLATFORM_START_DATES_CONFIG_FILEPATH @@ -21,9 +22,11 @@ @click.option( "-d", "--date", - required=True, + "--start-date", + required=False, + default=None, type=click.DateTime(formats=("%Y-%m-%d", "%Y%m%d", "%Y.%m.%d")), - callback=datetime_to_date, + callback=lambda _ctx, _param, value: value if value is None else value.date(), ) @click.option( "--end-date", @@ -40,11 +43,18 @@ default=None, help="If given, run temporal composite for `--date` through this end date.", ) +@click.option( + "--last-n-days", + required=False, + type=click.INT, + default=None, + help="If given, run temporal composite for the last n dates.", +) @click.option( "-h", "--hemisphere", required=True, - type=click.Choice(get_args(Hemisphere)), + type=click.Choice([*get_args(Hemisphere), "both"]), ) @click.option( "--base-output-dir", @@ -72,22 +82,43 @@ ) def cli( *, - date: dt.date, + date: dt.date | None, end_date: dt.date | None, - hemisphere: Hemisphere, + last_n_days: int | None, + hemisphere: Hemisphere | Literal["both"], base_output_dir: Path, overwrite: bool, ): + + if last_n_days and (date or end_date): + raise RuntimeError( + "`--last-n-days` is incompatible with `--date` and `--end-date`" + ) + + if last_n_days: + date = dt.date.today() - dt.timedelta(days=last_n_days) + # The end date should be the day before today. We only expect today's + # data to be available on the following day. + end_date = dt.date.today() - dt.timedelta(days=1) + if end_date is None: end_date = copy.copy(date) + logger.info(f"Creating NRT data for {date} through {end_date}") + overwrite_str = " --overwrite" if overwrite else "" - run_cmd( - f"export PLATFORM_START_DATES_CONFIG_FILEPATH={NRT_PLATFORM_START_DATES_CONFIG_FILEPATH} &&" - f"{CLI_EXE_PATH} nrt" - f" --hemisphere {hemisphere}" - f" --base-output-dir {base_output_dir}" - f" --date {date:%Y-%m-%d}" - f" --end-date {end_date:%Y-%m-%d}" + overwrite_str - ) + if hemisphere == "both": + hemispheres = ["north", "south"] + else: + hemispheres = [hemisphere] + + for hemi in hemispheres: + run_cmd( + f"export PLATFORM_START_DATES_CONFIG_FILEPATH={NRT_PLATFORM_START_DATES_CONFIG_FILEPATH} &&" + f"{CLI_EXE_PATH} nrt" + f" --hemisphere {hemi}" + f" --base-output-dir {base_output_dir}" + f" --date {date:%Y-%m-%d}" + f" --end-date {end_date:%Y-%m-%d}" + overwrite_str + )