;+
;
; NAME: 
;       PLOT_CRAB
;
; PURPOSE:
;       This is a main program used to display occultation results for the Crab
;       to try to examine cuts on the database made in angle and energy.
;
; CATEGORY:
;       BATSE, OCCULTS
;
; CALLING SEQUENCE:
;
;
; CALLS:
;	RESISTANT_MEAN, ZOOM_COOR, LINECOLORS
;
; 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:
;	discsp_crab.sav must be in the current directory.
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	Version 1, Dec 1995.
;-
if not exist(crab) then begin
;
;      Find discsp_crab.sav on the system somewhere!!!
;
	checkvar, indir, ''
                dirs = [indir,'./','[]', get_subdirs('USER_DISK0:[richard.occults]'), $
                  get_subdirs('USER_DISK0:[ajking.occults]'), $
                  get_subdirs('/umbra/schwartz/occults'), $
                  get_subdirs('~richard/occults' ),get_subdirs('/umbra/ajking'),$
                  get_subdirs('~ajking') ]
                checkvar, infile,  'discsp_crab.sav'
                file = loc_file( infile, path=dirs)
                if file ne '' then restore, file,/verb else message,"CAN'T FIND '+INFILE
	wbest=intarr(2,16435)
	for i=0,16434 do for j=0,1 do $
	 wbest(j,i)=(sort(crab(i).counts(1,j,*)))(0)
	c=fltarr(2,n_elements(crab))
	ncrab=n_elements(crab)
	for i=0,ncrab-1 do for j=0,1 do $
		c(j,i)=crab(i).counts(0,j,wbest(j,i))
	;cd,'/umbra/sdac/util/math',current=current
	;resolve_routine,'resistant_mean'
	;cd,current
	window,0
	window,1
endif

wset,0
plot, findgen(41)*2,histogram(theta,bin=2,min=0,max=80)
zoom_coor,tt
wset,1

wthet=where(theta gt tt(0) and theta le tt(1), nthet)
x=[(det_edgesn(0,0,wthet))(*),(det_edgesn(1,0,wthet))(*)]
xx=[(de2n(0,0,wthet))(*),(de2n(1,0,wthet))(*)]
y=[(total(crab_respn(*,wthet),1))(*),(crab_respn(1,wthet))(*)]
yy=[(total(cr2n(*,wthet),1))(*),(cr2n(1,wthet))(*)]
z=[(total(c(*,wthet),1))(*),(c(1,wthet))(*)]
zz=z
q = indgen(nthet)
if fcheck(qb,0) then begin
	x=x(q) & y=y(q) & z=z(q) & xx=xx(q) & yy=yy(q) & zz=zz(q)
endif
 
a=histogram(alog(x),bin=.1,r=r,omax=omax,omin=omin)
w=indgen(r(0))
w=[r(w),r(w+1)-1]
w=transpose(reform(w,r(0),2))
wuse = where(a gt 10, nuse)
aa=histogram(alog(xx),bin=.1,r=rr,omax=omax,omin=omin)
ww=indgen(rr(0))
ww=[rr(ww),rr(ww+1)-1]
ww=transpose(reform(ww,rr(0),2))
wwuse = where(aa gt 10, nnuse)
xu=fltarr(nuse)
yu=xu
zu=xu
xxu=fltarr(nnuse)
yyu=xxu
zzu=xxu

for i=0,nuse-1 do begin 
	wu    = r(w(0,wuse(i)):w(1,wuse(i)))
   	resistant_mean, z(wu), 2.0, mean,wused=wz 
   	zu(i) = mean 
	xu(i) = avg( x(wu(wz))) 
	yu(i) = avg( y(wu(wz))) 
endfor
for i=0,nnuse-1 do begin
	wwu    = rr(ww(0,wwuse(i)):ww(1,wwuse(i)))
   	resistant_mean, zz(wwu), 2.0, mean, wused=wwz
   	zzu(i) = mean 
	xxu(i) = avg( xx(wwu(wwz)))
	yyu(i) = avg( yy(wwu(wwz))) 
endfor
plot_oi,xu, yu, psy=-1
oplot,xu, zu,psy=-4
linecolors
oplot, xxu, yyu, psy=-1, colo=3
oplot, xxu, zzu, psy=-4, colo=3

end
