# video5.py
# Andreas Franzen, DL1YDR 2023
# start as "python3 video5.py"
# result is "video5.avi"
# The progam needs the following Debian packages: python3-matplotlib ffmpeg gnucap
import matplotlib.pyplot as plt
import numpy as np
import os
t_total_ns = 25
# L = 250 nH/m
# R = 2,23 Ohm/m
# C = 101 pF/m
# G = 14,4 uS/m
Lt = 250.e-9/200.
Rt = 2.23/200.
Ct = 101.e-12/100.
Rpt = 1./14.4e-6*100.
os.system("mkdir tmp 2>/dev/null")
os.system("rm tmp/*.png")
f1 = open("tmp/video.ckt","w")
f1.write("'RG58_2m.ckt\n")
f1.write(".subckt RG58_1cm 1 2 3\n")
f1.write("L1 1 4 " + str(Lt) + "\n")
f1.write("R1 4 5 " + str(Rt) + "\n")
f1.write("C1 5 2 " + str(Ct) + "\n")
f1.write("R3 5 2 " + str(Rpt) + "\n")
f1.write("R2 5 6 " + str(Rt) + "\n")
f1.write("L2 6 3 " + str(Lt) + "\n")
f1.write(".ends\n")
f1.write("v1 1 0 generator(10.)\n")
f1.write("R1 1 2 50.\n")
for i in range(200):
    f1.write("X" + str(i+1) + " (" + str(i+2) + " 0 " + str(i+3) + ") RG58_1cm\n")
f1.write("R2 202 0 450.\n")
#f1.write(".generator rise=0.5e-9 width=5.e-9 fall=0.5e-9\n")
f1.write(".generator rise=0. width=2.5e-9 fall=0. frequency=400.e6\n")
#f1.write(".generator rise=0.5e-9\n")
f1.write(".print tran")
for i in range(202):
    f1.write(" v(" + str(i+1) + ")")
f1.write("\n")
f1.write(".op\n")
f1.write(".tran "+str(t_total_ns+1)+".e-9 0.04e-9 >tmp/video_u.dat\n")
f1.write(".print tran")
for i in range(200):
    f1.write(" i(X" + str(i+1) + ".L1)")
f1.write(" i(X200.L2)")
f1.write("\n")
f1.write(".op\n")
f1.write(".tran "+str(t_total_ns+1)+".e-9 0.04e-9 >tmp/video_i.dat\n")
f1.close()
os.system("gnucap -b tmp/video.ckt")
f2 = open("tmp/video_u.dat","r")
d = f2.readline()
f3 = open("tmp/video_i.dat","r")
d = f3.readline()
xv = np.arange(-0.01,2.005,0.01)
x = np.arange(0,2.005,0.01)
for i in range(t_total_ns*25+1):
    s = f2.readline()
    p = s.split()
    t_ns = float(p[0])*1.e9
    del p[0]
    v = [float(item) for item in p]
    s = f3.readline()
    p = s.split()
    del p[0]
    I = [float(item) for item in p]
    vr = [i*i for i in range(201)]
    vl = [i*i for i in range(201)]
    Ir = [i*i for i in range(201)]
    Il = [i*i for i in range(201)]
    for j in range(201):
        vr[j] = (50.*I[j]+v[j+1])/2.
        vl[j] = (-50.*I[j]+v[j+1])/2.
        Ir[j] = (I[j]+v[j+1]/50.)/2.
        Il[j] = (I[j]-v[j+1]/50.)/2.
#    if i == 200:
#        print(i)
#        print(v)
    plt.rcParams['font.family'] = 'monospace'
    fig,(ax0,ax1,ax2,ax3,ax4,ax5) = plt.subplots(6,1,dpi=100,figsize=(12.8,7.2))
    fig.suptitle("u0=10V, 400MHz for 2.5ns, R1=50\u03A9, R2=450\u03A9, 2m of RG58, SWR=9, "+f'{round(t_ns):2d}'+'ns')
    ax0.grid('on')
    ax1.grid('on')
    ax2.grid('on')
    ax3.grid('on')
    ax4.grid('on')
    ax5.grid('on')
    ax0.set_xticklabels([])
    ax1.set_xticklabels([])
    ax2.set_xticklabels([])
    ax3.set_xticklabels([])
    ax4.set_xticklabels([])
    ax5.set_xlabel('position in m')
    ax0.set_ylabel('u in V')
    ax1.set_ylabel('i in A')
    ax2.set_ylabel('u_r in V')
    ax3.set_ylabel('i_r in A')
    ax4.set_ylabel('u_l in V')
    ax5.set_ylabel('i_l in A')
    ax0.set_ylim(ymin=-10.5,ymax=10.5)
    ax0.plot(xv,v,color='r',linewidth=2.5)
    ax1.set_ylim(ymin=-0.21,ymax=0.21)
    ax1.plot(x,I,color='b',linewidth=2.5)
    ax2.set_ylim(ymin=-10.5,ymax=10.5)
    ax2.plot(x,vr,color='r',linewidth=2.5)
    ax3.set_ylim(ymin=-0.21,ymax=0.21)
    ax3.plot(x,Ir,color='b',linewidth=2.5)
    ax4.set_ylim(ymin=-10.5,ymax=10.5)
    ax4.plot(x,vl,color='r',linewidth=2.5)
    ax5.set_ylim(ymin=-0.21,ymax=0.21)
    ax5.plot(x,Il,color='b',linewidth=2.5)
    filename = 'tmp/video'+str(i).zfill(4)+'.png'
    print(filename)
    plt.tight_layout()
    plt.savefig(filename,format='png')
    plt.close(fig)
#        plt.show()
#    for p1 in p:
#        print(p1)
#        p1 = float(p1)
#        print(p1)
#    v = float(s)
#    if i == 100:
#    print(p)
f2.close()
f3.close()
os.system("rm video5.avi;ffmpeg -i tmp/video%04d.png video5.avi")
