C This program demonstrates how to use Callable IDL from a FORTRAN program.

C Data for five functions are generated; the data are imported into IDL,
C and a companion IDL program ( is invoked to plot the data.
C The companion program brings up a widget application. Included are a
C button to exit the application and a draw widget where the imported data
C are plotted.

C All of the arguments and keywords which are passed to the companion
C progrsam (and, subsequently, to the IDL PLOT procedure), are defined in
C this FORTRAN program.

C The use of companion IDL programs for Callable IDL applications is very
C desirable. If designed carefully, they can simplify both "sides" of
C the Callable IDL environment.

C In addition to the companion IDL program, a set of "wrapper" functions,
C written in C, are used to "step" from FORTRAN into the actual IDL
C internal functions that are called. These "wrapper" functions are located
C in the file callable_wraps.c .

C Written by Doug Loucks
C Research Systems, Inc., Oct, 1998

C Function data generation and plot annotation are taken from a program by
C Bill Gadzuk and Barbara am Ende, NIST, October, 1998

C Define a few IDL parameters based upon those found in export.h

C This eases changes to the size of the function arrays.

C Declare the arrays for the function data.
     *          Y1(NUMPOINTS),
     *          Y2(NUMPOINTS),
     *          Y3(NUMPOINTS),
     *          Y4(NUMPOINTS),
     *          Y5(NUMPOINTS)

      INTEGER*4 options, just_cleanup, status
      INTEGER*4 ndim, dim(1), type
      INTEGER*4 callback /0/
      INTEGER*4 zero /0/
      CHARACTER*1 null

C Initialize local variables.
      null = char(0)

C Initialize Callable IDL.
      options = 0
      status = IDL_Init( options )

C Check for problems. Exit if Callable IDL can't be initialized.
      if (status .eq. IDL_FALSE) then
        print *, 'Failure to Initialize Callable IDL. Status: ', status
        call exit

C Generate the function data to be plotted (From Barbara am Ende).
      EX = 0.0
      DEX = 0.1

      DO 15 J=1, NUMPOINTS
        X1 = EX
        X2 = EX / 2.0
        X3 = EX / 3.0
        X4 = EX / 4.0
        X5 = EX / 5.0
        Y1(J) = X1 * (2.+X1) / (1.+X1)**2
        Y2(J) = X2 * (2.+X2) / (1.+X2)**2
        Y3(J) = X3 * (2.+X3) / (1.+X3)**2
        Y4(J) = X4 * (2.+X4) / (1.+X4)**2
        Y5(J) = X5 * (2.+X5) / (1.+X5)**2
        XP(J) = EX
        EX = EX + DEX

C Prepare to import these data into the IDL environment. First, define
C the arguments to the IDL function that imports data.
      ndim = 1
      dim(1) = NUMPOINTS
      type = IDL_TYP_FLOAT
      callback = 0
      zero = 0

C Import each of the function arrays. For your code, use one function 
C each call forarray you need to import.
C In these examples, the name of the array in IDL is given as the
C first argument, and is enclosed in single quotes.
C The second array name (the 5th argument) is the array name
C as used in the Fortran code.  If you wish to keep the two separately
C named, you could rename the IDL array in quotes as "IDL_xp".
C If so, you should also change the name of the array farther down when
C call the function, IDL_ExecuteStr to include the argument IDL_xp

      call IDL_ImportNamedArray( 'xp' // null, ndim, dim,
     *     type, xp, callback, zero )

      call IDL_ImportNamedArray( 'y1' // null, ndim, dim,
     *     type, y1, callback, zero )

      call IDL_ImportNamedArray( 'y2' // null, ndim, dim,
     *     type, y2, callback, zero )

      call IDL_ImportNamedArray( 'y3' // null, ndim, dim,
     *     type, y3, callback, zero )

      call IDL_ImportNamedArray( 'y4' // null, ndim, dim,
     *     type, y4, callback, zero )

      call IDL_ImportNamedArray( 'y5' // null, ndim, dim,
     *     type, y5, callback, zero )
C Execute the IDL command that runs the companion IDL program. The
C companion program is passed the data which have been imported into
C IDL.  Here you are calling the name of the procedure contained within
C and  passing arguments that contain the array names and the
C keywords needed for the plot command.
      call IDL_ExecuteStr(
     *  'align, xp, y1,y2,y3,y4,y5,
     *  xtitle = ''INCIDENT ENERGY (EV)'',
     *  xstyle = 1, xticks=4, xminor=10, xrange = [0,8], xsize=500,
     *  ytitle = ''T TO V EFFICIENCY'',
     *  ystyle = 1, yrange = [0,1], ysize=500,
     *  yticks = 1, yminor = 10,
     *  charsize = 1, psym=0,
     *  title=''ALIGN -- Callable IDL Example''' // null )

C Do the IDL cleanup stuff. With just_cleanup set to one, the IDL_Cleanup
C function returns to this program.
      just_cleanup = 1
      status = IDL_Cleanup( just_cleanup )

      print *, 'FORTRAN program TESTPLOT -- Done -- status:', status