% measure_bunch_tunes - Acquire multiple measurements, average, fit
%
% Copyright (c) 2009 Dimtel, Inc., All Rights Reserved
%
% $Id: measure_bunch_tunes.m,v 1.1.1.1 2010/10/05 16:43:02 dim Exp $
% $Date: 2010/10/05 16:43:02 $
% $Author: dim $
% $Revision: 1.1.1.1 $
% $Log: measure_bunch_tunes.m,v $
% Revision 1.1.1.1  2010/10/05 16:43:02  dim
% Initial import of iGp12
%
%

clear;
acq.sys = input('iGp device name [TEST]: ', 's');
acq.count = input('Data sets to acquire [6]: ');
if (isempty(acq.count))
  acq.count=6;
end

sys = acq.sys;

% Save existing settings, set up acquisition conditions
acq.nm{1} = ['IGPF:', sys, ':ACQ_EN'];
acq.nm{2} = ['IGPF:', sys, ':ACQ_SINGLE'];
acq.nm{3} = ['IGPF:', sys, ':RECLEN'];
acq.nm = acq.nm(:);

acq.vals = lcaGet(acq.nm, 1, 'char');

lcaPut(acq.nm{1}, '0');
lcaPut(acq.nm(2:3), {'1'; '8388608'});

for acq_cnt=1:acq.count
  ch = ['IGPF:', sys, ':ACQ_EN'];
  lcaPut(ch, 1);
  while (strcmp(lcaGet(ch), 'OFF') == 0)
    pause(2);
  end
  iGp_read
  acq.dirs(acq_cnt, :) = prm.tm;
end
clear sys acq_cnt;

% Restore conditions
lcaPut(acq.nm(end:-1:1), acq.vals(end:-1:1));

dirs = acq.dirs;

% Save the re-analysis script
cd(dirs(1,:));
save dirs acq;
fid = fopen('replot_bunch_tunes.m', 'w');
fprintf(fid, 'clear;\nload dirs;\ndirs=acq.dirs;\n[Md, Nd] = size(dirs);\n');
fprintf(fid, 'cd(''..'');\nproc_bunch_tunes;\ncd(dirs(1,:));\n');
fclose(fid);
cd('..');

[Md, Nd] = size(dirs);

% Do the processing
proc_bunch_tunes;
