2

I used f2py and created the dynamic fortran library, and try to call subroutine for creation the file with formated strings. But I have so problems with string array. All numerical arrays were converted correctly, but I have error about string arrays:

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

This is my Python code:

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 ])

...other numerical arrays

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)

This is my Fortran code:

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
Palina
  • 49
  • 1
  • if you are using the same variable names in your python code as in your fortran code then on first glance it looks to me as if you did not give the arguments in the same order. so fortran gets a wrong datatype for the dd argument. you might want to check that. – WWhisperer Feb 29 '16 at 08:31
  • You can use `dtype='c'`, see http://stackoverflow.com/questions/22293180/passing-numpy-string-format-arrays-to-fortran-using-f2py – artscan Mar 01 '16 at 02:30

0 Answers0