# Python Programm zum Plotten der Daten des periodisch angetriebenen Pendels (Übungsblatt 12)
# Film erzeugen mittels ...
# ffmpeg -framerate 10 -i './Pendel_%04d.jpg' -s 1100x500  AngetriebenesPendel.mp4

import matplotlib.pyplot as plt             # Python Bibliothek zum Plotten (siehe https://matplotlib.org/ )
import numpy as np                          # Python Bibliothek fuer Mathematisches (siehe https://numpy.org/ )
import matplotlib
import matplotlib.gridspec as gridspec

def Mod_Winkel(liste_winkel):
    shift=0
    lwn=[]
    for w in liste_winkel:
        if(w+shift>=np.pi):
            shift=shift-2*np.pi
        if(w+shift<=-np.pi):
            shift=shift+2*np.pi
        lwn.append(w+shift)
    return lwn

data = np.genfromtxt("./GetriebenesPendel.dat")  # Einlesen der berechneten Daten

w_m=Mod_Winkel(data[:,2])

params = {
    'figure.figsize'    : [11,5],
#    'text.usetex'       : True,
    'axes.titlesize' : 14,
    'axes.labelsize' : 16,
    'xtick.labelsize' : 14 ,
    'ytick.labelsize' : 14
}
matplotlib.rcParams.update(params)

set_l=(data[0,4]**2 + data[0,5]**2)**0.5
plot_max=set_l + 0.5
plot1_max=np.max(data[:,3])*1.1
plot1_min=np.min(data[:,3])*1.1
step = 5
fig = plt.figure()
gs = gridspec.GridSpec(1, 2, width_ratios=[1,1], wspace=0.28)
ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])

anz_bilder=400
for it in range(anz_bilder):
    print(it)
    ax1.cla()
    ax2.cla()
    ax1.set_xlabel(r"$\rm x $")
    ax1.set_ylabel(r"$\rm y $")
    ax1.set_aspect('equal')
    ax2.set_xlabel(r"$\rm \theta(t)$")
    ax2.set_ylabel(r"$\rm \dot{\theta}(t) = \frac{d \theta(t)}{dt}$")
    ax1.set_xlim(-plot_max,plot_max)
    ax1.set_ylim(-plot_max,plot_max)
    ax2.set_xlim(-np.pi,np.pi)
    ax2.set_ylim(plot1_min,plot1_max)
    ax1.scatter(0, 0, s=30, marker='o', c="black")
    ax1.scatter(data[step*it,4], data[step*it,5], s=80, marker='o', c="blue")
    ax1.plot([0,data[step*it,4]],[0,data[step*it,5]] ,c="black",linewidth=1)
    ax2.scatter(w_m[0:step*it], data[0:step*it,3],c="black",s=1,marker='o')
    ax2.scatter(w_m[step*it], data[step*it,3], s=80, marker='o', c="blue")
    # Bild-Ausgabe mit Speicherung eines individuellen Iteration-Namens
    pic_name = "./Bilder/" + "Pendel_" + "{:0>4d}".format(it) + ".jpg"
    plt.savefig(pic_name, dpi=100,bbox_inches="tight",pad_inches=0.05,format="jpg")
plt.close()
