1

I have a large, usually square, matrix which I calculate in Fortran. After this, I need to print out this array into a .txt or .dat file so that it can be read by Python. Initially I had problems since the format of the array kept getting changed ( meaning the rows and columns got messed up), but I managed to find a way to reconstruct the array in Python via the following method:

say I have a 1000 x 1000 matrix, and then to print it out I do

open(6,file='/home/alpha_x.dat')
do i =1,1000
    write(6,'(1000F14.7)')(alpha_x(i,j), j=1,1000)
end do 
open(5,file='/home/alpha_y.dat')
do i =1,1000
    write(5,'(1000F14.7)')(alpha_y(i,j), j=1,1000)
end do 

Afterwards to read this in Python, I simply use

np.loadtxt('alpha_x.dat')
alpha_x = np.transpose(alpha_x)#This is required since the row-column order is interchanged in Fortran and Python

This worked fine until I realised that, for example, if I had a matrix with dimensions 2000 x 2000 and I do all this in Fortran and read it in Python, the array turns out to have dimensions 4000 x 1000. I can't help but think that this means there must be something wrong in the way I save the matrix in Fortran...Seems as if the matrix is somehow converted into n columns x 1000 rows for whatever multiple. How would I fix this so that I can reconstruct the array with original dimensions in Python?

ThunderFlash
  • 412
  • 5
  • 20

0 Answers0