function [controlstruct,data,mode,MineralList,extradata,extralabel,bulkcomposition] = APHIDengine...
(datafile,backgroundfile,maxerror,ErrorPerfectWeight,savedirectory,PhaseCheck)
%APHIDengine is the heart of the APHID algorithm
%OUTPUT
%controlstruct = the basic data required to run APHID (struct)
%data = the raw composition and phase data (cell array)
%mode = a cell array that contains modal analysis information
%MineralList = a struct array that contains the key into into the data cell array
%extradata = the values of extra columns that were included in the input .txt file
%extralabel = the column labels of the extra columns that were include in the input file
%bulkcomposition = a cell array that contains an integrated bulk composition
%as well as information about how elements are partitioned between phases
% 
%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 13, 2003
 


%converting all input data into a structure, controlstruct contains the basic 
%information required to run APHID
controlstruct = struct(...
'ErrorPerfectWeight',{ErrorPerfectWeight},...
'infile',{datafile},...
'backgroundfile',{[backgroundfile]},...
'changeiron1',{1},...
'changeiron0',{0},...
'maxerror',{maxerror},...
'savefile',{savedirectory},...
'PhaseCheck',{PhaseCheck});

%This block of code reads the mineral model data from the mineral model .txt file
%If there is any error for any reason, loadbackgroundexception is called in order
%to provide the user with detailed information about why the read of their 
%mineral model failed.  mineralstruct is a structure array that contains all of the 
%information about the stoichiometric models
try 
	[outstruct mineralstruct] = loadbackground(controlstruct);
catch
	loadbackgroundexception(controlstruct);
end


%Reading the input text file
[thelabels data extradata extralabel] = loadtext(controlstruct,outstruct.Elements);

if PhaseCheck == 1  %getting information used by CheckFunction to check phase identification
	controlstruct = PhaseCheckPrep(extradata,extralabel,controlstruct);
end
%Performing the classification this is the heart of the routine
[data MineralCell MineralList] = thefinder(controlstruct,thelabels,data,outstruct,mineralstruct);
%only perform modeanalysis and get a composite bulk composition if there is more then one
%analysis being classified 
if length(data) > 1
	mode = modeanalysis(data,MineralList,controlstruct);
	bulkcomposition = GetBulkComposition(data,mode,MineralList,controlstruct,outstruct.Elements);


else  %If this is a single analysis then print out information about the analysis
	disp(' ');
	disp('Phase Identified as:');
	disp(data{1}{1})
	disp(' ')


	filename = [pwd filesep 'Output' filesep 'LastSingle.csv'];
	writecsv(filename,data{1}(1));
	if ~strcmp(data{1}{1},'Undetermined') %Only print this data if the program identified a phase
		DisplaySingleAnalysis(MineralList,data,controlstruct)
	end
	mode = [];
	bulkcomposition = [];
end