Sorry for asking such a basic questions. I am working on some FOTRAN77 codes and trying to call it from Python. However, I have found some problems in returning two or more values from a function.
Below is the code. It has four inputs (APPRAT,APPNUM,APSPAC,KOC), and I want to return three parameter values (APPTOT,KD,TDEGF), which are stored in GENEEC3. My compiled code works well when only one parameter is returned, but does not work when I request it to send three parameters back.
So please give me some suggestions and thanks everyone for the help!
Function GENEEC3 (APPRAT,APPNUM,APSPAC,KOC)
REAL GENEEC3(3)
CHARACTER*1 METHOD,AGAIN,WETTED,ADSORP,AIRFLG,GRNFLG,ORCFLG,GRSIZE
Cf2py intent(in) APPRAT,APPNUM,APSPAC,KOC,METHAF,WETTED,METHOD,AIRFLG
Cf2py intent(in) YLOCEN,GRNFLG,ORCFLG,INCORP,SOL,METHAP,HYDHAP,FOTHAP
Cf2py intent(out) GENEEC3(3)
C
APPTOT=APPRAT*APPNUM
TDEGF = APPNUM * APSPAC
KD = 0.0116 * KOC
C
GENEEC3(1)=APPTOT
GENEEC3(2)=KD
GENEEC3(3)=TDEGF
C
RETURN
END Function GENEEC3
I tried to define fortran function and let it work with f2py but f2py seems to create a function wrapper where return value is scalar. i couldn't figure out how to get it straight.
instead i tried to define subroutine. Then f2py cleverly guessed that what I really wanted was array valued function! I confirmed below work on both gfortran and pgf90.
f2py --fcompiler=gnu95 -c -m geneec3 geneec3.f90
then in python
>>> import geneec3
>>> geneec3.geneec3(1,1,1,1)
>>> array([ 1. , 0.0116, 1. ], dtype=float32)
>>>
geneec3.f90
subroutine GENEEC3 (APPRAT,APPNUM,APSPAC,KOC, results)
implicit none
REAL, dimension(3), intent(out) :: results
real, intent(in) :: apprat, appnum, apspac, koc
real apptot, tdegf, kd
C
APPTOT=APPRAT*APPNUM
TDEGF = APPNUM * APSPAC
KD = 0.0116 * KOC
C
results(1)=APPTOT
results(2)=KD
results(3)=TDEGF
END subroutine GENEEC3