PRO Integration, xP, yP, result, STATIC = static, $
	NODISPLAY = noDisplay, MDISP = mdisp

;+
;NAME:
;	Integration
; PURPOSE:
;	Computes the area between a plot and two points given.
;	The User is responsible for the coherence of the operation.
; CATEGORY:
;	1D
; CALLING SEQUENCE:
;	Integration, x, y [, result ]
; INPUTS:
;	x: The abscissae, must be a vector
;	y: The function f(x) to integrate (points), must be a vector
; OUTPUT
;	result: the value of the integrated area
; KEYWORDS:
;	/STATIC: If present, the boundary values for the integration
;		will be asked on the terminal. Otherwise they
;		will be clicked with the mouse.
;	/NODISPLAY: If present, the result of the integration is
;		not shown, only stored in "result".
; SIDE EFFECT:
;	Windows opened if working on X
; RESTRICTION:
;	For vectors with a small number of elements (smaller than
;	the resolution of the screen window) the limits of the
;	integral could be not totally vertical, due to the 
;	discretization of the function.
;
;-
; MODIFICATION HISTORY:
;	Created in July 1991 by A.Csillaghy

IF Keyword_Set( MDISP ) THEN BEGIN
  LoadSelection, d1, d2, x, y 
  x = x(*,0) & y = y(*,0)
ENDIF ELSE BEGIN
  x = xP(*,0)
  y = yP(*,0)
ENDELSE

IF Keyword_Set(STATIC) THEN BEGIN
  hms = GetHMS()
  Clear_Screen
  Print, 'Integration: insert the boundary  values: '
  Print, ' '
  Read_Test, 'Minimum x: ',Min(x),x0, HMS = hms
  Read_Test,'Maximum x: ',Max(x), x1, HMS = hms
  Read_Test,'Y - Value: ',0., y1
ENDIF ELSE BEGIN
  IF !d.name EQ 'X' THEN BEGIN
    plotWin = !d.window
    winNr = WMesg( 'Click to the lowest <x> point ' , $
	'Zoom Plot', 500,200 )
    WSet, plotWin
    Cursor, x0, dummy, /UP
    WDelete, winNr

    winNr = WMesg( 'Click to the highest <x,y> point' , $
	'Zoom Plot', 500,200 )
    WSet, plotWin
    Cursor, x1, y1, /UP
    WDelete, winNr
  ENDIF ELSE BEGIN
      Print, 'Move the cursor to the first x point and press <Return>'
      IF !d.name EQ 'TEK' THEN CursorVT, x0, dummy $
      ELSE Cursor, x0, dummy
      Print, 'Move the cursor to the second <x,y> point and press <Return>'
      IF !d.name EQ 'TEK' THEN CursorVT, x1, y1 $
      ELSE Cursor,x1,y1
  ENDELSE
ENDELSE

distance = Where( (x GE x0) AND (x LE x1) )

xVector = [ x0, x(distance),x1, x0 ]
yVector = [ y1, y(distance),y1, y1 ]

result = Poly_Area( xVector, yVector )

PolyFill, xVector, yVector

IF !d.name EQ 'X' THEN BEGIN
  msgWinNr = WMesg(['The value of the integral is: ',' ', $
	'      '+String(result) , ' ' , ' ', $
	'Click a mouse button in this window to continue ... ' ],  $
	'Integral', 300, 100 )
  Cursor, d1, d2 
  WDelete, msgWinNr
ENDIF ELSE BEGIN
  Print, 'Value of Integral: '+StrTrim(result,2)
  Print, 'Hit any key to continue ... '
  HAK
ENDELSE


END

  
