function [emithist] = ppemithistgenxy(x, y, xp, yp, intens, dx, dy)
%
% function [emithist] = ppemithistgenxy(x, y, xp, yp, intens, dx, dy)
%
%    PPEMITHISTGENXY - generate emittance plot from pepperpot data
%
%    This version of the PPEMITHISTGEN function only randomly assigns X & Y
%    position data, not X' and Y'.

    xnew = [] ; ynew = [] ; xpnew = [] ; ypnew = [] ;
    intens = round(intens) ;
    cumint = cumsum(intens) ;

    if size(dx) == 1
        dx = dx.*(ones(length(x),1)) ;
    end

    if size(dy) == 1
        dy = dy.*(ones(length(x),1)) ;
    end

    for i = 1:length(x)
        if intens(i) > 0
            if i == 1
                xnew(1:cumint(i)) = x(i) - (dx(i)./2) + (rand([intens(i) 1]).*dx(i)) ;
                ynew(1:cumint(i)) = y(i) - (dy(i)./2) + (rand([intens(i) 1]).*dy(i)) ;
                xpnew(1:cumint(i)) = xp(i) ;
                ypnew(1:cumint(i)) = yp(i) ;
            else
                xnew((cumint(i-1) + 1):cumint(i)) = x(i) - (dx(i)./2) + (rand([intens(i) 1]).*dx(i)) ;
                ynew((cumint(i-1) + 1):cumint(i)) = y(i) - (dy(i)./2) + (rand([intens(i) 1]).*dy(i)) ;
                xpnew((cumint(i-1) + 1):cumint(i)) = xp(i) ;
                ypnew((cumint(i-1) + 1):cumint(i)) = yp(i) ;
            end
        end
    end

    emithist  = [xnew' ynew' xpnew' ypnew'] ;

    return