I have the following fortran code that I used to create a binary unformatted file:
PROGRAM main_program
implicit none
open (10, form='unformatted', file='myFile.xyz')
write(10) 1
write(10) 400, 400, 1
write(10) 1.0d0, 1.25d0, 1.50d0, 1.75d0, 2.0d0, 2.25d0
write(10) 3.0d0, 3.25d0, 3.50d0, 3.75d0, 4.0d0, 4.25d0, 4.50d0, 4.75d0, 5.00d0
close(10)
END
I want to read the generated 'myFile.xyz' from the above program using python. For some reason maybe because of the way the fortran formats the file writup, reading this file does not seem straightforward in python. Here is a python code that I have tried:
import numpy as np
file_path = "myFile.xyz"
# Read the binary file using numpy
with open(file_path, "rb") as file:
data = np.fromfile(file, dtype=np.float64)
# Extract the first three integers (data[0:3])
integers_data = data[:3]
# Extract the double-precision floating-point values (skip the first 3 integers)
float_values = data[3:]
# Output the extracted data
print("The first three integers in the binary file are:", integers_data)
print("The double-precision floating-point values in the binary file are:", float_values)
How could I read myFile.xyz in python?
I tried different index ranges for the variable and printed the output. Nothing seem to work properly.