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

import shapefile
import numpy as np
import matplotlib.pyplot as plt

def plot_distr_funct(X, xlabel):
    plt.figure()
    plt.hist(X, bins="auto")
    plt.xlabel(xlabel)
    plt.ylabel("number of occurrences")
    
    X = np.array(X)
    X.sort()
    nx = np.size(X)

    print(xlabel + ":")
    print("minimum value:", X[0])
    print("maximum value:", X[-1])
    print("number of values:", nx, "\n")

    plt.figure()
    plt.plot(X, (1 + np.arange(nx)) / nx)
    plt.xlabel(xlabel)
    plt.ylabel("distribution function")

def get_var(end_filename):
    with shapefile.Reader("extremum" + end_filename) as extremum, \
         shapefile.Reader("outermost_contour" + end_filename) as outermost_cont:
        speed = []
        r_outer = []

        for rec_extr, rec_outer in zip(extremum.iterRecords(),
                                       outermost_cont.iterRecords()):
            if rec_extr.speed != 1e4: speed.append(rec_extr.speed)
            if rec_extr.valid == 1: r_outer.append(rec_outer.r_eq_area)

    with shapefile.Reader("max_speed_contour" + end_filename) \
         as max_speed_contour:
        r_speed = []

        for rec in max_speed_contour.iterRecords():
            if rec.r_eq_area != - 100: r_speed.append(rec.r_eq_area)

    return speed, r_outer, r_speed

if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument("-e", "--end_filename",
                        help = "add character string after "
                        "extremum, outermost contour and max_speed_contour",
                        default = "")
    args = parser.parse_args()

    speed, r_outer, r_speed = get_var(args.end_filename)

    plot_distr_funct(speed, "speed, in m s-1")
    plt.axhline(0.5, linestyle = "--", color = "black")
    plt.axvline(linestyle = "--", color = "black")

    plot_distr_funct(r_outer, "equivalent radius of outermost contour, in km")
    plot_distr_funct(r_speed, "equivalent radius of max-speed contour, in km")

    plt.show()