-
GUEZ Lionel authoredGUEZ Lionel authored
inst_eddies_Aviso.py 2.80 KiB
#!/usr/bin/env python3
"""This script creates the directory {SHPC_dir}/Slice_{my_slice},
containing a time slice of instantaneous eddies. If
{SHPC_dir}/Slice_{my_slice} already exists then it is replaced.
Use this script if you have one file per date, with the three fields
ssh, u and v together in this file, and you want to specify the input
files for all dates through a template file name.
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.
The script reads the file inst_eddies_nml.txt. The slice number, in
particular, is taken from this file.
"""
import argparse
import datetime
import sys
import glob
import os
from dateutil import parser
import f90nml
import inst_eddies
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(
"-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...")
else:
sys.exit('"inst_eddies_nml.txt" not found -- Aborting')
inst_eddies_nml = f90nml.read("inst_eddies_nml.txt")
# We choose to use d as the first date index. We could choose anything.
if "uniform_lon_lat" not in inst_eddies_nml["input_ssh_nml"]:
inst_eddies_nml["input_ssh_nml"]["uniform_lon_lat"] = True
# (same default value as in Fortran program)
inst_eddies.loop_inst_eddies(
args.SHPC_dir,
d,
filename_generator(args.template, first_date, last_date),
args.bbox,
inst_eddies_nml,
)