pro qView_ModifyData, state

@compile_opt.pro		; On error, return to caller

;+
; NAME:
;	qView_ModifyData
; PURPOSE:
; CATEGORY:
; CALLING SEQUENCE:
; INPUTS:
; OPTIONAL INPUT PARAMETERS:
; OUTPUTS:
; OPTIONAL OUTPUT PARAMETERS:
; INCLUDE:
; EXTERNAL:
; CALLS:
; SEE ALSO:
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
; EXAMPLE:
; PROCEDURE:
; MODIFICATION HISTORY:
;	OCT-2002, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;-

type = widget_info(state.base_type, /droplist_select)
widget_control, state.base_type, get_uvalue=ctype
message, /info, ctype[type]

; User value of state.base_ref is initialized to 0, so we test for # elements <=1.

widget_control, state.base_ref, get_uvalue=raw_data, /no_copy
if n_elements(raw_data) le 1 then widget_control, state.getdata, get_uvalue=raw_data, /no_copy

widget_control, state.box[0,0], get_uvalue=box
nx = box[1,1]-box[0,1]+1
ny = box[3,1]-box[2,1]+1
nimg = n_elements(raw_data)/(nx*ny)

widget_control, /hourglass

case type of
0:														; Unprocessed images
1: begin												; Subtract same value from all images
	widget_control, state.zactive[2,1], get_value=zbase
	data = raw_data-zbase
end
2: begin												; Subtract average of same area for all images
	widget_control, state.box[0,0], get_uvalue=box

	xb0 = box[0,3]-box[0,1]  &  xe0 = box[1,3]-box[0,1]	; Box used to define base
	yb0 = box[2,3]-box[2,1]  &  ye0 = box[3,3]-box[2,1]

	data = raw_data[xb0:xe0,yb0:ye0,*]
	data = total(data,1)/(xe0-xb0+1)
	data = total(data,1)/(ye0-yb0+1)

	data = reform(SuperArray(data,/lead,nx*ny),nx,ny,nimg)
	data = raw_data-data
end
3: begin												; Polyfit columns
	widget_control, state.order, get_value=nd

	iy = lindgen(ny)
	f  = fltarr(nx,nd+1)
	data = raw_data
	for n=0,nimg-1 do begin								; Loop over images
		for i=0,nx-1 do begin							; Loop over columns
			f[i,*] = poly_fit(iy,raw_data[i,*,n],nd,yfit1)
			data[i,*,n] = raw_data[i,*,n]-yfit1			; Subtract fit
		endfor
	endfor
end
4: begin												; Polyfit rows
	widget_control, state.order, get_value=nd

	ix = lindgen(nx)
	f  = fltarr(ny,nd+1)
	data = raw_data
	for n=0,nimg-1 do begin								; Loop over images
		for i=0,ny-1 do begin							; Loop over rows
			f[i,*] = poly_fit(ix,raw_data[*,i,n],nd,xfit1)
			data[*,i,n] = raw_data[*,i,n]-xfit1			; Subtract fit
		endfor
	endfor
end
5: begin												; Polyfit image
	widget_control, state.order, get_value=nd

	data = raw_data
	for n=0,nimg-1 do data[*,*,n] = raw_data[*,*,n]-sfit(raw_data[*,*,n], nd)
end
6: begin												; Running difference (shift is cyclic)
	data = raw_data-shift(raw_data,0,0,1)
end
7: begin												; Difference with reference image
	widget_control, state.base_ref, get_value=refimg
	img = qView_ImageInfo(state,file=refimg-1)
	widget_control, state.base_ref, set_value=refimg	; Update refimg field in widget

	data = raw_data-SuperArray(raw_data[*,*,img],nfiles,/trail)
end
endcase

if type eq 0 then begin
	zmin = min(raw_data,max=zmax)
	widget_control, state.getdata, set_uvalue=raw_data, /no_copy
endif else begin
	widget_control, state.base_ref, set_uvalue=raw_data, /no_copy
	zmin = min(data,max=zmax)
	widget_control, state.getdata, set_uvalue=data, /no_copy
endelse

widget_control, state.box[2,0], set_uvalue=[zmin,zmax]

qView_XYZ_Update, state

return  &  end
