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

Create a function from the body of the script

So we can call it from another script with a generated list of files.
parent 78045196
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,68 @@ import netCDF4
import time
import csv
def loop_inst_eddies(files, bbox, d):
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")
if os.access("main_nml.txt", os.R_OK):
print("Will use main_nml.txt.")
else:
sys.exit('"main_nml.txt" not found')
nco_instance = nco.Nco()
for orient in ["cyclo", "anti"]:
if os.access(f"SHPC_{orient}", os.F_OK): shutil.rmtree(f"SHPC_{orient}")
os.mkdir(f"SHPC_{orient}")
perf_report = open("perf_report.csv", "w", newline='')
writer = csv.writer(perf_report, lineterminator = "\n")
writer.writerow(["date", "elapsed NCO", "elapsed Fortran"])
for nc_file in files:
print("inst_eddies.py: Date:", d)
print("Input file:", nc_file)
if os.access(nc_file, os.F_OK):
my_pc = time.perf_counter()
if bbox:
xmin, xmax, ymin, ymax = bbox
nco_instance.ncks(nc_file, output = "cropped.nc",
options = [f"--dimension=longitude,{xmin},{xmax}",
f"--dimension=latitude,{ymin},{ymax}"])
nc_file = "cropped.nc"
nco_instance.ncpdq(nc_file, output = "unpacked.nc",
options = ["--unpack"])
os.symlink("unpacked.nc", "h.nc")
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, input = str(d) + "\n",
text = True)
elapsed_Fortran = time.perf_counter() - my_pc
os.remove("h.nc")
os.remove("uv.nc")
writer.writerow([d, elapsed_NCO, elapsed_Fortran])
else:
print("inst_eddies.py: Missing file:", nc_file)
for orient in ["cyclo", "anti"]:
with open(f"SHPC_{orient}/ishape_last.txt", "a") as ishape_last, \
shapefile.Reader(f"SHPC_{orient}/extremum") as shpr:
n_shapes = len(shpr)
ishape_last.write(str(n_shapes - 1) + "\n")
d += 1
perf_report.close()
os.remove("unpacked.nc")
if bbox: os.remove("cropped.nc")
argparser = argparse.ArgumentParser()
argparser.add_argument("file", nargs = "+",
help = "NetCDF file containing SSH and velocity at a "
......@@ -28,63 +90,4 @@ argparser.add_argument("-b", "--bbox", nargs=4, type = float,
args = argparser.parse_args()
d = input("Enter first date (integer value):")
d = int(d)
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")
if os.access("main_nml.txt", os.R_OK):
print("Will use main_nml.txt.")
else:
sys.exit('"main_nml.txt" not found')
nco_instance = nco.Nco()
for orient in ["cyclo", "anti"]:
if os.access(f"SHPC_{orient}", os.F_OK): shutil.rmtree(f"SHPC_{orient}")
os.mkdir(f"SHPC_{orient}")
perf_report = open("perf_report.csv", "w", newline='')
writer = csv.writer(perf_report, lineterminator = "\n")
writer.writerow(["date", "elapsed NCO", "elapsed Fortran"])
for nc_file in args.file:
print("inst_eddies.py: Date:", d)
print("Input file:", nc_file)
if os.access(nc_file, os.F_OK):
my_pc = time.perf_counter()
if args.bbox:
xmin, xmax, ymin, ymax = args.bbox
nco_instance.ncks(nc_file, output = "cropped.nc",
options = [f"--dimension=longitude,{xmin},{xmax}",
f"--dimension=latitude,{ymin},{ymax}"])
nc_file = "cropped.nc"
nco_instance.ncpdq(nc_file, output = "unpacked.nc",
options = ["--unpack"])
os.symlink("unpacked.nc", "h.nc")
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, input = str(d) + "\n",
text = True)
elapsed_Fortran = time.perf_counter() - my_pc
os.remove("h.nc")
os.remove("uv.nc")
writer.writerow([d, elapsed_NCO, elapsed_Fortran])
else:
print("inst_eddies.py: Missing file:", nc_file)
for orient in ["cyclo", "anti"]:
with open(f"SHPC_{orient}/ishape_last.txt", "a") as ishape_last, \
shapefile.Reader(f"SHPC_{orient}/extremum") as shpr:
n_shapes = len(shpr)
ishape_last.write(str(n_shapes - 1) + "\n")
d += 1
perf_report.close()
os.remove("unpacked.nc")
if args.bbox: os.remove("cropped.nc")
loop_inst_eddies(args.file, args.bbox, d)
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