% Simple program to modify a lammps data file
infile = 'rst1.data';
outfile = 'modrst1.data';
datain = fopen(infile,'r');
dataout = fopen(outfile,'w');
nfound = 0;
while (nfound==0)
    id = fgetl(datain);
    fprintf(dataout,'%s\n',id);
    if (regexp(id,'xlo'))
        xx = str2num(id(1:regexp(id,'xlo')-1));
    elseif (regexp(id,'ylo'))
        yy = str2num(id(1:regexp(id,'ylo')-1));
    elseif (regexp(id,'zlo'))
        zz = str2num(id(1:regexp(id,'zlo')-1));
    elseif (regexp(id,'atoms'))
        natoms = str2num(id(1:regexp(id,'atoms')-1));
    elseif (regexp(id,'Atoms'))
        nfound = 1;
    end
end
xcm = mean(xx); ycm = mean(yy);
rad = (xx(2)-xx(1))*0.3; rad2=rad*rad;
% Now read atom data and modify output
id = fgetl(datain);
fprintf(dataout,'%s\n',id);
for i = 1:natoms
    id = fgetl(datain);
    dta = str2num(id);
    x = dta(3); y = dta(4); z = dta(5);
    dx = x-xcm; dy = y-ycm; rr = dx*dx+dy*dy;
    if (rr<rad2)
        dta(2)=1;
    else
        dta(2)=2;
    end
    fprintf(dataout,'%d %d %f %f %f %d %d %d\n',dta);
end
% Copy the rest of the file
while (feof(datain)==0)
    id = fgetl(datain);
    fprintf(dataout,'%s\n',id);
end
% Close files
fclose(datain);
fclose(dataout);