function cbu_write_fdt(bvals, bvecs, outpath)
% Write FSL FDT bvals and bvecs files from parameters
% FORMAT write_fdt_files(bvals, bvecs, outpath)
% 
% bvals   - Nx1 vector with diffusion gradient strength
%           (one per diffusion acquisition, N=no of acquisitions)
% bvecs   - Nx3 matrix with diffusioon gradient directions
%           
% outpath - path to write FDT bvals, bvecs text files
%           (defaults to current working directory)
% 
% If no input args, fetch bvals, bvecs using cbu_diffusion_params
% 
% Matthew Brett 30 April 2007
  
if nargin < 2
  [bvals, bvecs] = cbu_diffusion_params();
end
if nargin < 3
  outpath = pwd;
end

fid = fopen('bvals', 'wt');
if fid == -1
  error('Could not open bvals output file')
end
fprintf(fid, '   %e', bvals);
fprintf(fid, '\n');
fclose(fid);

fid = fopen('bvecs', 'wt');
if fid == -1
  error('Could not open bvecs output file')
end
% Implement nasty hack for inverted Y gradient directions
disp('Inverting Y gradient directions, I hope that is OK')
bvecs(:,2) = bvecs(:, 2)*-1
for D = 1:3
  fprintf(fid, '   %e', bvecs(:,D));
  fprintf(fid, '\n');
end
fclose(fid);
