
;+
;
; NAME: 
;       PLOT_OCC_ONDAY
;
; PURPOSE:
;       Early software for analyzing occultations in DISCSP
;
; CATEGORY:
;       BATSE, OCCULTS
;
; CALLING SEQUENCE:
;
;
; CALLS:
;	none
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	THIS IS OBSOLETE AND NO LONGER SUPPORTED.
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	written July 1995
;	Version 2, ras, June 1996 with documentation
;-
pro plot_occ_onday, date, channel=ic, inradec=inradec, nsum=nsum, itran=itran,$
	iyrange = iyrange, out_trans=out_trans,  occout=occout

if n_elements(itran)  then !p.multi =0 else !p.multi = [0,4,8]
linecolors
if !d.name eq 'PS' then charsize=.8 else charsize=1.5
;read in the first 2 hrs and plan the rest of the day

start1 = anytim(/utim, date) + 1.
endt  = start1 + 7200.

read_occ_discsp, start1, endt, ut, discsp,xpos, ypos, zpos

checkvar, inradec,[83.516, 22.0197] ;use epoch 2000 not 1950 [82.89, 21.98]
checkvar, ic, 0

dn_trans, ut, xpos, ypos, zpos, day_ut, night_ut, inradec=inradec, d_period=period,$
	days=days, nights=nights


trans0 = [days, nights]

daynight = fix([1+days*0,nights*0])
daynight = daynight(sort(trans0))
trans0 = trans0(sort(trans0))
wtrans = where( trans0 ge start1 and trans0 le start1+86399.)
trans0 = trans0(wtrans)
daynight = daynight(wtrans)

;now recalculate at the end of the day
start1 = anytim(/utim, date) + 86400. - 7201.
endt  = start1 + 7200.

read_occ_discsp, start1, endt, ut, discsp,xpos, ypos, zpos, zradecs=zradecs, $
	xradecs=xradecs

dn_trans, ut, xpos, ypos, zpos, day_ut, night_ut, inradec=inradec, d_period=period,$
	days=days, nights=nights

trans1 = [days, nights]

trans1 = trans1(sort(trans1))
wtrans = where( trans1 ge anytim(/utim, date) + 1. $
	and trans1 le anytim(/utim, date)+86399. )
trans1 = trans1(wtrans)

;Average the two solutions, weight by time

slide = 1-findgen(n_elements(trans0))/(n_elements(trans0)-1)
trans2 = ( trans0*slide + (1-slide)*trans1 )
out_trans=trans2

start1 = anytim(/utim, date) + 1.

occ_dat={occ_dat, ut_occ:0.d0, daynight:0L, det_id:0L, start_time:0.0d0, end_time:0.0d0, $
	zradec:lonarr(2), xradec:lonarr(2)}	

occout = replicate( occ_dat, 32)
occ_count = 0
for i=0, n_elements(trans2)-1 do begin 
	
  if n_elements(itran) eq 0 or fcheck(itran,-1) eq i then begin
	start = trans2(i)- 2400 > start1
	endt  = trans2(i) + 2400 < (start1 + 86400.-1)
	read_occ_discsp, start, endt, ut, discsp,xpos, ypos, zpos, det_id=det_id,$
		zradecs=zradecs, xradecs=xradecs, utchange=utchange
	
	dn_trans, ut, xpos, ypos, zpos, days, nights, inradec=inradec, $
		d_period=period, bad_data=bad_data, ntrans=ntrans

	if bad_data or ntrans eq 0  then plot, findgen(10), fltarr(10) else begin
	trans = [days, nights]
	daynight = [days*0+1,nights*0]
	daynight = daynight(sort(trans))
	trans = trans(sort(trans))
	current = min( abs(trans -  trans2(i) ), icurrent)
	current = trans( icurrent)
	out_trans(i) = current
	w = where( ut ge current-500. and ut le current +500, nrange )
	setutbase, current
	utp = ut(w) - current
	y = discsp(ic,w)/2.048 ;-smooth(discsp(ic,w),301 < nrange-1)
	tocc = min( abs(current-ut(w)), iocc)
	wrange = ((iocc - 175)>0 + indgen(350)) 
	wrange = wrange( where( wrange le (n_elements(utp)-1) ))
		;Evaluate data quality
	wlow = where( utp(wrange) lt 0., nlow)
	whi  = where( utp(wrange) gt 0., nhi)
	if nlow ge 100. and nhi ge 100. then $
	if n_elements( where(  y(wrange(wlow))  gt 0.) ) ge 100 and $
	   n_elements( where(  y(wrange(whi ))  gt 0.) ) ge 100 then begin

		occ_dat.daynight= daynight(icurrent)
		occ_dat.ut_occ = current
		occ_dat.det_id=det_id
		if utchange eq 0 or ( utchange gt 0 and current le utchange) then begin
			occ_dat.zradec=zradecs(*,0)
			occ_dat.xradec=xradecs(*,0) 
		endif else begin
			occ_dat.zradec=zradecs(*,1)
			occ_dat.xradec=xradecs(*,1) 
		endelse
		occ_dat.start_time = ut(w(wrange(0)))
		occ_dat.end_time   = ut(w(wrange(n_elements(wrange)-1)))
		
		occout(occ_count) = occ_dat
		occ_count = occ_count+1
	endif
	yrange = avg(y(iocc-8+indgen(16))) +fcheck(iyrange,[-50,50])
	if not n_elements(itran)  then begin 
		utplot, utp, y, psym=10, yminor=-1, $
		nsum=fcheck(nsum,1), yrang=yrange, charsi=charsize, $
		timerange=atime(getutbase()+[-180,180])
		oplot, dblarr(2), crange('y'), color=7-2*daynight(icurrent)

	endif else if i eq itran then begin
		utplot, utp, y, psym=10,  yminor=-1, $
		nsum=fcheck(nsum,1), yrange=iyrange, $
		timerange=atime(getutbase()+[-180,180])
		oplot, dblarr(2), crange('y'), color=7-2*daynight(icurrent)

		endif
	endelse
  endif
endfor

if keyword_set(average) then begin
	!p.multi= 0
	nsum = (fcheck(nsum,1)>1)
	nbins= 1200 / nsum * nsum
	wd = 1200 + indgen( nbins )
	wn = 1199 - indgen( nbins )
	wn = wn( wn )
	newbins = nbins /nsum
	x = [ rebin(timerate(wn),newbins), rebin(timerate(wd),newbins)]
	y = [ rebin(occrate(ic,wn)*weight(wn),1, newbins), $
		rebin(occrate(ic,wd)*weight(wd),1, newbins)]
	y = f_div( y(*), $
	[ rebin(weight(wn), newbins), rebin(weight(wd), newbins)]) /2.048
	plot, x, y, $
	yrange=fcheck(iyrange,[-00,00]), psy= 3
endif
occout = occout(0:occ_count-1)

end

	
