From 8b7d1795f5dc0b7be5525071839234110a49f2d7 Mon Sep 17 00:00:00 2001 From: Lionel GUEZ <guez@lmd.ens.fr> Date: Tue, 29 Jun 2021 10:41:37 +0200 Subject: [PATCH] Replace positional argument template by optional Replace positional argument template by optional argument. This argument is now needed only in case we process several dates, since the first date is now read from the NetCDF file. --- Inst_eddies/Tests/long_tests.json | 7 ++++--- Inst_eddies/Tests/short_tests.json | 5 +++-- Inst_eddies/inst_eddies_in.py | 20 ++++++++++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Inst_eddies/Tests/long_tests.json b/Inst_eddies/Tests/long_tests.json index dc291d18..ca3fb74e 100644 --- a/Inst_eddies/Tests/long_tests.json +++ b/Inst_eddies/Tests/long_tests.json @@ -66,7 +66,8 @@ "$build_dir/Inst_eddies/inst_eddies.py", "-b", "1.625", "15.625", "-38.375", "-30.375", "$large_input_dir/dt_global_allsat_phy_l4_20060101_20190101.nc", - "dt_global_allsat_phy_l4_%Y%m%d_20190101.nc", "-l", "20060115" + "--template=dt_global_allsat_phy_l4_%Y%m%d_20190101.nc", "-l", + "20060115" ] }, { @@ -76,8 +77,8 @@ [ "$build_dir/Inst_eddies/inst_eddies.py", "$large_input_dir/dt_global_allsat_phy_l4_20060101_20190101.nc", - "dt_global_allsat_phy_l4_%Y%m%d_20190101.nc", "-l", "2006-01-02", - "-b", "5.875", "12.125", "-32.125", "-26.875" + "--template=dt_global_allsat_phy_l4_%Y%m%d_20190101.nc", "-l", + "2006-01-02", "-b", "5.875", "12.125", "-32.125", "-26.875" ] } ] diff --git a/Inst_eddies/Tests/short_tests.json b/Inst_eddies/Tests/short_tests.json index d4746041..ee871c47 100644 --- a/Inst_eddies/Tests/short_tests.json +++ b/Inst_eddies/Tests/short_tests.json @@ -303,7 +303,8 @@ "$build_dir/Inst_eddies/inst_eddies.py", "-b", "16.125", "20.875", "-38.875", "-34.125", "$large_input_dir/dt_global_allsat_phy_l4_20060101_20190101.nc", - "dt_global_allsat_phy_l4_%Y%m%d_20190101.nc", "-l", "20060102" + "--template=dt_global_allsat_phy_l4_%Y%m%d_20190101.nc", "-l", + "20060102" ] }, { @@ -313,7 +314,7 @@ [ "$build_dir/Inst_eddies/inst_eddies.py", "$src_dir/Inst_eddies/Tests/Input/Region_1/huv_region_1_2006_01_01.nc", - "huv_region_1_%Y_%m_%d.nc", "-l", "2006-01-04" + "--template=huv_region_1_%Y_%m_%d.nc", "-l", "2006-01-04" ] }, { diff --git a/Inst_eddies/inst_eddies_in.py b/Inst_eddies/inst_eddies_in.py index 74b9edb9..182ea0f5 100755 --- a/Inst_eddies/inst_eddies_in.py +++ b/Inst_eddies/inst_eddies_in.py @@ -13,13 +13,15 @@ import subprocess import shapefile import sys from dateutil import parser +import netCDF4 argparser = argparse.ArgumentParser() argparser.add_argument("first_file", help = "NetCDF file containing SSH and velocity at a " "single date") -argparser.add_argument("template", - help = "template of basename with date format") +argparser.add_argument("--template", + help = "template of basename with date format, only " + "used for several dates") argparser.add_argument("-l", "--last_date", help = "%%Y-%%m-%%d") argparser.add_argument("-b", "--bbox", nargs=4, type = float, metavar = ("xmin", "xmax", "ymin", "ymax")) @@ -33,13 +35,23 @@ inst_eddies_exe = "@CMAKE_CURRENT_BINARY_DIR@/inst_eddies" if not os.access(inst_eddies_exe, os.X_OK): sys.exit(inst_eddies_exe + " not found or not executable") -dirname, basename = path.split(args.first_file) -my_date = datetime.datetime.strptime(basename, args.template).date() +# Get the first date from the time coordinate in the NetCDF file: +with netCDF4.Dataset(args.first_file) as f: + my_date = f["time"][:].item() + +my_date = round(my_date) +my_date = datetime.date(1950, 1, 1) + datetime.timedelta(my_date) +#-- + +dirname = path.dirname(args.first_file) nc_file = args.first_file nco_instance = nco.Nco() if args.last_date: last_date = parser.parse(args.last_date).date() + + if last_date > my_date and not args.template: + sys.exit("template option required for several dates") else: last_date = my_date -- GitLab