pro ads_into_pnt, infil, pntdir=pntdir, run_time=run_time
;
;+
;NAME:
;	ads_into_pnt
;PURPOSE:
;	Insert the ADS results into the PNT file
;INPUT:
;	infil	- the ADS filename to be inserted into PNT files
;OPTIONAL KEYWORD INPUT:
;	pntdir	- the directory that has the PNT files.
;		  if not defined, it uses $DIR_GEN_PNT
;HISTORY:
;	Written 18-Jul-92 by M.Morrison
;	27-Aug-92 (MDM) - Allowed an array of input file names
;	30-Mar-93 (MDM) - Corrected bug which was resulting in the last
;			  orbit of data in the ADS file not being written
;			  out to the PNT file
;	 1-Apr-93 (MDM) - Removed error - was clearing ADS bit, but that
;			  is not good when two ADS files update different
;			  parts of the same orbit.
;-
;
start_time = systime(1)
if (n_elements(pntdir) eq 0) then pntdir = '$DIR_GEN_PNT'
;
for ifil=0,n_elements(infil)-1 do begin
    rd_ads, infil(ifil), header, data		;TODO - change to return .day,.time in "data" ??
    n = n_elements(data)
    ;
    daytim = anytim2ints(header)
    daytim = replicate(daytim, n)
    daytim = anytim2ints(daytim, off=data.time/1000.)
    tim2orbit, [daytim(0),daytim(n-1)], wid=wid
    wid = wid( uniq(wid) )
    ;
    nmissed = 0
    ;
    iin = 0L
    last_iorbit = -99
    for iwid=0,n_elements(wid)-1 do begin		;should mostly be within one week - only one wid
	pntfil = concat_dir(pntdir, 'pnt' + wid(iwid) + 'a.01')
	rd_fheader, pntfil, fheader
	rd_neworb_p, pntfil, neworb_p
	last_daytim = anytim2ints([0,0])	;get structure defined
	last_daytim.time = fheader.last_time
	last_daytim.day  = fheader.last_day

	qnew_file = 0
	while (not qnew_file) do begin
	    del = int2secarr( daytim(iin), last_daytim)	;should be negative
	    qnew_file = (del gt 0)
	    if (not qnew_file) then begin
		xx = int2secarr( neworb_p, daytim(iin) )
		ss = where(xx le 0, count)
		if (count eq 0) then iorbit = -1 else iorbit = ss(count -1)	;better have data at or before first time in PNT file
		if (iorbit ne last_iorbit) then begin
		    if (last_iorbit ne -99) then begin
			print, 'Writing one orbit of data into ', filnam, ' data starts at ', fmt_tim(pnt(0))
			openu, lun, filnam, /get_lun
			rdwrt, 'W', lun, ibyt, 0, pnt
			free_lun, lun
		    end

		    rd_pnt, daytim(iin), daytim(iin), pnt, ibyt=ibyt, filnam=filnam
		    ;pnt.ads = 0			;clear any previous values set - MDM added 30-Mar-93
		    last_iorbit = iorbit
		end

		ii = tim2dset(pnt, daytim(iin), delta=delta)
		if (abs(delta(0)) le 0.25) then begin
		    pnt(ii).sc_pntg = data(iin).position
		    pnt(ii).ads     = 1
		end else begin
		    ;print, 'Cannot match ', fmt_tim(daytim(iin)), ' delta time = ', delta(0), '  NMISS = ', nmissed, ' of ', iin
		    nmissed = nmissed + 1
		end
	
		iin = iin + 1
		if (iin eq n) then qnew_file = 1
	    end
	end
    end
end
;
if (last_iorbit ne -99) then begin
    print, 'Writing one orbit of data into ', filnam, ' data starts at ', fmt_tim(pnt(0))
    openu, lun, filnam, /get_lun
    rdwrt, 'W', lun, ibyt, 0, pnt
    free_lun, lun
end
;
print, 'Total #Missed = ', nmissed, ' of ', n
end_time = systime(1)
run_time = (end_time-start_time)/60.
end
