function outcell= GetBulkComposition(Data,Modal,MineralList,controlstruct,elements)
%outcell = GetBulkComposition(Data,Modal,EndMembers,MineralList,controlstruct,elements)
%The purpose of this function is to getermine the bulk composition of the entire set
%of analysis.  In addition this code determines the perfect percentage oxide 
%weight for all elements (including unanalyzed components such as H2O) and saves this information 
%over the inputs of the weights in Data.
%GetBulkComposition sends this information to the screen and also saves this information
%to output directory as "Bulk.csv"
%
%INPUT
%Data = Data is the output of thefinder.m.  This is the raw data for all of the analysis (cell array)
%Modal = Model is the output of ModalAnalysis.m.  This is the summary of the the raw data (cell array)
%EndMembers = This cell array is the output of CreateEndmembers.m and contains all the information
%about the weights (percentage oxide) and molecular of all the endmembers 
%MineralList = This structure contains the informatain about the order of data in Data
%controlstruct = the output of createinputstruct and contains all options information about the ru
%elements = a cell array of strings that contains the names of the elements considered in this
%model run
%
%OUTPUT
%outcell = A cell array that contains the bulk composition and element partitioning information
%This is the same cell displayed on screen and saved to the .csv file
%
%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 17, 2003

%loading background info 
WeightPerfectPosition = MineralList(1).WeightPerfectLocation;
NumberDifMinerals = length(MineralList);
Weights = zeros(NumberDifMinerals,length(elements));
    
for k = 1:NumberDifMinerals  %the main loop
	MineralNameList(k) = {MineralList(k).MineralName}; 
	TheMineral = {MineralList(k).MineralName};
	modalrow = GetColumnsGeneral(Modal(:,1)',TheMineral);
	modalrow = modalrow{2,1};
	numbermineral = Modal{modalrow,2};
	list = [Modal{modalrow,4}];
	Perfectlocation = MineralList(k).WeightPerfectLocation;
	Perfectorder = MineralList(k).WeightPerfectOrder;
	ProportionLocation  = MineralList(k).EndMemberLocation;
	DensityVec = MineralList(k).density;
	for m = 1:length(Perfectorder)
		if strncmp(Perfectorder{m},'Fe',2)
			Perfectorder(m) = {'Fe'};
		end
	end
            
	Sendto = GetColumnsGeneral(elements,Perfectorder);
	Sendto = [Sendto{2,:}];
        
	counter = 0;
 	for m = 1:numbermineral
		Density = DensityVec*Data{list(m)}{ProportionLocation}';
 		Weights(k,Sendto) = Weights(k,Sendto) + Data{list(m)}{Perfectlocation}*Density;
 		counter = counter + 1;
	end
end

sumweights = sum(Weights);  %the bulk composition is actually computed by adding the phase
if length(sumweights) > 1    %compositions together
	bulkcomp =  sumweights./sum(sumweights);
else
	bulkcomp = Weights/sumweights;
end
    
warning off
for k = 1:NumberDifMinerals
	Weights(k,:) = Weights(k,:)./sumweights;
end
warning on
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
disp('Bulk Composition');
outcell = [elements {''}; double2cell(bulkcomp*100) {'Bulk Composition'} ; ...
double2cell(Weights*100) MineralNameList'];
disp(outcell)
outfile = [controlstruct.savefile filesep 'Bulk.csv'];
displaystring = sprintf('Bulk composition information saved at %s\n\n',outfile);
disp(displaystring);
writecsv(outfile,outcell)
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');    