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

Read a list of files and the first date

We want to generalize our possible sources of NetCDF files. We no
longer make assumptions of their filenames. And the time step may not
be the day so we cannot infer a timestep number from the filename. So
we just read a list of files as arguments and we read the first date
from standard input. We pass the date to standard input of the Fortran
executable.
parent 1aff57d9
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
"""We are assuming that all input NetCDF files are in the same
directory and that the time coordinate is in days since 1950-1-1."""
import argparse
import datetime
......@@ -18,13 +16,9 @@ import time
import csv
argparser = argparse.ArgumentParser()
argparser.add_argument("first_file",
argparser.add_argument("file", nargs = "+",
help = "NetCDF file containing SSH and velocity at a "
"single date")
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"))
args = argparser.parse_args()
......@@ -33,26 +27,15 @@ 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")
# 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)
#--
if os.access("main_nml.txt", os.R_OK):
print("Will use main_nml.txt.")
else:
sys.exit('"main_nml.txt" not found')
dirname = path.dirname(args.first_file)
nc_file = args.first_file
d = input("Enter first date (integer value):")
d = int(d)
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
for orient in ["cyclo", "anti"]:
if os.access(f"SHPC_{orient}", os.F_OK): shutil.rmtree(f"SHPC_{orient}")
os.mkdir(f"SHPC_{orient}")
......@@ -61,8 +44,8 @@ perf_report = open("perf_report.csv", "w", newline='')
writer = csv.writer(perf_report, lineterminator = "\n")
writer.writerow(["date", "elapsed NCO", "elapsed Fortran"])
while True:
print("inst_eddies.py: Date:", my_date)
for nc_file in args.file:
print("inst_eddies.py: Date:", d)
if os.access(nc_file, os.F_OK):
my_pc = time.perf_counter()
......@@ -80,11 +63,12 @@ while True:
os.symlink("unpacked.nc", "uv.nc")
elapsed_NCO = time.perf_counter() - my_pc
my_pc = time.perf_counter()
subprocess.run(inst_eddies_exe, check = True)
subprocess.run(inst_eddies_exe, check = True, input = str(d) + "\n",
text = True)
elapsed_Fortran = time.perf_counter() - my_pc
os.remove("h.nc")
os.remove("uv.nc")
writer.writerow([my_date, elapsed_NCO, elapsed_Fortran])
writer.writerow([d, elapsed_NCO, elapsed_Fortran])
else:
print("inst_eddies.py: Missing file:", nc_file)
......@@ -94,10 +78,7 @@ while True:
n_shapes = len(shpr)
ishape_last.write(str(n_shapes - 1) + "\n")
my_date += datetime.timedelta(1)
if my_date > last_date: break
basename = my_date.strftime(args.template)
nc_file = path.join(dirname, basename)
d += 1
perf_report.close()
os.remove("unpacked.nc")
......
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