function out = cations2weights(instruct,input,factor,order)
%out = cations2weights(instruct,input,factor)
%This code converts cation components to oxide weight components 
%INPUT
%instruct = the output of prepweights2cation.m, a structure that contains neccessary information
%input = the input cations (double vector)
%factor = a strecthing factor that controls the final weight total
%OUTPUT
%out = The output weights (in the same order as input, double vector)
%
%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

oxygen = instruct.Oxygens;
gramformula = instruct.gramformula;
cations = instruct.cation;
charges = instruct.charges;

molepropoxides = input./cations;
weightpropoxides = molepropoxides.*gramformula;
warning off
out = weightpropoxides/(sum(weightpropoxides)/factor);

if nargin == 4  %only if called from within thefinder (for cases where there are unanalyzed elements)
	Hcol = GetColumnsGeneral3(order,{'H'});

	if Hcol{2,1} ~= -1
		Hcol = order{2,Hcol{2,1}};
		Hvalue = out(Hcol);
		out = weightpropoxides/(sum(weightpropoxides)/(factor+Hvalue));
	end
end
warning on
temp = find(isnan(out));
out(temp) = 0;
