;+
; PROJECT:
;	HESSI
; NAME:
;	HSI_TIME_JUMP
;
; PURPOSE:
;	This function returns the HESSI event times corrected for 10 bit counter rollover.
;
; CATEGORY:
;	Util
;
; CALLING SEQUENCE:
;	corrected_times = hsi_time_jump( intimes [, min_diff] )
;
; CALLS:
;	none
;
; INPUTS:
;       Intimes - 10 bit time counter values from detector events in telemetry packets.
;
; OPTIONAL INPUTS:
;	Min_diff - disregard differences less than or equal to this value.  
;	This can be used to accommodate the race
;	condition mentioned by Dave Curtis in the HSI_SYS_007C.DOC icd on telemetry, section 3.3.2.  
;	If this value is other
;	than 0, and simulated telemetry is being used, it must be created with the same value.  
;	At Level 0 we haven't reconciled
;	and made this consistent.
;
; OUTPUTS:
;       The function returns the corrected counter values.
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	Take differences, find negative values, add back 1024 binary microseconds per change.
;
; MODIFICATION HISTORY:
;	Version 1, richard.schwartz@gsfc.nasa.gov, 25-sep-1998, Level 0.
;
;-


function  hsi_time_jump, intimes,   min_diff=min_diff

;
rollover  = 1024
checkvar, min_diff, 0
;LOOK FOR Negative jumps in the INTIMES vector
;
reset_times = intimes
;
;
if n_elements(intimes) le 1 then goto,ALLDONE
;

diff  = intimes - intimes(1:*)
;

w = [where( diff  gt min_diff, ncount),n_elements(diff)]
;
if ncount eq 0 then goto,ALLDONE
;


ntot = (lindgen(ncount)+1)* rollover
;intimes = reset_times        ;don't change input variable.
;

;
;CORRECT FOR THE Overflow

for i=0,ncount-1 do reset_times(w(i)+1)=reset_times(w(i)+1:w(i+1))+ntot(i)
;
ALLDONE:
return, reset_times
end
