我需要一個腳本來遞歸跨目錄結構,從目錄中的文件中提取數字,然後對這些數字執行計算。我使用Python作爲腳本的主要語言,但想要使用Fortran進行數值計算。 (我更喜歡Fortran,它是一個更好的數值工具)使用f2py嵌入Python中的Fortran
我正在嘗試使用f2py,但我不斷收到奇怪的錯誤。 f2py正在抱怨我的變量聲明,試圖將字符(*)更改爲整數並追加!當我在變量聲明之後立即註釋時,將其添加到我的變量名稱中。
該子程序太長而無法在此處發佈,但需要兩個參數,即輸入文件名和輸出文件名。它打開輸入文件,讀取數字,處理它們,然後寫入輸出文件。我打算使用Python腳本在每個目錄中寫入數字文件並在其上調用Fortran子例程。
我可以嘗試發佈一個相同問題的小例子,但是有沒有與f2py共同'陷阱'?我使用的是gfortran v4.6.1,python v3.2.2和f2py v2。
編輯:這裏是一個小例子具有相同的錯誤:
itimes-SF(含有子程序從蟒使用的文件):
module its
contains
subroutine itimes(infile,outfile)
implicit none
! Constants
integer, parameter :: dp = selected_real_kind(15)
! Subroutine Inputs
character(*), intent(in) :: infile ! input file name
character(*), intent(in) :: outfile ! output file name
! Internal variables
real(dp) :: num ! number to read from file
integer :: inu ! input unit number
integer :: outu ! output unit number
integer :: ios ! IOSTAT for file read
inu = 11
outu = 22
open(inu,file=infile,action='read')
open(outu,file=outfile,action='write',access='append')
do
read(inu,*,IOSTAT=ios) num
if (ios < 0) exit
write(outu,*) num**2
end do
end subroutine itimes
end module its
itests.f(Fortran的驅動器程序):
program itests
use its
character(5) :: outfile
character(5) :: infile
outfile = 'b.txt'
infile = 'a.txt'
call itimes(infile, outfile)
end program itests
A.TXT:
編譯和運行itests後b.txt itimes-S僅使用gfortran:但是
1.0000000000000000
4.0000000000000000
9.0000000000000000
16.000000000000000
25.000000000000000
36.000000000000000
49.000000000000000
64.000000000000000
81.000000000000000
104.03999999999999
使用f2py.py -c -m its itimes-s.f
運行f2py產生許多錯誤。 (由於長度未張貼,但如果有人想要我可以張貼它們)
請發表例子。在此之後提出任何建議會容易得多。 – Rook 2011-12-15 23:12:19
您可以將您的子程序減少到具有相同問題的最小程度,然後將其發佈到此處。 – steabert 2011-12-16 13:28:50