function CheckFunction(phaseuser,phasecalc,MineralStruct,index,MineralList,weights,...
perfectweights,weightsoriginal,neworder,weights1,perfectweights1,weightsoriginal1,...
neworder1,sumadditive)
%The purpose of check function is to compare the phase classification calculated
%with APHID and phase classification provided by the user.  The user supplied
%phase classification is provided in a column called "MineralName" in the 
%input file
%
%INPUT
%phaseuser = the name of phase as identified by the user (string)
%phasecalc = the name of phase as identified by APHID (string)
%MineralStruct = the structure created as raw output from the background file
%created in loadbackgroundfile
%index = The number of the analysis currently be considered (scaler double)
%MineralList = The key into the raw data format (double vector)
%
%For calculated phase
%weights = the model weights for the calculated phase (double vector)
%perfectweights = the model perfect weights for the calculated phase (double vector)
%weightsoriginal = the original analysis (double vector)
%neworder = A cell array of strings that contains the names of the components (column headings)
%
%These last for inputs are mirrored by inputs which have "1" appended, these
%represent the same data for the user supplied phase 
%
%OUTPUT
%The output of this function is sent to screen and contains the diagnostic 
%information (value and limits of exchange vectors) and original and model weights
%for the calculated and user supplied phase, this will provide the user
%with the neccessary information to determine if the misidentification is 
%due to a bad analysis or problems with the stoichiometric model for either
%calculated or user supplied phase.
%
%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 21, 2003

LMineralStruct = length(MineralStruct);  %Identifying the calculated phase in MineralList
for k = 1:LMineralStruct
	if strcmp(phasecalc,MineralStruct(k).MineralName)
		whichcalc = k;
		break
	end
	if k == LMineralStruct  %this if the phase is Undetermined
		whichcalc = 0;  
	end
end

disp(sprintf('----------Analysis %d----------',index))
if whichcalc == 0  %if phase undetermined then just print this message
	disp('Calculated phase is Undetermined');
else  % otherwise print the following diagnostics
	disp(sprintf('Calculated Phase is %s',phasecalc));
	lside= [{''};{'Original'}; {'Perfect'} ; {'Model'}];
	dispthis =[neworder {'Total'}; double2cell([weightsoriginal; perfectweights; weights]) [{sum(abs(weightsoriginal))};...
	{sum(abs(perfectweights))}; {sum(abs(weights))}]];
	disp([dispthis lside])
	dispthis = [MineralStruct(whichcalc).AdditiveExchangeVector;...
	double2cell(MineralStruct(whichcalc).Limits);...
	double2cell(sumadditive{whichcalc}')];
	disp([dispthis [{'Components'}; {'UpperLimit'};{'LowerLimit'};{'Value'}]])
end

LMineralStruct = length(MineralStruct);
for k = 1:LMineralStruct
	if strcmp(phaseuser,MineralStruct(k).MineralName)  %finding index into MineralStruct
		whichuser = k;
		break
	end
	if k == LMineralStruct  %for Undetermined or if the user has supplied a name that does not match a mineral model
		disp(sprintf('For analysis %d , the user supplied phase %s does not have a mineral model',index,phaseuser));
		return
	end
end

disp(sprintf('User Phase is %s',phaseuser));  %printing information for the user phase
lside= [{''};{'Original'}; {'Perfect'} ; {'Model'}];
dispthis =[neworder1 {'Total'}; double2cell([weightsoriginal1; perfectweights1; weights1]) [{sum(abs(weightsoriginal1))};...
{sum(abs(perfectweights1))}; {sum(abs(weights1))}]];
disp([dispthis lside])

dispthis = [MineralStruct(whichuser).AdditiveExchangeVector;...
double2cell(MineralStruct(whichuser).Limits);...
double2cell(sumadditive{whichuser}')];
disp([dispthis [{'Components'}; {'UpperLimit'};{'LowerLimit'};{'Value'}]]);