Skip to content
Snippets Groups Projects
create_slice.py 2.41 KiB
Newer Older
Lionel GUEZ's avatar
Lionel GUEZ committed
#!/usr/bin/env python3

import os
import pathlib
GUEZ Lionel's avatar
GUEZ Lionel committed
import shutil
Lionel GUEZ's avatar
Lionel GUEZ committed

import shapefile


def create_slice(SHPC_dir, my_slice, init_date, with_proj):
Lionel GUEZ's avatar
Lionel GUEZ committed
    if os.access(f"{SHPC_dir}/Slice_{my_slice}", os.F_OK):
        shutil.rmtree(f"{SHPC_dir}/Slice_{my_slice}")

    for orient in ["Cyclones", "Anticyclones"]:
        os.makedirs(f"{SHPC_dir}/Slice_{my_slice}/{orient}")

        with shapefile.Writer(
            f"{SHPC_dir}/Slice_{my_slice}/{orient}/extremum", shapefile.POINT
        ) as w:
            w.field("ssh", "N", 13, 6)
            w.field("date", "N", 5)
            w.field("eddy_index", "N", 5)
            w.field("speed", "N", 13, 6)

        if with_proj:
            with shapefile.Writer(
                f"{SHPC_dir}/Slice_{my_slice}/{orient}/extr_proj",
                shapefile.POINT,
            ) as w:
                w.field("date", "N", 5)
                w.field("eddy_index", "N", 5)

        for fname in ["outermost_contour", "max_speed_contour"]:
            with shapefile.Writer(
                f"{SHPC_dir}/Slice_{my_slice}/{orient}/{fname}",
                shapefile.POLYGON,
            ) as w:
                w.field("r_eq_area", "N", 10, 4)
                w.field("ssh", "N", 13, 6)
                w.field("date", "N", 5)
                w.field("eddy_index", "N", 5)

        pathlib.Path(
            f"{SHPC_dir}/Slice_{my_slice}/{orient}/ishape_last.txt"
        ).touch()

    pathlib.Path(f"{SHPC_dir}/Slice_{my_slice}/timings.txt").touch()

    with open(f"{SHPC_dir}/Slice_{my_slice}/date_range.txt", "w") as f_obj:
        f_obj.write(f"{init_date} {init_date}\n")

Lionel GUEZ's avatar
Lionel GUEZ committed

if __name__ == "__main__":
    import sys

    import f90nml

    if len(sys.argv) != 3:
        sys.exit("Required arguments: SHPC-dir initial-date")
Lionel GUEZ's avatar
Lionel GUEZ committed

    if os.access("inst_eddies_nml.txt", os.R_OK):
        print("Will use inst_eddies_nml.txt")
Lionel GUEZ's avatar
Lionel GUEZ committed
    else:
        sys.exit('"inst_eddies_nml.txt" not found -- Aborting')

    inst_eddies_nml = f90nml.read("inst_eddies_nml.txt")

    try:
        my_slice = inst_eddies_nml["main_nml"]["slice"]
    except KeyError:
        my_slice = 0  # same default value as in Fortran program

    try:
        with_proj = not inst_eddies_nml["input_ssh_nml"]["uniform_lon_lat"]
    except KeyError:
        with_proj = False  # same default value as in Fortran program

    create_slice(
        SHPC_dir=sys.argv[1],
        my_slice=my_slice,
        init_date=sys.argv[2],
        with_proj=with_proj,
    )