; $Id: fcs_reduce.pro,v 1.3 2005/02/08 17:20:27 azuah Exp $
;+
; NAME:
;       FCS_REDUCE
;
; PURPOSE:
;
;       Widget interface for FCS data reduction.
;
; AUTHOR:
;
;       Robert M. Dimeo, Ph.D.
;		NIST Center for Neutron Research
;       100 Bureau Drive
;		Gaithersburg, MD 20899
;       Phone: (301) 975-8135
;       E-mail: robert.dimeo@nist.gov
;       http://www.ncnr.nist.gov/staff/dimeo
;
; CATEGORY:
;
;       Widgets, neutron scattering data reduction
;
; CALLING SEQUENCE:
;
;       FCS_REDUCE, GROUP_LEADER = group_leader
;
;
; INPUT PARAMETERS:
;
;       NONE
;
; INPUT KEYWORDS:
;
;       NONE
;
; REQUIRED PROGRAMS:
;
;		FCSDATA__DEFINE.PRO which defines the object class
;		called "FCSDATA".
;
; COMMON BLOCKS:
;
;       NONE
;
; RESTRICTIONS
;
;       NONE
;###############################################################################
; LICENSE:
;  The software in this file is written by an employee of 
;  National Institute of Standards and Technology 
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used of if the code in this file is
;  included in another product.
;
;###############################################################################
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_cleanup,tlb
widget_control,tlb,get_uvalue = pState
if obj_valid((*pState).oClean) then obj_destroy,(*pState).oClean
if obj_valid((*pState).oTreat) then obj_destroy,(*pState).oTreat
if obj_valid((*pState).oRaw) then obj_destroy,(*pState).oRaw
if (*pState).group_leader eq 0L then ptr_free,(*pState).davePtr
wdelete,(*pState).winPix
ptr_free,pState
help,/heap,/brief
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_plot,event
widget_control,event.top,get_uvalue = pState
if not(obj_valid((*pState).oTreat)) then return
slider = widget_info(event.top,find_by_uname = 'GROUP')
widget_control,slider,get_value = sliderval
elo = widget_info(event.top,find_by_uname = 'ELO')
widget_control,elo,get_value = elo
ehi = widget_info(event.top,find_by_uname = 'EHI')
widget_control,ehi,get_value = ehi
if (*pState).autoscale eq 1 then begin
  (*pState).oTreat->getProperty,erange = erange,datarange = datarange
  ymin = datarange[0] < 0
  dy = 0.1*(datarange[1]-ymin)
  (*pState).yrange = [ymin,datarange[1]+dy]
  xmin = erange[0] > elo
  xmax = erange[1] < ehi
  (*pState).xrange = [xmin,xmax]
  (*pState).oTreat->draw,detector = fix(sliderval[0]),xrange = (*pState).xrange, $
	xstyle = 1,qSelect = (*pState).qSelect
endif else begin
  (*pState).oTreat->draw,detector = fix(sliderval[0]),xrange = (*pState).xrange, $
	yrange = (*pState).yrange,ystyle = 1,xstyle = 1,qSelect = (*pState).qSelect
endelse

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_refresh,event
widget_control,event.top,get_uvalue = pState
wset,(*pState).winPix
fcs_reduce_plot,event
wset,(*pState).winVis
device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pState).winPix]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_load,event
widget_control,event.top,get_uvalue = pState
oRaw = obj_new('fcsdata',dataDirectory = (*pState).dataDirectory)
test = oRaw->loaddata(group_leader = event.top)
if test eq -1 then begin
  obj_destroy,oRaw
  return
endif
if obj_valid((*pState).oClean) then $
	obj_destroy,[(*pState).oClean,(*pState).oTreat,(*pState).oRaw]

(*pState).oRaw = oRaw
fcs_reduce_data,event
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_win_events,event
widget_control,event.top,get_uvalue = pState
if not(obj_valid((*pState).oClean)) then return
case event.type of
0:	begin		; button press
	  (*pState).mouse = event.press
	  if (*pState).mouse eq 4 then begin
	    (*pState).autoscale = 1
        fcs_reduce_refresh,event
	  endif
	  if (*pState).mouse eq 1 then begin
	    (*pState).xbox[0] = event.x
	    (*pState).ybox[0] = event.y
        fcs_reduce_refresh,event
	    empty
	    (*pState).autoscale = 0
	    widget_control,(*pState).win,/draw_motion_events
	  endif
	end
1:	begin	; button release
	 if (*pState).mouse eq 1 then begin
	  xll = (*pState).xbox[0] < (*pState).xbox[1]
	  yll = (*pState).ybox[0] < (*pState).ybox[1]
	  w = abs((*pState).xbox[1] - (*pState).xbox[0])
	  h = abs((*pState).ybox[1] - (*pState).ybox[0])
	  xur = xll + w
	  yur = yll + h
	  ll = convert_coord(xll,yll,/device,/to_data)
	  ur = convert_coord(xur,yur,/device,/to_data)
	  (*pState).xrange = [ll[0],ur[0]]
	  (*pState).yrange = [ll[1],ur[1]]
      fcs_reduce_refresh,event
	  (*pState).mouse = 0B
	  widget_control,(*pState).win,draw_motion_events = 0
	 endif
	 if (*pState).mouse eq 4 then begin
	  fcs_reduce_refresh,event
	  (*pState).mouse = 0B
	  widget_control,(*pState).win,draw_motion_events = 0
	 endif
	end
2:	begin	; mouse motion
	  if (*pState).mouse eq 1 then begin
	  	(*pState).xbox[1] = event.x
	  	(*pState).ybox[1] = event.y
	  	xc = [(*pState).xbox[0],event.x,event.x,$
	  	      (*pState).xbox[0],$
	  	      (*pState).xbox[0]]
	  	yc = [(*pState).ybox[0],(*pState).ybox[0],$
	  	      event.y,event.y,$
	  	      (*pState).ybox[0]]
	  	wset,(*pState).winVis
	  	device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pState).winPix]
	  	plots,xc,yc,/device
	  	empty
	  endif
	end
else:
endcase
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_rebin,event
widget_control,event.top,get_uvalue = pState
if not(obj_valid((*pState).oTreat)) then return
(*pState).oTreat->rebindata,group_leader = event.top
fcs_reduce_refresh,event
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_restore,event
widget_control,event.top,get_uvalue = pState
if not(obj_valid((*pState).oTreat)) then return
Restore, 'clean_object.sav'
if obj_valid((*pState).oTreat) then obj_destroy,(*pState).oTreat
(*pState).oTreat = clean_object
;sliderId = widget_info(event.top,find_by_uname = 'GROUP')
;(*pState).oTreat->getProperty,detectors = detectors
;ny = n_elements(detectors)
;widget_control,sliderId,set_slider_max = ny-1,set_value = 0
fcs_reduce_refresh,event
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_save_dave,event
widget_control,event.top,get_uvalue = pState
if not(obj_valid((*pState).oTreat)) then return
(*pState).oTreat->saveDave,group_leader = event.top,davePtr = (*pState).davePtr
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_data,event
widget_control,event.top,get_uvalue = pState
if not(obj_valid((*pState).oRaw)) then return
if obj_valid((*pState).oClean) then begin
  obj_destroy,[(*pState).oClean,(*pState).oTreat]
endif

raw_object = (*pState).oRaw
save, raw_object, Filename='secret.sav'

restore,'secret.sav'
(*pState).oClean = raw_object

restore,'secret.sav'
(*pState).oTreat = raw_object

low = widget_info(event.top,find_by_uname = 'LO_CHANNEL')
high = widget_info(event.top,find_by_uname = 'HI_CHANNEL')
widget_control,low,get_value = lo
lo_chan = fix(lo[0])
widget_control,high,get_value = hi
hi_chan = fix(hi[0])

sigma = widget_info(event.top,find_by_uname = 'SIGMA')
widget_control,sigma,get_value = sig
sigma = float(sig[0])

if (*pState).elSelect then begin
  el_id = widget_info(event.top,find_by_uname = 'ELCHANNEL')
  widget_control,el_id,get_value = el_chan
  el_chan = fix(el_chan[0])
  dummy = (*pState).oClean->timetoenergy(elastic_channel = el_chan, $
  			lo_chan = lo_chan,hi_chan = hi_chan,sigma = sigma)
  dummy = (*pState).oTreat->timetoenergy(elastic_channel = el_chan, $
  			lo_chan = lo_chan,hi_chan = hi_chan,sigma = sigma)
endif else begin
  dummy = (*pState).oClean->timetoenergy(lo_chan = lo_chan,hi_chan = hi_chan, $
  		sigma = sigma)
  dummy = (*pState).oTreat->timetoenergy(lo_chan = lo_chan,hi_chan = hi_chan, $
  		sigma = sigma)
endelse

;if (*pState).QSelect then begin
;  el_id = widget_info(event.top,find_by_uname = 'QREBCHANNELS')
;  widget_control,el_id,get_value = qbins
;  nqbins = fix(qbins[0])
;  (*pState).oTreat->phi_to_q,nqbins = nqbins
;  (*pState).oClean->phi_to_q,nqbins = nqbins
;  sliderId = widget_info(event.top,find_by_uname = 'GROUP')
;  widget_control,sliderId,set_slider_max = nqbins-1,set_value = 0
;endif else begin
;  sliderId = widget_info(event.top,find_by_uname = 'GROUP')
;  (*pState).oTreat->getProperty,detectors = detectors
;  ny = n_elements(detectors)
;  widget_control,sliderId,set_slider_max = ny-1,set_value = 0
;endelse

clean_object = (*pState).oClean
save,clean_object,filename = 'clean_object.sav'

fcs_reduce_refresh,event
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_phi_to_q,event
widget_control,event.top,get_uvalue = pState
if not(obj_valid((*pState).oRaw)) then return
if (*pState).QSelect then begin
  el_id = widget_info(event.top,find_by_uname = 'QREBCHANNELS')
  widget_control,el_id,get_value = qbins
  nqbins = fix(qbins[0])
  (*pState).oTreat->phi_to_q,nqbins = nqbins
  (*pState).oClean->phi_to_q,nqbins = nqbins
  sliderId = widget_info(event.top,find_by_uname = 'GROUP')
  widget_control,sliderId,set_slider_max = nqbins-1,set_value = 0
endif else begin
  sliderId = widget_info(event.top,find_by_uname = 'GROUP')
  (*pState).oTreat->getProperty,detectors = detectors
  ny = n_elements(detectors)
  widget_control,sliderId,set_slider_max = ny-1,set_value = 0
  fcs_reduce_data,event
endelse
fcs_reduce_refresh,event
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_event,event
; Event dispatcher
widget_control,event.top,get_uvalue = pState
uname = widget_info(event.id,/uname)
case uname of
'QUIT': $
	begin
		widget_control,event.top,/destroy
	end
'LOADTOF': $
	begin
		fcs_reduce_load,event
	end
'GROUP': $
	begin
		fcs_reduce_refresh,event
	end
'ELO': $
	begin
		fcs_reduce_refresh,event
	end
'EHI': $
	begin
		fcs_reduce_refresh,event
	end
'REBINDATA': $
	begin
		fcs_reduce_rebin,event
	end
'RESTORE': $
	begin
		fcs_reduce_restore,event
	end
'ELCHANNEL': $
	begin
		fcs_reduce_data,event
	end
'LO_CHANNEL': $
	begin
		fcs_reduce_data,event
	end
'HI_CHANNEL': $
	begin
		fcs_reduce_data,event
	end
'ELCHBOX': $
	begin
		(*pState).elSelect = event.select
		fcs_reduce_data,event
	end
'QREBCHBOX': $
	begin
		(*pState).QSelect = event.select
		fcs_reduce_phi_to_q,event
	end
'QREBCHANNELS': $
	begin
		fcs_reduce_phi_to_q,event
	end
'SIGMA': $
	begin
		fcs_reduce_data,event
	end
'SAVE_DAVE': $
	begin
		fcs_reduce_save_dave,event
	end
else:
endcase
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce,group_leader = group_leader,dataDirectory = dataDirectory
loadct,0,/silent
; Widget definition module
if n_elements(group_leader) eq 0 then group_leader = 0L
if n_elements(dataDirectory) eq 0 then dataDirectory = ''
tlb = widget_base(/row,title = 'FCS Data Reduction', $
	group_leader = group_leader, mbar = bar)

filemenu = widget_button(bar,/menu,value = 'FILE')
void = widget_button(filemenu,value = 'Load TOF ASCII file',uname = 'LOADTOF')
void = widget_button(filemenu,value = 'Quit',uname = 'QUIT')
colBase = widget_base(tlb,/col)

void = cw_field(colBase,/row,title = 'ELO (meV)',value = '-20.0',$
	/return_events,uname = 'ELO')
void = cw_field(colBase,/row,title = 'EHI (meV)',value = '5.0', $
	/return_events,uname = 'EHI')
void = widget_slider(colBase,title = 'Group',min = 0,max = 62,value = 0, $
	uname = 'GROUP')

rowBase = widget_base(colBase,/row)
void = widget_button(rowBase,value = 'Rebin',uname = 'REBINDATA')
void = widget_button(rowBase,value = 'Restore',uname = 'RESTORE')
rbase1 = widget_base(colBase,/row)
exBase = widget_base(rbase1,/row,/nonexclusive)
void = widget_button(exBase,value = 'Elastic channel',uname = 'ELCHBOX')
void = cw_field(rbase1,/row,title = '',value = 405,uname = 'ELCHANNEL', $
	/return_events,xsize = 4)

rbase1a = widget_base(colBase,/row)
exBase1 = widget_base(rbase1a,/row,/nonexclusive)
void = widget_button(exBase1,value = '# Q-bins',uname = 'QREBCHBOX')
void = cw_field(rbase1a,/row,title = '',value = 25,uname = 'QREBCHANNELS', $
	/return_events,xsize = 4)

void = cw_field(colBase,/row,title = 'Sigma',value = 1.0,uname = 'SIGMA', $
	/return_events,xsize = 4)
bgBase = widget_base(colBase,/col,/frame)
void = widget_label(bgBase,value = 'Background range (channels)')
rbase2 = widget_base(bgBase,/row)
void = cw_field(rBase2,/row,title = 'Low',value = 950,uname = 'LO_CHANNEL', $
	/return_events,xsize = 4)
void = cw_field(rBase2,/row,title = 'High',value = 960,uname = 'HI_CHANNEL', $
	/return_events,xsize = 4)

void = widget_button(colBase,value = 'Save as DAVE',uname = 'SAVE_DAVE')
xsize = 400 & ysize = 400
win = widget_draw(tlb,xsize = xsize,ysize = ysize,/button_events, $
	event_pro = 'fcs_reduce_win_events')

widget_control,tlb,/realize
widget_control,win,get_value = winVis
window,/free,/pixmap,xsize = xsize,ysize = ysize
winPix = !d.window
if group_leader ne 0L then begin
  widget_control,group_leader,get_uvalue = statePtr
  davePtr = (*statePtr).davePtr
endif else begin
  davePtr = ptr_new(/allocate_heap)
endelse
state = {	winVis:winVis,			$
			winPix:winPix,			$
			win:win,				$
			group_leader:group_leader, $
			davePtr:davePtr,		$
			dataDirectory:dataDirectory,	$
			mouse:0B,				$
			autoscale:1,			$
			xrange:[0.,1.],			$
			yrange:[0.,1.],			$
			xbox:[0.,1.],			$
			ybox:[0.,1.],			$
			elSelect:0,				$
			QSelect:0,				$
			oRaw:obj_new(),			$	; raw TOF data
			oClean:obj_new(),		$	; clean TOF data (after T2E)
			oTreat:obj_new()		}	; processed TOF data (after T2E)
pState = ptr_new(state,/no_copy)
widget_control,tlb,set_uvalue = pState
wname = 'fcs'
xmanager,wname,tlb,/no_block,cleanup = 'fcs_reduce_cleanup', $
	event_handler = 'fcs_reduce_event'

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
