X
75 Rate this article:
No rating

[INTERNAL] Short fortran example program communcates with IDL

Zachary Norman

A short Fortran program called "square.f90" that can be called from IDL is shown below:

subroutine square(argc,argv) !called by IDL


    integer*4 argc
        integer*8 argv(*)

    j = LOC(argc)

    call sqarea(%VAL(argv(1)),%VAL(argv(2)),%VAL(argv(3)))

    RETURN
end

subroutine sqarea(a,b,sum)
    REAL, INTENT( IN ) :: a,b
        REAL, INTENT( INOUT ) :: sum
    !REAL*4 a,b,sum
    !REAL*4 array(n), sum
    
        print*, a

    sum = 0.0
        sum = a * b
        print*, "sum: ", sum


    RETURN


end

Compiling

The program can be compiled with the following command on Linux and Mac:

gfortran -shared -fPIC -o libsquare.so square.f90

Calling from IDL

A example how to call the Fortran program from IDL is shown below:

IDL> a=5.2                                                                
IDL> b=6.3                                                                
IDL> sum = 0.0                                                            
IDL> s=call_external('/home/tech_support/libsquare.so', 'square_',a,b,sum)
   5.19999981    
 sum:    32.7599983

Written and reviewed by DS (9/17/2014)