FORTRAN77 function returns an array

Go To StackoverFlow.com

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   
2012-04-04 19:48
by TH339
cant you use fortran 90 or later? i doubt fortran 77 support array as a function return valu - yosukesabai 2012-04-04 19:55
@yosukesabai, I am sorry that I could not change it to fortran90.. - TH339 2012-04-04 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 - yosukesabai 2012-04-04 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 - NealB 2012-04-04 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 - yosukesabai 2012-04-04 20:00
@yosukesabai, I am using gfortran as my compiler does that mean I can write fortran95 stuff in this fortran77 code?thank - TH339 2012-04-04 20:53
@NealB, thanks for the confirmatio - TH339 2012-04-04 20:53
yes, gfortran compiles f90, f95 codes. if you need to use "fixed format", that old f77 style code, you use -ffixed-form flag in gfortran. i tried your code, but gfortran gave segfault, even i compiled to create fortran executable. i can let the code to work with commecial compiler. i will test a bit mor - yosukesabai 2012-04-04 21:01
@yosukesabai, thanks so much. I still have some naive questions. Do I need to change .for code to f95 in order to active the feature of returning an array - TH339 2012-04-04 21:07
see in my example. turned out that it is easier to write fortran subroutine. f2py automatically returned it to python function. as array valued function is relatively new in fotran, i guess there are lots of fotran code written as subroutine which make more sense to treat as function in python - yosukesabai 2012-04-04 21:25


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
2012-04-04 21:22
by yosukesabai
Thank you so much for your help! That really saves me a lot of time - TH339 2012-04-04 21:38
Ads