function [output,MineralList,CurrentMinerals,controlstruct] = SelectMineral(output,Difference,...
OutOfLimitVector,MineralStruct,sumadditive,k,controlstruct,sumcations,...
sumweights,sumorder,MineralList,CurrentMinerals,mintemp,sumweightsoriginal,...
sumcsationsoriginal,PerfectDifference,sumperfectweights,SolidSolution,EndMemberValues,...
TheEndMembers)
%SelectMineral
%The purpose of this function is to perform the actual selection of the phase after everything 
%has been calculated.  The INPUT and OUTPUT of this function are too numerous to describe fully,
%but this function is only called by thefinder.m, so the intested user could scan that file
%in order to better understand what is going on
%
%Written by K. Steffen (ksteffen@unm.edu) at the University of New Mexico,
%Department of Earch and Planetary Science
%Copyright 2003 Kurt Steffen
%
%Last Modified June 19, 2003

%removing analyses that are out of limits
track = 1:length(Difference);
TheOuts = find(OutOfLimitVector == 1);
track(TheOuts) = [];
Difference(TheOuts) = [];
PerfectDifference(TheOuts) = [];

weights = [];
weightsoriginal = [];
perfectweights = [];

[theminimum theindex] = min(Difference);
[theminimumPerfect theindexPerfect] = min(PerfectDifference);
if controlstruct.ErrorPerfectWeight == 1  %In this case use the perfect weight error
	theminimum = theminimumPerfect;
	theindex = theindexPerfect;
end

theindex = track(theindex);
themineral = mintemp(theindex);
    
      
                          
if ~isempty(theindex) & theminimum <= controlstruct.maxerror & theindex ~= -1 %is the analysis identified
	SolidSolution = SolidSolution(theindex);
	EndMemberValues= EndMemberValues{theindex};
	TheEndMembers = TheEndMembers{theindex};
	Additive = sumadditive{theindex};
	[neworder cations weights] =...
	PrepWeightsandCationsOrder(sumcations,sumweights,sumorder,theindex);
	[crap cationsoriginal weightsoriginal] = ...
	PrepWeightsandCationsOrder(sumcsationsoriginal,sumweightsoriginal,sumorder,theindex);
	[neworder crap perfectweights] =...
	PrepWeightsandCationsOrder(sumcations,sumperfectweights,sumorder,theindex);
	if ~isempty(MineralList) %Have any phases been previously identified
		ismineral = GetColumnsGeneral3(CurrentMinerals,themineral);
		ismineral = ismineral{2,1};
	else
		ismineral = -1;
	end
 
	if ismineral == -1 %this adds to MineralList if an example of this phase has not been identified
		
		temp = 5;
		EndMemberlocation = temp;
		temp = temp + 1;
		Cationlocation = temp;
		temp = temp + 1;
            
		Weightlocation = temp;
		temp = temp + 1;
		WeightPerfectlocation = temp;
		temp = temp + 1;
		Additivelocation = temp;
		MineralListStruct = struct(...
		'Molecular',MineralStruct(theindex).Molecular,...
		'MineralName',themineral,...
		'MineralNameLocation',1,...
		'ErrorLocation',2,...
		'CationOriginalLocation',4,...
		'CationOriginalOrder',{neworder},...
		'WeightOriginalLocation',3,...
		'WeightOriginalOrder',{neworder},...
		'WeightPerfectLocation',{WeightPerfectlocation},...
		'WeightPerfectOrder',{neworder},...
		'CationLocation',Cationlocation,...
		'CationOrder',{neworder},...
		'AdditiveLocation',{Additivelocation},...
		'density',MineralStruct(theindex).density,...
		'AdditiveOrder',{MineralStruct(theindex).AdditiveExchangeVector},...
		'EndMemberLocation',EndMemberlocation,...
		'EndMemberOrder',{TheEndMembers},...
		'WeightLocation',Weightlocation,...
		'WeightOrder',{neworder});

		if isempty(MineralList)  %placing MineralListStruct into structure array
			CurrentMinerals = themineral;
			MineralList = MineralListStruct;
			ismineral = 1;
		else
                	MineralList(length(MineralList)+1) = MineralListStruct;
                	CurrentMinerals(length(MineralList)) = themineral;
                	ismineral = length(MineralList);
            	end

	end


	%saving the raw data
	if ~isempty(MineralList(ismineral).MineralNameLocation)
		output{k}(MineralList(ismineral).MineralNameLocation) = themineral;
	end
	if ~isempty(MineralList(ismineral).ErrorLocation)
		output{k}(MineralList(ismineral).ErrorLocation) = {theminimum};
	end
	if ~isempty(MineralList(ismineral).CationOriginalLocation)
		output{k}(MineralList(ismineral).CationOriginalLocation) = {cationsoriginal};
	end
	if ~isempty(MineralList(ismineral).WeightOriginalLocation)
		output{k}(MineralList(ismineral).WeightOriginalLocation) = {weightsoriginal};
	end
	if ~isempty(MineralList(ismineral).EndMemberLocation)
		output{k}(MineralList(ismineral).EndMemberLocation) = {EndMemberValues};
	end
	if ~isempty(MineralList(ismineral).CationLocation)
		output{k}(MineralList(ismineral).CationLocation) = {cations};
	end
	if ~isempty(MineralList(ismineral).WeightLocation)
		output{k}(MineralList(ismineral).WeightLocation) = {weights};
	end
	if ~isempty(MineralList(ismineral).AdditiveLocation)
		output{k}(MineralList(ismineral).AdditiveLocation) = {Additive'};
	end
	if ~isempty(MineralList(ismineral).WeightPerfectLocation)
	output{k}(MineralList(ismineral).WeightPerfectLocation) = {perfectweights};
	end
else	%if the phase can not be identified
	output(k) = {{'Undetermined'}};
end

if controlstruct.PhaseCheck == 1 %Comparing calculated phase to user supplied phase
	if ~strcmp(controlstruct.PhaseCheckVector{k},output{k}{1})  %user and calculated are different

		%Calculating information for uses supplied phase
		LMineralStruct = length(MineralStruct);
		for m = 1:LMineralStruct
			if strcmp(controlstruct.PhaseCheckVector{k},MineralStruct(m).MineralName)
				whichuser = m;
				break
			end
				whichuser = -1;
		end

		if whichuser ~= -1
			[neworder1 cations weights1] =...
			PrepWeightsandCationsOrder(sumcations,sumweights,sumorder,whichuser);
			[crap cationsoriginal weightsoriginal1] = ...
			PrepWeightsandCationsOrder(sumcsationsoriginal,sumweightsoriginal,sumorder,whichuser);
			[neworder1 crap perfectweights1] =...
			PrepWeightsandCationsOrder(sumcations,sumperfectweights,sumorder,whichuser);
		else
			weights1 = [];
			weightsoriginal1 = [];
			perfectweights1 = [];
			neworder1 = [];
		end

		if strcmp(output{k}{1},'Undetermined')
			neworder = [];
			weights = [];
			perfectweights = [];
			weightsoriginal= []; 
		end

		%Displaying Info
		CheckFunction(controlstruct.PhaseCheckVector{k},output{k}{1},MineralStruct,k,MineralList,...
		weights,perfectweights,weightsoriginal,neworder,weights1,perfectweights1,weightsoriginal1,neworder1,...
		sumadditive);
 
		controlstruct.phasechecktest  = 1;
	end
end
