;+
; PROJECT
;	HESSI
; NAME:
;	HSI_ANY2SCTIME
;
; PURPOSE:
;	This function returns SSW time formats in the format of the spacecraft clock.
;
; CATEGORY:
;	UTIL
;
; CALLING SEQUENCE:
;	sc_format_time = hsi_any2sctime( intime )
;
; CALLS:
;	none
;
; INPUTS:
;       INTIME - time readable by anytim().  Most time formats under SSW.
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       The function returns a stucture with two tags, seconds (true seconds)
;	                                             , bmicro16 (16 binary microseconds)
;	A binary microsecond is a single clock pulse on our 2^20 HZ counter.
;	1 binary microsecond is exactly 1/1.04876 seconds.
;
; OPTIONAL OUTPUTS:
;	none
;
; OPTIONAL KEYWORD INPUTS:
;	FULL - use HESSI_SCTIME_FULL structure with binary microsecond resolution, DEFAULT
;	L64  - use long64 integer format for full clock resolution.
;	SHORT- use HESSI_SCTIME structure with 16 binary microsecond resolution.
;
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	This function will eventually incorporate spacecraft clock drift, currently assumes that
;	there is no drift between the spacecraft MHZ (2^20) oscillator.
;
; PROCEDURE:
;
;
; MODIFICATION HISTORY:
;	Version 1, richard.schwartz@gsfc.nasa.gov, 4-sep-1998.
;	Version 2, richard.schwartz@gsfc.nasa.gov, 14-sep-1999. Fixed sctime structure creation bug.
;		HESSI RELEASE LEVEL 0.
;       Version 3, jimm@ssl.berkeley.edu, 2-jun-2001, added
;       /no_clock_drift option, replaced no_clock_drift with _extra
;	20-sep-2001, ras, added Long64 (L64) output format
;	25-sep-2001, ras, time conversion is now 1-1. The nominal rate for the
;	spacecraft clock is 2^20 ticks per second.  Now that is corrected to
;	make conversion between UTC and Spacecraft time smooth between the
;	fiducials (points with known UTC and Spacecraft time);
;	12-oct-2001, ras, ensure that single element intime returns as scalar in L64
;	10-apr-2002, ras, eliminate leap second check and correct for
;	out of range times with no fiducial interpolation
;-
function hsi_any2sctime, intime,  _extra=_extra



;Here we synchronize the value of the HESSI spacecraft clock counter to the closest reference time.
;There are two values here.  One is the 48 bit value of the clock
;counter, and the other is a UT reference time.
;Added no_clock_drift, 2-jun-2001, jmm


fiducials = hsi_get_clock_synch(intime,  _extra=_extra)

mjd_lim   = minmax( fiducials.ut_ref )
;diff = hsi_sctime_diff( sctime_in, fiducials.counter )

fcounter= hsi_sctime_convert( fiducials.counter,/l64)
ndiff_intervals = n_elements(fcounter) - 1
intime_sec = anytim( intime, /sec )


if ndiff_intervals ge 1 then begin
scdiff= fcounter[1:*] - fcounter
utdiff= fiducials.ut_ref[1:*]- fiducials.ut_ref
sclocktick_per_sec = scdiff / utdiff ;nominally 1048576.
;How many intervals?



sctime = n_elements( intime_sec ) eq 1? 0LL : lon64arr( n_elements(intime_sec) )
for i=0, ndiff_intervals-1 do begin
	sel = where( intime_sec ge fiducials.ut_ref[i] and intime_sec lt fiducials.ut_ref[i+1], nsel)
	if nsel ge 1 then sctime[sel] = $
	long64( (intime_sec[sel]-fiducials.ut_ref[i]) * sclocktick_per_sec[i] +0.5) + $
	fcounter[i]
	endfor
;
; This should not be reached for data with real clock drifts, so the nominal values
; will suffice, ras, 9-apr-02, to be replaced soon
;
mmfid = minmax( fiducials.ut_ref)
sel = where( intime_sec lt mmfid[0] or intime_sec gt mmfid[1], nsel)
if nsel ge 1 then sctime[sel] = $
	long64( (intime_sec[sel]-fiducials.ut_ref[0]) * sclocktick_per_sec[0] +0.5) + $
	fcounter[0]

endif else sctime = long64( (intime_sec - fiducials.ut_ref[0]) * 2d^20 + 0.5) + fcounter[0]

return,   hsi_sctime_convert( sctime, _extra=_extra, default='FULL' )

;Not needed below, there are no leap seconds during the fiducials
;
get_leap_sec, leap_mjd

leap_mjd = leap_mjd + 1 ;seconds added at end of the day!
temp_mjd = replicate( anytim(/mjd,0.0d0), n_elements(leap_mjd))
temp_mjd.mjd = leap_mjd
leap_mjd = anytim( temp_mjd )
wk = where( leap_mjd gt (mjd_lim[0] < min(fiducials.ut_ref)) $
 and leap_mjd le (mjd_lim[1]  > max(fiducials.ut_ref)) , nkeep)



;if nkeep ge 1 then stop

delta_time = sctime * 0.0
if nkeep ge 1 then begin
	leap_mjd = leap_mjd[wk]
	for i=0,nkeep-1 do begin
		wleap = where( intime_sec ge leap_mjd[i], nleap)
		if nleap gt 0 then delta_time[wleap] = delta_time[wleap] + 1.0
		wleap = where( intime_sec lt leap_mjd[i], nleap)
		if nleap gt 0 then delta_time[wleap] = delta_time[wleap] - 1.0
		endfor
	endif

sctime = sctime + long64( delta_time * 2LL^20 +0.5)



return,   hsi_sctime_convert( sctime, _extra=_extra, default='FULL' )
end

