f2py字符串数组声明

时间:2016-02-29 08:03:20

标签: python arrays fortran f2py

我使用了f2py并创建了动态fortran库,并尝试调用子例程来创建带有格式化字符串的文件。但是我对字符串数组有这么大的问题。所有数值数组都已正确转换,但我有关于字符串数组的错误:

0-th dimension must be 2 but got 0 (not defined).
Traceback (most recent call last):
File "py_try.py", line 55, in <module>
     writelittler.write_obs(p,z,t,td,spd,wdir,slp,ter,xlat,xlon,date_char,num_met,num    _lev,kx,dd_strvar,station_strvar,synop,string4, bogus, iseq_num, iunit)
writelittler.error: failed in converting 14th argument `dd' of writelittler.write_obs to C/Fortran array

这是我的Python代码:

import numpy as np
import ctypes
from ctypes import c_int, c_char
writelittler=ctypes.CDLL("/writelittler.so")
p = np.array([ 982.6, 999.7 ])

......其他数值数组

bogus = 0
kx=2
iseq_num = 0
iunit=2
date_char = '      20160128060000'
  dd = np.array([ '1111111111111111111111111111111111111111',  '6666666666666666666666666666666666666666'])
 station = np.array([ 'V111111111111111111111111111111111111111','M111111111111111111111111111111111111111' ])

num_met=6
num_lev=1
synop='FM-12 SYNOP                             '
string4='                                        '
writelittler.write_obs(p,z,t,td,spd,wdir,slp,ter,xlat, xlon,date_char,num_met,num_lev,kx,dd_strvar, station_strvar,synop,string4, bogus, iseq_num, iunit)

这是我的Fortran代码:

subroutine write_obs(p, z, t, td, spd, wdir, xlon, kx, slp, ter, xlat,  date_char, dd, station, num_met, num_lev, synop, string4, bogus, iseq_num, iunit) bind(C, name='write_obs')
implicit none
integer k, kx, num_met, num_lev, iseq_num, iunit, ierr
real p(kx), slp(kx), z(kx), t(kx), td(kx)
real spd(kx), ter(kx), xlat(kx), xlon(kx), wdir(kx)
character*20 date_char
character*40 dd(kx), station(kx), synop, string4
logical bogus
character*84 rpt_format
character*22 meas_format
character*14 end_format

rpt_format = ' ( 2f20.5 , 2a40 , '&
&' 2a40 , 1f20.5 , 5i10 , 3L10 , '&
&' 2i10 , a20 , 13( f13.5 , i7 ) )'

meas_format = ' ( 10( f13.5 , i7 ) ) '
end_format = ' ( 3 ( i7 ) )'

do 100 k=1 , kx

write ( UNIT = iunit , iostat = ierr , FMT = rpt_format ) &
& xlat(k), xlon(k), dd(k), station(k), &
& synop , string4, ter(k), num_met, 0, 0, iseq_num, 0, &
& .true., bogus, .false., &
& -888888, -888888, date_char, slp(k), 0, &
& -888888., 0, -888888., 0, -888888., 0, &
& -888888., 0, -888888., 0, -888888., 0, -888888., 0, &
& -888888., 0, -888888., 0, -888888., 0, -888888., 0, &
& -888888., 0

write ( UNIT = iunit , iostat = ierr , FMT = meas_format ) &
& p(k), 0, z(k), 0, t(k), 0, td(k), 0, &
& spd(k), 0, wdir(k), 0, &
& -888888., 0, -888888., 0, -888888., 0, -888888., 0


write ( UNIT = iunit , iostat = ierr, FMT = meas_format ) &
& -777777., 0, -777777., 0, float(num_lev), 0, &
& -888888., 0, -888888., 0, -888888., 0, &
& -888888., 0, -888888., 0, -888888., 0, &
& -888888., 0

write ( UNIT = iunit, iostat = ierr, FMT = end_format ) &
& num_lev, 0, 0

if (ierr .NE. 0 ) then
   print '(A,I5,A)','Troubles writing a sounding.Error #', ierr
   stop 'writing_error'
endif
100 continue
return
end subroutine write_obs

0 个答案:

没有答案