Skip to content
Snippets Groups Projects
inst_eddies_Aviso.py 2.23 KiB
Newer Older
#!/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
Lionel GUEZ's avatar
Lionel GUEZ committed
it is adequate when there is a date pattern in file names.

import argparse
import datetime
import sys
import glob
import os
from dateutil import parser
import f90nml
GUEZ Lionel's avatar
GUEZ Lionel committed

def filename_generator(template, first_date, last_date):
    my_date = first_date
GUEZ Lionel's avatar
GUEZ Lionel committed

    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:
GUEZ Lionel's avatar
GUEZ Lionel committed
            sys.exit(
                "inst_eddies_Aviso.py:filename_generator: more than one "
                "file matching template"
            )
        yield nc_file
        my_date += datetime.timedelta(1)

GUEZ Lionel's avatar
GUEZ Lionel committed

argparser = argparse.ArgumentParser()
argparser.add_argument("SHPC_dir")
GUEZ Lionel's avatar
GUEZ Lionel committed
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(
    "-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
if os.access("inst_eddies_nml.txt", os.R_OK):
    print("Will use inst_eddies_nml.txt, ignoring date in dates_nml...")
else:
    sys.exit('"inst_eddies_nml.txt" not found -- Aborting')

inst_eddies_nml = f90nml.read("inst_eddies_nml.txt")
Lionel GUEZ's avatar
Lionel GUEZ committed
# We choose to use d as the first date index. We could choose anything.
inst_eddies_nml["dates_nml"]["date"] = d

GUEZ Lionel's avatar
GUEZ Lionel committed
inst_eddies.loop_inst_eddies(
GUEZ Lionel's avatar
GUEZ Lionel committed
    filename_generator(args.template, first_date, last_date),
    args.bbox,
    inst_eddies_nml,
GUEZ Lionel's avatar
GUEZ Lionel committed
)