function htt = makett(hij, Internal)

% MAKETT - projects metric perturbation into TT gauge
%
% htt = makett(hij, Internal);
%
% hij       metric perturbation for gravitational wave source in 3x3xtime
%           format
% Internal  struct describing source orientation to line of sight
%   .x      cos angle with respect to source Z-axis
%   .phi    azimuth angle from source x axis
%
% htt       metric perturbation projected into TT gauge in 2xtime format
%       htt(1,:) - plus polarization
%       htt(2,:) - cross polarization
%   q.v. LIGO-T040020-01-Z for + and x conventions
%
% SEE ALSO: GRAVEN

% $Id: makett.m,v 1.10 2004/05/12 19:59:10 stuver Exp $

% INITIALIZE output variable
% CHECK inputs for errors
% DEFINE angles
% DEFINE rotation matrix
% DEFINE plus and cross bases
% PROJECT metric pertubation onto bases
% RETURN pluss and cross polarization timeseries


% Initalize output variable
htt = [];

% Check for errors
errorcheck(hij, Internal);

% Define angles from structure
cosTheta = Internal.x;
sinTheta = sqrt(1-cosTheta^2);
phi = Internal.phi;

% Rotate coordinates
R = [cos(phi)*cosTheta, sin(phi)*cosTheta, -sinTheta; ...
	  -sin(phi), cos(phi), 0; ...
	  cos(phi)*sinTheta, sin(phi)*sinTheta, cosTheta];

% Form the plus and cross bases
% R(1,:) = x basis
% R(2,:) = y basis
eplus = R(1,:)'*R(1,:)-R(2,:)'*R(2,:);
ecross = R(1,:)'*R(2,:)+R(2,:)'*R(1,:);

% For each timestep, project the metric perturbation onto the plus and
% cross basis
% h+ = 0.5*(h^{ij}eplus_{ij})
% hx = 0.5*(h^{ij}ecross_{ij})
% * N.B. Multiply h^{ij}e_{ij} by 0.5 since dot(e_{ij},e_{ij}) = 2
for k = 1:size(hij,3)
    htt(1,k) = 0.5*sum(sum(hij(:,:,k).*eplus));
    htt(2,k) = 0.5*sum(sum(hij(:,:,k).*ecross));
end

return

% ~~~ END MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function errorcheck(hij, Internal)

% ERRORCHECK - checks makeh's input data for errors and stops the code if
%              an error is found
%
% errorcheck(hij, Internal)
%
% hij       metric perturbation for gravitational wave source
% Internal  struct describing location of detector on source sky
%   .x      cos angle with respect to source Z-axis
%   .phi    azimuth angle from source x axis

% DETERMINE size of hij
% CHECK all inputs for errors

% Determine size of hij
checkSizeHij = size(hij);

if checkSizeHij(1) ~= 3 & checkSizeHij(2) ~= 3
    % hij must be in 3x3xtime form
    checkSizeHij
    error('makeh: hij is not in 3x3xtime form')
end

% Internal must be a structure
if ~strcmp(class(Internal), 'struct')
    Internal
    error('makett: Internal must be a structure')
end

if ~isfield(Internal, 'x')
    % Internal must contain the field x
    Internal
    error('makeh: The Internal struct must contain the field x')
end

if ~isfield(Internal, 'phi')
    % Internal must contain the field phi
    Internal
    error('makeh: The Internal struct must contain the field phi')
end

if abs(Internal.x) > 1 | ~isreal(Internal.x)
    % If Internal.x exists, it must have a value between -1 and 1
    Internal.x
    error('makeh: Internal.x must be real and between -1 and 1')
end

if Internal.phi < -pi | Internal.phi > pi | ~isreal(Internal.phi)
    % If Internal.phi exits, it must be real and have a value between
    % -pi and pi
    Internal.phi
    error(['makeh: Internal.phi must be real and betweeen -pi '...
            'and pi'])
end

return