; $Id: fcs_reduce_manip.pro,v 1.5 2005/02/08 17:20:27 azuah Exp $
;###############################################################################
; 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.
;
;###############################################################################
;
;	Facility to add or subtract .tof (fcs time-of-flight ascii) files
;	can  be used  with fcs .tof OR _raw.dave files
;	
;
;	 
;
;	
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_manip_cleanup,tlb2

widget_control,tlb2,get_uvalue = ppState
;if n_elements((*ppstate).group_leader) eq 0 then begin
   ptr_free,(*ppState).addfilesptr
   obj_destroy,(*ppstate).o
   ptr_free,ppState
;endif else begin
;   ptr_free,(*ppState).addfilesptr
;endelse
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcsSendInfo,ppstate
 obj=(*ppstate).o
 fcsInfo={fcsinfoevent, $
		id:(*ppstate).notifyids[0] ,$
		top:(*ppstate).notifyids[1] ,$
		handler:0L ,$
		obj:obj	}
if widget_info((*ppstate).notifyids[0],/valid) then begin
  widget_control,(*ppstate).notifyids[0],send_event=fcsInfo
endif
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_ADDFILES,event
widget_control,event.top,get_uvalue = ppState
filelist=(*(*ppstate).addfilesptr)

num=n_elements(filelist)
addobj=objarr(n_elements(filelist))

wheredave=where(strmatch(*(*PpSTATE).ADDFILESPTR,'*dave',/fold_case) eq 1,dave)
treatedfiles=filelist

for i=0,n_elements(filelist)-1 do begin
        print,filelist[i]
	;strip file list down to the files:
        ;f2=StrSplit(filelist[i],'/',/Extract)
        ;file=f2[N_elements(f2)-1]
        file=filelist[i]
         addobj[i]=obj_new('fcsdata',$
				dataDirectory = (*ppState).dataDirectory)   ; create the objects
									   ;and populate
        void=where(wheredave eq i,count)
	if (count eq 1) then begin        
		test = addobj[i]->loaddave(group_leader = event.top, filename=file)
	endif else begin
		test = addobj[i]->loaddata(group_leader = event.top, filename=file)
	endelse
	if test eq -1 then begin
 	    obj_destroy,addobj[i]
	    print,'killed the object'
  	    return
	endif
        treatedfiles[i]=file
endfor

;endelse

;test to see what info is involved in these files:
ndets=fltarr(num)
wavelength=fltarr(num)
filename=strarr(num)
temperature = fltarr(num)
nchan = fltarr(num)
run = strarr(num)
rtime = fltarr(num)
user = strarr(num)
comments = strarr(num)
chanwidth = fltarr(num)
chop_pulses = fltarr(num)
chop_freq = fltarr(num)
totalmonitor = fltarr(num)
date = strarr(num)
treatmentarr=ptrarr(num,/allocate_heap)

detectors=fltarr(num,63)
dettype=strarr(num,64)
dist = fltarr(num,107)
time=fltarr(num,1024)
alldata = fltarr(num,1024,64)
error = fltarr(num,1024,64)
rawdata = fltarr(num,1024,64)
rawerror = fltarr(num,1024,64)

for i=0,num-1 do begin
addobj[i]->getproperty,ndets = ndets1, $
			wavelength = wavelength1, $
                        detectors = detectors1, $
                        filename = filename1, $
			rawdata = rawdata1,$
			rawerror = rawerror1,$
                        alldata = alldata1, $
                        error = error1, $
                        temperature = temperature1, $
			dettype = dettype1,$
			time = time1,$
			dist = dist1,$
			mon = mon1,$
			nchan = nchan1,$
			run = run1,$
			rtime = rtime1,$
			user = user1,$
			comments = comments1,$
			chanwidth = chanwidth1,$
			chop_pulses = chop_pulses1,$
			chop_freq = chop_freq1 ,$
			totalmonitor = totalmonitor1,$
			date = date1,$
			treatment=treatment1
;copy into array

			ndets[i]=ndets1
			wavelength[i]=wavelength1
			detectors[i,*]=detectors1
			filename[i]=filename1
			rawdata[i,*,*]=rawdata1
			rawerror[i,*,*]=rawerror1
		 	alldata[i,*,*]=alldata1
			error[i,*,*]=error1
			temperature[i]=temperature1
			dettype[i,*]=dettype1
			time[i,*]=time1
			dist[i,*]=dist1
 			nchan[i]=nchan1
			run[i]=run1
			rtime[i]=rtime1
			user[i]=user1
			comments[i]=comments1
			chanwidth[i]=chanwidth1
			chop_pulses[i]=chop_pulses1
			chop_freq[i] =chop_freq1
			totalmonitor[i] =totalmonitor1
			date[i] = date1
			*treatmentarr[i]=treatment1
endfor

for i=0,n_elements(filelist)-1 do begin
      obj_destroy,addobj[i]
endfor
;obj_destroy,obj_valid()

; Do some consistency checking:

not_ok =where (wavelength ne wavelength[0],count)
if count ne 0 then begin
  void=dialog_message("Wavelengths are Different... Please fix",/error)
  return
endif

not_ok =where (chanwidth ne chanwidth[0],count)
if count ne 0 then begin
  void=dialog_message("Chan. Widths are Different... Please fix",/error)
  return
endif

not_ok =where (chop_freq ne chop_freq[0],count)
if count ne 0 then begin
  void=dialog_message("Chopper Freq. are Different... Please fix",/error)
  return
endif

not_ok =where (nchan ne nchan[0],count)
if count ne 0 then begin
  void=dialog_message("time channels are Different... Please fix",/error)
  return
endif

for i=1,num-1 do begin
   not_time =where (time[0] ne time[i],count)
   if count ne 0 then begin
     void=dialog_message("time channels are Different... Please fix",/error)
     return
   endif

    not_type =where (dettype[0] ne dettype[i],count)
   if count ne 0 then begin
     void=dialog_message("Detectors are Different... Please fix",/error)
     return
   endif

    not_angle =where (detectors[0] ne detectors[i],count)
   if count ne 0 then begin
     void=dialog_message("Angles are Different... Please fix",/error)
     return
   endif
endfor

not_ok =where (dist ne dist[0],count)
if count ne 0 then begin
  void=dialog_message("Detector Distances are Different... Please fix",/error)
  return
endif

not_ok =where (ndets ne ndets[0],count)
if count ne 0 then begin
  void=dialog_message("Number of Detector are Different... Please fix",/error)
  return
endif

totalmonitor1=total(totalmonitor)
chop_pulses1=total(chop_pulses)
rtime1=total(rtime)


diff =where (date ne date[0],count)
if count ne 0 then begin
   date1=date[0]+'/'+date[diff[0]]
endif

diff =where (comments ne comments[0],count)
if count ne 0 then begin
   comments1=strtrim(comments[0],2)+'/'+strtrim(comments[diff[0]],2)
endif

diff =where (user ne user[0],count)
if count ne 0 then begin
   user1=strtrim(user[0],2)+'/'+strtrim(user[diff[0]],2)
endif

diff =where (run ne run[0],count)
if count ne 0 then begin
   run1=run[0]+run[diff[0]]
endif


;need to handle data, errors and temperature.
;take mean temperature
temperature1=(moment(temperature))[0]

;data, ... straight add
alldata1=fltarr(1024,64)
error1=fltarr(1024,64)
for i=0,num-1 do begin
  alldata1=alldata1+rawdata[i,*,*]
  error1=sqrt(error1^2+rawerror[i,*,*]^2)
endfor

rawdata1=alldata1
rawerror1=error1 

mon1=alldata1[*,0]

timestamp='Time: '+ strmid(systime(0),4,3)+'-'$
		+strmid(systime(0),8,2)+'-'+strmid(systime(0),20,4)+' '$
		+strmid(systime(0),11,5)

; how to define the new treatment file?
treatment=[['Files added together '],treatedfiles]
treatment = [[treatment],timestamp]
treatment = [[treatment],'----------']
for i=0,n_elements(treatedfiles)-1 do begin
	treatment = [[treatment],'File History: ' + treatedfiles[i]]
	treatment = [[treatment],*treatmentarr[i]]
	treatment = [[treatment],'----------']
endfor
treatment = [[treatment],'--------------------------']

ptr_free,treatmentarr

darr=intarr(63,63)  ;include detectors array...
darr=darr*0   ;make sure 0's every where.
for i=0,62 do begin
 darr[i,i]=1
endfor

incdet=intarr(63)
incdet=incdet*1

;Now, add the data to these files and populate the 'o' data
o = obj_new('fcsdata',dataDirectory = (*ppState).dataDirectory)
o->setproperty,ndets=ndets1, $
			wavelength=wavelength1, $
			detectors=detectors1, $
			filename=filename[0], $
			rawdata=rawdata1,$
			rawerror=rawerror1,$
			alldata=alldata1, $
			mon=mon1, $
			error=error1, $
			temperature=temperature1, $
			dettype=dettype1, $
			time=time1, $
			dist=dist1, $
 			nchan=nchan1, $
			run=run1, $
			rtime=rtime1, $
			user=user1, $
			comments=comments1, $
			chanwidth=chanwidth1, $
			chop_pulses=chop_pulses1, $
			chop_freq =chop_freq1, $
			totalmonitor =totalmonitor1, $
			date = date1, $
			treatment=treatment,$
			loaded=1,$
			incdet=intarr(63),$
			grpdet=darr

;see if 'o' exists, if so destroy and create a new one:
if obj_valid((*ppState).o) then obj_destroy,(*ppState).o
(*ppState).o = o

print,'ADDED: created object'
widget_control,event.top,set_uvalue = ppState

;if this was called from the main FCS_REDUCTION program, send this along....

if total((*ppstate).notifyids) gt 0 then fcssendinfo,ppstate
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_SUBFILES,event
widget_control,event.top,get_uvalue = ppState
code=WIDGET_INFO((*PpSTATE).FACTOR,/DROPLIST_SELECT)

case code of
	0:$
	 	begin
		fun = 'monitor'
		end
	1:$
		begin
		fun ='time'
		end
	2:$
		begin
		fun ='arb'
		end
else:
endcase


file1=(*ppstate).subfile1
;f2=StrSplit(file1,'/',/Extract)
;file1=f2[N_elements(f2)-1]
wheredave1=where(strmatch(file1,'*dave',/fold_case) eq 1,dave1)

file2=(*ppstate).subfile2
;f2=StrSplit(file2,'/',/Extract)
;file2=f2[N_elements(f2)-1] 
wheredave2=where(strmatch(file2,'*dave',/fold_case) eq 1,dave2)



objfirst=obj_new('fcsdata',$
			dataDirectory = (*ppState).dataDirectory)   ; create the objects
								   ;and populate
if (dave1) then begin
	test = objfirst->loaddave(group_leader = event.top, filename=file1)
endif else begin
	 test = objfirst->loaddata(group_leader = event.top, filename=file1)
endelse
if test eq -1 then begin
 	 obj_destroy,objfirst
  	return
endif


objsub=obj_new('fcsdata',$
			dataDirectory = (*ppState).dataDirectory)   ; create the objects
								   ;and populate
if (dave2) then begin
	test = objsub->loaddave(group_leader = event.top, filename=file2)
endif else begin
	 test = objsub->loaddata(group_leader = event.top, filename=file2)
endelse
if test eq -1 then begin
 	 obj_destroy,objsub
  	return
endif


objfirst->getproperty,ndets = ndets1, $
			wavelength = wavelength1, $
            detectors = detectors1, $
            filename = filename1, $
            rawdata = rawdata1, $
            rawerror = rawerror1, $
            alldata = alldata1, $
            error = error1, $
            temperature = temperature1, $
			dettype = dettype1,$
			time = time1,$
			dist = dist1,$
			;mon = mon1,$
			nchan = nchan1,$
			run = run1,$
			rtime = rtime1,$
			user = user1,$
			comments = comments1,$
			chanwidth = chanwidth1,$
			chop_pulses = chop_pulses1,$
			chop_freq = chop_freq1 ,$
			totalmonitor = totalmonitor1,$
			date = date1,$
			treatment=treatment1

objSUB->getproperty,ndets = ndets2, $
			wavelength = wavelength2, $
            detectors = detectors2, $
            filename = filename2, $
            rawdata = rawdata2, $
            rawerror = rawerror2, $
            alldata = alldata2, $
            error = error2, $
            temperature = temperature2, $
			dettype = dettype2,$
			time = time2,$
			dist = dist2,$
			;mon = mon2,$
			nchan = nchan2,$
			run = run2,$
			rtime = rtime2,$
			user = user2,$
			comments = comments2,$
			chanwidth = chanwidth2,$
			chop_pulses = chop_pulses2,$
			chop_freq = chop_freq2 ,$
			totalmonitor = totalmonitor2,$
			date = date2,$
			treatment=treatment2

;need elastic_peak positions later so:

elastic1=objfirst->findelastic()
elastic2=objsub->findelastic()

obj_destroy,[objsub,objfirst]


IF (wavelength1 ne wavelength2) then begin
  void=dialog_message("Wavelengths are Different... Please fix",/error)
  return
endif

if chanwidth1 ne chanwidth2 then begin
  void=dialog_message("Chan. Widths are Different... Please fix",/error)
  return
endif

if chop_freq1 ne chop_freq2 then begin
  void=dialog_message("Chopper Freq. are Different... Please fix",/error)
  return
endif

if nchan1 ne nchan2 then begin
  void=dialog_message("time channels are Different... Please fix",/error)
  return
endif

   not_time =where (time1 ne time2,count)
   if count ne 0 then begin
     void=dialog_message("time channels are Different... Please fix",/error)
     return
   endif

    not_type =where (dettype1 ne dettype2,count)
   if count ne 0 then begin
     void=dialog_message("Detectors are Different... Please fix",/error)
     return
   endif

    not_angle =where (detectors1 ne detectors2,count)
   if count ne 0 then begin
     void=dialog_message("Angles are Different... Please fix",/error)
     return
   endif

not_ok =where (dist1 ne dist2,count)
if count ne 0 then begin
  void=dialog_message("Detector Distances are Different... Please fix",/error)
  return
endif

not_ok =where (ndets1 ne ndets2,count)
if count ne 0 then begin
  void=dialog_message("Number of Detector are Different... Please fix",/error)
  return
endif

;;;;;;;;;;;;;;;;;; handle the subtraction

diff =where (date1 ne date2,count)
if count ne 0 then begin
   date=date1+'/'+date2
endif

comments=comments1
diff =where (comments1 ne comments2,count)
if count ne 0 then begin
   comments=strtrim(comments1,2)+'/'+strtrim(comments2,2)
endif

user=user1
diff =where (user1 ne user2,count)
if count ne 0 then begin
   user=strtrim(user1,2)+'/'+strtrim(user2,2)
endif

run=run1
diff =where (run1 ne run2,count)
if count ne 0 then begin
   run=run1+run2
endif

;;;;;;;; what to do??? keep them identical to initial file

totalmonitor=totalmonitor1;   +totalmonitor2
chop_pulses=chop_pulses1;   +chop_pulses2
rtime=rtime1;   +rtime2


;need to handle data, errors and temperature.
;take mean temperature
temperature=((temperature1+temperature2)/2.0)

;data, ... multiply second data set by factor

uname = 'ARBFACTOR'
id = widget_info(event.top,find_by_uname = uname)

case fun of
	'monitor':$	
		begin
		   factor=totalmonitor1/totalmonitor2
		   (*ppstate).arb=factor
		   ;update text widget
		   widget_control,id,set_value=factor
		   print,'mon1 =',totalmonitor1
		   print,'mon2 =',totalmonitor2

		end
	'time':$	
		begin
	  	   factor=rtime1/rtime2
		   (*ppstate).arb=factor
		   ;update text widget
		    widget_control,id,set_value=factor
		   print,'time 1 =',rtime1
 		   print,'time 2 =',rtime2	
		end
	'arb':$	
		begin
		     factor=(*ppstate).arb
		     ;update text widget
		     
		end
else:
endcase

print,'factor = ',factor


;shift second data set to line up elastic peak???


sft=elastic1-elastic2
tmp=shift(rawdata2,sft,0)
rawdata2=tmp
rawdata=rawdata1-factor*rawdata2

;errors are squared:
rawerror=sqrt(rawerror1^2+factor^2*rawerror2^2)

mon=rawdata[*,0]


timestamp='Time: '+ strmid(systime(0),4,3)+'-'$
		+strmid(systime(0),8,2)+'-'+strmid(systime(0),20,4)+' '$
		+strmid(systime(0),11,5)

; how to define the new treatment file?
treatment='Files subtracted '+ file1 +' - '+ string(factor)+' *'+file2
treatment = [[treatment],timestamp ]
treatment = [[treatment],'----------']
treatment = [[treatment],'File History: ' + file1]
treatment = [[treatment],treatment1]
treatment = [[treatment],'----------']
treatment = [[treatment],'File History: ' + file2]
treatment = [[treatment],treatment2]
treatment = [[treatment],'--------------------------']


darr=intarr(63,63)  ;include detectors array...
darr=darr*0   ;make sure 0's every where.
for i=0,62 do begin
 darr[i,i]=1
endfor

incdet=intarr(63)
incdet=incdet*1


o = obj_new('fcsdata',dataDirectory = (*ppState).dataDirectory)
o->setproperty,ndets=ndets1, $
			wavelength=wavelength1, $
			detectors=detectors1, $
			filename=filename1, $
			rawdata=rawdata, $
			alldata=rawdata,$
			mon=mon, $
			rawerror=rawerror, $
			error=rawerror, $
			temperature=temperature, $
			dettype=dettype1, $
			time=time1, $
			dist=dist1, $
 			nchan=nchan1, $
			run=run, $
			rtime=rtime, $
			user=user, $
			comments=comments, $
			chanwidth=chanwidth1, $
			chop_pulses=chop_pulses, $
			chop_freq =chop_freq1, $
			totalmonitor =totalmonitor, $
			date = date1, $
			treatment=treatment,$
			loaded=1,$
			incdet=intarr(63),$
			grpdet=darr



;see if 'o' exists, if so destroy and create a new one:

if obj_valid((*ppState).o) then obj_destroy,(*ppState).o
(*ppState).o = o

print, 'SUB: created obj'

widget_control,event.top,set_uvalue = ppState
;if this was called from the main FCS_REDUCTION program, send this along....
if total((*ppstate).notifyids) gt 0 then fcssendinfo,ppstate
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_SAVEFILE,event,add=add, sub=sub
widget_control,event.top,get_uvalue = ppState

;do some work on the filename and
;determine which ..'go' was pressed:

if ( n_elements(add) eq 1) then  begin
  add=1
  txt=widget_info(event.top,find_by_uname='ADDOUTTXT')
  widget_control,txt,get_value=outfile
  if outfile[0] eq '' then return
endif

if ( n_elements(sub) eq 1) then begin
  sub=1
  txt=widget_info(event.top,find_by_uname='SUBOUTTXT')
  widget_control,txt,get_value=outfile
  if outfile[0] eq '' then return
endif

;check to see what 'outfile' is ... 

f2=StrSplit(outfile[0],'/',/Extract)
file2=f2[N_elements(f2)-1] 
wheredave2=where(strmatch(file2,'*.dave',/fold_case) eq 1,dave2)

if dave2 ge 1 then begin
   outfile=StrSplit(file2,'.dave',/Extract,/fold_case,/regex)
   outfile= (*!dave_defaults).workdir+outfile
endif else begin
  outfile=file2
  outfile= (*!dave_defaults).workdir+outfile
endelse


(*ppState).o->saveDave,	group_leader = event.top,	$
						davePtr = (*ppState).davePtr,	$
						n_phi_t = 0, $
						n_phi_e = 0, $
						s_phi_e = 0, $
						s_q_e = 0, $
						g_e = 0,$
						raw=1,$
						file = outfile
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_manip_event,event
widget_control,event.top,get_uvalue = ppState
uname = widget_info(event.id,/uname)
directory=(*ppstate).datadirectory

case uname of
'ADD':	$
	begin
		filelist=DIALOG_PICKFILE(path=(*!dave_defaults).datdir,$
                /READ,/MUST_EXIST,$
       	   	FILTER=['*.tof','*.dave'],/multiple_files)
       	 	if n_elements(filelist) le 1 then return
        	(*(*ppstate).addfilesptr)=filelist
		widget_control,event.top,set_uvalue = ppState
	end
'ADD_GO':	$
	begin
		if n_elements(*(*ppstate).addfilesptr) eq 0 then return
		fcs_addfiles,event
		txt=widget_info(event.top,find_by_uname='ADDOUTTXT')
                widget_control,txt,get_value=addout
		if addout[0] eq '' then return
		fcs_SAVEfile,event,add=1
	end

'FILE1':	$
	begin
		file1=DIALOG_PICKFILE(path=(*!dave_defaults).datdir,$
                /READ,/MUST_EXIST,$
       	   	FILTER=['*.tof','*.dave'],/multiple_files)
       	 	if n_elements(file1) eq 0 then return
        	(*ppstate).SUBfile1=file1
		widget_control,event.top,set_uvalue = ppState
	end
'FILE2':	$
	begin
		file2=DIALOG_PICKFILE(path=(*!dave_defaults).datdir,$
                /READ,/MUST_EXIST,$
       	   	FILTER=['*.tof','*.dave'],/multiple_files)
       	 	if n_elements(file2) eq 0 then return
        	(*ppstate).SUBFILE2=file2
		widget_control,event.top,set_uvalue = ppState
	end
'SUB_GO':	$
	begin
		if (*ppstate).SUBFILE1[0] eq '' then return
		if (*ppstate).SUBFILE2[0] eq '' then return
		fcs_SUBfiles,event
		txt=widget_info(event.top,find_by_uname='SUBOUTTXT')
                widget_control,txt,get_value=subout
		if subout[0] eq '' then return
		fcs_SAVEfile,event,sub=1
	end

; 'ADDOUTTXT':	$
;	begin
;		widget_control,event.id,get_value=addout
;		if n_elements(*(*ppstate).addfilesptr) eq 0 then return
;		fcs_addfiles,event
;		if addout[0] eq '' then return
;		fcs_SAVEfile,event,add=1
;	end
; 'SUBOUTTXT':	$
;	begin
;		if (*ppstate).SUBFILE1[0] eq '' then return
;		if (*ppstate).SUBFILE2[0] eq '' then return
;		fcs_SUBfiles,event
;		if  (*ppstate).subout[0] eq '' then return
;		fcs_SAVEfile,event,sub=1
;	end
'DISMISS':	$
	begin
		widget_control,event.top,/destroy
	end
'ARBFACTOR':	$
	begin
		widget_control,event.id,get_value=factor
		(*ppstate).arb=factor
		widget_control,event.top,set_uvalue = ppState
	end

else:
endcase

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro fcs_reduce_manip,	group_leader = group_leader,	$
			notifyIDs = notifyIDs,		$
			err = err,			$
			datadirectory = datadirectory	,$
			daveptr = daveptr			
; Widget definition module
registerName = 'fcs_reduce_manip'
err = 0
if xregistered(registerName) then begin
  err = 1
  return
endif
if n_elements(group_leader) eq 0 then begin
  group_leader = 0L
endif
if n_elements(notifyIDS) eq 0 then begin
  notifyIDS = [0L,0L]
endif

if n_elements(daveptr) eq 0 then begin
  daveptr=create_dave_pointer(daveptr,instrument=fcs)
endif

if n_elements(datadirectory) eq 0 then begin
  datadirectory = '/home/craig/idl/fcs'
endif


; Set up the widget layout
tlb2 = widget_base(group_leader = group_leader,/col, $
	title = 'Time-of-flight data manipulation', $
	/base_align_left,/tlb_frame_attr)
base1 = widget_base(tlb2,/col,frame=2)
base1a = widget_base(base1,/col)
void= widget_label(base1a,value='ADD A NUMBER OF FILES')
void = widget_button(base1a,value = 'Choose Files to Add', $
	uname = 'ADD')
base1b = widget_base(base1,/row)
addout=''
void= cw_field(base1b,title='Dave Output :',value=addout,uname='ADDOUTTXT',/all_events)
void = widget_button(base1b,value = 'Go!', $
	uname = 'ADD_GO')

Base2 = widget_base(tlb2,/col,frame=2)
Base2a = widget_base(base2,/col)
void = widget_label(Base2a,value = 'SUBTRACT ONE FILE FROM ANOTHER')
Base2c = widget_base(base2,/row)
void = widget_button(base2c,value = 'File 1', $
	uname = 'FILE1')
void = widget_label(Base2c,value = ' -')
FACTOR = widget_droplist(base2a,value=['Monitor norm', 'Time norm','Arb. factor'])
arb=1.0

void= cw_field(base2c,title='',value=arb,uname='ARBFACTOR',/all_events,xsize=10,/floating)

void = widget_button(base2c,value = 'File 2', $
	uname = 'FILE2')

base2b = widget_base(base2,/row)
subout=''
void= cw_field(base2b,title='Dave Output :',value=subout,uname='SUBOUTTXT',/all_events)
void = widget_button(base2b,value = 'Go!', $
	uname = 'SUB_GO')

rowBase8 = widget_base(tlb2,/row)
void = widget_button(rowBase8,value = 'Dismiss',uname = 'DISMISS')

widget_control,tlb2,/realize
state =		{	group_leader:group_leader, $
			notifyIDs:notifyIDs	, $
			datadirectory:datadirectory,$
			FACTOR:FACTOR, $
			SUBFILE1:'', $
			SUBFILE2:'', $
			o:obj_new(),	$
			addfilesptr:ptr_new(/allocate_heap),$
			addout:addout,$
			subout:subout,$
			arb:arb,$
			daveptr:daveptr}

ppState = ptr_new(state,/no_copy)
widget_control,tlb2,set_uvalue = ppState

xmanager,registerName,tlb2,/no_block,cleanup = 'fcs_reduce_manip_cleanup'
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
