function [data, mode, MineralList, ExtraData, ExtraLabel,BulkComposition] =  APHID
%[data, mode, MineralList, ExtraData, ExtraLabel] =  APHID
%APHID is the calling program used to perform Automated PHase IDentification
%using weighted least squares and conversion to additive/exchange components
%This code was produced by Kurt Steffen (ksteffen@unm.edu) at the Department
%of Earth and Planetary Sciences, University of New Mexico
%
%APHID and all functions called by APHID and written by Kurt Steffen
%(All functions included in the distribution of APHID held on the American 
%Mineralogist website) are copyrighted under the GPL (GNU Public License)
%and can therefore be freely distributed and are available as open source.
%However any derivatives of APHID or the APHID least-squares additive/exhange 
%algorithm for phase classification must bear this same copyright.
%See www.gnu.org for details
%
%An APHID users guide (manual.pdf), describing the form of the input data and
%background (model) file should have been included with this file
%
%In order to run APHID the MATLAB working directory should be changed
%to the directory that contains this .m file (APHID directory) and APHID
% should be entered at the MATLAB command line.  As distributed APHID will
%automatically perform phase classification on the test data set file using the
%test mineral model file.  An interested user can use this data set and mineral 
%file as a basis for their work
%
%INPUT
%The input for APHID is in the form of 3 .txt files
%The first file is AphidInput.txt which should be in the same directory as
%this .m file.  This file contains four lines: The first line is the name
%of the input that contains the compositional data to be phase classified. This
%file should be placed in the folder InputData which is an immediate subdirectory
%of the APHID directory.  The structure of this file is discussed in the APHID
%users guide.  The second line is the name of the mineral model file to be used
%during this run.  This file should be located in the BackgroundFiles directory
%which again is an immediate subdirectory of the APHID directory.  The structure
%of the Mineral (Stoichiometric) model file are discussed in the APHID users guide
%The third line of AphidInput.txt should contain the line "Total" or "Ratio", telling
%APHID how it should compute the error between the original and stoichiometric model
%compositions. The fourth line of AphidInput.txt contains the error theshold. The
%significance of the error theshold and the error compution mechanism are discussed
%the Computers & Geosceince paper with which this code was distributed.
%
%OUTPUT
%The structure of the APHID output is rather complex and uses cell arrays and structures
%However all of the raw data pertaining the phase classification and compositional
%variation is present. Please see the user's guide for further details. 
%
%Written by K. Steffen (ksteffen@unm.edu) at the University of New Mexico,
%Department of Earch and Planetary Science
%Copyright 2003 Kurt Steffen
%
%I'm very interested to find out about how scientists are using APHID, and especially
%about any problems they are having with the code and documentation, and any possibile
%applications of the PCC data that the APHID algorithm produces.
%
%Last Modified June 13th, 2003
lasterr('');
try
	Current_Directory = pwd;
    %Comment Out Next Line if Compiling
	addpath([pwd filesep 'Functions'])  %Adding path to private functions that APHID uses
	%Reading input data from AphidInput.txt
	[datafile,backgroundfile,ErrorPerfectWeight,maxerror,PhaseCheck] = InRead;

	%Setting file names
	datafile = [pwd filesep 'InputData' filesep datafile];
	savedirectory = [pwd filesep 'Output'];
	backgrounddirectory = [pwd filesep 'BackgroundFiles' filesep backgroundfile];

	%This is where the real work is done
	[controlstruct data mode MineralList ExtraData ExtraLabel BulkComposition] = ...
	APHIDengine(datafile,backgrounddirectory,maxerror,ErrorPerfectWeight,savedirectory,PhaseCheck);

    %Comment Out Next Line if Compiling
	rmpath([pwd filesep 'Functions']) %Removing path to private functions APHID uses
catch
	cd(Current_Directory)
    %Comment Out Next Line if Compiling
	rmpath([pwd filesep 'Functions']) %Removing path to private functions APHID uses
	disp(lasterr)
end