python - Passing numpy string-format arrays to fortran using f2py -
my aim print 2nd string python numpy array in fortran, ever first character printed, , it's not right string either.
can tell me correct way pass full string arrays fortran?
the code follows:
testpy.py
import numpy np import testa4 strvar = np.asarray(['aa','bb','cc'], dtype = np.dtype('a2')) testa4.testa4(strvar)
testa4.f90
subroutine testa4(strvar) implicit none character(len=2), intent(in) :: strvar(3) !character*2 not work here - why? print *, strvar(2) end subroutine testa4
compiled with
f2py -c -m testa4 testa4.f90
output of above code
c
desired output
bb
per documentation, f2py likes string arrays passed dtype='c' (i.e., '|s1'). gets part of way there, although there oddities array shape going on behind scenes (e.g., in lot of tests found fortran keep 2 character length, interpret 6 characters being indicative of 2x6 array, i'd random memory in output). (as far tell), requires treat fortran array 2d character array (as opposed 1d "string" array). unfortunately, couldn't take assumed shape , ended passing number of strings in argument.
i'm pretty sure i'm missing obvious, should work time being. why character*2 doesn't work ... have no idea.
module char_test contains subroutine print_strings(strings, n_strs) implicit none ! inputs integer, intent(in) :: n_strs character, intent(in), dimension(2,n_strs) :: strings !f2py integer, intent(in) :: n_strs !f2py character, intent(in), dimension(2,n_strs) :: strings ! misc. integer*4 :: j j=1, n_strs write(*,*) strings(:,j) end end subroutine print_strings end module char_test ---------------- import numpy np import char_test ct strings = np.array(['aa', 'bb', 'cc'], dtype='c').t ct.char_test.print_strings(strings, strings.shape[1]) strings = np.array(['ab', 'cd', 'ef'], dtype='c').t ct.char_test.print_strings(strings, strings.shape[1]) -->python run_char_test.py aa bb cc ab cd ef
Comments
Post a Comment