; $Id: dcs_sampletemps.pro,v 1.5 2005/02/14 17:50:33 azuah Exp $
; Written by J.R.D. Copley.
; DCS_SAMPLETEMPS.PRO
;
; This program reads in sample temperatures and plots them for several files.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;###############################################################################
;
; 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_sampletempsCleanup,tlb
widget_control,tlb,get_uvalue = pState
wdelete,(*pState).winState.winPix
ptr_free,(*pState).winState.xPtr,(*pState).winState.yPtr
ptr_free,pState
return
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro dcs_sampletempsQuit,event
widget_control,event.top,/destroy
return
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro dcs_sampletempsPlot,event
widget_control,event.top,get_uvalue = pState
;
; Generate x and y values.
y = [$
	[*(*pState).dcsStatePtr[0]],$
	[*(*pState).dcsStatePtr[1]],$
	[*(*pState).dcsStatePtr[2]]]
npts=n_elements(y[*,0])
x = findgen(npts)+1
;
; Generate x and y titles.
runnames=*(*pState).runnamesPtr
xt0=runnames[0]
pos0=strpos(xt0,'200',/reverse_search)
xtitle = "First run is "+strmid(xt0,pos0,11)
ytitle = "Temperature (K)"
;
; Generate x and y ranges (in data units)
if (*pState).winState.autoscale eq 1 or $
	(*pState).winState.autoscale eq 2 then begin
  (*pState).winState.xrange = [0.0,float(npts)]
endif
;
if (*pState).winState.autoscale eq 1 or $
	(*pState).winState.autoscale eq 3 then begin
  dy = 0.05*(max(y)-min(y))
  if (dy eq 0.0) then dy = min(y)*0.2
  (*pState).winState.yrange = [(min(y)-dy) > 0,max(y)+dy > 1]
endif
;
xrange = (*pState).winState.xrange
yrange = (*pState).winState.yrange
;
; Initial plot with no data, no x axes.
plot,x,y(*,0),ytitle=ytitle,yrange=yrange,ystyle=1,$
	position = [0.15,0.15,0.925,0.85],/nodata,color=1,$
	xrange=xrange,xstyle=4

index=*(*pState).runindexPtr
nruns=n_elements(runnames)
;
; Plot temperature data for each run
for k=0,nruns-1 do begin
	xx=x[index[k]]
	yy=y[index[k]]
	plots,xx,yy,psym=2,color=2
	if (index[k] lt index[k+1]-1) then begin
		xx=[x[index[k]],x[index[k+1]-1]]
		yy=[y[index[k],0],y[index[k+1]-1,0]]
		oplot,xx,yy,color=2,thick=2,psym=0
	endif
	xx=x[index[k]:index[k+1]-1]
	yy=y[index[k]:index[k+1]-1,1]
	oplot,xx,yy,color=3,thick=1,psym=-4
	yy=y[index[k]:index[k+1]-1,2]
	oplot,xx,yy,color=4,thick=1,psym=-4
endfor
;
; Define corner coordinates in data units
xl=!x.crange[0]
xr=!x.crange[1]
yb=!y.crange[0]
yt=!y.crange[1]
xw=xr-xl
yw=yt-yb
;
; Plot upper and lower x axes.
oplot,[xl,xr],[yb+yw*0.001,yb+yw*0.001],color=1,thick=1
oplot,[xl,xr],[yt-yw*0.001,yt-yw*0.001],color=1,thick=1
;
; Define x and y offsets and widths in normalized units.
xoff=!x.window[0]
xwid=!x.window[1]-!x.window[0]
yoff=!y.window[0]
ywid=!y.window[1]-!y.window[0]
;
; Plot the title.
xyouts,xoff+xwid*0.5,(yoff+ywid)+0.05,$
   xtitle,alignment = 0.5,charsize=1.5,/normal,color=1
;
; Plot x tick labels and vertical lines as x ticks.
yc=yb-0.02*yw
for j = 0,nruns-1 do begin
	runstring=strmid(runnames[j],pos0+4,7)
  xc=index[j]+1
	xyouts,xc,yc,$
  	runstring,alignment = 0.0,charsize=1.0,$
  	orientation = -45.0,/data,color=1
	oplot,[xc,xc],[yb,yt],color=1,thick=1.2,linestyle=1
endfor
;
; Plot a legend
descrip=["Set point","Control","Sample"]
for j=0,2 do begin
	xs=xl+j*xw/3
	ys=yt+yw*0.02
	plots,[xs,xs+xw*0.08],[ys+0.01,ys+0.01],/data,color=j+2,thick=2
	xyouts,xs+xw*0.1,ys,descrip[j],$
		/data,alignment=0.0,charsize=1.2,charthick=1,color=1
endfor
return
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro dcs_sampletempsRead,event
widget_control,event.top,get_uvalue = pState

davesysvars
dcs_sysvars
;
dave_makeDavePtr,davePtr=davePtr
dcs_definedavestructure,davePtr=davePtr
;
files = dialog_pickfile(dialog_parent = event.top,title = 'Select run files',$
	path=!dcs_data_directory,/multiple_files)
files=files[sort(files)]
if (files[0] eq '' or files[0] eq ' ') then begin
	widget_control,event.top,/destroy
	return
endif
;
;files=["C:\DCS raw data\20010703_01.dcs.gz",$
;	"C:\DCS raw data\20010702_07.dcs.gz",$
;	"C:\DCS raw data\20010702_08.dcs.gz",$
;	"C:\DCS raw data\20010702_09.dcs.gz",$
;	"C:\DCS raw data\20010702_99.dcs.gz",$
;	"C:\DCS raw data\20010617_06.dcs.gz"]
;
nf=n_elements(files)
nofirstline=0
printinfo=0
omithisto=2
filetype="binary"
;
files=files[sort(files)]
;
progressBar = Obj_New($
	"SHOWPROGRESS",$
;	/cancelbutton,$;	(How does this work?)
	title="Reading DCS raw data files.",$
	xsize=300,$
	ysize=15,$
	message="Please be patient ...",$
	color=184)
;
progressBar->Start
;
dcs_readdatafile,files[0],omithisto,printinfo,filetype,davePtr=davePtr
;
incr=100.0/nf
progressBar->Update,incr
;
result=where((*(*(*davePtr).dataStrPtr).specificPtr).temp_sample ge 0,ntemps)
t_sample=0.0
t_control=0.0
t_setpoint=0.0
if (ntemps eq 1) then begin
	t_sample=(*(*(*davePtr).dataStrPtr).specificPtr).temp_sample[0]
	t_control=(*(*(*davePtr).dataStrPtr).specificPtr).temp_control[0]
	t_setpoint=(*(*(*davePtr).dataStrPtr).specificPtr).temp_setpoint
	if (t_sample eq 300.0 and t_control eq 300.0 and t_setpoint eq 300.0) then begin
		t_sample=-10.0
		t_control=-10.0
		t_setpoint=-10.0
	endif
endif
if (ntemps gt 1) then begin
	t_sample=(*(*(*davePtr).dataStrPtr).specificPtr).temp_sample[0:ntemps-1]
	t_control=(*(*(*davePtr).dataStrPtr).specificPtr).temp_control[0:ntemps-1]
	t_setpoint=(fltarr(ntemps)+1)*$
		(*(*(*davePtr).dataStrPtr).specificPtr).temp_setpoint
endif
n2=ntemps
;
if (nf gt 1) then begin
	for irun=1,nf-1 do begin
		file_path=files[irun]
		dcs_readdatafile,file_path,omithisto,printinfo,filetype,davePtr=davePtr
		progressBar->Update,irun*incr
		result=where((*(*(*davePtr).dataStrPtr).specificPtr).temp_sample ge 0,ntemps)
		if (ntemps eq 1) then begin
			ts=(*(*(*davePtr).dataStrPtr).specificPtr).temp_sample[0]
			tc=(*(*(*davePtr).dataStrPtr).specificPtr).temp_control[0]
			tsp=(*(*(*davePtr).dataStrPtr).specificPtr).temp_setpoint
			if (ts eq 300.0 and tc eq 300.0 and tsp eq 300.0) then begin
				ts=-10.0
				tc=-10.0
				tsp=-10.0
			endif
			t_sample=[t_sample,ts]
			t_control=[t_control,tc]
			t_setpoint=[t_setpoint,tsp]
			n2=[n2,n2[irun-1]+ntemps]
		endif
		if (ntemps gt 1) then begin
			t_sample=[t_sample,$
				(*(*(*davePtr).dataStrPtr).specificPtr).temp_sample[0:ntemps-1]]
			t_control=[t_control,$
				(*(*(*davePtr).dataStrPtr).specificPtr).temp_control[0:ntemps-1]]
			t_setpoint=[t_setpoint,(fltarr(ntemps)+1)*$
				(*(*(*davePtr).dataStrPtr).specificPtr).temp_setpoint]
			n2=[n2,n2[irun-1]+ntemps]
		endif
	endfor
endif
;
progressBar->Destroy
Obj_Destroy, progressBar
;
(*pState).nruns=nf
(*pState).runindexPtr=ptr_new([0,n2])
(*pState).runnamesPtr=ptr_new(files)
(*pState).dcsStatePtr[0] = ptr_new(t_setpoint)
(*pState).dcsStatePtr[1] = ptr_new(t_control)
(*pState).dcsStatePtr[2] = ptr_new(t_sample)

wset,(*pState).winState.winPix
dcs_sampletempsPlot,event
wset,(*pState).winState.winVis
device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pState).winState.winPix]
empty
return
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro dcs_sampletempsDraw,event
widget_control,event.top,get_uvalue = pState
format="(f10.1)"
;
case event.type of
0:	begin		; button press
	  (*pState).winState.mouse = event.press
	  if (*pState).winState.mouse eq 4 then begin
	    (*pState).winState.autoscale = 1
	    return
	  endif
	  if (*pState).winState.mouse eq 1 then begin
	    (*pState).winState.xbox[0] = event.x
	    (*pState).winState.ybox[0] = event.y
	    wset,(*pState).winState.winVis
	    device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pState).winState.winPix]
	    empty
	    (*pState).winState.autoscale = 0
	  endif
	end
1:	begin	; button release
	  xll = (*pState).winState.xbox[0] < (*pState).winState.xbox[1]
	  yll = (*pState).winState.ybox[0] < (*pState).winState.ybox[1]
	  w = abs((*pState).winState.xbox[1] - (*pState).winState.xbox[0])
	  h = abs((*pState).winState.ybox[1] - (*pState).winState.ybox[0])
	  xur = xll + w
	  yur = yll + h
	  ll = convert_coord(xll,yll,/device,/to_data,/double)
	  ur = convert_coord(xur,yur,/device,/to_data,/double)
	  (*pState).winState.xrange = [ll[0],ur[0]]
	  (*pState).winState.yrange = [ll[1],ur[1]]
	  wset,(*pState).winState.winPix
	  dcs_sampletempsPlot,event
	  wset,(*pState).winState.winVis
	  device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pState).winState.winPix]
	  (*pState).winState.mouse = 0B
	end
2:	begin	; mouse motion
	  result=convert_coord(event.x,event.y,/device,/to_data,/double)
	  index=*(*pstate).runindexPtr
	  runnames=*(*pstate).runnamesPtr
	  runnum=fix(max(where(result[0] gt index+1)))
	  if (runnum ge 0 and runnum le n_elements(runnames)-1) then begin
		  pos0=strpos(runnames[runnum],'200',/reverse_search)
			runstring=strmid(runnames[runnum],pos0+4,7)
			xcurs_info="[Run "+runstring+":"+$
				strcompress(string(result[0]-index(runnum),format="(i3)"))+$
				"]"
			ycurs_info=string(result[1],format=format)+"K"
			info=xcurs_info+ycurs_info
		endif else info=""
		widget_control,(*pState).curswindow,set_value=info
	  if (*pState).winState.mouse eq 1 then begin
	  	(*pState).winState.xbox[1] = event.x
	  	(*pState).winState.ybox[1] = event.y
	  	xc = [(*pState).winState.xbox[0],event.x,event.x,(*pState).winState.xbox[0],$
	  	      (*pState).winState.xbox[0]]
	  	yc = [(*pState).winState.ybox[0],(*pState).winState.ybox[0],event.y,event.y,$
	  	      (*pState).winState.ybox[0]]
	  	wset,(*pState).winState.winVis
	  	device,copy = [0,0,!d.x_size,!d.y_size,0,0,(*pState).winState.winPix]
	  	plots,xc,yc,/device,color=1
	  	empty
	  endif
	end
else:
endcase
return
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro dcs_sampletempsPrint,event
thisDevice = !d.name
deviceKeywords = ps_form(Cancel = canceled,parent = event.top)
if canceled ne 1 then begin
  SET_PLOT, 'PS'
  DEVICE, _Extra = deviceKeywords
  dcs_sampletempsPlot,event
  DEVICE, /CLOSE_FILE
  set_plot,thisDevice
endif
return
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro dcs_sampletemps,event
; Widget definition module

daveDeSensitizeButtons,event

amcolor = {color, $ ; name
red:   [255,0,255,  0,  0,  0,255,255,152,128,128,128,255,178,255],$ ; red values
green: [255,0,  0,255,  0,255,  0,255,120,128,128,128, 54,192,128],$ ; green values
blue:  [255,0,  0,  0,255,255,255,  0,103,220, 60,128, 54,220,128] $ ; blue values
}
colorset
oldct=dcs_getctinfo()
tvlct,amcolor.red,amcolor.green,amcolor.blue
;
tlb = widget_base(/row,title = 'DCS Sample temperatures Display',mbar = bar)
filemenu = widget_button(bar,value = 'File',/menu)
void = widget_button(filemenu,value = 'Select raw data files',$
	event_pro = 'dcs_sampletempsRead')
void = widget_button(filemenu,value = 'Print to a postscript file',$
       event_pro = 'dcs_sampletempsPrint')
void = widget_button(filemenu,value = 'Quit',event_pro = 'dcs_sampletempsQuit')
base2 = widget_base(tlb,/col)
base3 = widget_base(base2,/align_center)
curswindow =widget_text(base3,xsize=26,ysize=1,/editable,font="COURIER*14")
xsize = !screensize[0]*0.8 & ysize = !screensize[1]*0.7
win = widget_draw(base2,xsize = xsize,ysize = ysize,/button_events,$
      event_pro = 'dcs_sampletempsDraw',/motion_events)

centertlb,tlb
widget_control,tlb,/realize
widget_control,win,get_value = winVis
window,/free,/pixmap,xsize = xsize,ysize = ysize
winPix = !d.window

winState =  {winPix:winPix,$
             winVis:winVis,$
             win:win,$
             xbox:[0.0,1.0],$
             xrange:[0.0d0,1.0d0],$
             ybox:[0.0,1.0],$
             yrange:[0.0d0,1.0d0],$
             xPtr:ptr_new(/allocate_heap),$
             yPtr:ptr_new(/allocate_heap),$
             xtitle:'',$
             ytitle:'',$
             title:'',$
             mouse:0B,$
             autoscale:1}

state = {curswindow:curswindow,$
         winState:winState,$
         dcsStatePtr:ptrarr(3,/allocate_heap),$
         nruns:1,$
         curves:[1,1,1],$
         runindexPtr:ptr_new(/allocate_heap),$
         runnamesPtr:ptr_new(/allocate_heap)}

widget_control,tlb,set_uvalue = ptr_new(state,/no_copy)
dcs_sampletempsRead,{event,id:void,top:tlb,handler:0l}
if (widget_info(tlb,/valid_id)) then $
	xmanager,'dcs_sampletemps',tlb,cleanup = 'dcs_sampletempsCleanup'

daveSensitizeButtons,event
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
