function [nkickersread, mk, m, rm_measured, reforbits, kicks, Corrector, Monitor] = BPXread_lx(fname)
%Input : fname - file name with kick data 
% nkickersread: integer - number of kicks read from the data file
% Corrector, Monitor: arrays of structures for correctors and monitors, respectively
%                     Serves as  database of available devices (positions, names in co, etc)
%                     Defined in HCorrectors.m and HMonitors.m
% mk: array of integers of length nkickersread - correspondance array for kickers, allows to tell wich kicker was kicked
%                     f.g. mk=[5,7] means that 5th and 7th  corrector from DB (Corrector) was used
% m: array of integers  - correspondance array for monitors, allows to tell wich monitors were read
%                     f.g. m=[4,6,9] means that 4th, 6th and 9th monitors from DB (Monitor) were read
% rm_measured: 2D array of floats - differences between kicked and reference orbits 
%                     for each kicker (row) for each monitor (column)
%                     (2,3) element is reading of 3rd monitor for the kick from 2nd kicker 
% reforbits: 2D array of floats - reference orbits, 
% kicks: array of floats of length nkickersread - values of the applied kicks 

fprintf(1,'Passed file name is >>%s<<\n', fname);

if (length(fname) == 0) %if empty string was passed use the following file (for tests)
  fprintf(1,'File name is empty\n');
  timestepf = '7332365109';
  filnam=['/mnt/psop/Psop/Archives/data/CTF3/2007/week25/ResponseMtxMeas/bpx' sprintf('%s',timestepf) '.dat'];
else
  filnam=fname
end

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('/mnt/ctfop/Public/ctf3emit');



fpo=fopen(filnam,'r');

if (fpo < 0)
 fprintf(1,'Can not open file %s \n', filnam);
 return
end

nkickers = 10;

nmomitors = 22;
tmps = 'xxx';

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

fprintf(1,'File provides data for %d kickers and %d monitors\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,'Size of array with defined monitor names %d %d \n',mnnr,mnnc);


m = zeros(nbpms,1); %correspondance table
mk = zeros(nkickers,1);
kicks = zeros(nkickers,1);

rm_measured = zeros(1,mnnc); 
reforbits = 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 monitor %d matches %d\n',i,j);
            break;
        end
    end
end

nkickersread = 0;
while (1),
    fprintf(1,'\n\n=====================================\n');
    
    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));
        reforbits(nkickersread + 1,i) =  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);
    

    for j=1:length(Corrector),
        fprintf(1,'Comparing %s %s\n',kikername,Monitor(j).name);
        if ( strcmp(kikername,Corrector(j).name) )
            mk(nkickersread + 1) = j;
            %Corrector(j).status = 1;
            fprintf(1,'Input corrector %d matches %d in DB\n',nkickersread + 1,j);
            break;
        end
    end
    
    
    
    kick = fscanf(fpo,'%f',1);
    fprintf(1,'Kick applied (added to CCV) %f\n', kick);
    kicks(nkickersread + 1,1) = 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;

