function loadbackgroundexception(controlstruct)
%This function is the error handler used to catch errors in reading the mineral model/stoichiometry file
%
%Written by K. Steffen (ksteffen@unm.edu) at the University of New Mexico,
%Department of Earch and Planetary Science and copyrighted under the GNU
%public license, see APHID.m and www.gnu.org for details
%
%Last Modified June 13, 2003

file = controlstruct.backgroundfile;
FID = fopen(file,'r');

try
	tempstring = fgetl(FID);
catch
	errorstring = sprintf('Could Not Open Background File %s\n',file); 
	error(errorstring)	
end

try 
	Elements = ParseLine(tempstring);
catch
	errorstring = sprintf('Error Reading Elements from Background File %s',file);
	error(errorstring);
end
try 
	tempstring = fgetl(FID);
	tempstring = fgetl(FID);
catch
	errorstring = sprintf('Error in seperation between Master Elements and Master Minerals in Background file %s',file);
	error(errorstring);
end
try 
	Minerals = ParseLine(tempstring);
catch
	errorstring = sprintf('Error Reading Minerals from Background File %s',file);
	error(errorstring);
end
	keeptrack = zeros(1,length(Minerals));
try
	tempstring = fgetl(FID);
	tempstring = fgetl(FID);
catch
	errorstring = sprintf('Error seperation between Master Minerals and Cation Data in Background file %s',file);
	error(errorstring);
end
try 
	GfElement = ParseLine(tempstring);
catch
	errorstring = sprintf('Error Reading Cation Information Labels from Background File %s',file);
	error(errorstring);
end

try 
	tempstring = fgetl(FID);
	GramFormulaList = sscanf(tempstring,'%f',length(GfElement));
catch
	errorstring = sprintf('Error Reading Molecular Weights from Background File %s',file);
	error(errorstring);
end

try 
	tempstring = fgetl(FID);
	CationsList = sscanf(tempstring,'%f',length(GfElement));
catch
	errorstring = sprintf('Error Reading Cations per Oxide from Background File %s',file);
	error(errorstring);
end


try 
	tempstring = fgetl(FID);
	ChargeList = sscanf(tempstring,'%f',length(GfElement));
catch
	errorstring = sprintf('Error Reading Cation charges from Background File %s',file);
	error(errorstring);
end

try
	tempstring = fgetl(FID);
	tempstring = fgetl(FID);
catch
	errorstring = sprintf('Error reading seperation between Cation Data and Master Exchange in Background file %s',file);
	error(errorstring);
end

	
try	
	AdditiveExchangeVector = ParseLine(tempstring);

	AdditiveExchange = length(AdditiveExchangeVector);
	AdditiveComponentsMaster = 0;
	for k = 1:AdditiveExchange
		temp = length(AdditiveExchangeVector{k});
		if temp > 3
			if strncmp('Add',AdditiveExchangeVector{k}(end-2:end),3)
				AdditiveComponentsMaster = AdditiveComponentsMaster + 1;
			end
		end
	end
catch
	errorstring = sprintf('Error Reading Master Additive Excchange Components from Background File %s',file);
	error(errorstring);
end

ExchangeComponentsMaster = AdditiveExchange-AdditiveComponentsMaster;

try 
	tempstring = fgetl(FID);

	counter = 1;
	if strncmp(sscanf(tempstring,'%s',1),'*',1)
		test = 1;
	else
		test = 0;
		AMaster = zeros(1,AdditiveExchange);
		CationAdditiveVector = cell(0);
	end

	while( test == 0)
		[temp,crap,crap,nextindex] = sscanf(tempstring,'%d',AdditiveExchange);
		AMaster(counter,:) = temp';
		CationAdditiveVector(counter) = {sscanf(tempstring(nextindex:end),'%s')};
		tempstring = fgetl(FID);
		counter = counter  + 1;
		if strncmp(sscanf(tempstring,'%s',1),'*',1)
			test = 1;
		end
	end

catch
	errorstring = sprintf('Error Reading Master Additive Excchange Matrix from Background File %s',file);
	error(errorstring);
end

CationComponentsMaster = counter-AdditiveComponentsMaster-1;
outstruct = struct(...
	'AMaster',{AMaster},...
	'CationAdditiveVector',{CationAdditiveVector},...
	'AdditiveExchangeVector',{AdditiveExchangeVector},...
	'Elements',{Elements},...
	'Minerals',{Minerals},...
	'GfElement',{GfElement},...
	'GramFormulaList',{GramFormulaList},...
	'CationsList',{CationsList},...
	'ChargeList',{ChargeList});

test = 0;
while( test == 0)
	try
		tempstring = fgetl(FID);
		MineralName = sscanf(tempstring,'%s');
		disp(sprintf('Reading Info for %s\n',MineralName)); 
	catch
		errorstring = sprintf('Error Reading Mineral Name from Background File %s',file);
		error(errorstring);
	end

	try 
		tempstring = fgetl(FID);
		CationComponents = sscanf(tempstring,'%f',1);
	catch
		errorstring = sprintf('Error Reading Number of Cation components from Background File %s',file);
		error(errorstring);
	end
	try
		tempstring = fgetl(FID);
		AdditiveComponents = sscanf(tempstring,'%f',1);
	catch
		errorstring = sprintf('Error Reading Number of Additive components from Background File %s',file);
		error(errorstring);
	end
	try
		tempstring = fgetl(FID);
		ExchangeComponents = sscanf(tempstring,'%f',1);
	catch
		errorstring = sprintf('Error Reading Number of Exchange components from Background File %s',file);
		error(errorstring);
	end
	try
		tempstring = fgetl(FID);
		EndMembers = sscanf(tempstring,'%f',1);
	catch
		errorstring = sprintf('Error Reading Number of EndMember components from Background File %s',file);
		error(errorstring);
	end
	try
		tempstring = fgetl(FID);
		IronOxidation = sscanf(tempstring,'%f',1);
	catch
		errorstring = sprintf('Error Reading Iron Oxidation from Background File %s',file);
		error(errorstring);
	end
	try
		tempstring = fgetl(FID);
		NumberOxygen = sscanf(tempstring,'%f',1);
	catch
		errorstring = sprintf('Error Reading Number Oxygen (Probe) from Background File %s',file);
		error(errorstring);
	end
	try
		tempstring = fgetl(FID);
		NumberOxygenHyd = sscanf(tempstring,'%f',1);
	catch
		errorstring = sprintf('Error Reading Number Oxygen (Hydrous) from Background File %s',file);
		error(errorstring);
	end

	A = zeros(CationComponents+AdditiveComponents,AdditiveComponents+ExchangeComponents);
	order = cell(1,CationComponents+AdditiveComponents);
	D = zeros(1,CationComponents+AdditiveComponents);
	C = cell(2,AdditiveComponents);
	EndMemberVector = cell(1,EndMembers);
	ThisAdditiveExchangeVector = cell(1,AdditiveComponents+ExchangeComponents);
	EndBase = zeros(1,EndMembers);
	EndMemberConversion = zeros(AdditiveComponents+ExchangeComponents,EndMembers);
	EndMemberCations = zeros(EndMembers,CationComponents);

	tempstring = ClearComment(tempstring,FID);
	try
		ThisAdditiveExchangeVector = ParseLine(tempstring);
	catch
		errorstring = sprintf('Error Reading Mineral Additive/Exchange Vector from Background File %s',file);
		error(errorstring);
	end
	try
		tempstring = fgetl(FID);
    		upperlimit  = sscanf(tempstring,'%f',AdditiveComponents+ExchangeComponents)';
	catch
		errorstring = sprintf('Error Reading Mineral Upper Limit from Background File %s',file);
		error(errorstring);
	end
	try
    		tempstring = fgetl(FID);
    		lowerlimit  = sscanf(tempstring,'%f',AdditiveComponents+ExchangeComponents)';
	catch
		errorstring = sprintf('Error Reading Mineral Lower Limit from Background File %s',file);
		error(errorstring);
	end

	try
		tempstring = ClearComment(tempstring,FID);
		ThisCationVector = ParseLine(tempstring);
	catch
		errorstring = sprintf('Error Reading Cation Vector in file  %s',file);
		error(errorstring);
	end

	try
		tempstring = fgetl(FID);
		D = sscanf(tempstring,'%f',AdditiveComponents+CationComponents);
	catch
		errorstring = sprintf('Error Reading Weight Vector in file %s',file);
		error(errorstring);
	end
	try
		tempstring = fgetl(FID);
		C = sscanf(tempstring,'%f',AdditiveComponents);
		if length(C) ~= AdditiveComponents
			errorstring = sprintf('Error Reading Additive Value Vector in file %s',file);
			error(errorstring);
		end
		temp = ThisCationVector(end-AdditiveComponents+1:end);
		C = [temp; double2cell(C')];
	catch
		errorstring = sprintf('Error Reading Additive Value Vector in file %s',file);
		error(errorstring);
	end
	
	try
		tempstring = ClearComment(tempstring,FID);
		EndMemberVector = ParseLine(tempstring);  
	catch
		errorstring = sprintf('Error Reading EndMember Vector in file%s',file);
		error(errorstring);
	end
	try  
		tempstring = fgetl(FID);
		EndBase  = sscanf(tempstring,'%f',EndMembers)';
	catch
		errorstring = sprintf('Error Reading EndMember base Vector in file %s',file);
		error(errorstring);
	end

	try
		for k = 1:AdditiveComponents+ExchangeComponents
 			tempstring = fgetl(FID);
    			EndMemberConversion(k,:) = sscanf(tempstring,'%f',EndMembers)';
		end
	catch
		errorstring = sprintf('Error Reading EndMember Conversion Matrix in file  %s',file);
		error(errorstring);
	end

	try 
		volume = [];
		tempstring = ClearComment(tempstring,FID);
		for k = 1:EndMembers
					temp = sscanf(tempstring,'%f',CationComponents+1);
					EndMemberCations(k,:) = temp(1:CationComponents)';  %The endmembers defined as cations
					volume(k) = temp(end);
					tempstring = fgetl(FID);
		end
	catch
		errorstring = sprintf('Error Reading EndMember Cation Matrix in file  %s',file);
		error(errorstring);
	end
	if tempstring ==  -1
		test = 1;
	else
		if strncmp(sscanf(tempstring,'%s',1),'*',1)
			test = 0;
		else
		test = 1;
		end
	end

	A = zeros(CationComponents+AdditiveComponents,AdditiveComponents+ExchangeComponents);

	try
		columns = GetColumnsGeneral(AdditiveExchangeVector,ThisAdditiveExchangeVector);
		columns = [columns{2,:}];
	catch
		temp = GetColumnsGeneral3(AdditiveExchangeVector,ThisAdditiveExchangeVector);
		whichbad = find([temp{2,:}] == -1);
		bad = temp(1,whichbad);
		errorstring = [];
		for k = 1:length(bad)
			errorstring = [errorstring ' ' [bad{k}]]; 
		end
		errorstring = ['The Additive/Exchange Components ' errorstring ' are not found in the master data of file ' sprintf('%s',file)];
		error(errorstring);

	end
		
	try
		rows = GetColumnsGeneral(CationAdditiveVector,ThisCationVector);
		rows = [rows{2,:}];
	catch
		temp = GetColumnsGeneral3(CationAdditiveVector,ThisCationVector);
		whichbad = find([temp{2,:}] == -1);
		bad = temp(1,whichbad);
		errorstring = [];
		for k = 1:length(bad)
			errorstring = [errorstring ' ' [bad{k}]]; 
		end
		errorstring = ['The Cation/Additive Components ' errorstring ' are not found in the master data of file ' sprintf('%s',file)];
		error(errorstring);

	end

	Atemp = AMaster(:,columns);
	Atemp = Atemp(rows,:);

	minoutstruct = struct(...
	'MineralName',{MineralName},...
	'A',{Atemp},... 
	'D',{D},...
	'order',{ThisCationVector},...
	'upperlimit',{upperlimit},...
	'lowerlimit',{lowerlimit},...
	'AdditiveExchangeVector',{ThisAdditiveExchangeVector},...
	'EndMemberVector',{EndMemberVector},...
	'EndBase',{EndBase},...
	'EndMemberConversion',{EndMemberConversion},... 
	'C',{C},...
	'NumberEndMembers',{EndMembers},...
	'NumberCationComponents',{CationComponents},...
	'NumberExchangeComponents',{ExchangeComponents},...
	'NumberAdditiveComponents',{AdditiveComponents},...
	'IronOxidation',{IronOxidation},...
	'EndMemberCations',{EndMemberCations},...
	'NumberOxygen',{NumberOxygen},...
	'NumberOxygenHyd',{NumberOxygenHyd});

	index = GetColumnsGeneral3(Minerals,{MineralName});
	index = index{2,1};
	if index ~= -1
		keeptrack(index) = 1;
		mineralstruct(index) = minoutstruct;
	end

end

bad = find(keeptrack == 0);
if ~isempty(bad)
	errorstring = [];
	for k = 1:length(bad)
		errorstring = [errorstring ' ' [Minerals{bad(k)}]]; 
	end
	errorstring = ['Data for the requested mineral(s) ' errorstring ' can not not be found in background file ' sprintf('%s',file)];
	error(errorstring); 
end

fclose(FID);




