pro bcs_dp_sync,dp_sync,roadmap,rm_idx,dp_idx
;+
;  Procedure to find which dp_sync records correspond to a given BCS roadmap
;  entry (within one dgi)
;
;  Input:
;           dp_sync     usual dp_sync structure
;           roadmap     usual roadmap structure
;           rm_idx      array of indices corrsponding to entries in roadmap
;                       for which a match in dp_sync in required
;
;  Output:
;           dp_idx(n,2)      array of indices for which dp_sync time is within
;                            the roadmap dgi. It is created with the size
;                            (n_elements(roadmap.time),2) for convenience.
;                            dp_idx(i,0) = first dp_sync datum in range
;                            dp_idx(i,1) = last  dp_sync satum in range
;                            if no dp_sync datum corresponding to the roadmap
;                            time interval is found then the dp_idx entries
;                            are set to -1.
;
;   Use:
;          IDL> bcs_dp_sync,dp_sync,roadmap,rm_idx,dp_idx
;
;  Performance:   Takes approx 25 secs to correlate a 1000 indices array.
;                 Any suggestions for speed-up would be welcome.
;
;    CDP   March 92
;    DMZ   Oct 92 -- modified to accept DGI from BCS index (as well as ROADMAP)
;-

;
;-- check if user entered BCS INDEX in lieu of ROADMAP
;   (that way DGI can be obtained from INDEX.BCS.DGI)
;
    
tags=strupcase(tag_names(roadmap))
yes_index=(tags(0) eq 'GEN') and (tags(1) eq 'BCS')

if yes_index then begin
 dgi=roadmap.bcs.dgi & temp=roadmap.gen
endif else begin
 dgi=roadmap.dgi & temp=roadmap
endelse

if yes_index then print,'Correlating index and dp_sync times....' else $
 print,'Correlating roadmap and dp_sync times....'

;
;  form output array
;
dp_idx = intarr(n_elements(temp.time),2)

;
;  check for roadmap starting before dp_sync - set index to -1 for those cases
;
first = max([min(where(temp.time gt dp_sync(0).time)),0])
if first gt 0 then dp_idx(0:first-1,*) = -1
;
;  initial search range (ie all)
;
dp1 = 0
dp2 = n_elements(dp_sync.time) - 1

;
;  loop over required roadmap indices
;
ndt = n_elements(dp_sync.time)
i1 = rm_idx(first)
i2 = rm_idx(n_elements(rm_idx)-1)

dd = (dp_sync.day - dp_sync(0).day)*86400.*1000.
ddr = (temp.day-temp(0).day)*86400.*1000.

for i=i1,i2  do begin

;
;  start/end time of this spectrum in millisec
;
   st = long(temp(i).time) + ddr(i)
   en = st + long(dgi(i)*125.0)
;
;  corresponding dp_sync entry... first and last
;
   dp_idx(i,0) = min(where(dp_sync(dp1:dp2).time+dd(dp1:dp2) gt st)) + dp1
   dp_idx(i,1) = max(where(dp_sync(dp1:dp2).time+dd(dp1:dp2) lt en)) + dp1
;
;  if roadmap time is not in range of dp_sync data expected then 
;  start from scratch
;
   if dp_idx(i,1) eq dp2 then begin
      dp1 = 0
      dp2 = ndt  - 1
      dp_idx(i,0) = min(where(dp_sync(dp1:dp2).time+dd(dp1:dp2) gt st)) + dp1
      dp_idx(i,1) = max(where(dp_sync(dp1:dp2).time+dd(dp1:dp2) lt en)) + dp1
   endif
;  print,i,dp1,dp2,dp_idx(i,0),dp_idx(i,1)
;
;  calculate length of search range for next time
;
   if dp_idx(i,0) ge 0 then begin
      if dp_sync(dp_idx(i,0)).acc_interval(0) gt 0 then begin
         rel = (dgi(i)*0.125)/dp_sync(dp_idx(i,0)).acc_interval(0) 
      endif else begin
         rel = 50
      endelse
   endif
   
;
;  save search range for next time
;
   dp1 = max([0,dp_idx(i,1)])
   dp2 = max([0,fix(dp1 + rel*3)])
   dp2 = min([dp2,ndt-1])

endfor

;
;  final check that all dp_sync times are within a sensible range 
;  of the roadmap times
;
maxdgi = max(dgi*0.125*1000.0)
bad = where(abs(temp.time+ddr - $
            dp_sync(dp_idx(*,0)).time+dd(dp_idx(*,0))) gt maxdgi)
if bad(0) ge 0 then dp_idx(bad,*) = -1
;
;  and if limits are reversed cos of bad times spacing
;
bad = where(dp_idx(*,0) gt dp_idx(*,1))
if bad(0) ge 0 then dp_idx(bad,*) = -1

return
end
