;+
; NAME:
;	qView_ApplyGain
; PURPOSE:
;
; CATEGORY:
;	qView widget
; CALLING SEQUENCE:
	PRO qView_ApplyGain, state, event, old, display=display, add=add
; INPUTS:
;	atate
;	event
;	old
; OPTIONAL INPUT PARAMETERS:
; OUTPUTS:
; OPTIONAL OUTPUT PARAMETERS:
; INCLUDE:
	@compile_opt.pro		; On error, return to caller
; CALLS:
;	TimeSet, qLine, qView_GetInfo, destroyvar
; SEE ALSO:
; COMMON BLOCKS:
	common qView_Gain_Save, boxes, ngains, x , y
;	(used internally only ??)
; SIDE EFFECTS:
; RESTRICTIONS:
; EXAMPLE:
; PROCEDURE:
; MODIFICATION HISTORY:
;	JULY-2001, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;-

InitVar, display, /key
InitVar, add	, /key

IF IsType(old, /undefined) THEN BEGIN
	ngains = 0

ENDIF ELSE IF add AND ngains EQ 0 THEN BEGIN

	ngains = ngains+1

	x = old
	y = [event.zbright, event.zback]

ENDIF ELSE IF add THEN BEGIN

	ngains = ngains+1

	x = [x, old]
	y = [ [y], [event.zbright, event.zback] ]

ENDIF

IF display AND ngains GT 0 THEN BEGIN

	faint = reform(y[6,*])
	gain  = reform(y[2,*])		; Already has background subtracted, and geometrical corrections applied
	gain  = gain/mean(gain)

	tmp = linfit(gain, faint)
	dc = tmp[0]
	ac = tmp[1]

	faint = (faint-dc)/gain-ac

	widget_control, state.wid_data, get_uvalue=data, /no_copy

	sz = size(data, /dim)

	FOR i=0,sz[2]-1 DO data[*,*,i] = (data[*,*,i]-dc)/gain[i]-ac
;	data = (data-dc)/SuperArray(gain, sz[0]*sz[1], /lead)-ac ; Needs LOTS of memory

	widget_control, state.wid_data, set_uvalue=data, /no_copy

	widget_control, state.wid_time, get_uvalue=trange

	IF IsTime(trange) THEN BEGIN
		widget_control, state.wid_pick, get_uvalue=allfiles, /no_copy
		t = allfiles[x,2]
		widget_control, state.wid_pick, set_uvalue=allfiles, /no_copy
		t = TimeSet(t)
	ENDIF

	ytitle = [	'gain, G (c='+strcompress(correlate(gain,faint),/rem)+')',	$
				'(background-'+strcompress(dc,/rem)+')/G-'+strcompress(ac,/rem)]


	tmp = qView_GetInfo(state, dim=dim, offset=offset)


	str = strarr(16)

	str[ 0] = 'dim = ['+strcompress(dim[0])+','+strcompress(dim[1])+']'
	str[ 1] = 'e = (convert_coord(!x.crange[1],!y.crange[1],/data,/to_normal))[0:1]'
	str[ 2] = 'aspect = float(!d.y_size)/!d.x_size'
	str[ 3] = 'd = 0.1'
	str[ 4] = 'b = e-d*[dim[0],dim[1]/aspect]/dim[0]'
	str[ 5] = 'plots, /normal, [b[0],e[0],e[0],b[0],b[0]],[b[1],b[1],e[1],e[1],b[1]]'

	str[ 6] = 'box = [ ['+strcompress(boxes[0])+','+strcompress(boxes[1])+'] ,'+	$
				      '['+strcompress(boxes[2])+','+strcompress(boxes[3])+'] ]'
	str[ 7] = 'e = b+d*[box[2],box[3]/aspect]/dim[0]'
	str[ 8] = 'c = b+d*[box[0],box[1]/aspect]/dim[0]'
	str[ 9] = 'plots, /normal, [c[0],e[0],e[0],c[0],c[0]],[c[1],c[1],e[1],e[1],c[1]]'
	str[10] = 'polyfill, /normal, [c[0],e[0],e[0],c[0],c[0]],[c[1],c[1],e[1],e[1],c[1]], color=200'

	str[11] = 'box = [ ['+strcompress(boxes[4])+','+strcompress(boxes[5])+'] ,'+	$
				      '['+strcompress(boxes[6])+','+strcompress(boxes[7])+'] ]'
	str[12] = 'e = b+d*[box[2],box[3]/aspect]/dim[0]'
	str[13] = 'c = b+d*[box[0],box[1]/aspect]/dim[0]'
	str[14] = 'plots, /normal, [c[0],e[0],e[0],c[0],c[0]],[c[1],c[1],e[1],e[1],c[1]]'
	str[15] = 'polyfill, /normal, [c[0],e[0],e[0],c[0],c[0]],[c[1],c[1],e[1],e[1],c[1]]'


	y = transpose([[gain],[faint]])

	qLine, group=event.handler, x, y, time=t,	$
		xmargin 	= [9,10],	$
		ytitle		= ['image number', ytitle],	$
		newyaxis	= [1,1],	$
		linestyle	= [0,1],	$
		ynozero		= [1,1],	$
		plotstr		= str

	destroyvar, boxes, ngains, x, y
ENDIF

RETURN  &  END
