传递numpy的字符串格式阵列FORTRAN使用f2py [英] Passing numpy string-format arrays to fortran using f2py

查看:201
本文介绍了传递numpy的字符串格式阵列FORTRAN使用f2py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在FORTRAN一个python numpy的阵列打印第二个字符串,但我只得到第一个字符印刷,它要么是不一定是正确的字符串。

谁能告诉我什么是正确的方式传递完整的字符串数组FORTRAN?

在code是如下:

testpy.py

 导入numpy的是NP
进口testa4strvar = np.asarray(['AA','BB','CC'] = DTYPE np.dtype('A2'))
testa4.testa4(strvar)

testa4.f90

 子程序testa4(strvar)
隐无字符(LEN = 2),意图(在):: strvar(3)
!字符* 2不能在这里工作 - 为什么?打印*,strvar(2)结束子程序testa4

与编译

  f2py -c -m testa4 testa4.f90

以上code的输出

 Ç

所需的输出

  BB


解决方案

%的的文档,f2py喜欢字符串数组与DTYPE ='c'的传递(即| S1')。这可以让你的存在方式的一部分,虽然有一些古怪与阵形去幕后(例如,在很多我的测试中我发现,FORTRAN将保持2个字符长度,但相互preT 6字符可以反映一个2x6的阵列,所以我会得到随机存储回到输出)。这(据我所知),需要你治疗的Fortran数组作为二维字符数组(相对于一维字符串数组)。不幸的是,我无法得到它采取假定外形,并最终传递字符串的数量作为参数。<​​/ P>

我是pretty肯定,我失去了一些东西很明显,但这应该暂时工作。至于为何CHARACTER * 2不工作...我真的不知道。

 模块char_testCONTAINSSUBROUTINE print_strings(字符串,n_strs)
    隐式NONE    !输入
    INTEGER,意向(IN):: n_strs
    CHARACTER,意向(IN),外形尺寸(2 n_strs)::字符串!f2py INTEGER,意向(IN):: n_strs
!f2py CHARACTER,意向(IN),外形尺寸(2 n_strs)::字符串    !杂项。
    INTEGER * 4 ::Ĵ
    DO J = 1,n_strs
        WRITE(*,*)字符串(:,j)的
    END DOEND SUBROUTINE print_strings前端模块char_test----------------导入numpy的是NP
进口char_test如CT字符串= np.array(['AA','BB','CC'] = DTYPE'C'):T
ct.char_test.print_strings(字符串,strings.shape [1])字符串= np.array(['AB',CD,EF] = DTYPE'C'):T
ct.char_test.print_strings(字符串,strings.shape [1]) - &GT;蟒蛇run_char_test.py
 AA
 BB
 CC
 AB
 光盘
 EF

My aim is to print the 2nd string from a python numpy array in fortran, but I only ever get the first character printed, and it's not necessarily the right string either.

Can anyone tell me what the correct way to pass full string arrays to fortran?

The code is as follows:

testpy.py

import numpy as 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 does 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 the documentation, f2py likes string arrays to be passed with dtype='c' (i.e., '|S1'). This gets you part of the way there, although there are some oddities with array shape going on behind the scenes (e.g., in a lot of my tests I found that fortran would keep the 2 character length, but interpret the 6 characters as being indicative of a 2x6 array, so I'd get random memory back in the output). This (as far as I could tell), requires that you treat the Fortran array as a 2D character array (as opposed to a 1D "string" array). Unfortunately, I couldn't get it to take assumed shape and ended up passing the number of strings in as an argument.

I'm pretty sure I'm missing something fairly obvious, but this should work for the time being. As to why CHARACTER*2 doesn't work ... I honestly 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


    DO j=1, n_strs
        WRITE(*,*) strings(:,j)
    END DO

END SUBROUTINE print_strings

END MODULE char_test

----------------

import numpy as np
import char_test as 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

这篇关于传递numpy的字符串格式阵列FORTRAN使用f2py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆