ディレクトリ構造全体を再帰的に処理し、ディレクトリ内のファイルから数値を抽出して、それらの数値に対して計算を実行するスクリプトが必要です。スクリプトのメイン言語としてPythonを使用していますが、数値計算にFortranを使用したいと考えていました。(私はFortranに慣れており、数値作業に適したツールです)
f2pyを使おうとしていますが、奇妙なエラーが発生し続けます。f2pyは変数宣言について不平を言っており、character(*)を整数に変更して追加しようとしています!変数宣言の直後にコメントがある場合は、変数名に追加します。
サブルーチンは長すぎてここに投稿できませんが、入力ファイル名と出力ファイル名の2つの引数を取ります。入力ファイルを開き、数値を読み取り、処理してから、出力ファイルに書き込みます。Pythonスクリプトを使用して、各ディレクトリに数値ファイルを書き込み、その上でFortranサブルーチンを呼び出すつもりです。
同じ問題で小さな例を投稿することもできますが、f2pyでよくある「落とし穴」はありますか?gfortran v4.6.1、python v3.2.2、およびf2pyv2を使用しています。
編集: これは同じエラーの小さな例です:
itimes-sf(Pythonから使用されるサブルーチンを含むファイル):
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:
1
2
3
4
5
6
7
8
9
10.2
gfortranのみを使用してitestsとitimes-sをコンパイルして実行した後のb.txt:
1.0000000000000000
4.0000000000000000
9.0000000000000000
16.000000000000000
25.000000000000000
36.000000000000000
49.000000000000000
64.000000000000000
81.000000000000000
104.03999999999999
ただし、を使用してf2pyを実行するとf2py.py -c -m its itimes-s.f
、多数のエラーが発生します。(長さのため投稿されていませんが、誰かが望むなら私はそれらを投稿することができます)