function pram_gen

% Generate PAF and tlan files for 'pram' experiment
% Modified from original one in v.5 to remove .fil generation
% and for single pulse, and heater on using DDS RAM UPDt 
% for power stepping    MTR 20190721

%--- Common/default parameters ---

par.dt=2.2; % DDS parameter time step (microseconds)
par.pstart=800; % pulse start time in tlan file (microseconds)
par.ipp=10000; % inter-pulse period (microseconds)
par.rxp_predelay=96; % time before pulse to turn rx protector on (microseconds)
par.rxp_postdelay=30; % time after pulse to turn rx protector off (microseconds)
par.radar_txs='11&12'; % the transmitter pair used for radar
par.heating_txs={'1&2','3&4','5&6','7&8','9&10'}; % transmitter pairs used for heating
%par.t_dump=4800000; % dump time (microseconds) NB: MUST match .elan file!
heatontime=48;  % seconds the heater is on

%--- Version-specific parameters ---

% code_A=[1  1  1  1  1 -1  1 -1 -1  1 -1 -1  1  1 -1 -1 -1  1 -1  1];
% code_B=[1  1  1  1  1 -1  1 -1 -1  1  1  1 -1 -1  1  1  1 -1  1 -1];

   code_A=[1];
   code_B=[1];

for par.blen=[10] % blen is baud length (microseconds)

  switch par.blen
    case 2
      par.dt=par.blen; % DDS parameter time step (microseconds)
      par.rx_dt=par.blen/2; % receiver sampling interval (microseconds)
      par.rx_start=-5*par.rx_dt; % time after start of pulse
                                 % to start sampling (microseconds)
      par.rx_nsamps=1125; % number of samples

      par.codes=[code_A code_B code_A -code_B; ...
                 code_A code_B -code_A code_B];

    case 5
      % NB: EISCAT Rx sampling is 1 sample/baud here
      par.dt=2.5; % DDS parameter time step (microseconds)
      par.rx_dt=5; % receiver sampling interval (microseconds)
      par.rx_start=-5*par.rx_dt; % time after start of pulse
                                 % to start sampling (microseconds)
      par.rx_nsamps=225; % number of samples

      par.codes=[code_A code_B; code_A -code_B];

    case 10
 %     par.rx_dt=5; % receiver sampling interval (microseconds)
 %     par.rx_start=-5*par.rx_dt; % time after start of pulse
                                 % to start sampling (microseconds)
 %     par.rx_nsamps=225; % number of samples

%      par.codes=[code_A; code_B];
      par.codes=[code_A];

    otherwise
      error('Unsupported baud length');
  end



  % Generate the files
  pram_tlan_gen_files(par);

end


function pram_tlan_gen_files(P)

% Derived parameters

codelen=size(P.codes,2); % number of bits in each code
plen=(codelen+1)*P.blen; % pulse length (microseconds)
ncodes=size(P.codes,1);

% Calculate pulse envelopes

npoints=ceil(plen/P.dt);
t=P.dt*(0:npoints-1);

for k=1:ncodes
  env(k,:)=rcosphasecode(t,P.blen,P.codes(k,:));
end

figure
%plot(t,env(1,:),'bo-',t,env(2,:),'ro-');
plot(t,env(1,:),'bo-');

xlabel('Time \mu s');
ylabel('Amplitude');

%ca=conv(P.codes(1,:),fliplr(P.codes(1,:)));
%cb=conv(P.codes(2,:),fliplr(P.codes(2,:)));
ca=conv(env(1,:),fliplr(env(1,:)));
%cb=conv(env(2,:),fliplr(env(2,:)));
nca=length(ca);

figure
%plot(1:nca,ca,'r',1:nca,cb,'b',1:nca,ca+cb,'k');
plot(1:nca,ca,'r');

%--- Write out the amplitude/phase files ---

fid=fopen(['pram_' num2str(P.blen) '_noflip.paf'],'w');
if fid==-1
  error('cannot open PAF file');
end

% header
fprintf(fid,'PAFPAR_VS 3.0\n');

% repeat for each code
%for j=1:4*ncodes
for j=1:ncodes
  fprintf(fid, '%% Pulse %d\n',j);

  % first amplitude and phase
  if P.dt >= 2.2
    fprintf(fid,'%d A 0\n',(j-1)*(npoints+2)+1);
  end
  fprintf(fid,'%d P 0\n',(j-1)*(npoints+2)+1);

  % amplitude and phase for our pulse
  for k=1:npoints
    if P.dt >= 2.2
      fprintf(fid,'%d A %.6f\n',(j-1)*(npoints+2)+k+1,abs(env(mod(j-1,ncodes)+1,k)));
    end
    if mod(j-1,2*ncodes) < ncodes
      phs=180*(env(mod(j-1,ncodes)+1,k) < 0);
    else
      phs=180*(env(mod(j-1,ncodes)+1,k) >= 0); % invert phase for second pair of codes
    end
    fprintf(fid,'%d P %.0f\n',(j-1)*(npoints+2)+k+1,phs);
  end

  % final tx off amplitude/phase
  if P.dt >= 2.2
    fprintf(fid,'%d A 0\n',j*(npoints+2));
  end
  fprintf(fid,'%d P 0\n',j*(npoints+2));

end

fclose(fid);

% PAF file with inverted phase in second group of pulses
% for O/X switching.

fid=fopen(['pram_' num2str(P.blen) '_flip.paf'],'w');
if fid==-1
  error('cannot open PAF file');
end

% header
fprintf(fid,'PAFPAR_VS 3.0\n');

% repeat for each code
%for j=1:4*ncodes
for j=1:ncodes
  fprintf(fid, '%% Pulse %d\n',j);

  % first amplitude and phase
  if P.dt >= 2.2
    fprintf(fid,'%d A 0\n',(j-1)*(npoints+2)+1);
  end
  fprintf(fid,'%d P 0\n',(j-1)*(npoints+2)+1);

  % amplitude and phase for our pulse
  for k=1:npoints
    if P.dt >= 2.2
      fprintf(fid,'%d A %.6f\n',(j-1)*(npoints+2)+k+1,abs(env(mod(j-1,ncodes)+1,k)));
    end
    if mod(j-1,2*ncodes) < ncodes
      phs=180*(env(mod(j-1,ncodes)+1,k) < 0);
    else
      phs=180*(env(mod(j-1,ncodes)+1,k) >= 0); % invert phase for second pair of codes
    end

    if j > 2*ncodes
      phs=180-phs;
    end

    fprintf(fid,'%d P %.0f\n',(j-1)*(npoints+2)+k+1,phs);
  end

  % final tx off amplitude/phase
  if P.dt >= 2.2
    fprintf(fid,'%d A 0\n',j*(npoints+2));
  end
  fprintf(fid,'%d P 0\n',j*(npoints+2));

end

fclose(fid);

%--- Write out the tlan file ---

heat_mode={'noheat','heat','noheat_pson'};

for j=1:length(heat_mode)

    fname=['pram_' num2str(P.blen) '_' heat_mode{j} '.tlan'];

    fid=fopen(fname,'w');
    if fid==-1
    error('cannot open tlan file');
    end

    fprintf(fid,'SETTCR 0\n');
    
    % the next command determines if we're heating or not
    
    switch heat_mode{j}
        case {'noheat','noheat_pson'}
            fprintf(fid,'AT      0.4  STMCt%s,RFOFF*         %% Reset RAM addr counter, kicking-in  AMP(1)\n', ...
                    P.radar_txs);
        case 'heat'
            fprintf(fid,'AT      0.0  ');
            for n=1:length(P.heating_txs)-1
                fprintf(fid,'RFON%s,',P.heating_txs{n});
            end
            fprintf(fid,'RFON%s',P.heating_txs{end});
            fprintf(fid,'  %% Start (continue) heating\n');
            fprintf(fid,'AT      0.4  STMCt%s',P.radar_txs);
            fprintf(fid,'  %% Reset RAM addr counter\n');
    end
    
    % the next command decides whether power-saving is enabled
    
    if strcmp(heat_mode{j},'noheat_pson')
        fprintf(fid,'AT      0.4  ');
        for n=1:length(P.heating_txs)-1
            fprintf(fid,'PSAVON%s,',P.heating_txs{n});
        end
        fprintf(fid,'PSAVON%s  %% Turn on power saving\n',P.heating_txs{end});
    end
                
%    fprintf(fid,'AT      0.7  STFIR   %% Start FIRs\n\n');

    fprintf(fid,'AT      1.0  RFOFF%s,TXSYNC,RXSYNC  %% SOUSY switch RF off\n\n', ...
            P.radar_txs);

%    fprintf(fid,'AT      1.3  CHL1R2            %% Left channels (=ch1) uses A/D 1\n'); 
%    fprintf(fid,'                               %% right channels (=ch2) use A/D 2\n\n');  

%    for codenum=1:4*ncodes
    for codenum=1:ncodes

      fprintf(fid,'SETTCR %.1f\n',(codenum-1)*P.ipp);

      if codenum > 1
        fprintf(fid,'AT      %.1f  UPDt%s\n',0.4,P.radar_txs);
      end

      % the pulse
 
      fprintf(fid,'AT      %.1f  RXP1ON,RXP2ON        %% Receiver protection ON\n\n',P.pstart-P.rxp_predelay); 

%      fprintf(fid,'AT      %.1f  CH1,CH2  %% Start sampling\n', ...
%              P.pstart + P.rx_start);

      fprintf(fid,'AT      %.1f  RFON%s,NOFLP*        %% Open SOUSY switch\n', ...
              P.pstart - 1,P.radar_txs);
      for k=1:npoints
        fprintf(fid,'AT      %.1f  UPDt%s               %% Kick in AMP(%d)\n', ...
                P.pstart+(k-1)*P.dt,P.radar_txs,k+1);
      end
      fprintf(fid,'AT      %.1f  UPDt%s               %% Kick in final amplitude\n', ...
              P.pstart+npoints*P.dt,P.radar_txs);
      fprintf(fid,'AT      %.1f  RFOFF%s              %% Close SOUSY switch\n\n', ...
              P.pstart+npoints*P.dt+1,P.radar_txs);

      fprintf(fid,'AT      %.1f  RXP1OFF,RXP2OFF  %% Rx protection off\n\n', ...
              P.pstart+plen+P.rxp_postdelay);

%      fprintf(fid,'AT      %.1f CH*OFF	%% Stop sampling (%d samples at %.1f us/sample)\n\n',P.pstart+P.rx_start+P.rx_nsamps*P.rx_dt,P.rx_nsamps,P.rx_dt);

    end

    fprintf(fid,'SETTCR 0\n');

    % Buflip and rep
%    fprintf(fid,'AT      %.1f BUFLIP\n',4*ncodes*P.ipp-20);
%    fprintf(fid,'AT      %.1f STC\n',4*ncodes*P.ipp-10);
%    fprintf(fid,'AT      %.1f REP\n',4*ncodes*P.ipp);
    fprintf(fid,'AT      %.1f REP\n',ncodes*P.ipp);

    fclose(fid);

end