;+
; NAME:
;       BSC_TIM2DSET
; PURPOSE:
;       extract indicies of BSC time range
; CALLING SEQUENCE:
;       SS=BSC_TIM2DSET(BSC_INDEX,CHAN,TSTART,TEND,COUNT=COUNT)
; INPUTS:
;       BSC_INDEX    - BSC index structures
;       CHAN         - channel to select
; KEYWORDS:
;       TSTART,TEND  - string start and end times of data to extract
;                      (both must be entered as strings using UTPLOT 
;                      conventions, e.g., '09:00'). 
;                      If only one time is entered, then data nearest that
;                      time is selected. 
;                      To select all data after certain time (say, 09:00), use:
;                      TEND='09:00',/TEND
;                      To select all data before 09:00, use: 
;                      /TSTART,TEND='09:00'
; RETURNED OUTPUTS:
;       SS           - vector of indicies corresponding to requested time 
;                      and channel.
; OPTIONAL OUTPUTS:
;       COUNT        - # of data points extracted
; PROCEDURE:
;       Simple use of WHERE
; HISTORY:
;       Written Oct'93 by D. Zarro (ARC)
;       Modified Dec'93, DMZ, changed TSTART and TEND usage
;       20-Jun-94, DMZ, fixed potential bug in time search
;-

function bsc_tim2dset,bsc_index,chan,tstart=tstart,tend=tend,count=count

on_error,1

ss=-1
ok=bsc_check(bsc_index)
if (not ok) then begin
 message,'usage --> SS=BSC_TIM2DSET(BSC_INDEX,CHAN,TSTART=TSTART,TEND=TEND)
endif

;-- force user to select channel since time can be different for
;   different channels

ss=bsc_chan(bsc_index,chan)
index=bsc_index(ss)

;-- now select subset based on time range

ndsets=n_elements(ss)
if ndsets eq 1 then return,ss
trange=[anytim2ints(index(0)),anytim2ints(index(ndsets-1))]
bstart=fmt_tim(index(0),/msec)
bend=fmt_tim(index(ndsets-1),/msec)


nostart=(n_elements(tstart) eq 0) and (n_elements(tend) ne 0)
noend=(n_elements(tend) eq 0) and (n_elements(tstart) ne 0)

dstart=gt_day(index(0),/string)
if n_elements(tstart) ne 0 then begin
 if datatype(tstart) eq 'STR' then begin
  dash=strpos(tstart,'-')                       ;-- date entered?
  if dash eq -1 then tstart=tstart+' '+dstart
  ts=anytim2ints(tstart)
  trange(0)=ts
 endif else tstart=bstart
endif else tstart=bstart

dend=gt_day(index(ndsets-1),/string)
if n_elements(tend) ne 0 then begin
 if datatype(tend) eq 'STR' then begin
  dash=strpos(tend,'-')                       ;-- date entered?
  if dash eq -1 then tend=tend+' '+dend
  ts=anytim2ints(tend)
  trange(1)=ts
 endif else tend=bend
endif else tend=bend

;-- single time entered?

if nostart then begin
 tstart=tend
 trange(0)=trange(1)
endif

if noend then begin
 tend=tstart
 trange(1)=trange(0)
endif

;-- check that entered times are ok

sec_day=24.*3600.
bday=gt_day(index)
btime=gt_time(index)/1000.+sec_day*(bday(ndsets-1)-bday(0))
diff=(trange.day)(1)-(trange.day)(0)
istart=(trange.time)(0)/1000.
iend=(trange.time)(1)/1000.+sec_day*diff

tb=[istart,iend]
istart=min(tb)
iend=max(tb)
 
;if (istart gt max(btime)) or (iend lt min(btime)) then begin
; message,'no BSC data during requested time interval'
;endif


;-- find nearest records

case 1 of
 nostart: begin
           in=where(btime le iend,cin)
           ok=in(cin-1) & count=1
          end
 noend:   begin
           en=where(btime ge istart,cen)
           ok=en(0) & count=1
          end
 else:    ok=where((btime ge istart) and (btime le iend), count)
endcase

if count eq 0 then message,'no data at specified times'
tstart=fmt_tim(index(ok(0)),/msec)
tend=fmt_tim(index(ok(count-1)),day_str,time_str,/msec)
ss=ss(ok) 

return,ss & end
