;+
;
; NAME: 
;       PLOT_RESPONSE
;
; PURPOSE:
;       This code is under development for looking at the results of the
;       occultation analysis vs angle and energy for the CRAB DISCSP data
;
; 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:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;
;-
if not exist(crab) then begin
	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))
endif
cosines=findgen(7)*(-.1) + .97
thetas = acos(cosines)*!radeg

e=findgen(195)+5.
apar=[ .0074*(30./50.)^2.12,2.12,1000.,2.12,1,2.12]
model='f_bpow
for i=0,6 do begin
	for j=0,194 do begin
	discsp_resp, [e(j),1000.], thetas(i), apar, resp, model=model
	rsp(j,i)=resp
	endfor
	endfor
end

ed=det_edgesn(*,0,*)
ed=reform(ed)

cmap=fltarr(60,26)
ebins=exp(findgen(61)*.05)*5
cosbins=findgen(27)*.025+.37
cmap=fltarr(2,60,26)
thet = rebin(reform(theta,1,n_elements(theta)),2,n_elements(theta))

cth=cos(thet/!radeg)
for i=0,25 do begin
	for j=0,59 do begin
	wcm = where( cth ge cosbins(i) and cth lt cosbins(i+1) and $
	ed ge ebins(j) and ed lt ebins(j+1), ncm)
	if ncm ge 4 then begin
	resistant_mean, c(wcm), 2, mean, sigma, num_reg, wused=wused_cm
	 cmap(0,j,i) = mean & cmap(1,j,i) = sigma
	endif
	endfor
	endfor
cm=reform(cmap,2,60*26)
 
window,0
cosines=findgen(7)*(-.1) + .97
thetas = acos(cosines)*!radeg
for i=0,6 do begin
	for j=0,194 do begin
	discsp_resp, [e(j),1000.], thetas(i), apar, resp, model=model
	rsp(j,i)=resp
	endfor
	endfor
ed=det_edgesn(*,0,*)
ed=reform(ed)
cmap=fltarr(60,26)
ebins=exp(findgen(61)*.05)*5
cosbins=findgen(27)*.025+.37
cmap=fltarr(2,60,26)
thet = rebin(reform(theta,1,n_elements(theta)),2,n_elements(theta))
cth=cos(thet/!radeg)
for i=0,25 do begin
	for j=0,59 do begin
	wcm = where( cth ge cosbins(i) and cth lt cosbins(i+1) and $
	ed ge ebins(j) and ed lt ebins(j+1), ncm)
	if ncm ge 4 then begin
	resistant_mean, c(wcm), 2, mean, sigma, num_reg, wused=wused_cm
 	cmap(0,j,i) = mean & cmap(1,j,i) = sigma
	endif
	endfor
	endfor
!p.multi=0
surface, reform(cmap(0,*,*)), avg(ebins(1:*)+ebins(0:*),0),avg(cosbins(0:*)+cosbins(1:*),0)
surface, reform(cmap(0,*,*)), (ebins(1:*)+ebins(0:*))/2,(cosbins(0:*)+cosbins(1:*))/2
cm=reform(cmap,2,60*26)
j=15
i=19
wcm = where( cth ge cosbins(i) and cth lt cosbins(i+1) and $
ed ge ebins(j) and ed lt ebins(j+1), ncm)
resistant_mean, c(wcm), 2, mean, sigma, num_reg, wused=wused_cm
sions: THEDATA (ORIENTATION).
surface, reform(cmap(0,*,*)), avg(ebins(1:*)+ebins(0:*),0),avg(cosbins(0:*)+cosbins(1:*),0)
surface, reform(cmap(0,*,*)), (ebins(1:*)+ebins(0:*))/2,(cosbins(0:*)+cosbins(1:*))/2
cm=reform(cmap,2,60*26)
j=15
i=19
wcm = where( cth ge cosbins(i) and cth lt cosbins(i+1) and $
ed ge ebins(j) and ed lt ebins(j+1), ncm)
resistant_mean, c(wcm), 2, mean, sigma, num_reg, wused=wused_cm
 
window,0
more,readme
cleanplot
dir,'*.gif
cosines=findgen(7)*(-.1) + .97
thetas = acos(cosines)*!radeg
for i=0,6 do begin
for j=0,194 do begin
discsp_resp, [e(j),1000.], thetas(i), apar, resp, model=model
rsp(j,i)=resp
endfor
endfor
end
surface, rsp, e, thetas
?
window,2
!p.multi=[0,1,2]
ed=det_edgesn(*,0,*)
ed=reform(ed)
yty
/ytype
/ytype,yran=[1,1e4]
cmap=fltarr(60,26)
ebins=exp(findgen(61)*.05)*5
cosbins=findgen(27)*.025+.37
cmap=fltarr(2,60,26)
for i=0,25 do begin
for j=0,59 do begin
endfor
endfor
end
thet = rebin(reform(theta,1,n_elements(theta)),2,n_elements(theta))
for i=0,25 do begin
for j=0,59 do begin
end
end
end
cth=cos(thet/!radeg)
for i=0,25 do begin
for j=0,59 do begin
wcm = where( cth ge cosbins(i) and cth lt cosbins(i+1) and $
ed ge ebins(j) and ed lt ebins(j+1), ncm)
if ncm ge 4 then begin
	resistant_mean, c(wcm), 2, mean, sigma, num_reg, wused=wused_cm
 cmap(0,j,i) = mean & cmap(1,j,i) = sigma
endif
endfor
endfor
end
!p.multi=0
surface, reform(cmap(0,*,*)), avg(ebins(1:*)+ebins(0:*),0),avg(cosbins(0:*)+cosbins(1:*),0)
surface, reform(cmap(0,*,*)), (ebins(1:*)+ebins(0:*))/2,(cosbins(0:*)+cosbins(1:*))/2
cm=reform(cmap,2,60*26)
j=15
i=19
wcm = where( cth ge cosbins(i) and cth lt cosbins(i+1) and $
ed ge ebins(j) and ed lt ebins(j+1), ncm)
resistant_mean, c(wcm), 2, mean, sigma, num_reg, wused=wused_cm
end
