1

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.

Hypernova
  • 21
  • 4
  • Please use tag [tag:fortran] for all Fortran questions. Your question is not version-specific so we do not even specify the tag for the old Fortran 90 at all. – Vladimir F Героям слава May 17 '21 at 13:13
  • 3
    What is ab supposed to be? ALWAYS use Implicit None (and never use real*8 - https://stackoverflow.com/questions/838310/fortran-90-kind-parameter) – Ian Bush May 17 '21 at 13:25
  • 1
    Does your real Fortran code actually write to `res_tc.txt`? The Fortran code that you posted doesn't write to that file at all. – jjramsey May 17 '21 at 14:01
  • 1
    I hope your compiler complains about the broken Fortran program (if it doesn't then you need to find a better compiler), so are you have addressed all issues identified when compiling? Also be sure to use `implicit none` as previously mentioned. – francescalus May 17 '21 at 14:07
  • 1
    In this block "with open('fortc.txt', 'w') as g:", isn't it necessary to close that block and "indent back" to the first column? (so that the file output completes) – roygvib May 17 '21 at 14:12
  • @jjramsey yes it does, I only posted a part of the code since it is proprietary and I am not able to share the rest of it. Same reason why I can't change it apart of the shown part – Hypernova May 17 '21 at 14:18
  • @roygvib yes, apologies for the mistake -the original issue is still there anyway. – Hypernova May 17 '21 at 14:19

1 Answers1

0

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
jsbueno
  • 99,910
  • 10
  • 151
  • 209