function [R]  = buildrotationmatrix2(xprimetrend, xprimeplunge, yprimetrend,yprimeplunge,zprimetrend,zprimeplunge, talkandplot)
%This takes the orientation of the original coordinate system as trends and
%plunges and 
%computes the direction cosines of the final coordinate system and then
%provides the rotation matrix of the form:
%     l l' l''
% R = m m' m''
%     n n' n"
%assumes the angles are in degrees
%assume that you want to rotate to northsouth
X = [1 0 0];
Y = [0 1 0];
Z = [0 0 1];



[xprime(1), xprime(2) xprime(3)] =plunge_trend_to_dir_cosines(xprimeplunge,xprimetrend);
[yprime(1), yprime(2) yprime(3)] =plunge_trend_to_dir_cosines(yprimeplunge,yprimetrend);
[zprime(1), zprime(2) zprime(3)] =plunge_trend_to_dir_cosines(zprimeplunge,zprimetrend);
%check to make sure you put them in right and they are orthogonal:
checkxy = dot(xprime,yprime);
checkxz = dot(xprime,zprime);
checkyz = dot(yprime,zprime);
if talkandplot==1
fprintf(1,'xprime l = %3.4f m = %3.4f n = %3.4f\n', xprime(1), xprime(2), xprime(3))
fprintf(1,'yprime l = %3.4f m = %3.4f n = %3.4f\n', yprime(1), yprime(2), yprime(3))
fprintf(1,'zprime l = %3.4f m = %3.4f n = %3.4f\n', zprime(1), zprime(2), zprime(3))
fprintf(1,'checks for orthogonality: xy %3.4f xz %3.4f yz %3.4f\n', checkxy, checkxz, checkyz)
primitive1;
text(0,1, 'N', 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom')
plotdiamond(xprimeplunge,xprimetrend);
plotdiamond(yprimeplunge,yprimetrend);
plotdiamond(zprimeplunge,zprimetrend);
    %plot final coordinate system
    [plunge, trend] = dir_cosines_to_plunge_trend(X(1), X(2), X(3));
    plotpoint(plunge,trend);
    [plunge, trend] = dir_cosines_to_plunge_trend(Y(1), Y(2), Y(3));
    plotpoint(plunge,trend);
    [plunge, trend] = dir_cosines_to_plunge_trend(Z(1), Z(2), Z(3));
    plotpoint(plunge,trend);
end

R(1,1) = X*xprime';
R(1,2) = X*yprime';
R(1,3) = X*zprime';
R(2,1) = Y*xprime';
R(2,2) = Y*yprime';
R(2,3) = Y*zprime';
R(3,1) = Z*xprime';
R(3,2) = Z*yprime';
R(3,3) = Z*zprime';




    