; $Id: dcs_heights.pro,v 1.5 2003/11/10 21:07:52 john Exp $
; Written by J.R.D. Copley.
;************************************************************************************************
pro dcs_heights_cleanup,tlb
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,tlb,get_uvalue=pState
ptr_free,pState
end


;************************************************************************************************
pro dcs_heights_handler,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=pState
if (event.id eq (*pState).done) then begin
	widget_control,event.top,/destroy
	return
endif
end


;************************************************************************************************
pro dcs_heights_calcs,pState,chosen,distances,disnames,results,resnames,messages
;************************************************************************************************
;
compile_opt strictarr
;
conv=25.4
;
; Inner shield
A=((*pState).indistances)[chosen[0],0]
B=((*pState).indistances)[chosen[0],1]
C=((*pState).indistances)[chosen[0],2]
D=((*pState).indistances)[chosen[0],3]
;
; Outer shield
E=((*pState).indistances)[chosen[1],4]
F=((*pState).indistances)[chosen[1],5]
G=((*pState).indistances)[chosen[1],6]
H=((*pState).indistances)[chosen[1],7]
;
; Vacuum can
I=((*pState).indistances)[chosen[2],8]
J=((*pState).indistances)[chosen[2],9]
; Displex
aa=((*pState).indistances)[chosen[3],10]
cc=((*pState).indistances)[chosen[3],11]

D1=aa+(B-D)+C
D2=cc+(E-H)
D3=cc+(F-H)+G
D4=J
;
Dp=I-(*pState).dis154-aa+(*pState).zvalue
Dpp=-I+(*pState).dis154+aa+A-D-(*pState).zvalue
Dppp=-I+(*pState).dis154+cc+E-H-(*pState).zvalue
;
Delta=Dp-(*pState).top2cn+(*pState).zvalue
;
distances=[A,B,C,D,E,F,G,H,I,J,aa,cc]
disnames='    '+['A','B','C','D','E','F','G','H','I','J','a','c']
;
results=[D1,D2,D3,D4,Dp,Dpp,Dppp,Delta]
resnames='    '+['D1   ','D2   ','D3   ','D4   ',"D'   ","D''  ","D''' ",'Delta']
;
messages=['MESSAGES',strarr(4)]
k=0
warning='This will not work:'
if (D1 ge D2) then begin
	k=k+1
	messages[k]=warning+' D1 < D2'
endif
if (D2 ge D3) then begin
	k=k+1
	messages[k]=warning+' D2 < D3'
endif
if (D3 ge D4) then begin
	k=k+1
	messages[k]=warning+' D3 < D4'
endif
if (Delta+(*pState).samhgt gt Dp+Dpp) then begin
	k=k+1
	messages[k]=warning+' Sample is too long.'
endif
if (Delta lt 0.0) then begin
	k=k+1
	messages[k]=warning+' Delta < 0'
endif
;
end


;************************************************************************************************
pro dcs_heights_get,event
;************************************************************************************************
;
compile_opt strictarr
;
widget_control,event.top,get_uvalue=pState
chosen=(*pState).chosen
zvalmin=(*pState).zvalmin
zvalmax=(*pState).zvalmax
zvalue=(*pState).zvalue
;
if (event.id eq (*pState).widz) then begin
	widget_control,event.id,get_value=zvalue
	if (zvalue lt zvalmin or zvalue gt zvalmax) then begin
		res=dialog_message("z value is outside limits")
	endif else begin
		(*pState).zvalue=zvalue
	endelse
	widget_control,event.id,set_value=string((*pState).zvalue,format='(f10.2)')
endif
;
if (event.id eq (*pState).widh) then begin
	widget_control,event.id,get_value=samhgt
	(*pState).samhgt=samhgt
	widget_control,event.id,set_value=string((*pState).samhgt,format='(f10.2)')
endif
;
if (event.id eq (*pState).widc) then begin
	widget_control,event.id,get_value=top2cn
	(*pState).top2cn=top2cn
	widget_control,event.id,set_value=string((*pState).top2cn,format='(f10.2)')
endif
;
k=where(event.id eq (*pState).widg,count)
if (count gt 0) then begin
	k=k[0]
	chosen[k]=event.index
	(*pState).chosen=chosen
endif
;
if (chosen[0] eq 3 or chosen[0] eq 4) then begin
	res=dialog_message("This heat shield is **MISSING**")
endif
;
widget_control,event.top,set_uvalue=pState
dcs_heights_calcs,pState,chosen,distances,disnames,results,resnames,messages
dcs_heights_writeout,pState,chosen,distances,disnames,results,resnames,messages
end


;************************************************************************************************
pro dcs_heights_writeout,pState,chosen,distances,disnames,results,resnames,messages
;************************************************************************************************
;
compile_opt strictarr
;
conv=25.4
zvalue=(*pState).zvalue
zoffset=(*pState).zoffset
;
sdistances=[[string(distances,format='(f8.1)')],[string(distances/conv,format='(f9.2)')]]
sresults=[[string(results,format='(f8.1)')],[string(results/conv,format='(f9.2)')]]
if (chosen[0] eq 0) then begin
	sdistances[0:3,*]=' '
	sresults[0,*]=' '
	sresults[5,*]=' '
endif
if (chosen[1] eq 0) then begin
	sdistances[4:7,*]=' '
	sresults[1:2,*]=' '
	sresults[6,*]=' '
endif
text1=['INPUT DISTANCES','             mm       in',disnames+" = "+sdistances[*,0]+sdistances[*,1]]
text2=['OUTPUT DISTANCES','                mm       in',resnames+" = "+sresults[*,0]+sresults[*,1],$
	'','  (z stage reading = '+strcompress(string(zvalue+zoffset,format='(f5.1)'))+')']
widget_control,(*pState).input,set_value=text1
widget_control,(*pState).output,set_value=text2
widget_control,(*pState).mess,set_value=messages
end


;************************************************************************************************
pro dcs_heights,event
;************************************************************************************************
;
compile_opt strictarr
;
res=dialog_message(["Standalone dcs_heights is no longer used.","",$
	"Instead please use the appropriate menu item within DAVE."])
return
;
if (n_elements(event) eq 0) then group_leader=0 else group_leader=event.top
;
inputfile=file_which('dcs_heights.dat',/include_current_dir)
openr,u,inputfile,/get_lun
	nquans=4
	choices=strarr(10,nquans)
	values=strarr(nquans)
	nchoices=intarr(nquans)
	chosen=intarr(nquans)
	ndistances=intarr(nquans)
	indistances=fltarr(10,12)
	jk=-1
	kk=intarr(4)
	dummy=''
	for k=0,nquans-1 do begin
		readf,u,kk
		nchoices[k]=kk[1]
		chosen[k]=kk[2]
		ndistances[k]=kk[3]
		readf,u,dummy
		values[k]=dummy
		readf,u,dummy
		temp=strsplit(dummy,' ',/extract)
		choices[0:nchoices[k]-1,k]=temp[0:nchoices[k]-1]
		dumdis=fltarr(nchoices[k])
		for j=0,ndistances[k]-1 do begin
			jk=jk+1
			readf,u,dumdis
			indistances[0:nchoices[k]-1,jk]=dumdis
		endfor
	endfor
	readf,u,dis154,zvalmin,zvalmax,zoffset
free_lun,u
;
zvalue=0.0
samhgt=100.0
top2cn=50.0
;
tlb=widget_base(title='Sample height calculations',/col,group_leader=group_leader)
	b1=widget_base(tlb,/row)
	sb1=lonarr(nquans+3)
	widg=lonarr(nquans+3)
	for k=0,nquans-1 do begin
		sb1[k]=widget_base(b1,/col)
			void=widget_label(sb1[k],value=values[k],/align_left)
			widg[k]=widget_combobox(sb1[k],xsize=120,value=choices[0:nchoices[k]-1,k],$
				event_pro="dcs_heights_get")
			widget_control,widg[k],set_combobox_select=chosen[k]
	endfor
		sb1[nquans]=widget_base(b1,/col)
			void=widget_label(sb1[nquans],value='z motor reading',/align_left)
			widz=widget_text(sb1[nquans],/editable,xsize=10,ysize=1,value=string(zvalue,format='(f10.2)'),font='Courier',$
				event_pro="dcs_heights_get")
		sb1[nquans+1]=widget_base(b1,/col)
			void=widget_label(sb1[nquans+1],value='Sample height',/align_left)
			widh=widget_text(sb1[nquans+1],/editable,xsize=10,ysize=1,value=string(samhgt,format='(f10.2)'),font='Courier',$
				event_pro="dcs_heights_get")
		sb1[nquans+2]=widget_base(b1,/col)
			void=widget_label(sb1[nquans+2],value='Sample top to center',/align_left)
			widc=widget_text(sb1[nquans+2],/editable,xsize=10,ysize=1,value=string(top2cn,format='(f10.2)'),font='Courier',$
				event_pro="dcs_heights_get")
	b2=widget_base(tlb,/row)
		inpb=widget_base(b2)
			input=widget_text(inpb,/editable,xsize=20,ysize=15,value=text1,font='Courier')
		outpb=widget_base(b2)
			output=widget_text(outpb,/editable,xsize=20,ysize=15,value=text2,font='Courier')
	mess=widget_text(tlb,/editable,ysize=5,value=messages,font='Courier')
	b3=widget_base(tlb,/row)
		done=widget_button(b3,value='Done')
geom=widget_info(tlb,/geometry)
geom2=widget_info(input,/geometry)
widget_control,input,scr_xsize=geom.xsize/2-4*geom2.margin
widget_control,output,scr_xsize=geom.xsize/2-4*geom2.margin
centertlb,tlb
widget_control,tlb,/realize
;
State={done:done,$
	input:input,$
	output:output,$
	mess:mess,$
	widg:widg,$
	nquans:nquans,$
	widz:widz,$
	widh:widh,$
	widc:widc,$
	chosen:chosen,$
	indistances:indistances,$
	dis154:dis154,$
	zvalmin:zvalmin,$
	zvalmax:zvalmax,$
	zoffset:zoffset,$
	zvalue:zvalue,$
	samhgt:samhgt,$
	top2cn:top2cn}
pState=ptr_new(State)
widget_control,tlb,set_uvalue=pState
;
dcs_heights_calcs,pState,chosen,distances,disnames,results,resnames,messages
dcs_heights_writeout,pState,chosen,distances,disnames,results,resnames,messages
;
xmanager,'dcs_heights',tlb,event_handler='dcs_heights_handler',cleanup='dcs_heights_cleanup'
;
end