function [datafile,backgroundfile,ErrorPerfectWeight,maxerror,PhaseCheck]= InRead
%function [datafile,backgroundfile,ErrorPerfectWeight,maxerror]= InRead
%InRead reads the basic data required to run APHID from AphidInput.txt 
%(locatad in MATLAB's) current working directory.
%
%OUTPUT
%datafile = the name of the .csv file which contains the compositonal
%data for phase classification
%backgroundfile = the name of the stoichiometric model file (.txt)
%that contains the the transformation data
%ErrorPerfectWeight = a booleen variable that equals 1 if APHID
%should calculate error based on 'Total' and zero if APHID should 
%calculate error based on 'Ratio'
%maxerror = in percent, the threshold value which seperates classified 
%from unclassified results
%phasecheck = Rather or not the check the calculated phase classification
%with the column "MineralName" in the input data (1 for check)
%
%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 17, 2003

	FID = fopen('APHIDInput.txt','r'); %Opening file
	if FID == -1 %if file can not be opened
		error('There must be a file called AphidInput.txt in the same directory as APHID')
	end
	string = GetNextDataLine(FID); %Reading data file line
	if ischar(string)
		datafile = string;
		disp(sprintf('Data File = %s',datafile)); 
	else
		error('Error reading the datafile from AphidInput.txt')
	end

	string = GetNextDataLine(FID); %Reading Mineral Model file line
	if ischar(string)
		backgroundfile = string;
		disp(sprintf('Background File = %s',backgroundfile)); 
	else
		error('Error reading the background file from AphidInput.txt')
	end

	string = GetNextDataLine(FID); %Reading Error Calculation method line
	if ischar(string)
		if strcmp('Total',string)
			ErrorPerfectWeight = 1;
			disp('Error type is total');
		elseif strcmp('Ratio',string)
			ErrorPerfectWeight = 0;
			disp('Error type is ratio');
		else
			error('The Error Type in the AphidInput.txt file must read either "Total" or ratio');
		end
	else
		error('Error reading the error type from AphidInput.txt')
	end

	string = GetNextDataLine(FID);  %Reading Error threshold line
	if ischar(string)
		maxerror = str2double(string);
		disp(sprintf('Error Theshold is %2.1f percent',maxerror)); 
	else
		error('Error reading the Error Threshold from AphidInput.txt')
	end

	string = GetNextDataLine(FID);  %Reading PhaseCheck line
	if ischar(string)
		PhaseCheck = str2double(string);
		if PhaseCheck == 1
			disp('PhaseCheck is on');
		else
			disp('PhaseChech if off');
		end
	else
		error('Error reading PhaseCheck from AphidInput.txt')
	end
