fortran和python可以同时装吗_使用f2py在Python中嵌入Fortran

博主想用Python脚本递归目录结构提取数字,用Fortran进行数值计算,尝试使用f2py时遇到奇怪错误,f2py抱怨变量声明。博主给出了相关子程序代码示例,使用gfortran编译运行正常,但用f2py运行产生大量错误,询问f2py常见“陷阱”。

我需要一个脚本来递归目录结构,从目录中的文件中提取数字,然后对这些数字执行计算 . 我使用Python作为脚本的主要语言,但是想要使用Fortran进行数值计算 . (我对Fortran更熟悉,它是一个更好的数字工作工具)

我正在尝试使用f2py,但我一直遇到奇怪的错误 . f2py抱怨我的变量声明,尝试将字符(*)更改为整数并附加!当我在变量声明后立即发表评论时,转到我的变量名称上 .

子程序太长而无法在此处发布,但需要两个参数,即输入文件名和输出文件名 . 它打开输入文件,读取数字,处理它们,然后写入输出文件 . 我打算使用Python脚本在每个目录中编写数字文件,并在其上调用Fortran子例程 .

我可以尝试使用相同的问题发布一个较小的示例,但是f2py有任何常见的“陷阱”吗?我正在使用gfortran v4.6.1,python v3.2.2和f2py v2 .

EDIT: 这是一个具有相同错误的小例子:

itimes-s.f(包含要从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.py -c -m its itimes-s.f 运行f2py会产生大量错误 . (由于长度不发布,但如果有人想我可以发布)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值