def makeAnimation(filename , movieName = None, nShaddow = 20, xRatio= 1.0, version = "foamStar" , rate = 1) :
"""
Make a 2D animation of the wave field.
"""
print "Making animation file : " , movieName
nShaddow = max(1,nShaddow)
data = pd.read_csv(filename, comment = "#" , header=None , delim_whitespace=True, dtype = float).as_matrix()
if version == "foamStar" :
with open(filename , "r") as f :
f.readline() ; f.readline() ; xVal = [float(x) for x in f.readline().split()[2:]]
#Remove x = 0.0
if 0.0 in xVal :
i = xVal.index(0.0)
xVal.pop(i)
data = np.delete(data, i+1 , axis = 1)
xVal = np.array(xVal)
xVal /= xRatio
else :
xVal = np.linspace(0,1 , len(data[1,:]) -1 )
fig, ax = plt.subplots()
ls = []
for i in range(nShaddow) :
if i == 0 :
color = "black"
else :
color = "blue"
ltemp, = ax.plot([], [], lw=1 , alpha = 1-i*1./nShaddow , color = color)
ls.append(ltemp)
ax.grid(True)
ax.set_xlim( min(xVal) , max(xVal) )
ax.set_ylim( -1.5 , +1.5 )
ax.set_xlabel("x")
ax.set_ylabel("Elevation(m)")
def run(itime):
ax.set_title("{}s".format(data[itime*rate,0]) )
for s in range(nShaddow):
if itime > s :
ls[s].set_data( xVal , data[ rate*(itime - s),1:] )
return ls
ani = animation.FuncAnimation( fig , run, range(len(data)), blit=True, interval=30, repeat=False)
if movieName is None :
plt.show()
else :
mywriter = animation.FFMpegWriter(fps = 25 , codec="libx264")
ani.save( movieName +'.mp4',writer=mywriter)
评论列表
文章目录