我正在尝试用Python包装一个相当大的Fortran库。我可以修改源代码,但显然要修改尽可能少。代码中定义了许多固定大小的数组,其中较低级别模块的参数指定了较低和/或较高的索引。所以可能有这样的说法
use parrrsw, only : j1, j2
integer(kind=4), intent(in), dimension(j1:j2) :: opt_depth
在试图找出如何使用f2py来适应这种情况时,我创建了下面的最小示例来显示我所遇到的一个错误。实际上,f2py似乎可以识别从rrsw
导入的索引,但是如果它们也被用于初始化数组,那么就无法使用它们来分配给数组。我想我可能用错了f2py的depend
语句,但是我还没有找到一个能帮助我修复它的例子。你知道吗
(实际上,大多数数组最初定义为可分配的,但后来用参数填充。这完全是另一个问题……但是如果有人有一个很好的方法来适应以这种方式系统地编写的代码,我很乐意听到。)
编辑2016-06-13:我决定尝试使用CFFI或ctypes进行模块导入。这个问题仍然存在,但对我的工作不再重要。你知道吗
示例代码:
测试\u rrsw.f90:
module test_rrsw
implicit none
integer, parameter :: im = selected_int_kind(6)
integer(kind=im), parameter :: jpb1=16, jpb2=20
integer(kind=im), parameter :: ng16=6, ng17=12, ng18=8, ng19=8, ng20=10
end module test_rrsw
测试\u.f90:
module test_minimal
implicit none
integer, parameter :: im = selected_int_kind(6)
integer, parameter :: rb = selected_real_kind(12)
contains
subroutine test_pars1(ng)
use test_rrsw, only : jpb1, jpb2, ng16, ng17, ng18, ng19, ng20
implicit none
!f2py integer intent(out) :: ng(16:20)
integer(kind=im), intent(out), dimension(16:20) :: ng
ng(jpb1:jpb2) = (/ ng16, ng17, ng18, ng19, ng20 /)
end subroutine test_pars1
subroutine test_pars2(ng)
use test_rrsw, only : jpb1, jpb2, ng16, ng17, ng18, ng19, ng20
implicit none
!f2py integer intent(aux) :: jpb1, jpb2
!f2py depend(jpb1,jpb2) :: ng
!f2py integer intent(out) :: ng(jpb1:jpb2)
integer(kind=im), intent(out), dimension(jpb1:jpb2) :: ng
write(*,*) jpb1, jpb2
ng(jpb1:jpb2) = (/ ng16, ng17, ng18, ng19, ng20 /)
end subroutine test_pars2
end module test_minimal
你知道吗测试.py地址:
#!/usr/bin/env python
import test_minimal
res1 = test_minimal.test_minimal.test_pars1()
res2 = test_minimal.test_minimal.test_pars2()
print 'pars1 =', res1, ' pars2 =', res2
跑_测试.sh地址:
#!/usr/bin/env bash
gfortran -c test_rrsw.f90
gfortran -c test_minimal.f90
f2py -c test_minimal.f90 test_rrsw.o -m test_minimal > log_f2py.txt
python test.py
使用./run_test.sh
从Python调用两个不同的子例程test_pars1
和test_pars2
产生输出。(警告:有时在运行这些例程后退出Python时会出现segfault。)如果其中任何一个对您不起作用,请告诉我。这是很多代码,但我希望尽可能明确地说明编译/链接/测试。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐