I have a python code (let's call it 'pycode.py') that creates a.txt file ('fortc.txt'). This file needs to be read by a.exe ('f90code.exe') generated through a fortran90 code. The fortran code then do some operation and write the result in another.txt file ('res_tc.txt'), previously created through pycode.py, that pycode.py will finally read and print. Here is the python code:
import csv
import sys, os
import numpy as np
with open('fortc.txt', 'w') as g:
writer = csv.writer(g, delimiter=" ", quoting=csv.QUOTE_NONE, escapechar=' ')
writer.writerow(['1', '2', '3', '4'])
f = open('res_tc.txt','w+')
os.system('./f90code.exe')
readres = np.loadtxt('res_tc.txt')
tc = float(readres)
print(tc)
f.close()
If I create the file 'fortc.txt' manually and then just run the f90 code, without passing though python, everything works fine and I get the expected result. However, if I run everything through python, 'fortc.txt' is created correctly, but apparently fortran is not able to read it properly. In fact, everything in 'fortc.txt' is read as '0.0000000000000', producing the following error when performing the operation:
Note: The following floating-point exceptions are signalling: IEEE_INVALID_FLAG IEEE_DENORMAL
And obviously I get a 'NaN' as result. Furthermore, it looks like fortran is not running the do loops correctly, since if I try to print something while inside a do loop, I don't get anything printed out. Here is the part of the f90 code that reads the file:
program tc
real*8, allocatable:: ne(:), kt(:), abund(:), z(:)
integer:: i, j, ndati, n
data ab/0.,0.1,0.31623,1./
open(96,file='fortc.txt',status='old')
n=0
do
read(96,*,end=320)
n=n+1
end do
320 continue
ndat=n
allocate(ne(ndat), kt(ndat), abund(ndat), z(ndat))
rewind(96)
do j=1, ndat
read(96,*) kt(j), abund(j), ne(j), z(j)
print*, kt(j)
end do
end program
There is no outcome for the print call at the end of the f90, just the error shown above and the 'NaN' (derived from the print within the python code). On the other hand, if the print statement is brought outside of the do loop, I get something printed ('0.00000000', as mentioned above). Again, this happens only when running the compiled fortran code through python, while if I just run the.exe from my terminal, it works as intended.
Is there something wrong about how the do loops are dealt with? The fact that the numbers within the.txt file are not read correctly, and that the print is not performed, suggested me that the issue could be there. However, I am not being able to fix it.
Your input file is empty when you call the fortran code:
with open('fortc.txt', 'w') as g:
By default, Python will flush data written to this file and close it when you exit the with
body. But you include your call to the fortran code in it.
If you want to use with
for file manipulation, rearrange your code in this way:
import csv
import sys, os
import numpy as np
with open('fortc.txt', 'w') as g:
writer = csv.writer(g, delimiter=" ", quoting=csv.QUOTE_NONE, escapechar=' ')
writer.writerow(['1', '2', '3', '4'])
f = open('res_tc.txt','w+') # not sure why the frotran program would output to "f" just because it is open. If it does, ok, it will wrk.
os.system('./f90code.exe')
readres = np.loadtxt('res_tc.txt')
tc = float(readres)
print(tc)
f.close() # you can use explicit open and close calls for fortc.txt as well
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.