C-------------------------------------------------------------------------
	INTEGER FUNCTION LOCATE2(n,ARRAY,val)
C-------------------------------------------------------------------------
C Find closest point in integer*2 ARRAY to value VAL by a Library Search
C-------------------------------------------------------------------------
	INTEGER*2 N,array(n),VAL
C find the direction of the array
	IF (ARRAY(N) .lt. ARRAY(1)) then
	  i1 = N
	  i2 = 1
	ELSEIF (ARRAY(N) .gt. ARRAY(1)) then
	  i1 = 1     
	  i2 = N
	ELSE
	  WRITE (*,*) 'Illegal range in LOCATE',1,ARRAY(1),N,ARRAY(N)
	  LOCATE2 = 1
	  RETURN
	ENDIF
	
C Is the point at an endpoint?
	IF (VAL .lt. ARRAY(I1)) then
	  LOCATE2 = I1
	  RETURN
	ELSEIF (VAL .gt. ARRAY(I2)) then
	  LOCATE2 = I2
	  RETURN
	ENDIF

C are we at adjacent points?
1	IF (ABS(i2-i1) .eq. 1) THEN
	  IF ((ARRAY(i2)-VAL) .gt. (VAL-ARRAY(i1))) THEN
	    LOCATE2 = i1
	  ELSE
	    LOCATE2 = i2
	  ENDIF
	  RETURN
	ELSE
	  im = (i2+i1)/2
	  if (array(im) .gt. VAL) then
	    i2 = im
	  ELSE
	    i1 = im
	  ENDIF
	ENDIF
	goto 1
	END
C-------------------------------------------------------------------------
	INTEGER FUNCTION LOCATEF(n,ARRAY,val)
C-------------------------------------------------------------------------
C find the closest point to in ARRAY to value VAL
C-------------------------------------------------------------------------
	REAL array(n),VAL

C find the direction of the array
	IF (ARRAY(N) .lt. ARRAY(1)) then
	  i1 = N
	  i2 = 1
	ELSEIF (ARRAY(N) .gt. ARRAY(1)) then
	  i1 = 1     
	  i2 = N
	ELSE
	  WRITE (*,*) 'Illegal range in LOCATE',1,ARRAY(1),N,ARRAY(N)
	  LOCATEF = 1
	  RETURN
	ENDIF

C Is the point at an endpoint?
	IF (VAL .lt. ARRAY(I1)) then
	  LOCATEF = I1
	  RETURN
	ELSEIF (VAL .gt. ARRAY(I2)) then
	  LOCATEF = I2
	  RETURN
	ENDIF

C are we at adjacent points?
1	IF (ABS(i2-i1) .eq. 1) THEN
	  IF ((ARRAY(i2)-VAL) .gt. (VAL-ARRAY(i1))) THEN
	    LOCATEF = i1
	  ELSE
	    LOCATEF = i2
	  ENDIF
	  RETURN
	ELSE
	  im = (i2+i1)/2
	  if (array(im) .gt. VAL) then
	    i2 = im
	  ELSE
	    i1 = im
	  ENDIF
	ENDIF
	goto 1
	END
