; $Id: dcs_sample_conditions.pro,v 1.8 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_sample_conditions,dcs_base,files_selected,nfiles_selected,runtemperatures,$
	conditions,assignments,message_str=message_str,skipdefaultchoices=skipdefaultchoices
;************************************************************************************************
;
compile_opt strictarr
;
;	Have user define conditions of sample runs and then
; associate each sample file with one of these conditions.
;
ok=0
nsamfiles=nfiles_selected[0]
sfnames=files_selected[0:nsamfiles-1]
;
while (not ok) do begin
;
	dcs_multiplechoice,dcs_base,decision,"Restore_sample_conditions",message_str,60,$
		skipdefaultchoices=skipdefaultchoices
;
	cancel=0
	if (decision eq 0) then begin
		file=!dcs_outp_directory+!dcs_rootname+".scn"
		if (not file_test(file)) then file=!dcs_default_root+".scn" $
			else file=!dcs_rootname+".scn"
		fullfilename=dialog_pickfile(filter="*.scn",/fix_filter,$
			path=!dcs_outp_directory,/must_exist,$
			file=file,get_path=file_path,$
			title="Select the file that contains the sample condition information.")
		cancel=fullfilename eq ""
		if (not cancel) then begin
			if (strlowcase(strmid(fullfilename,3,/reverse)) ne ".scn") then $
				fullfilename=fullfilename+".scn"
			restore,filename=fullfilename
			nscons=n_elements(assignments)
			if (nsamfiles ne nscons) then begin
				x=dialog_message("The number of sample condition assignments ("+$
					strcompress(nscons,/remove_all)+") does not match the number of sample files ("+$
					strcompress(nsamfiles,/remove_all)+").",/error)
				cancel=1
			endif
		endif
	endif
;
	if (decision eq 1 or cancel) then begin
;
; Ask user to define one or more choices of sample condition for the sample files.
		float_conditions=fltarr(nsamfiles)
		float_conditions[0]=runtemperatures[0]
		index=0
		for k=1,nsamfiles-1 do begin
			newtemperature=1
			for kp=0,k-1 do begin
				if (runtemperatures[k] eq runtemperatures[kp]) then newtemperature=0
			endfor
			if (newtemperature) then begin
				index=index+1
				float_conditions[index]=runtemperatures[k]
			endif
		endfor
		nconditions=index+1
;
		conditions=strarr(nsamfiles)
		for k=0,nconditions-1 do conditions[k]=strcompress(string(float_conditions[k],format='(f7.2)'))+"K"
;
		dcs_define_conditions,dcs_base,nsamfiles,conditions,nochange
;
		assignments=intarr(nsamfiles)
		dummy=where(conditions ne "",nconditions)
		if (nochange) then begin
			for k=0,nsamfiles-1 do begin
				for j=0,nconditions-1 do begin
					if (runtemperatures[k] eq conditions[j]) then assignments[k]=j+1
				endfor
			endfor
		endif
;
;	Associate each sample file with a sample condition.
		if (nconditions eq 1) then begin
	 		assignments=intarr(nsamfiles)+1
	 	endif else begin
			dcs_choose_conditions,dcs_base,conditions,sfnames,assignments
		endelse
;
	endif
;
	checksum=fix(total(where(assignments eq 0)))
	message=strarr(nsamfiles)
;
; Print list of conditions.
	if (checksum lt 0) then begin
		ncon=n_elements(conditions)
		message=strarr(nsamfiles+ncon+3)
		message[0]='These are the sample files corresponding to each sample condition.'
		imsg=0
		for i=1,ncon do begin
			index=where(assignments eq i,nindex)
			if (nindex ge 1) then begin
				imsg=imsg+1
				if (conditions[i-1] eq "") then $
					message[imsg]='No defined condition: '$
				else $
					message[imsg]='Condition: '+conditions[i-1]
				for j=0,nindex-1 do begin
					imsg=imsg+1
					message[imsg]='     '+sfnames[index[j]]
				endfor
			endif
		endfor
	  message[imsg+1]='To revise conditions click "Back".'
	  message[imsg+2]='Otherwise click "Proceed".'
		message=message[0:imsg+2]
		maxlen=60
		blabel=['Back','Proceed']
		def_button=1
		can_button=-1
		if (skipdefaultchoices) then decision=def_button else $
		dcs_multichoice_modal,message,decision,blabel,def_button,$
			group_leader=dcs_base,alignment=-1
		if (decision eq 1) then ok=1
	endif else return
endwhile
;
dcs_multiplechoice,dcs_base,decision,"Save_sample_conditions",message_str,60,$
	skipdefaultchoices=skipdefaultchoices
if (decision eq 0) then begin
	repeat begin
		file=!dcs_rootname+".scn"
		fullfilename=dialog_pickfile(filter="*.scn",/fix_filter,$
			path=!dcs_outp_directory,$
			file=file,get_path=file_path,$
			title="Define the sample conditions 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 ".scn") then $
			fullfilename=fullfilename+".scn"
		save,filename=fullfilename,conditions,assignments
	endif
endif
;
end
