How to make python only read the file when it is complete

Asked

Viewed 173 times

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

  • But I do not understand why it is going wrong, the file being updated always has dimensions 100x100

  • Unfortunately I can’t help you. But try it line by line and also print it out to make sure it’s all right.

  • 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!

  • What it looks like is that your list loaded in line has a different amount of elements than n. First I think you need to check with something like if len(line) != n: print line to see if it is truncated even due to read/write timing.

1 answer

1


Problem:

The calculation time is usually longer than the recording time, so considering a small file (100x100), I assume you are calculating with the file open:

  1. Opens the file for reading
  2. Computes value
  3. Saves value
  4. Closes file

In this process, it is likely that Python will not be able to open the file... or that it will read the file in half before the calculation ends.

There are two ways to avoid this:

Python approach:

Use the Popen Constructor to open the file. It returns an object with method Wait() that will wait until Fortran closes the file.

Fortran approach:

I suggest we review Fortran’s own code.

You must store the values in memory and give Flush only when it is completed.

call flush(valores)

I hope I’ve helped!

:)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.