I want Fortran to read the python output file.

Asked 1 years ago, Updated 1 years ago, 115 views

Thank you for your help.
Input conversion error occurs when the Fortran 90(ifort) program reads only numeric files created with python 3.6 .
I tried decoding to ascii or binary using the tofile of numpy (See this), but I don't think I can solve it at all.
If you understand, please tell me how to deal with it.Thank you for your cooperation.

Python Code

with open("infile.txt", "wb") as outfile:
outfile.write("".join(primit[0])+"\n")
outfile.write("".join(primit[1])+"\n")
outfile.write("".join(primit[2])+"\n")
outfile.write(str(jsonIn["constant value"]["a"])+"\n"))
outfile.write(str(jsonIn["constant value"]["b"])+"\n"))
outfile.write(str(jsonIn["constant value"]["c"])+"\n"))
outfile.write(str(jsonIn["constant value"]["d"])+"\n"))
outfile.write(str(jsonIn["constant value"]["e"])+"\n")

Fortran code

open(INFILE, file='infile.txt', form='unformed', access='direct', rec=4)
read(INFILE, '(F10.5, F10.5, F10.5)')primit_vec(1,1),primit_vec(2,1),primit_vec(3,1)
! read(INFILE, '(F10.5, F10.5, F10.5)')primit_vec(1,2),primit_vec(2,2),primit_vec(3,2)
! read(INFILE, '(F10.5, F10.5, F10.5)')primit_vec(1,3),primit_vec(2,3),primit_vec(3,3)
! read(INFILE, '(I5)')a
! read(INFILE, '(F10.5)')b
! read(INFILE, '(F10.5)')c
! read(INFILE, '(F10.5)')d
! read(INFILE, '(F10.5)')e

python3 fortran

2022-09-30 21:44

1 Answers

If Fortran input data follows textual 14.2 formatting - Fortran introduction: data input/output, Python output data can be achieved by applying the following features:

Format conversion with Python, format (for example, 0 fill, exponential notation, hexadecimal)
How to use Python's f string (formatted string literal)
[Introduction to Python] How to write strings using the format function
Printing Python-inclusive notation

By the way, more detailed specifications of Fortran were found here.
Format Specifications, I-shaped editing, F-shaped editing, Format of Data Edit Descriptor
Or FORTRAN 77 Language Reference Chapter 5 Input/Output

Python, which corresponds to a five-digit decimal integer I5 in Fortran, is considered '{:>5}', and Python, which corresponds to a 10-digit real number F10.5, is considered '{:>10.5f}'.
However, if the integer number exceeds the number of digits in the format, or if the number of digits is minus - even within the limit, Python will increase the number of digits and display & output.
Fortran reads that more than a specified number of digits will be either an error or ignored, so you will need to consider the number of digits in the format beforehand or check the range of digits.

For example, I made the following dummy data in Python.

primit=[1111.11111,2222.22222,3333.333],
          [4444.44444,5555.55555,6666.66666],
          [7777.77777,8888.88888,9999.99999]]

a=12345
a2 = 5
a3=-12345

b = 1234.44444
c=5.5555
d = 1234.6
e=123456.77777

Define the output format like this:

fmtI5='{:>5}'
fmtF10_5 = '{:>10.5f}'

Try print instead of write to the file.
Please add a new line when writing the file.
The first three real numbers are listed in list notation.

 print (*[f'{n:>10.5f}' for n in prime [0]])
print(*[f'{n:>10.5f}'for n in primitive[1]])
print(*[f'{n:>10.5f}'for n in primitive[2]])

print(fmtI5.format(a))
print(fmtI5.format(a2))
print(fmtI5.format(a3))

print(fmtF10_5.format(b))
print(fmtF10_5.format(c))
print(fmtF10_5.format(d))
print(fmtF10_5.format(e))

The output is as follows:
Empty lines are added for ease of viewing.

1111.11111 2222.22222 3333.33333
4444.44444 5555.55555 6666.66666
7777.77777 8888.88888 9999.99999

12345
    5
-12345 #Error? Or -1234?

1234.44444
   5.55555
1234.60000
123456.77777# Error? Or is it treated as 1234.56777?


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.