3
I’m doing a program that reads a file. txt that updates itself in the execution of another program in Fortran and creates a real-time animation of the temperature map of a board, however it is giving an error due to Fortran updating the file in real time, because in a few moments python opens the file before the Fortran loop ends and picks up an incomplete array which will therefore crash the program. Any idea how to get the two of them to run together?
Follow the code in question below
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
n=100
x = np.linspace(0, 99, num=100)
y = np.linspace(0, 99, num=100)
X,Y = np.meshgrid(x, y)
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
def animate(i):
Temp_matrix=np.empty(shape=[0,n])
file = open('Temperature.txt', 'r')
for i in range (1,n+1):
line = file.readline().strip().split()
line=np.float32(line)
Temp_matrix=np.append(Temp_matrix,[line],axis=0)
fig.clear()
CS = plt.contourf(X,Y,Temp_matrix,9)
colorb=plt.colorbar(CS)
plt.title('Placa 1x1')
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
Simply put, your mistake apparently lies in
Temp_matrix=np.append(Temp_matrix,[line],axis=0)
(...)all the input array dimensions except for the concatenation axis must match exactly
– lazyFox
But I do not understand why it is going wrong, the file being updated always has dimensions 100x100
– Laura Pereira de Castro
Unfortunately I can’t help you. But try it line by line and also print it out to make sure it’s all right.
– lazyFox
Thank you, I was able to understand the reason for the mistake, if you can see the issues I made and see if there are any proposals I would appreciate. But anyway thanks for the help so far!
– Laura Pereira de Castro
What it looks like is that your list loaded in
line
has a different amount of elements thann
. First I think you need to check with something likeif len(line) != n: print line
to see if it is truncated even due to read/write timing.– Pagotti