Skip to content
Snippets Groups Projects
Commit d408a231 authored by Lionel GUEZ's avatar Lionel GUEZ
Browse files

Use template and dates as positional arguments

We rewrite `inst_eddies_Aviso.py` so it is more convenient to use. The
script is only useful for several input files. Otherwise we can
directly use `inst_eddies.py`. So instead of optional arguments
template and last date, we use positional arguments. Then there is no
point in distinguishing dirname and basename. Also, it does not make
sense any longer to read the date from the content of the first
file. The script becomes more general: we do not assume that all input
NetCDF files are in the same -directory and that the time coordinate
is in days since 1950-1-1. The interface is also clearer.
parent c6f9247d
No related branches found
No related tags found
No related merge requests found
...@@ -98,11 +98,8 @@ ...@@ -98,11 +98,8 @@
"15.625", "15.625",
"-38.375", "-38.375",
"-30.375", "-30.375",
"$Aviso_DT_dir/dt_global_allsat_phy_l4_20060101_20210726.nc", "$Aviso_DT_dir/dt_global_allsat_phy_l4_%Y%m%d_20210726.nc",
"0", "2006-01-01", "2006-01-15", "0"
"--template=dt_global_allsat_phy_l4_%Y%m%d_20210726.nc",
"-l",
"20060115"
], ],
"exclude_cmp": ["timings.txt", "perf_report.csv"] "exclude_cmp": ["timings.txt", "perf_report.csv"]
}, },
...@@ -131,11 +128,8 @@ ...@@ -131,11 +128,8 @@
], ],
"command": [ "command": [
"$build_dir/Inst_eddies/inst_eddies_Aviso.py", "$build_dir/Inst_eddies/inst_eddies_Aviso.py",
"$Eurec4A_NRT_dir/dataset-duacs-global-allsat-phy-2020-01-31.nc", "$Eurec4A_NRT_dir/dataset-duacs-global-allsat-phy-%Y-%m-%d.nc",
"0", "2020-01-31", "2020-02-02", "0"
"--template=dataset-duacs-global-allsat-phy-%Y-%m-%d.nc",
"-l",
"20200202"
], ],
"description": "Date 2020-02-01 is missing. Test of the script with a missing date.", "description": "Date 2020-02-01 is missing. Test of the script with a missing date.",
"exclude_cmp": ["timings.txt", "perf_report.csv"] "exclude_cmp": ["timings.txt", "perf_report.csv"]
...@@ -144,15 +138,9 @@ ...@@ -144,15 +138,9 @@
"description": "This is a case where the speed is not defined on a whole segment.", "description": "This is a case where the speed is not defined on a whole segment.",
"command": [ "command": [
"$build_dir/Inst_eddies/inst_eddies_Aviso.py", "$build_dir/Inst_eddies/inst_eddies_Aviso.py",
"--template=dt_global_allsat_phy_l4_%Y%m%d_20210726.nc", "$Aviso_DT_dir/dt_global_allsat_phy_l4_%Y%m%d_20210726.nc",
"--last_date=1993-03-31", "1993-01-01", "1993-03-31", "0",
"--bbox", "--bbox", "20", "30", "35", "42"
"20",
"30",
"35",
"42",
"$Aviso_DT_dir/dt_global_allsat_phy_l4_19930101_20210726.nc",
"0"
], ],
"create_file": [ "create_file": [
"config_nml.txt", "config_nml.txt",
...@@ -164,15 +152,9 @@ ...@@ -164,15 +152,9 @@
"description": "This is used in several tests in Overlap.", "description": "This is used in several tests in Overlap.",
"command": [ "command": [
"$build_dir/Inst_eddies/inst_eddies_Aviso.py", "$build_dir/Inst_eddies/inst_eddies_Aviso.py",
"--template=dt_global_allsat_phy_l4_%Y%m%d_20210726.nc", "$Aviso_DT_dir/dt_global_allsat_phy_l4_%Y%m%d_20210726.nc",
"--last_date=2006-01-15", "2006-01-01", "2006-01-15", "0",
"--bbox", "--bbox", "0.125", "29.875", "-59.875", "-15.125"
"0.125",
"29.875",
"-59.875",
"-15.125",
"$Aviso_DT_dir/dt_global_allsat_phy_l4_20060101_20210726.nc",
"0"
], ],
"create_file": [ "create_file": [
"config_nml.txt", "config_nml.txt",
......
...@@ -349,11 +349,8 @@ ...@@ -349,11 +349,8 @@
], ],
"command": [ "command": [
"$build_dir/Inst_eddies/inst_eddies_Aviso.py", "$build_dir/Inst_eddies/inst_eddies_Aviso.py",
"--template=huv_region_1_%Y_%m_%d.nc", "$src_dir/Inst_eddies/Tests/Input/Region_1/huv_region_1_%Y_%m_%d.nc",
"-l", "2006-01-01", "2006-01-04", "0"
"2006-01-04",
"$src_dir/Inst_eddies/Tests/Input/Region_1/huv_region_1_2006_01_01.nc",
"0"
], ],
"input": "20454\n", "input": "20454\n",
"exclude_cmp": ["timings.txt", "perf_report.csv"] "exclude_cmp": ["timings.txt", "perf_report.csv"]
...@@ -440,15 +437,9 @@ ...@@ -440,15 +437,9 @@
"description": "This is used in several tests in Overlap.", "description": "This is used in several tests in Overlap.",
"command": [ "command": [
"$build_dir/Inst_eddies/inst_eddies_Aviso.py", "$build_dir/Inst_eddies/inst_eddies_Aviso.py",
"--template=dt_global_allsat_phy_l4_%Y%m%d_20210726.nc", "$Aviso_DT_dir/dt_global_allsat_phy_l4_%Y%m%d_20210726.nc",
"--last_date=2006-01-15", "2006-01-01", "2006-01-15", "0",
"--bbox", "--bbox", "16.125", "20.875", "-38.875", "-34.125"
"16.125",
"20.875",
"-38.875",
"-34.125",
"$Aviso_DT_dir/dt_global_allsat_phy_l4_20060101_20210726.nc",
"0"
], ],
"create_file": ["config_nml.txt", "&config_nml /\n"], "create_file": ["config_nml.txt", "&config_nml /\n"],
"exclude_cmp": ["timings.txt", "perf_report.csv"] "exclude_cmp": ["timings.txt", "perf_report.csv"]
......
#!/usr/bin/env python3 #!/usr/bin/env python3
"""We are assuming that all input NetCDF files are in the same """Compared to inst_eddies.py, this script takes as arguments a
directory and that the time coordinate is in days since 1950-1-1.""" template, a first date and a last date, instead of a list of files. So
it as adequate when there is a date pattern in file names.
"""
import argparse import argparse
import datetime import datetime
from os import path
import sys import sys
from dateutil import parser from dateutil import parser
import netCDF4
import inst_eddies import inst_eddies
import glob import glob
def filename_generator(first_file, first_date, last_date, template): def filename_generator(template, first_date, last_date):
dirname = path.dirname(first_file)
nc_file = first_file
my_date = first_date my_date = first_date
while True: while my_date <= last_date:
yield nc_file nc_file = my_date.strftime(template)
my_date += datetime.timedelta(1)
if my_date > last_date: break
basename = my_date.strftime(template)
nc_file = path.join(dirname, basename)
# The template may contain a wild card as long as there is no # 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 # more than one file matching it. A missing file is ok and
...@@ -36,41 +31,26 @@ def filename_generator(first_file, first_date, last_date, template): ...@@ -36,41 +31,26 @@ def filename_generator(first_file, first_date, last_date, template):
sys.exit("inst_eddies_Aviso.py:filename_generator: more than one " sys.exit("inst_eddies_Aviso.py:filename_generator: more than one "
"file matching template") "file matching template")
yield nc_file
my_date += datetime.timedelta(1)
argparser = argparse.ArgumentParser() argparser = argparse.ArgumentParser()
argparser.add_argument("first_file", argparser.add_argument("template",
help = "NetCDF file containing SSH and velocity at a " help = "template of NetCDF file name (containing SSH "
"single date") "and velocity at a single date), including path, "
argparser.add_argument("slice", type = int, "with date.strftime format codes (and possible shell "
help = "slice number to create") "wildcards)")
argparser.add_argument("--template", argparser.add_argument("first_date", help = "%%Y-%%m-%%d")
help = "template of basename with date.strftime format " argparser.add_argument("last_date", help = "%%Y-%%m-%%d")
"codes (and possible shell wildcards), only used for " argparser.add_argument("slice", type = int, help = "slice number to create")
"several dates")
argparser.add_argument("-l", "--last_date", help = "%%Y-%%m-%%d")
argparser.add_argument("-b", "--bbox", nargs=4, type = float, argparser.add_argument("-b", "--bbox", nargs=4, type = float,
metavar = ("xmin", "xmax", "ymin", "ymax")) metavar = ("xmin", "xmax", "ymin", "ymax"))
args = argparser.parse_args() args = argparser.parse_args()
first_date = parser.parse(args.first_date).date()
# Get the first date from the time coordinate in the NetCDF file. (In last_date = parser.parse(args.last_date).date()
# general, we cannot get it from the name of the first file because d = (first_date - datetime.date(1950, 1, 1)).days
# the first file may be the only file, and then there is no template
# option and the file name might not contain the date.)
with netCDF4.Dataset(args.first_file) as f:
d = f["time"][:].item()
d = round(d)
first_date = datetime.date(1950, 1, 1) + datetime.timedelta(d)
#--
if args.last_date:
last_date = parser.parse(args.last_date).date()
if last_date > first_date and not args.template:
sys.exit("template option required for several dates")
else:
last_date = first_date
# We choose to use d as the first date index. We could choose anything. # We choose to use d as the first date index. We could choose anything.
inst_eddies.loop_inst_eddies(filename_generator(args.first_file, first_date, inst_eddies.loop_inst_eddies(filename_generator(args.template, first_date,
last_date, args.template), last_date),
args.bbox, d, my_slice = args.slice) args.bbox, d, my_slice = args.slice)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment