我有一个文件是以big-endian格式写入的。我写了一个库来访问这个文件。现在我试图用这个库在一个Python脚本中访问这个文件。为此我写了一些例程,然后用F2py进行编译。问题是在gfortran(我使用的编译器)中,选项"-fconvert=big-endian "只有在主程序中使用时才有效果,而不是在库中。所以我无法在python中正确访问这个文件。
下面我举一个小例子来重现这个问题。
文件 "fileTest.bin "是由该程序创建的。
program ttt
implicit none
real(kind=4) :: m(2,2)
m=10.0
open(unit=100,file='fileTeste.bin', form='unformatted')
write(100)m
close(100)
end program
to compile:
gfortran -o ttt.x -fconvert=big-endian ttt.f90
./ttt.x
我写了一个测试模块,在python中读取这个大-endian文件。
module test
implicit none
contains
subroutine openFile(fileName)
character(len=*), intent(in) :: fileName
real(kind=4), allocatable :: array(:,:)
print*,"open File:", trim(fileName)
open(unit=100,file=trim(fileName),form='unformatted')
allocate(array(2,2))
read(100)array
print*,array
end subroutine openFile
end module test
compile using f2py:
f2py -c -m test --f90flags='-fconvert=big-endian' test.f90
在Python中加载测试模块。
from test import test as t
a=t.open(teste.bin)
open File:fileTeste.bin
1.15705214E-41 1.15705214E-41 1.15705214E-41 1.15705214E-41
那么,我怎样才能用F2py读取一个big-endian文件?