警告.....我是一个相对的python noob,并且使用f2py是全新的。
我试图尽职尽责并在这里和其他地方搜索我的问题的答案,但我没有运气。这很可能是由于我对这个问题的无知。
我的问题:我有一个遗留的fortran(f77)子程序以及子程序用来从数据文件中读取各种数组的几个函数。这个数据文件本身就是几个f77原子物理代码之一的结果。
我想使用f2py包装fortran子例程/函数,并以numpy数组的形式访问存储在文件中的数据。我坚持如何在python中正确分配数组。我在Ubuntu 14.04(64位)上使用Anaconda python distibrution(Python 3.4 - 64位)和gfortran编译器。
我成功(我希望)使用py2f -h <filename.py2f> <input files>
设法使用py2f创建了一个python签名文件。这是签名文件的内容。我为它的长度道歉。这是一个相当冗长的子程序,并且有很多变量。
! -*- f90 -*-
! Note: the context of this file is case sensitive.
subroutine xxdata_04(iunit,ndlev,ndtrn,ndmet,ndqdn,nvmax,titled,iz,iz0,iz1,bwno,npl,bwnoa,lbseta,prtwta,cprta,il,qdorb,lqdorb,qdn,iorb,ia,cstrga,isa,ila,xja,wa,cpla,npla,ipla,zpla,nv,scef,itran,maxlev,tcode,i1a,i2a,aval,scom,beth,iadftyp,lprn,lcpl,lorb,lbeth,letyp,lptyp,lrtyp,lhtyp,lityp,lstyp,lltyp,itieactn,ltied) ! in xxdata04_string.for
integer :: iunit
integer, optional,check(len(ia)>=ndlev),depend(ia) :: ndlev=len(ia)
integer, optional,check(shape(tcode,0)==ndtrn),depend(tcode) :: ndtrn=shape(tcode,0)
integer, optional,check(len(bwnoa)>=ndmet),depend(bwnoa) :: ndmet=len(bwnoa)
integer, optional,check(len(qdn)>=ndqdn),depend(qdn) :: ndqdn=len(qdn)
integer, optional,check(len(scef)>=nvmax),depend(scef) :: nvmax=len(scef)
character*3 :: titled
integer :: iz
integer :: iz0
integer :: iz1
real*8 :: bwno
integer :: npl
real*8 dimension(ndmet) :: bwnoa
logical dimension(ndmet),depend(ndmet) :: lbseta
real*8 dimension(ndmet),depend(ndmet) :: prtwta
character dimension(ndmet,9),depend(ndmet) :: cprta
integer :: il
real*8 dimension((ndqdn*(ndqdn+1))/2),depend(ndqdn) :: qdorb
logical dimension((ndqdn*(ndqdn+1))/2),depend(ndqdn) :: lqdorb
real*8 dimension(ndqdn) :: qdn
integer :: iorb
integer dimension(ndlev) :: ia
character dimension(ndlev,(*)),depend(ndlev) :: cstrga
integer dimension(ndlev),depend(ndlev) :: isa
integer dimension(ndlev),depend(ndlev) :: ila
real*8 dimension(ndlev),depend(ndlev) :: xja
real*8 dimension(ndlev),depend(ndlev) :: wa
character dimension(ndlev,1),depend(ndlev) :: cpla
integer dimension(ndlev),depend(ndlev) :: npla
integer dimension(ndmet,ndlev),depend(ndmet,ndlev) :: ipla
real*8 dimension(ndmet,ndlev),depend(ndmet,ndlev) :: zpla
integer :: nv
real*8 dimension(nvmax) :: scef
integer :: itran
integer :: maxlev
character dimension(ndtrn,1) :: tcode
integer dimension(ndtrn),depend(ndtrn) :: i1a
integer dimension(ndtrn),depend(ndtrn) :: i2a
real*8 dimension(ndtrn),depend(ndtrn) :: aval
real*8 dimension(nvmax,ndtrn),depend(nvmax,ndtrn) :: scom
real*8 dimension(ndtrn),depend(ndtrn) :: beth
integer :: iadftyp
logical :: lprn
logical :: lcpl
logical :: lorb
logical :: lbeth
logical :: letyp
logical :: lptyp
logical :: lrtyp
logical :: lhtyp
logical :: lityp
logical :: lstyp
logical :: lltyp
integer :: itieactn
logical dimension(ndlev),depend(ndlev) :: ltied
end subroutine xxdata_04
function i4unit(iunit) ! in i4unit.for
integer :: iunit
integer :: i4unit
end function i4unit
subroutine xxword(ctext,cdelim,nfirst,iwords,ifirst,ilast,nwords) ! in xxword.for
character*(*) :: ctext
character*(*) :: cdelim
integer :: nfirst
integer, optional,check(len(ifirst)>=iwords),depend(ifirst) :: iwords=len(ifirst)
integer dimension(iwords) :: ifirst
integer dimension(iwords),depend(iwords) :: ilast
integer :: nwords
end subroutine xxword
subroutine xxslen(cstrng,ifirst,ilast) ! in xxslen.for
character*(*) :: cstrng
integer :: ifirst
integer :: ilast
end subroutine xxslen
subroutine xxprs1(ndmet,string_bn,wno,cpl,npt,ipla,zpla,ifail) ! in xxprs1.for
integer*4, optional,check(len(ipla)>=ndmet),depend(ipla) :: ndmet=len(ipla)
character*(*) :: string_bn
real*8 :: wno
character*1 :: cpl
integer*4 :: npt
integer*4 dimension(ndmet) :: ipla
real*8 dimension(ndmet),depend(ndmet) :: zpla
integer*4 :: ifail
end subroutine xxprs1
function r8fctn(str,iabt) ! in r8fctn.for
character*(*) :: str
integer :: iabt
real*8 :: r8fctn
end function r8fctn
function i4fctn(str,iabt) ! in i4fctn.for
character*(*) :: str
integer :: iabt
integer :: i4fctn
end function i4fctn
function i4idfl(n,l) ! in i4idfl.for
integer :: n
integer :: l
integer :: i4idfl
end function i4idfl
subroutine xxpars(ndmet,strng1,npt,bwnoa,lseta,prtwta,cprta,ifail,itype) ! in xxpars.for
integer*4, optional,check(len(bwnoa)>=ndmet),depend(bwnoa) :: ndmet=len(bwnoa)
character*(*) :: strng1
integer*4 :: npt
real*8 dimension(ndmet) :: bwnoa
logical dimension(ndmet),depend(ndmet) :: lseta
real*8 dimension(ndmet),depend(ndmet) :: prtwta
character dimension(ndmet,(*)),depend(ndmet) :: cprta
integer*4 :: ifail
integer*4 :: itype
end subroutine xxpars
subroutine xxrmve(cstrg1,cstrg2,crmve) ! in xxrmve.for
character*(*) :: cstrg1
character*(*) :: cstrg2
character*1 :: crmve
end subroutine xxrmve
subroutine xxcase(input,output,type_bn) ! in xxcase.for
character*(*) :: input
character*(*) :: output
character*2 :: type_bn
end subroutine xxcase
! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/
然后我用f2py -c -m <filename> <input files>
编译。编译完成时有一些警告,但没有错误。生成的python共享对象是adf04_2py.cpython-35m-x86_64-linux-gnu.so
我已经开始构建一个python脚本,我想用它来导入共享对象并调用&#39; xxdata_04&#39;子程序。到目前为止,我的过程就是这个......
我确定有更好的方法可以做到这一点,但是fortran代码很多而且有些冗长,而不是我自己的代码,所以这似乎是最直接的方法。
到目前为止,我的脚本看起来像这样:
import adf04_2py as adf
import numpy as np
itieactn = 1
iunit = 19 ; titled = '---' ; iz = 18 ; iz0 = 0 ; iz1 = 0 ; il = 0
bwno = 0.0 ; npl = 0 ; bwnoa = 1.0 ; prtwta = 0.0
qdorb = 0.0 ; qdn =0.0 ; iorb = 0 ; ia = 0 ; cstrga = '-'
isa = 0 ; ila = 0 ; xja = 0.0 ; wa = 0.0 ; cpla = '-' ; npla = 0 ; ipla = 0
zpla = 0 ; nv = 0 ; scef = 0.0 ; itran = 0 ; maxlev = 0 ; tcode = '-'
ila = 0 ; i2a = 0 ; aval = 0.0 ; scom = 0.0 ; beth = 0 ; iadftyp = 0
cprta = np.asarray([['---------'],['---------'],['---------']],dtype='c')
lbseta = False ; lqdorb = False ; lprn = False ; lcpl= False ; lorb = False
lbeth = False ; letyp = False ; lptyp = False ; lrtyp = False ; lhtyp = False
lityp = False ; lstyp = False ; lltyp = False ; ltied = False
adf04_dat = adf.xxdata_04(iunit, titled, iz, iz0, iz1 , bwno, npl, bwnoa ,
lbseta , prtwta , cprta , il , qdorb , lqdorb ,
qdn , iorb , ia , cstrga , isa , ila , xja , wa ,
cpla , npla, ipla , zpla , nv , scef , itran ,
maxlev , tcode , ila , i2a , aval , scom , beth ,
iadftyp , lprn, lcpl , lorb , lbeth , letyp, lptyp ,
lrtyp , lhtyp , lityp , lstyp , lltyp , itieactn ,
目前,它失败了:
runfile('/home/ivan/GoogleDrive/AUAMO/codes/read_adf04_2py/xxdata_04/read_adf2py.py', wdir='/home/ivan/GoogleDrive/AUAMO/codes/read_adf04_2py/xxdata_04')
File "/home/ivan/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/home/ivan/anaconda3/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/home/ivan/GoogleDrive/AUAMO/codes/read_adf04_2py/xxdata_04/read_adf2py.py", line 32, in <module>
ltied )
error: failed in converting 11st argument `cprta' of adf04_2py.xxdata_04 to C/Fortran array
根据签名文件,cprta应该是形状的字符数组((ndmet),(字符数)),其中ndmet是亚稳态的数量,字符数是9.我试过使用np.asarray分配它。转换第11个参数`cprta&#39; adf04_2py.xxdata_04到C / Fortran数组&#34;错误。
有任何帮助吗?我完全清楚我不知道自己在做什么。
答案 0 :(得分:1)
感谢CT朱解决这个问题。从终端运行脚本提供了更丰富的错误消息。
0-th dimension must be fixed to 1 but got 3 (real index=0)
Traceback (most recent call last):
File "read_adf2py.py", line 33, in <module>
ltied )
adf04_2py.error: failed in converting 11st argument `cprta' of adf04_2py.xxdata_04 to C/Fortran array
我重新分配了字符串数组,使其尺寸为1 x 9,并且该错误消失了。
cprta = np.asarray(['---------'],dtype='c')
现在我遇到了段错误,但我认为这是一个单独的问题。
谢谢CT朱!