%io_writelcmraw.m
%Jamie Near, McGill University 2014.
%
% USAGE:
% RF=io_writelcmraw(data_struct,outfile,metab);
% 
% DESCRIPTION:
% Take a simulated metabolite basis spectrum in matlab structure format, and
% output it into LCModel RAW format to be used in an LCModel basis spectrum.
% 
% INPUTS:
% data_struct    = simulated metabolite basis spectrum in matlab structure
%                   format.
% outfile        = name of the output .RAW file.
% metab          = Abbreviated name of the metabolite (ie. 'Cr', 'Glu',
%                   etc.)
%
% OUTPUTS:
% RF            = Same as input.  Not used.  The primary output of this
%                   function is a text file in LCModel raw format.  
    
function RF=io_writelcmraw(data_struct,outfile,metab);
%function RF=writelcmraw(data_struct,outfile);
idx  = find(strcmp(data_struct.name,metab));
'Using Osprey version'

expname=[data_struct.seq{1} num2str(data_struct.te) '_' data_struct.name{idx} 'Sim'];
comment=['Simulated LCModel basis set for ' data_struct.name{idx} ' ' data_struct.seq{1}...
    ' experiment with echo time ' num2str(data_struct.te) ' ms'];

t=repmat(data_struct.t',[1 data_struct.sz(2:end,1)]);
hzpppm = data_struct.Bo*42.577;
f = (4.65-data_struct.centerFreq)*hzpppm;
fids = data_struct.fids(:,idx);
fids=fids.*exp(-1i*t*f*2*pi);

%Take the complex conjugate becuase the sense of rotation in LCModel seems to
%be opposite to that used in FID-A.

RF=zeros(length(fids(:)),2);
RF(:,1)=real(fids(:));
RF(:,2)=-imag(fids(:));



%write to txt file for jmrui
fid=fopen(outfile,'w+');
fprintf(fid,' This RAW file was created using the MATLAB spectral');
fprintf(fid,'\n simulation program developed by Robin Simpson');
fprintf(fid,'\n and Jamie Near, FMRIB 2010');
fprintf(fid,'\n\n Experiment Name : %s' ,expname);
fprintf(fid,'\n Comment : %s',comment);
fprintf(fid,'\n\n User defined parameters:');
fprintf(fid,'\n\n Sweep Width = %1.4f Hz',data_struct.spectralwidth);
fprintf(fid,'\n Vector Size = %i points',data_struct.n);
fprintf(fid,'\n Apodization = %1.4f Hz',data_struct.linewidth);
fprintf(fid,'\n B0 Field    = %1.4f T',data_struct.Bo);
fprintf(fid,'\n\n $NMID ID=''%s''',data_struct.name{idx});
fprintf(fid,'\n FMTDAT=''(2E16.6)''');
fprintf(fid,'\n VOLUME=1.00000');
fprintf(fid,'\n TRAMP=1.00000');
fprintf(fid,'\n $END');
fprintf(fid,'\n  % 1.6E  % 1.6E',RF'); %space after "%" sign forces hanging negative sign
fprintf(fid,'\n');
fclose(fid);
