function tim_bin,index,bin_interval,start_time=start_time,stop_time=stop_time,times=times
;+
; NAME:
;   tim_bin
; PURPOSE:
;   Given a vector of times in any Yohkoh format and time interval,
;   return a vector of indices corresponds to the bin intervals.
;
; CALLING SEQUENCE:
;   indices = tim_bin(time_vec, bin_interval)
;   indices = tim_bin(time_vec, bin_interval, start_time='15:52 9-nov')
;   indices = tim_bin(time_vec, bin_interval, start_time='15:52 9-nov',times=times)
;
; INPUTS:
;   time_vec	- A vector of times in any Yohkoh format
;   bin_interval- Length of the time bin (sec)
;
; OPTIONAL INPUT KEYWORDS:
;   start_time	- Start time -- default is the first time in time_vec
;   stop_time	- Last time  -- default is the last time in time_vec.  This is
;				always inclusive.  That is, enough bins are returned
;				to include this time.
; OPTIONAL OUTPUT KEYWORDS:
;   times	- Times of the beginning of each time bin
;
; HISTORY:
;    9-Nov-94, J. R. Lemen LPARL, Written.
;-


if (n_elements(index) eq 0) or (n_elements(bin_interval) eq 0) then begin
   message,'tim_bin,index,bin_interval,times=times,		$',/cont
   message,'         start_time=start_time,stop_time=stop_time',/cont
   return,-1
endif

jj = int2secarr( index )
time_vec = index(jj) & nn = n_elements(time_vec) - 1

; Set up the start and stop times:

if n_elements(start_time) eq 0 then start_timex = time_vec(0)  else start_timex = start_time
if n_elements(stop_time)  eq 0 then stop_timex  = time_vec(nn) else stop_timex  = stop_time

; Set up times

nbin = ((addtime(stop_timex,diff=start_timex,/sec)+2*bin_interval) / bin_interval) > 1
times = anytim2ints(start_timex, off=findgen(nbin) * bin_interval)
t0 = int2secarr(times,start_time)
t1 = t0(1:*)			; End times of each bin

tt = int2secarr(index,start_time)

npts = n_elements(index)
vv   = lonarr(npts)
for i=0,npts-1 do vv(i) = (where((tt(i)-t0 ge 0) and (tt(i)-t1 lt 0)))(0)

return,vv
end
