;+
; NAME:
;	GAIN_PLOT
; PURPOSE:
;	Plots the detector gain and energy resolution for the two 
;	BCS flight detectors from the data contained in gain log.
; CALLING SEQUENCE:  
;	.run gain_plot
; INPUT:
;	Reads from table in $DIR_BCS_LOGS:GAIN_LOG.DAT
; OUTPUT:
;	plots can be directed to PS if desired
; OPTIONAL OUTPUT:
;
; HISTORY
;	RDB  Nov 1990	with enhancements
;	RDB  16-Feb-92	General plot unit
;	RDB  21-Jun-92	Capitalized $DIR_BCS_LOGS for Unix
;			Used CONCAT_DIR
;-
npnts = 100
centroid = fltarr(4,npnts)
fwhm = fltarr(4,npnts)
days = fltarr(npnts)
eggse = intarr(npnts)

;;;openr,1,'disk$user1:[solar_a.bcs_inst.detector.logs]gain.log
get_lun,dlun
openr,dlun,concat_dir('$DIR_BCS_LOGS','gain_log.dat')
;	skip title stuff
ss=''
for j=0,4 do begin readf,dlun,ss & print,ss & endfor

date=intarr(3)
vars=fltarr(8)
jj=0
ieg=''

while not eof(dlun) do begin
	readf,dlun,format='(i2,2(x,i2),a2,4(2x,2f7.3))',date,ieg,vars
;;	day = fix(days79([date(2),date(1),date(0)])) - fix(days79([90,1,1]))
	ex2int,[0,0,0,0,1,1,90],msody,days_90
	ex2int,[0,0,0,0,date],msody,days_curr
	day = fix(days_curr - days_90)
	print,format='(3i3,i5,8f8.3,a2)',date,day,vars,ieg
	qq = vars([0,2,4,6]) & centroid(4*jj) = qq
	qq1 = vars([1,3,5,7]) & fwhm(4*jj) = qq1
	days(jj) = day
	if strupcase(strmid(ieg,1,1)) eq 'E'  then eggse(jj)=1
	jj = jj+1
	endwhile

close,dlun
free_lun,dlun

centroid = centroid(*,0:jj-1)
fwhm = fwhm(*,0:jj-1)
days = days(0:jj-1)		; - fix(days79([90,1,1]))
eggse = eggse(0:jj-1)
qq = where(eggse gt 0)

;	define which point used as zero for gain plot
q0 = min(where(eggse eq 0))	;use first point when on s/c
q0 = min(where(days eq 632))	;use first point after launch
navg = 4

;	define if all points joined
ijoin = 1

ans=''
read,'Plot',ans

tek
set_xy
!xtitle = 'Days Since 1/1/90' + $
  string("   (Last point: ",date,fix(days(jj-1)),")",'(a,i2,2(1h/,i2),i5,a)')
!fancy=1
!x.style=1 & !y.style=1
!x.range = [300.,(fix(days(jj-1))+60)/50*50]

!grid=0
!mtitle = 'BCS-A Gain History  (F2)'
!y.range = [13,23]	;!ymin=13 & !ymax=23
!ytitle = 'Resolution (percent)'


!p.psym=-1
cfwhm = fwhm(0,*) & vv = where(cfwhm gt 0) & !c=0
plot,days(vv),fwhm(vv*4+0)/centroid(vv*4+0)*100.
!p.psym=-2
cfwhm = fwhm(1,*) & vv = where(cfwhm gt 0) & !c=0
oplot,days(vv),fwhm(vv*4+1)/centroid(vv*4+1)*100.

ans=''
read,ans

!grid=1
!yticks=10
!y.range = [0.9,1.1]	;!ymin=0.9 & !ymax=1.1
!ytitle = 'Gain'

!p.psym = 1*ijoin
cgain = centroid(0,*) & gain0 = total(centroid(0,q0:q0+navg))/n_elements(where(centroid(0,q0:q0+navg) gt 0))
;;cgain = centroid(0,*) & gain0 = centroid(0,q0)
vv = where(cgain gt 0) & !c=0
plot,days(vv),(centroid(vv*4+0) - gain0)/gain0 + 1.
vv1 = where((cgain gt 0) and (eggse eq 0))
!p.psym = -1
oplot,days(vv1),(centroid(vv1*4+0) - gain0)/gain0 + 1.

!p.psym = 2*ijoin
cgain = centroid(1,*) & gain0 = total(centroid(1,q0:q0+navg))/n_elements(where(centroid(1,q0:q0+navg) gt 0))
;;cgain = centroid(1,*) & gain0 = centroid(1,q0)
vv = where(cgain gt 0) & !c=0
oplot,days(vv),(centroid(vv*4+1) - gain0)/gain0 + 1.
vv1 = where((cgain gt 0) and (eggse eq 0))
!p.psym = -2
oplot,days(vv1),(centroid(vv1*4+1) - gain0)/gain0 + 1.
!p.psym = 5 & oplot,days(qq),0.91*eggse(qq)		;flag EGSE data

read,ans

!mtitle = 'BCS-B Gain History  (F4)'

!grid=0
!yticks=0
!y.range = [13,23]	;!ymin=13 & !ymax=23
!ytitle = 'Resolution (percent)'

!p.psym=-1
cfwhm = fwhm(2,*) & vv = where(cfwhm gt 0) & !c=0
plot,days(vv),fwhm(vv*4+2)/centroid(vv*4+2)*100.
!p.psym=-2
cfwhm = fwhm(3,*) & vv = where(cfwhm gt 0) & !c=0
oplot,days(vv),fwhm(vv*4+3)/centroid(vv*4+3)*100.

read,ans

!grid=1
!yticks=10
!y.range = [0.9,1.1]	;!ymin=0.9 & !ymax=1.1
!ytitle = 'Gain'

!p.psym = 1*ijoin
cgain = centroid(2,*) & gain0 = total(centroid(2,q0:q0+navg))/n_elements(where(centroid(2,q0:q0+navg) gt 0))
;;cgain = centroid(2,*) & gain0 = centroid(2,q0)
vv = where(cgain gt 0) & !c=0
plot,days(vv),(centroid(vv*4+2) - gain0)/gain0 + 1.
!p.psym = -1
vv1 = where((cgain gt 0) and (eggse eq 0))
oplot,days(vv1),(centroid(vv1*4+2) - gain0)/gain0 + 1.

!p.psym = 2*ijoin
cgain = centroid(3,*) & gain0 = total(centroid(3,q0:q0+navg))/n_elements(where(centroid(3,q0:q0+navg) gt 0))
;;cgain = centroid(3,*) & gain0 = centroid(3,q0)
vv = where(cgain gt 0) & !c=0
oplot,days(vv),(centroid(vv*4+3) - gain0)/gain0 + 1.
vv1 = where((cgain gt 0) and (eggse eq 0))
!p.psym = -2
oplot,days(vv1),(centroid(vv1*4+3) - gain0)/gain0 + 1.
!p.psym = 5 & oplot,days(qq),0.91*eggse(qq)		;flag EGSE data

!type=0 & !yticks=0
!grid=0 & !p.psym=0
end
