%
% Program proj_sympws.m extracts the projection matrices from SYMPWS binary
% file output.
%
% Version 1.0, 2006-08-06
%
in_proj = fopen('D4_6h_proj','r');
out_proj = fopen('sympws_proj_D4_6h','a');
no_wave_vectors = fread(in_proj,1,'int8');
fwrite(out_proj, no_wave_vectors, 'int8');
wave_vector = 0;
while wave_vector < no_wave_vectors
  wave_vector = wave_vector + 1;
  itotal = fread(in_proj,1,'int8');
  ndi = fread(in_proj,1,'int8');
  for I = 1:itotal
    for II = 1:ndi
      for III = 1:2
        tmatridel(III) = fread(in_proj,1,'float64');
      end
      tmat(II,I) = tmatridel(1) + i*tmatridel(2);
    end
  end
  col = itotal;
  while col < ndi
    itotal = fread(in_proj,1,'int8');
    ndi = fread(in_proj,1,'int8');
    for I = 1:itotal
      for II = 1:ndi
        for III = 1:2
          tmatridel(III) = fread(in_proj,1,'float64');
        end
        tmat(II,col+I) = tmatridel(1) + i*tmatridel(2);
      end 
    end
    col = col + itotal;
  end
  tmatriorg(1,1:ndi,1:ndi) = real(tmat(1:ndi,1:ndi));
  tmatriorg(2,1:ndi,1:ndi) = imag(tmat(1:ndi,1:ndi));
  fwrite(out_proj, ndi, 'int16');
  fwrite(out_proj, tmatriorg(1:2,1:ndi,1:ndi), 'float64');
end
fclose(in_proj);
fclose(out_proj);
fprintf('proj_sympws has finished executing');