import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler

from splineA import splineA


def fig5_15():
    ### this requires the subaxis and parseArgs files
    ### it also requires the splineA.m

    # spline interpolation

    # data
    a = 0
    b = 1
    nx = 6
    xd = np.linspace(a, b, nx)
    yd = np.zeros(nx)
    for iy in range(nx):
        yd[iy] = np.cos(2 * np.pi * xd[iy])

    # exact
    n = 400
    xp = np.linspace(a, b, n)
    y = np.zeros(n)
    for i in range(n):
        y[i] = np.cos(2 * np.pi * xp[i])

    # find splines
    ys = splineA(xd, yd, xp)
    yf = splineA(xd, yd, xp, [1, 1], [0, 0])

    plt.close("all")
    # get(gcf)
    fig, axes = plt.subplots(2, 1, figsize=(9.0, 4.6), sharex=True)
    fig.subplots_adjust(left=0.09, right=0.98, bottom=0.15, top=0.98, hspace=0.18)
    co = np.array([
        [0, 0, 1],
        [0, 0.5, 0],
        [1, 0, 0],
        [0, 0.75, 0.75],
        [0.75, 0, 0.75],
        [0.75, 0.75, 0],
        [0.25, 0.25, 0.25],
    ])
    for ax in axes:
        ax.set_prop_cycle(cycler(color=co))

    # subplot(2,1,1)
    ax = axes[0]
    ax.plot(xp, ys, "-b", linewidth=2)
    ax.plot(xp, y, "--r", linewidth=2)
    ax.plot(xd, yd, "or", markersize=9, linewidth=2)
    ax.set_xlim(a, b)
    ax.set_ylim(-1, 1)
    ax.grid(True)
    ax.set_ylabel("y-axis")
    ax.legend(
        [" Natural", " Exact"],
        loc="upper center",
        prop={"size": 12, "weight": "bold"},
    )
    ax.tick_params(labelsize=12)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")

    # subplot(2,1,2)
    ax = axes[1]
    ax.plot(xp, yf, "-b", linewidth=2)
    ax.plot(xp, y, "--r", linewidth=2)
    ax.plot(xd, yd, "or", markersize=9, linewidth=2)
    ax.set_xlim(a, b)
    ax.set_ylim(-1, 1)
    ax.grid(True)
    ax.set_xlabel("x-axis")
    ax.set_ylabel("y-axis")

    # get(legend)
    ax.legend(
        [" Clamped", " Exact"],
        loc="upper center",
        prop={"size": 12, "weight": "bold"},
    )
    ax.tick_params(labelsize=12)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")

    for ax in axes:
        for spine in ax.spines.values():
            spine.set_visible(True)

    plt.show()

    # plt.savefig('/Users/mark/Desktop/cs.eps')


if __name__ == "__main__":
    fig5_15()
