function a = qtr2ann(q,s)
% QTR2ANN   reads in data at a quarterly frequency and annualizes it.
%           a = qtr2ann(q,s) assumes that the first column of matrix 
%           q is in a form 1990.3 where 1990 is the year and 3 is the 
%           quarter of the first observation.  If s=0, the annual
%           numbers are averages of quarterly numbers. If s=1, the
%           annual number is the 4th quarter of each year.
%           a = qtr2ann(q) uses s=0 as the default.
%              

%           Ellen McGrattan, 6-1-01

if nargin<2;
  s = 0;
end;

[m,n] = size(q);
yr0   = floor(q(1,1));
yr1   = floor(q(m,1));
qtr0  = round((q(1,1)-yr0)*10);
qtr1  = round((q(m,1)-yr1)*10);
if s==0;
  nyr            = yr1-yr0+1;
  a              = zeros(nyr,n-1);
  a(:,1)         = [yr0:yr1]';
  if qtr0 < 4;
    a(1,2:n)     = mean(q(1:5-qtr0,2:n));
  else
    a(1,2:n)     = q(1,2:n);
  end;
  if qtr1 > 1;
    a(nyr,2:n)   = mean(q(m-qtr1+1:m,2:n));
  else
    a(nyr,2:n)   = q(m,2:n);
  end;
  q              = q(6-qtr0:m-qtr1,:);
  for j=2:n; 
    z            = zeros(4,nyr-2);
    z(:)         = q(1:4*(nyr-2),j);
    a(2:nyr-1,j) = mean(z)';
  end;
else
  if qtr1<4;
    nyr          = yr1-yr0;
    yr1          = yr1-1;
  else
    nyr          = yr1-yr0+1;
  end;
  a              = zeros(nyr,n-1);
  a(:,1)         = [yr0:yr1]';
  a(1,2:n)       = q(5-qtr0,2:n);
  q              = q(6-qtr0:m,:);
  a(2:nyr,2:n)   = q(4:4:(nyr-1)*4,2:n);
end;


