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

from os import path

Lionel GUEZ's avatar
Lionel GUEZ committed
import shapefile
import numpy as np
import jumble_matplotlib
GUEZ Lionel's avatar
GUEZ Lionel committed

Lionel GUEZ's avatar
Lionel GUEZ committed
def read(dirname):
    """Read the three dbf files in dirname and return speed, radius and
    amplitude of outermost contour, radius and amplitude of maximum
    speed contour, as Numpy arrays.
    Select valid speed values.
Lionel GUEZ's avatar
Lionel GUEZ committed
    extr_file = path.join(dirname, "extremum")
    outer_file = path.join(dirname, "outermost_contour")
    max_speed_file = path.join(dirname, "max_speed_contour")
GUEZ Lionel's avatar
GUEZ Lionel committed

    with shapefile.Reader(extr_file) as extremum, shapefile.Reader(
        outer_file
    ) as outerm_cont, shapefile.Reader(max_speed_file) as max_speed_cont:
Lionel GUEZ's avatar
Lionel GUEZ committed
        speed = []
        rad_outer = []
        rad_speed = []
        amp_outer = []
        amp_speed = []

GUEZ Lionel's avatar
GUEZ Lionel committed
        for rec_extr, rec_outer, rec_max in zip(
            extremum.iterRecords(),
            outerm_cont.iterRecords(),
            max_speed_cont.iterRecords(),
        ):
            if rec_extr.speed != 1e4:
                speed.append(rec_extr.speed)
            rad_outer.append(rec_outer.r_eq_area)
            amp_outer.append(rec_extr.ssh - rec_outer.ssh)
GUEZ Lionel's avatar
GUEZ Lionel committed

            if rec_max.r_eq_area != -100:
                rad_speed.append(rec_max.r_eq_area)
                amp_speed.append(rec_extr.ssh - rec_max.ssh)
GUEZ Lionel's avatar
GUEZ Lionel committed
    return {
        "speed": np.array(speed),
        "rad_outer": np.array(rad_outer),
        "rad_speed": np.array(rad_speed),
        "amp_outer": np.abs(np.array(amp_outer)),
        "amp_speed": np.abs(np.array(amp_speed)),
    }
GUEZ Lionel's avatar
GUEZ Lionel committed

def plot_all(dict_list, label=None):
    """dict_list: list of dictionaries. label: list of labels, one label
    for each dictionary.
GUEZ Lionel's avatar
GUEZ Lionel committed

    fig_list = []
    fig = jumble_matplotlib.fig_hist(
GUEZ Lionel's avatar
GUEZ Lionel committed
        "speed, in m s-1", [d["speed"] for d in dict_list], label
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_distr_funct(
        "speed, when positive, in m s-1",
GUEZ Lionel's avatar
GUEZ Lionel committed
        [d["speed"][d["speed"] >= 0] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_distr_funct(
        "speed, when negative, in m s-1",
GUEZ Lionel's avatar
GUEZ Lionel committed
        [-d["speed"][d["speed"] < 0] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_hist(
        "equivalent radius of outermost contour, in km",
GUEZ Lionel's avatar
GUEZ Lionel committed
        [d["rad_outer"] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_distr_funct(
        "equivalent radius of outermost contour, in km",
GUEZ Lionel's avatar
GUEZ Lionel committed
        [d["rad_outer"] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_hist(
GUEZ Lionel's avatar
GUEZ Lionel committed
        "equivalent radius of max-speed contour, in km",
        [d["rad_speed"] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_distr_funct(
        "equivalent radius of max-speed contour, in km",
GUEZ Lionel's avatar
GUEZ Lionel committed
        [d["rad_speed"] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_hist(
GUEZ Lionel's avatar
GUEZ Lionel committed
        "amplitude of outermost contour, in m",
        [d["amp_outer"] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_distr_funct(
GUEZ Lionel's avatar
GUEZ Lionel committed
        "amplitude of outermost contour, in m",
        [d["amp_outer"] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_hist(
GUEZ Lionel's avatar
GUEZ Lionel committed
        "amplitude of max-speed contour, in m",
        [d["amp_speed"] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    fig = jumble_matplotlib.fig_distr_funct(
GUEZ Lionel's avatar
GUEZ Lionel committed
        "amplitude of max-speed contour, in m",
        [d["amp_speed"] for d in dict_list],
        label,
    )
    fig_list.append(fig)
    return fig_list
GUEZ Lionel's avatar
GUEZ Lionel committed


GUEZ Lionel's avatar
GUEZ Lionel committed
    import argparse
    import matplotlib.pyplot as plt
Lionel GUEZ's avatar
Lionel GUEZ committed

GUEZ Lionel's avatar
GUEZ Lionel committed
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "dir", help="directory containing collection of shapefiles"
    )
GUEZ Lionel's avatar
GUEZ Lionel committed
    parser.add_argument("--save", action="store_true")
GUEZ Lionel's avatar
GUEZ Lionel committed
    args = parser.parse_args()
    d = read(args.dir)
    fig_list = plot_all([d])
GUEZ Lionel's avatar
GUEZ Lionel committed

    if args.save:
        for i, fig in enumerate(fig_list):
            fig.savefig(f"figure_{i}.png")

        print(f"Created figure_[0-{len(fig_list)-1}].png")
    else:
        plt.show()