
timestepf = '7332365109';
filnam=['c:\temp\bpx' sprintf('%s',timestepf) '.dat'];

cd ('crorbit');
  HCorrectors;
  HMonitors;
  nbpms = length(Monitor);
  fprintf(1,'Number of correctors %d\n', length(Corrector));
  fprintf(1,'Number of Monitors %d\n', nbpms);
cd('g:/users/c/ctfop/Public/ctf3emit');



fpo=fopen(filnam,'r');

nkickers = 10;

nmomitors = 22;
tmps = 'xxx';

[nkickers, nmomitors] = fscanf(fpo,'%d %d',2);

fprintf(1,'%d %d\n',nkickers, nmomitors);

tmps = fscanf(fpo,'%s',1);
fprintf(1,'%s\n',tmps);

line = fgets(fpo)

monitornames = sscanf(line,'%s',[12,inf]);

[mnnr,mnnc] = size(monitornames);
fprintf(1,'%d %d \n',mnnr,mnnc);


m = zeros(nbpms);
rm_measured = zeros(1,mnnc);


for i=1:mnnc,
    for j=1:nbpms,
        amonitorname = transpose(monitornames(:,i));%stupid MatLab reads string so they are columnwise. Idiot.
        %fprintf(1,'Comparing %s %s\n',amonitorname,Monitor(j).name);
        if ( strcmp(amonitorname,Monitor(j).name) )
            m(i) = j;
            Monitor(j).status = 0;
            fprintf(1,'Input minitor %d matches %d\n',i,j);
            break;
        end
    end
end

nkickersread = 0;
while (1),
    
    reforbit = zeros(mnnc);
    diforbit = zeros(mnnc);

    [kickerno,nread]  = fscanf(fpo,'%d',1);
    if (nread < 1)
        fprintf(1,'End of file\n');
        break;
    end
    fprintf(1,'Kickerno %d\n', kickerno);
    
    kikername = fscanf(fpo,'%s',1);
    fprintf(1,'Kicker name %s\n', kikername);

    ccvkick   = fscanf(fpo,'%f',1);
    fprintf(1,'Kicker CCV %f\n', ccvkick);

    type      = fscanf(fpo,'%s',1);
    fprintf(1,'Row Type  %s\n', type);

    reforbit  = fscanf(fpo,'%f',mnnc);
    fprintf(1,'Reference: ');
    for i=1:mnnc,
        fprintf(1,' %f ',reforbit(i));
    end
    fprintf(1,'\n');
    
    fgets(fpo);%reads the rest of the junk at the end of the line
    
    [kickerno2,nread]  = fscanf(fpo,'%d',1);
    if (nread < 1)
        fprintf(1,'End of file\n');
        break;
    end
    
    fprintf(1,'Kickerno2 %d\n', kickerno2);
    
    if (kickerno2 ~= kickerno)
      fprintf(1,'File corrupted kickerno2 != kickerno %d %d\n', kickerno2,kickerno);
      return;
    end

    kikername = fscanf(fpo,'%s',1);
    fprintf(1,'Kicker name %s\n', kikername);
    
    kick = fscanf(fpo,'%f',1);
    fprintf(1,'Kick applied (added to CCV) %f\n', kick);

    type      = fscanf(fpo,'%s',1);
    fprintf(1,'Row Type  %s\n', type);

    diforbit  = fscanf(fpo,'%f',mnnc);
    fprintf(1,'Defference: ');
    for i=1:mnnc,
        fprintf(1,' %f ',diforbit(i));
        rm_measured(nkickersread + 1,i) =  diforbit(i);
    end
    fprintf(1,'\n');
    
    fgets(fpo);%reads the rest of the junk at the end of the line

   
    nkickersread = nkickersread + 1;
end

fprintf(1,'There was %d kicks performed\n',nkickersread);
fclose(fpo);


return;
    refline = fgets(fpo);
    difline = fgets(fpo);
    if ( (length(refline) < 10) || (length(difline) < 10) )
        fprintf(1,'End of reading\n');
        break
    end
