0

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   
10
  • cant you use fortran 90 or later? i doubt fortran 77 support array as a function return value Commented Apr 4, 2012 at 19:55
  • @yosukesabai, I am sorry that I could not change it to fortran90... Commented Apr 4, 2012 at 19:57
  • why is that...? f90 is superset of f77, so almost all f77 code is valid f90 code. and there are free f90 compilers these days if money is an issue (gfortran for example) Commented Apr 4, 2012 at 19:59
  • I think a function in FORTRAN returns a single value, an array probably doesn't qualify. Been about 30 years since I last wrote anything in FORTRAN so things may have evolved a bit since then! Commented Apr 4, 2012 at 19:59
  • and, if you want array returned, you are already getting into f90, and you have used intent directive, which is introduced in f90. Commented Apr 4, 2012 at 20:00

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help! That really saves me a lot of time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.