如何使用 f2py 将字符串数组传递给 Fortran 子例程

How to pass array of strings to Fortran subroutine using f2py

经过一些在线挖掘和试错,我仍然想知道如何通过 f2py 将字符串数组从 Python 传递到 Fortran。

我在 string.f90 中有 Fortran 子例程:

  SUBROUTINE FOO(A)
  CHARACTER*5,dimension(10),intent(inout):: A
  PRINT*, "A=",A
  END

那我运行f2py -m mystring -c string.f90。 编译成功。

python 会话在 test.py:

import mystring
import numpy as np
nobstot=10
xstring=np.empty(nobstot,dtype='S5')
xstring[0]="ABCDE"
mystring.foo(xstring)

运行 python test.py,我有错误信息:

1-th dimension must be 5 but got 0 (not defined).
Traceback (most recent call last) :
File "test.py", line 6, in <module>
mystring.foo(xstring)
mystring.error: failed in converting 1st argument `a' of mystring.foo to C/Fortran array

在f2py编译步骤中,调用了gfortran和gcc编译器。

>>> print mystring.foo.__doc__之后,有:

foo(a)
Wrapper for ``foo``.
Parameters
---------
a : in/output rank-2 array('S') with bounds (10,5)

所以,我尝试 test.py 作为:

import mystring
import numpy as np
nobstot=10
xstring=np.empty((nobstot,5),dtype='S1')
print xstring.shape
xstring[0]="ABCDE"
mystring.foo(xstring)

然后运行python test.py,错误信息是:

Traceback (most recent call last):
File "test.py", line 7, in <module>
mystring.foo(xstring)
ValueError: failed to initialize intent(inout) array -- input 'S' not compatible to 'c'

在 Fortran 之间传递字符串有点棘手。

用这行你建立了实用的 two-dimension character-array 10 * 5

CHARACTER*5,dimension(10),intent(inout):: A

尝试将其更改为

CHARACTER*10,intent(inout):: A

这使它成为一个包含 10 个字符的 one-dimensional 数组。 如果它有效,但输出是垃圾,请检查两者是否是相同的字符格式(ascii/multibyte 或 unicode)。

首先,要将字符串数组传递给 Fortran,在 Python 中,您必须创建一个形状为 (<number of strings>, <string length>) 的字符数组,填充其内容,然后将 char 数组传递给生成的 f2py功能。使用您的示例:

xstring = np.empty((nobstot, 5), dtype='c')
xstring[0] = "ABCDE"
xstring[1] = "FGHIJ"
mystring.foo(xstring)

为了使其正常工作,您还需要更改您的 Fortran 代码:

subroutine foo(A)
character*5, dimension(10), intent(in) :: A
print*, "A(1)=",A(1)
print*, "A(2)=",A(2)
end

请注意,intent(inout) 已替换为 intent(in)。这是因为 Python 中的字符串以及 numpy 字符串数组中的字符串是不可变的,但在 Fortran 中它们可能不是。因此,Python 字符串的内存布局不能简单地传递给 Fortran 函数,用户必须如上所述重新组织字符串数据。

其次,如果您的 Fortran 代码更改了字符串,如 intent(inout) 的用法所示,您需要将此类字符串参数声明为 intent(in, out),例如,通过使用 f2py 指令。下面是一个完整的例子:

subroutine foo(A)
character*5, dimension(10), intent(inout) :: A
!f2py intent(in, out) A
print*, "A(1)=",A(1)
print*, "A(2)=",A(2)
A(1)="QWERT"
end

F2py调用:

f2py -m mystring -c string.f90

Python 测试脚本:

import mystring
import numpy as np
nobstot = 10
xstring = np.empty((nobstot, 5), dtype='c')
xstring[0] = "ABCDE"
xstring[1] = "FGHIJ"
xstring = mystring.foo(xstring)
print("xstring[0]=",string[0].tostring())
print("xstring[1]=",string[1].tostring())

控制台输出:

 A(1)=ABCDE
 A(2)=FGHIJ
xstring[0]= QWERT
xstring[1]= FGHIJ

这行得通吗@Pearu? 用 ord 转换 ASCII -> int 并直接发送一个 numpy 数组

xstring = np.zeros((10, 5)).astype(int)
strings = ["ABCDE","FGHIJ"]
for istring,string in enumerate(strings):
   for ichar,char in enumerate(string):
      xstring[istring,ichar] = ord(char)
 
mystring.foo(xstring)

根据此处的评论,我尝试在我的 Fortran/python 代码中重现这种方法,但没有成功。在我的代码中,我需要传递一个大小未知的字符串数组,以及一个长度未知的字符串。例如,在我的 Fortran 代码中我有:

   character(len=*), dimension(:), intent(in)  :: string

关注这个话题,这对我不起作用。

所以我做了一些修改,找到了一种对我有用并在这里分享的方法。我知道这是一个老话题,但我相信其他人可以使用它。

当我将一个字符串数组从 python 传递给 Fortran 时,f2py 将一个字符串数组转换成类似 string(:,:) 的形式,其中第一维是字符串长度,第二维是数组长度。考虑到这一点,我以这种方式修改 Fortran:

subroutine foo(A)
   character, dimension(:,:), intent(in) :: A
   character(len=25), allocatable :: B(:)
   integer :: strLen
   integer :: arrLen

   strLen = size(A,1)
   arrLen = size(A,2)
   allocate(B(arrLen))
   do i = 1, arrLen
      B(i) = transfer(A(:,i),B(i)(1:strLen))
   enddo
   
   do i=1, arrLen
      print*,trim(B(i))
   enddo

end

Python 测试脚本,具有不同的字符串和数组长度:

import mystring
import numpy as np

print('strings for test 1:')
xstring = np.array(['ABCDE','FGHIJ'],dtype='c').T
mystring.foo(xstring)

print('strings for test 2:')
isisList = ['amsua_n15    ', 'amsua_n18    ', 'amsua_n19    ', 'amsua_metop-a']
xstring = np.array(isisList,dtype='c').T
mystring.foo(xstring)

控制台结果:

strings for test 1:
 ABCDE
 FGHIJ
strings for test 2:
 amsua_n15
 amsua_n18
 amsua_n19
 amsua_metop-a