	FUNCTION BinarySea, errStat, stTime, day, time
;	-----------------------------------------------------------------
;+							18-Apr-1991
;	Name:
;		BinarySea
;	Purpose: 
;		Binary search of time and day vectors (if present) for the 
;		occurance of the value "stTime" and return the data index
;		value for the nearest match.
;	Calling Sequence:
;		index = BinarySea(errStat, stTime, day, time)
;	Inputs:
;		stTime 	value of the search keys (time and day) as follows:
;			stTime = {stru_Nam, time: LONG(0), day: FIX(0)}
;			where: time is milliseconds of the day and day is
;			days since 1-1-1979.
;               day     primary search parameter: vector of the # of days 
;			since 1-1-1979.
;		time	secondary search parameter: vector of the # of 
;			milliseconds of the day.
;	Outputs: 
;		errStat	= 1 	indicates success
;			  0	indicates failure to find any elements
;				which satisfy the input constraints.
;			  -1	indicates input times at or outside the
;				range of the input search parameters.
;	Returned:
;	        BinarySea	element value of the input search parameters
;				which matches the search key value.
;	Side Effects:
;		none
;	Restrictions:
;		1). Search parameters are assumed to be linear arrays of 
;			equal length.
;		2). Both time and day must be present (ver. 1 limit)
;		3). Note that search key value is in the form of a structure.
;	HISTORY:
;		Written by GAL 25-Feb-1991
;		18-Apr-91 feature corrections
;-
;	--------------------------------------------------------------------	
;	ON_error, 2			;force return to caller on error

;	Check the number Of FORMAL PARAMETERS:
	CASE N_PARAMS() OF
	  3:  BEGIN
	      	Print, 'Single Key Search of data not supported yet'
		i = -1 
		errStat = 0
		Return, i
	      END
	  4:  BEGIN
;		Normal search of data: day and time.
;		Check day parameter and Define the range of the search var:
		s_info = SIZE(day)
		IF (s_info(0) NE 1) THEN BEGIN	;must be a LINEAR array
		  Print, 'ERRROR---SEARCH PARAMETER must be a LINEAR ARRAY'
       		  Print, 'Return from BiSeaTime without searching'
		  i = 0			;MDM added 11-Nov-91
		  errStat = 0
		  Return, i
		ENDIF
		ndset = FIX( s_info(s_info(0)) )	;# of elements in sea
							;parameter
		s_inf_t = SIZE(time)
		IF (ndset NE FIX( s_inf_t(s_inf_t(0)) ) ) THEN BEGIN
		  Print, 'Error-- Search arrays not the same size!!!'
		  errStat = 0
		  i = -1
		  Return, i
		ENDIF
	      END
	  ELSE: BEGIN
		  Print, 'BinarySea--Wrong number of formal Parameters'
		  i = -1
		  errStat = 0
		  RETURN, i
	        END
	ENDCASE

; Initialize search parameters:
	l = 0
	r = ndset - 1
	found = 0			;false

;	Check Search key at the bounds of the input times:
	IF ( stTime.day LE day(0) ) THEN BEGIN
	  IF (stTime.day LT day(0) ) THEN BEGIN	;do not need to ck time
	    found = 1			;done
	    i = 0			
	    errStat = -1		;bounds flag set
	    Print, '**** Input Time AT or BEFORE 1st Data Entry ****'
	  ENDIF ELSE BEGIN		;stTime.day = day(0)
	    IF (stTime.time LE time(0) ) THEN BEGIN	;ck msod
	      found = 1			;done
	      i = 0 
	      errStat = -1
	      Print, '**** INPUT TIME AT or BEFORE 1st DATA ENTRY ****'
	    ENDIF
	  ENDELSE
	ENDIF 

	IF ( stTime.day GE day(ndset-1) ) THEN BEGIN
	  IF ( stTime.day GT day(ndset-1) ) THEN BEGIN
	    found = 1			;done
	    i = ndset -1
	    errStat = -1
	    Print, '**** INPUT TIME AT or AFTER Last DATA ENTRY ****'
	  ENDIF ELSE BEGIN		;stTime.day = day(ndset-1)
	    IF (stTime.time GE time(ndset-1) ) THEN BEGIN	;ck msod
	      found = 1			;done
	      i = ndset - 1
	      errStat = -1		;bounds flag set
	      Print, '**** Input Time AT or AFTER Last Data Entry ****'
	    ENDIF 
	  ENDELSE
	ENDIF 

;	Search and FIND element i which corresponds to start time:
	WHILE ((l LE r) AND (NOT found)) DO BEGIN
	  i = (l + r)/2
	  print,'*** compare i and i+ 1: ', i , i + 1
	  IF ((stTime.day GE day(i))AND( stTime.day LE day(i+1) ))THEN BEGIN

	    IF (day(i) LT day(i+1)) THEN BEGIN	;day # changed
	      PRINT, '**** DAY boundary encountered ****'
	      delT = LONG(86400)			;sec/day
	      tsec_i = time(i) / 1000	;ck times in sec from time(i)
	      tsec_i1= (day(i+1)-day(i))*delT + time(i+1)/1000
	      stsec  = (stTime.day - day(i))*delT + stTime.time/1000

	      IF ((stTime.day EQ day(i))OR(stTime.day EQ day(i+1))) THEN BEGIN
		IF ((stsec GE tsec_i)AND(stsec LE tsec_i1)) THEN BEGIN	
		  found = 1		;done
	          errStat = 1		;succussful search
	          Print, '**** FOUND at i =', i
		ENDIF ELSE BEGIN	;are sec low or high?
		  IF (tsec_i LT stsec) THEN BEGIN
		    l = i + 1		;increment lower limit
		  ENDIF ELSE BEGIN 
		    r = i - 1		;decrement upper limit
		  ENDELSE
		ENDELSE
	      ENDIF ELSE BEGIN		;must have day# gap
		found = 1		;done
		errStat = 1		;succussful search
		Print, '**** Found between day gaps at i =', i
	      ENDELSE
	    ENDIF ELSE BEGIN		;no day boundary found
	      delTime = LONG(0)
	      IF((stTime.time GE time(i))AND(stTime.time LE 	$
				(time(i+1) + delTime) )) THEN BEGIN
	        found = 1		;start time found--set to true
	        IF(stTime.time EQ (time(i+1) + delTime)) THEN BEGIN 	
		  i = i + 1			;save looping thur again
	        ENDIF
	        errStat = 1		;succussful search
	        Print, '**** FOUND at i =', i
	      ENDIF ELSE BEGIN	;is the msod low or high?
	        IF (time(i) LT stTime.time) THEN BEGIN  
	          l = i + 1	;increment lower limit
	        ENDIF ELSE BEGIN
		  r = i - 1	;decrement uppper limit
	        ENDELSE                     
	      ENDELSE		;msod test
	    ENDELSE			;end of day boundary check

	  ENDIF ELSE BEGIN	;is the day# low or high?
	    IF (day(i) LT stTime.day) THEN BEGIN
	        l = i + 1	;increment lower limit
	    ENDIF ELSE BEGIN
		r = i - 1	;decrement uppper limit
	    ENDELSE                     	    	
	  ENDELSE		;days since 79 test
	ENDWHILE	

	RETURN, i
	END
