; $Id: dcs_choose_det_eff.pro,v 1.15 2005/10/19 21:10:37 jrdcopley 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_choose_det_eff,dcs_base,det_eff_qty,det_eff_err,ichoice,message_str,nhedet=nhedet,$
	vansumfile=vansumfile,skipdefaultchoices=skipdefaultchoices,$
	logentry=logentry
;************************************************************************************************
;
compile_opt strictarr
;
; ichoice=0 if detector efficiencies are unity.
; ichoice=1 if detector efficiencies are computed.
; ichoice=2 if detector efficiencies are computed and saved.
; ichoice=3 if detector efficiencies are restored.
;
det_eff_qty=fltarr(nhedet)+1
det_eff_err=fltarr(nhedet)
;
internalchoice=ichoice
;
if (internalchoice eq 1 or internalchoice eq 2) then begin
	if (vansumfile eq "") then begin
		logentry=logentry+string(10B)+$
		"There is no vanadium file so efficiencies cannot be computed."
		result=dialog_message(["There is no vanadium file so efficiencies cannot be computed.",$
			"Detector efficiencies will be set to unity."],/error)
		internalchoice=0
	endif
endif
;
; The variable "method" controls how the detector efficiencies are calculated.
; method=0 means the total counts are computed for each detector. (NOT AVAILABLE)
; method=1 means the sum of the elastic peaks is fitted to a Gaussian.
; method=2 means elastic peaks are individually fitted to Gaussians. (NOT AVAILABLE)
if (internalchoice eq 1 or internalchoice eq 2) then $
;
; BEGIN
;	Following lines commented out (SIMPLIFICATION PROJECT)
;	dcs_multiplechoice,dcs_base,method,$
;		"Detector_efficiency_calculation_method",message_str,60,$
;		skipdefaultchoices=skipdefaultchoices
;	Following line substituted (SIMPLIFICATION PROJECT)
	method=1
; END
;
; This code has been added to handle Vanadium self-shielding corrections.
; vssf_decision=0 means Vanadium angle-dependent self-shielding factors are to be included.
; vssf_decision=1 means there is no Vanadium self-shielding correction.
;
vssf_decision=1
if (internalchoice eq 1 or internalchoice eq 2) then begin
	dcs_multiplechoice,dcs_base,vssf_decision,"Vanadium_selfshielding",$
		message_str,60,skipdefaultchoices=skipdefaultchoices
endif
;
;	The following line defines the nhedet-element array vssf.
vssf=fltarr(nhedet)+1.0
;
if (internalchoice eq 1 or internalchoice eq 2) then begin
	if (vssf_decision eq 0) then begin
		filename=dialog_pickfile(path=!dcs_outp_directory,$
			title="Define the Vanadium self-shielding file.")
		cancel=filename eq ""
		if (cancel) then begin
			vssf_decision=1
		endif else begin
			openr,unit,filename,/get_lun
			line=''
			readf,unit,line
			if (line eq 'Slab geometry') then begin
				nlines=0
				readf,unit,nlines
				for i=0,nlines-1 do readf,unit,line
				values=fltarr(7)
				for i=0,nhedet-1 do begin
					if (eof(unit)) then begin
						res=dialog_message("End-of-file encountered."+$
							"No Vanadium self-shielding correction will be applied.")
						vssf_decision=1
						break
					endif
					readf,unit,values
					vssf[i]=values[1]
				endfor
			endif else begin
				res=dialog_message("Angle-dependent Vanadium self-shielding "+$
					"corrections can only be performed for slab samples.")
				vssf_decision=1
			endelse
			free_lun,unit
		endelse
	endif
endif
;
case internalchoice of
	0: logentry=logentry+string(10B)+"Detector efficiencies have been set to unity."
	1: begin
		dcs_define_det_eff,dcs_base,det_eff_qty,det_eff_err,method,nhedet=nhedet,$
			vansumfile=vansumfile,logentry=logentry
		logentry=logentry+string(10B)+"Detector efficiencies have been computed."
; This code has been added to handle Vanadium self-shielding corrections.
		if (vssf_decision eq 1) then begin
			det_eff_qty=det_eff_qty/vssf
			norm=mean(det_eff_qty)
			det_eff_qty=det_eff_qty/norm
			logentry=logentry+string(10B)+"Detector efficiencies have "+$
				"been weighted by self-shielding factor."
		endif
	end
	2: begin
		dcs_define_det_eff,dcs_base,det_eff_qty,det_eff_err,method,nhedet=nhedet,$
			vansumfile=vansumfile,logentry=logentry
		logentry=logentry+string(10B)+"Detector efficiencies have been computed."
		repeat begin
			file=!dcs_rootname+".eff"
			fullfilename=dialog_pickfile(filter="*.eff",/fix_filter,$
				path=!dcs_outp_directory,$
				file=file,get_path=file_path,$
				title="Define the detector efficiency file.")
			cancel=fullfilename eq ""
			compok=strcmp(file_path,!dcs_outp_directory,/fold_case)
			if (not cancel and not compok) then res=dialog_message("You may not change the directory.")
		endrep until (cancel or compok)
		if (not cancel) then begin
			if (strlowcase(strmid(fullfilename,3,/reverse)) ne ".eff") then $
				fullfilename=fullfilename+".eff"
			save,filename=fullfilename,det_eff_qty,det_eff_err
			logentry=logentry+string(10B)+$
				"Detector efficiencies have been saved in file "+fullfilename
		endif
	end
	3: begin
		file=!dcs_outp_directory+!dcs_rootname+".eff"
		if (not file_test(file)) then file=!dcs_default_root+".eff" $
			else file=!dcs_rootname+".eff"
		fullfilename=dialog_pickfile(filter="*.eff",/fix_filter,$
			path=!dcs_outp_directory,/must_exist,$
			file=file,get_path=file_path,$
			title="Select the detector efficiency file.")
		cancel=fullfilename eq ""
		if (not cancel) then begin
			if (strlowcase(strmid(fullfilename,3,/reverse)) ne ".eff") then $
				fullfilename=fullfilename+".eff"
			restore,filename=fullfilename
			logentry=logentry+string(10B)+$
				"Detector efficiencies have been restored from file "+fullfilename
		endif
	end
	else:
endcase
;
end