[Numpy-discussion] String arrays from Python to Fortran

Jared MacCleary jared.subscript at gmail.com
Sun Mar 22 18:45:31 EDT 2009


Hi all,
No doubt it's a simple fix, but I'm stumped.  I've tried several ways to
send an array of strings from Python to a Fortran function, but nothing
seems to work.  I don't know what I'm missing.

My Python and Fortran code are very simple.  At this point, I'm just trying
to get anything to work.  In Python I try to create an array of strings, and
the Fortran subroutine is supposed to print each element of that array on a
separate line.  When I try, I get this error:


Traceback (most recent call last):
  File "testp.py", line 13, in <module>
    print_string(a, len(a)) #this is imported from the compiled Fortran code
ValueError:* failed to initialize intent(inout) array -- input 'S' not
compatible to 'c'*



I've googled for the answer but without success.  The only encouraging info
I've come across was an announcement accompanying the release of a previous
version of f2py, declaring that string arrays had finally been fully
implemented.  So it must be possible.  I'm still pretty new to Python and
I'm brand new to Fortran.  I'd appreciate any advice you can give.

My Fortran and Python code are below.

Thanks a lot,
Jared


*My Fortran code:*

subroutine print_string (a, c)
 implicit none
 character(len=255), dimension(c), intent(inout)::  a
integer, intent(in) :: c
integer :: i
 do i = 1, size(a)
print*, a(i)
end do

end subroutine print_string

*My Python code:*

from test import *
from numpy import *

a = "this is the test string."
a = a.split()

b = a

a = char.array(a, itemsize=1, order = 'Fortran')

print_string(a, len(a)) #this is imported from the compiled Fortran code
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090322/e6fd74b7/attachment.html>


More information about the NumPy-Discussion mailing list