Skip to content
Snippets Groups Projects
inst_eddies_Aviso.py 2.09 KiB
#!/usr/bin/env python3

"""Compared to inst_eddies.py, this script takes as arguments a
template, a first date and a last date, instead of a list of files. So
it is adequate when there is a date pattern in file names.

"""

import argparse
import datetime
import sys
from dateutil import parser
import inst_eddies
import glob


def filename_generator(template, first_date, last_date):
    my_date = first_date

    while my_date <= last_date:
        nc_file = my_date.strftime(template)

        # The template may contain a wild card as long as there is no
        # more than one file matching it. A missing file is ok and
        # will be dealt with in inst_eddies.py.

        path_list = glob.glob(nc_file)

        if len(path_list) == 1:
            nc_file = path_list[0]
        elif len(path_list) >= 2:
            sys.exit(
                "inst_eddies_Aviso.py:filename_generator: more than one "
                "file matching template"
            )

        yield nc_file
        my_date += datetime.timedelta(1)


argparser = argparse.ArgumentParser()
argparser.add_argument("SHPC_dir")
argparser.add_argument(
    "template",
    help="template of NetCDF file name (containing SSH "
    "and velocity at a single date), including path, "
    "with date.strftime format codes (and possible shell "
    "wildcards)",
)
argparser.add_argument("first_date", help="%%Y-%%m-%%d")
argparser.add_argument("last_date", help="%%Y-%%m-%%d")
argparser.add_argument("slice", type=int, help="slice number to create")
argparser.add_argument("periodic", choices=["t", "f"])
argparser.add_argument(
    "-b",
    "--bbox",
    nargs=4,
    type=float,
    metavar=("xmin", "xmax", "ymin", "ymax"),
)
args = argparser.parse_args()
first_date = parser.parse(args.first_date).date()
last_date = parser.parse(args.last_date).date()
d = (first_date - datetime.date(1950, 1, 1)).days

# We choose to use d as the first date index. We could choose anything.
inst_eddies.loop_inst_eddies(
    args.SHPC_dir,
    filename_generator(args.template, first_date, last_date),
    args.bbox,
    d,
    my_slice=args.slice,
    periodic=args.periodic,
)