; $Id: dcs_display_contents.pro,v 1.13 2005/02/14 17:50:14 azuah Exp $
; Written by J.R.D. Copley.
;************************************************************************************************
;###############################################################################
;
; 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 or if the code in this file is
;  included in another product.
;
;###############################################################################
pro dcs_display_contents_get_contents,contents,bitflips,irun,ntemps,$
	t_sample,t_control,davePtr=davePtr
;************************************************************************************************
;
compile_opt strictarr
;
; Define the string array "contents", to be displayed on the screen.
	contents=[$
		"Command:  "+(*(*(*davePtr).dataStrPtr).specificPtr).command,$
		"Comments: "+(*(*(*davePtr).dataStrPtr).specificPtr).comments,$
		"Start and stop times:   "+$
			(*(*(*davePtr).dataStrPtr).specificPtr).start_date+"       "+$
			(*(*(*davePtr).dataStrPtr).specificPtr).stop_date+"       ",$
		"Run duration: "+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).duration,format='(f10.1)')+" sec,  i.e."+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).duration/3600.0,format='(f8.2)')+" hrs.","",$
		"Wavelength, master speed, resolution, SR mode, SR denominator, Tsdmin:",$
			"     "+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).ch_wl,format='(f7.3)')+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).ch_ms,format='(f10.1)')+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).ch_res,format='(i3)')+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).ch_srmode,format='(i3)')+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).ch_srdenom,format='(i3)')+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).tsdmin,format='(f8.1)'),$
		"Chopper phases:            "+$
			strjoin(string((*(*(*davePtr).dataStrPtr).specificPtr).ch_phase*0.01,format='(f8.2)')),$
		"Chopper programmed delays: "+$
			strjoin(string((*(*(*davePtr).dataStrPtr).specificPtr).ch_delay,format='(f8.1)')),""]
;
; Compute means and standard deviations of sample and control temperatures.
	result=where((*(*(*davePtr).dataStrPtr).specificPtr).temp_sample ge 0,ntemps)
	t_sample=0.0
	t_control=0.0
	if (ntemps ge 1) then begin
		if (ntemps eq 1) then begin
			t_sample=(*(*(*davePtr).dataStrPtr).specificPtr).temp_sample[0]
			mean_temp_sample=t_sample
			stdv_temp_sample=0.0
		endif else begin
			t_sample=(*(*(*davePtr).dataStrPtr).specificPtr).temp_sample[0:ntemps-1]
			result=moment(t_sample)
			mean_temp_sample=result[0]
			stdv_temp_sample=sqrt(result[1])
		endelse
		result=where((*(*(*davePtr).dataStrPtr).specificPtr).temp_control ge 0,ntemps)
		if (ntemps eq 1) then begin
			t_control=(*(*(*davePtr).dataStrPtr).specificPtr).temp_control[0]
			mean_temp_control=t_control
			stdv_temp_control=0.0
		endif else begin
			t_control=(*(*(*davePtr).dataStrPtr).specificPtr).temp_control[0:ntemps-1]
			result=moment(t_control)
			mean_temp_control=result[0]
			stdv_temp_control=sqrt(result[1])
		endelse
;
; Write temperature information (or an appropriate message).
		addition=[$
			"T_Setpoint="+$
				string((*(*(*davePtr).dataStrPtr).specificPtr).temp_setpoint,format='(f7.2)')+"   "+$
				"T_Sample= "+$
				strcompress(string(mean_temp_sample,format='(f7.2)')+"("+$
				string(stdv_temp_sample,format='(f7.2)'),/remove_all)+")   "+$
				"T_Control= "+$
				strcompress(string(mean_temp_control,format='(f7.2)')+"("+$
				string(stdv_temp_control,format='(f7.2)'),/remove_all)+") ["+$
				strcompress(string(ntemps),/remove_all)+" values]"]
	endif else addition="No temperature monitoring performed."
	contents=[contents,addition,""]
;
; Add some more information to the string array "contents".
	addition=[$
		"Motor positions: "+$
			strjoin(strtrim(string((*(*(*davePtr).dataStrPtr).specificPtr).motor_pos,format='(f9.4)'),1),"  "),$
		"Collimator amplitude, mean, motion: "+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).coll_amp,format='(f7.2)')+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).coll_mean,format='(f7.2)')+"  "+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).coll_osc,format='(i1)'),$
		"Datamax, highmax: "+string((*(*(*davePtr).dataStrPtr).specificPtr).datamax,format='(i14)')+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).highmax,format='(i14)'),$
		"Highsource[0:8] =  |"+strjoin((*(*(*davePtr).dataStrPtr).specificPtr).highsource[0:8],"|")+"|",$
		"High end counts (0-8)  :  "+$
			strjoin(strtrim((*(*(*davePtr).dataStrPtr).specificPtr).detsum[913:921],1),"  "),$
		"Highsource[9:16]=  |"+strjoin((*(*(*davePtr).dataStrPtr).specificPtr).highsource[9:16],"|")+"|",$
		"High end counts (9-16) :  "+$
			strjoin(strtrim((*(*(*davePtr).dataStrPtr).specificPtr).detsum[922:928],1),"  ")+$
			"  "+strtrim((*(*(*davePtr).dataStrPtr).specificPtr).nframes,1),$
		"Startchoice=  "+(*(*(*davePtr).dataStrPtr).specificPtr).startchoice,$
		"Total detector, high end and overall counts: "+$
			strtrim((*(*(*davePtr).dataStrPtr).specificPtr).grandsum[0],1)+"  "+$
			strtrim((*(*(*davePtr).dataStrPtr).specificPtr).grandsum[2],1)+"  "+$
			strtrim((*(*(*davePtr).dataStrPtr).specificPtr).grandsum[4],1),$
		"Totals, crate 0 (0-8)  :  "+$
			strjoin(strtrim(transpose((*(*(*davePtr).dataStrPtr).specificPtr).totals[0,0:8]),1),"  "),$
		"Totals, crate 0 (9-13):  "+$
			strjoin(strtrim(transpose((*(*(*davePtr).dataStrPtr).specificPtr).totals[0,9:13]),1),"  "),$
		"Totals, crate 1 (0-8)  :  "+$
			strjoin(strtrim(transpose((*(*(*davePtr).dataStrPtr).specificPtr).totals[1,0:8]),1),"  "),$
		"Totals, crate 1 (9-16):  "+$
			strjoin(strtrim(transpose((*(*(*davePtr).dataStrPtr).specificPtr).totals[1,9:16]),1),"  "),$
		"Chopper distances: "+$
			strjoin(string((*(*(*davePtr).dataStrPtr).specificPtr).ch_dis,format='(f9.1)')),$
		"Beam monitor distances, detector distance: "+$
			strjoin(string((*(*(*davePtr).dataStrPtr).specificPtr).fc_dis,format='(f8.1)'))+$
			string((*(*(*davePtr).dataStrPtr).specificPtr).det_dis,format='(f8.1)'),$
		"Slot positions (LOW RES): "+$
			strjoin(string((*(*(*davePtr).dataStrPtr).specificPtr).ch_slots[*,0],format='(f7.2)')),$
		"Slot positions (MED RES): "+$
			strjoin(string((*(*(*davePtr).dataStrPtr).specificPtr).ch_slots[*,1],format='(f7.2)')),$
		"Slot positions (HI  RES): "+$
			strjoin(string((*(*(*davePtr).dataStrPtr).specificPtr).ch_slots[*,2],format='(f7.2)')),$
		"Chopper built-in delays:  "+" "+$
			strjoin(string((*(*(*davePtr).dataStrPtr).specificPtr).ch_bid,format='(f7.3)')),$
		""]
	contents=[contents,addition]
;
; Add some sanity check information (regarding repeats, resets and bit flips).
	if (total((*(*(*davePtr).dataStrPtr).specificPtr).repeats) + $
		total((*(*(*davePtr).dataStrPtr).specificPtr).resets) eq 0) then $
		addition="No repeats or resets." else $
		addition=[$
		"Repeats, crate 0:  "+strjoin(strtrim(transpose((*(*(*davePtr).dataStrPtr).specificPtr).repeats[0,*]),1),"  "),$
		"Repeats, crate 1:  "+strjoin(strtrim(transpose((*(*(*davePtr).dataStrPtr).specificPtr).repeats[1,*]),1),"  "),$
		"Resets,  crate 0:  "+strjoin(strtrim(transpose((*(*(*davePtr).dataStrPtr).specificPtr).resets[0,*]),1),"  "),$
		"Resets,  crate 1:  "+strjoin(strtrim(transpose((*(*(*davePtr).dataStrPtr).specificPtr).resets[1,*]),1),"  ")]
	contents=[contents,addition]
	if ((*(*(*davePtr).dataStrPtr).specificPtr).badbrd + (*(*(*davePtr).dataStrPtr).specificPtr).baddet eq 0) then $
		addition="No bad boards or bad inputs." else $
		addition="BAD BOARDS = "+string((*(*(*davePtr).dataStrPtr).specificPtr).badbrd,format='(i3)')+"   "+$
			"BAD INPUTS = "+string((*(*(*davePtr).dataStrPtr).specificPtr).baddet,format='(i3)')
	contents=[contents,addition]
	nflips=0
	for k=0,4 do begin
		if (bitflips[1,k,irun-1] ne 1000) then begin
			addition="BIT FLIP --> time, detector, value = "+$
				strtrim(string(bitflips[0,k,irun-1]),1)+" "+$
				strtrim(string(bitflips[1,k,irun-1]),1)+" "+$
				strtrim(string(bitflips[2,k,irun-1]),1)
			contents=[contents,addition]
			nflips=nflips+1
		endif
	endfor
	if (nflips eq 0) then	contents=[contents,"No bit flips."]
;
end


;************************************************************************************************
pro dcs_display_contents_save_handler,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=pstate,/no_copy
;
; Handle the "save" button. This saves the array "contents".
if (event.id eq (*pstate).savecontents) then begin
	extension=".out"
	path=!dcs_outp_directory
	file=!dcs_rootname
	repeat begin
		fullfilename=dialog_pickfile(filter="*"+extension,/fix_filter,path=path,$
			file=file,get_path=file_path,title="Define output file.")
		cancel=fullfilename eq ""
	endrep until (cancel or strcmp(file_path,path,/fold_case))
	if (not cancel) then begin
		if (strlowcase(strmid(fullfilename,3,/reverse)) ne extension) then $
			fullfilename=fullfilename+extension
		openw,lun,fullfilename,/get_lun
		printf,lun,(*pstate).title
		printf,lun,""
		printf,lun,(*pstate).contents
		free_lun,lun
	endif
	widget_control,event.top,set_uvalue=pstate,/no_copy
	return
endif
;
end


;************************************************************************************************
pro dcs_display_contents_print_handler,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=pstate,/no_copy
;
; Handle the "print" button. This prints the array "contents".
if (event.id eq (*pstate).printcontents) then begin
	tempfile="temp_print.txt"
	openw,lun,tempfile,/get_lun
	printf,lun,(*pstate).title
	printf,lun,""
	printf,lun,(*pstate).contents
	free_lun,lun
	dcs_print_textfile,file=tempfile
	file_delete,tempfile
	widget_control,event.top,set_uvalue=pstate,/no_copy
	return
endif
;
end



;************************************************************************************************
pro dcs_display_contents_handler,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=pstate,/no_copy
;
; Respond to the "Main menu", "Prev", "Next" and "Proceed" buttons.
if (event.id eq (*pstate).cancel) then istep=999
if (event.id eq (*pstate).prev) then istep=-1
if (event.id eq (*pstate).next) then istep=1
if (event.id eq (*pstate).done) then istep=0
;
*((*pstate).continue)=istep
;
widget_control,event.top,set_uvalue=pstate,/no_copy
widget_control,event.top,/destroy
;
end



;************************************************************************************************
pro dcs_display_contents,dcs_base,files_selected,nruns,davePtr=davePtr,bitflips
;************************************************************************************************
; This routine displays selected quantities for each of the files selected.
;
compile_opt strictarr
;
;
; Work through the runs. Start with first run and work forwards (or backwards).
irun=1
istep=1
while (1) do begin
;
; Read the file header.
	nofirstline=0
	printinfo=0
	omithisto=2
	file_path=files_selected[irun-1]
	title="Contents of file "+file_path
	filetype="binary"
	dcs_readdatafile,file_path,omithisto,printinfo,filetype,davePtr=davePtr
;
	dcs_display_contents_get_contents,contents,bitflips,irun,ntemps,$
		t_sample,t_control,davePtr=davePtr
;
; Begin to construct the widget.
	xsize=max(strlen(contents))
	ysize=size(contents,/dimension)
	xoffset=!screensize[0]*0.02
	yoffset=!screensize[0]*0.02
	scr_xsize=!screensize[0]*0.98
	scr_ysize=!screensize[1]*0.65
;
	tlb=widget_base(title=title,/col,group_leader=dcs_base)
;
; Create a row of some buttons.
	buttons=widget_base(tlb,/row,/align_left)
		cancel=widget_button(buttons,value="Main menu")
		savecontents=widget_button(buttons,value="Save",event_pro=$
			"dcs_display_contents_save_handler")
		printcontents=widget_button(buttons,value="Print",event_pro=$
			"dcs_display_contents_print_handler")
		prev=widget_button(buttons,value="Prev")
		next=widget_button(buttons,value="Next")
		done=widget_button(buttons,value="PROCEED")
;
;	Add space for the actual information.
	list=widget_text(tlb,/scroll,value=contents,xsize=xsize,ysize=ysize[0])
;
; Realize the widget.dave

	dave_position_tlb,tlb
	widget_control,tlb,/realize
;
; Initialize some pointers. These point to variables needed after the widget is
; destroyed.
	istep=1
	continue=ptr_new(istep)
	state={cancel:cancel,prev:prev,next:next,done:done,continue:continue,$
		contents:contents,title:title,savecontents:savecontents,$
		printcontents:printcontents,$
		detsum:(*(*(*davePtr).dataStrPtr).specificPtr).detsum,t_sample:t_sample,t_control:t_control,$
		timsum:(*(*(*davePtr).dataStrPtr).specificPtr).timsum,$
		file_path:file_path,irun:irun,nruns:nruns}
;
	widget_control,tlb,set_uvalue=ptr_new(state),/no_copy
;
	xmanager,'dcs_display_contents',tlb,event_handler='dcs_display_contents_handler'
;
	istep=*continue
	heap_gc
;
;	Decide whether to return (cancel or proceed), move forward or move backward.
	if (istep eq 0) then return
	if (istep eq 999) then begin
		nruns=0
		return
	endif
	irun=irun+istep
	irun=irun > 1 < nruns
	ptr_free,continue
endwhile
;
end