Warm tip: This article is reproduced from serverfault.com, please click

Converting Fortran array to numpy array

发布于 2020-11-28 02:28:36

I am in the process of converting a Fortran 90 code to python. The Fortran code includes multidimensional array like:

integer fvpair(3,12,2)
integer jpair(3,2)
real*8 aread(3,12)

I wonder if the following numpy array are correct equivalent, assuming zero initialization:

fvpair = np.array([[np.zeros(3)],[np.zeros(12)],[np.zeros(2)]])
jpair = np.array([[np.zeros(3)],[np.zeros(2)]])
aread = np.array([[np.zeros(3)],[np.zeros(12)]])
Questioner
Bob
Viewed
1
DYZ 2020-11-28 13:11:29

If you want to preserve the original Fortran array storage order (column-major), do not forget to pass the order='F' flag!

fvpair = np.zeros((3,12,2), dtype=int, order='F')
jpair = np.zeros((3,2), dtype=int, order='F')
aread = np.zeros((3,2), dtype=float64, order='F')