Passing String to function

12 views
Skip to first unread message

Batu

unread,
Feb 26, 2011, 8:30:35 AM2/26/11
to Fwrap Users
Dear Fwrap Users:

I am trying to use Fwrap to wrap a scientific fortran library (getorb,
http://www.deos.tudelft.nl/ers/precorbs/tools/). I have been using
python only for a year or so, and I don't have much knowledge on
Fortran, so it is possible that I am missing something simple.

I was able to run fwrap with the following command:
fwrapc -v getorb_batu.f --build --fcompiler=gnu95 --name=getorb_batu --
override -lgfortran

getorb_batu.f is based on the getorb.f and also includes all the other
functions copied from several other files.

When I try to run the command, I need to pass a path to data (orbit)
folder as a string, but seems like I can only pass 1 character.

In [71]: getorb.getorb(631152000.0,lat,lon,orbit,"home",True)
Out[71]: (2, 631152000.0, 0.0, 0.0, 0.0, 'h', 1)

In [72]: getorb.getorb(631152000.0,lat,lon,orbit,"ome",True)
(getorb: scanning directory o)
Out[72]: (2, 631152000.0, 0.0, 0.0, 0.0, 'o', 1)

I tried changing the path variable to a bytearray, but then it gives
the following error message:

In [62]: getorb.getorb(631152000.0,lat,lon,orbit,path,True)
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)

/home/bosmanoglu/projectLocker/getorb/getorb_batu/<ipython console> in
<module>()

/home/bosmanoglu/projectLocker/getorb/getorb_batu/getorb_batu.so in
getorb_batu.getorb (getorb_batu.c:1272)()
38 cdef extern from "string.h":
39 void *memcpy(void *dest, void *src, size_t n)
---> 40 cpdef api object getorb(fwr_real_x8_t time, fwr_real_x8_t lat,
fwr_real_x8_t lon, fwr_real_x8_t orbit, fw_bytes path, fwl_logical_t
verbose):
41 """
42 getorb(time, lat, lon, orbit, path, verbose) ->
(fw_ret_arg, time, lat, lon, orbit, path, verbose,)

TypeError: Argument 'path' has incorrect type (expected str, got
bytearray)

Here is the function declaration from getorb.f:
**GETORB -- Get orbital position of satellite from ODR files
*+
FUNCTION GETORB (TIME, LAT, LON, ORBIT, PATH, VERBOSE)
INTEGER*4 GETORB
REAL*8 TIME, LAT, LON, ORBIT
CHARACTER PATH*(*)
LOGICAL VERBOSE

I am a little puzzled at the moment. Can you think of what the problem
might be?

Best Regards,
batu.

Batu

unread,
Mar 1, 2011, 1:22:49 PM3/1/11
to Fwrap Users
Dear All,

I do not know if this mailing list is still active. I did not get any
responses to my email, but today I was able to come up with a
solution. The only thing I had to do was change the declaration of the
PATH variable from
CHARACTER PATH*(*)
to
CHARACTER*(*) PATH

and now I can run the code I need, and pass a string to the path
variable. I guess I will have to go through and make changes to
similar declarations through the code. But for now, here is the output
I got with the mod:
In [6]: getorb.getorb(631151999,lat,lon,orbit,"/home/bosmanoglu/data/
orbits/DOR.ODR/", True)
(getorb: reading arclist /home/bosmanoglu/data/orbits/DOR.ODR//
arclist)
(getorb: orbit file /home/bosmanoglu/data/orbits/DOR.ODR//ODR.031)
Out[6]:
(0,
631151999.0,
-51.891608202160711,
-18.980847901621775,
805172.38447675982,
'/home/bosmanoglu/data/orbits/DOR.ODR/',
1)

And this is what I get when I run the fortran executable:
bosmanoglu@beau:~$ getorb sec=631151999.000 ~/data/orbits/DOR.ODR/
(getorb: reading arclist /home/bosmanoglu/data/orbits/DOR.ODR//
arclist)
(getorb: orbit file /home/bosmanoglu/data/orbits/DOR.ODR//ODR.031)
631151999.000 0 -51.8916082 -18.9808479 805172.384 4199881.110
-1444564.891 -5628914.821

Seems promising,
Best,
batu.

Reply all
Reply to author
Forward
0 new messages