function [ppposdat,ppemitdat,pppixemit5d,pppixemit2d,ppemithist] = ...
    ppanalyse_simdat(ppimfname,imwidth)
% Function PPANALYSE_SIMDAT.M
%
%    [ppposdat,ppemitdat,pppixemit5d,pppixemit2d,ppemithist] =
%           ppanalyse_simdat(ppimfname,imwidth)
%
%    The function PPANALYSE rather ham-fistedly collects together a series
%    of subfunctions used to calculate emittance data from pepperpot
%    images.  The required input variables are as follows:
%
%        PPBGFNAME - the filename of the background image.
%
%        LPOS, RPOS - the left- and right-hand ruler positions for the
%                     background image.
%
%        MIDPOS - the central position calculated from the background
%                 image.
%
%        PPIMFNAME - the filename of the data image.
%
%        PPLIM - the cut to apply to the data
%
%        WIDTH - the width of the search window used to calculate the mean
%                weighted spot position.
%
%        ZLEN - the longitudinal distance from tungsten screen to phosphor
%               screen.
%
%        ROTANG - the rotation angle of the pepperpot assembly
%
%        ENERGY - the energy of the beam in eV.
%
%        PLT - a 1D array of plotting data: PLT(1:2) is passed to
%               PEPPERPLOTFUN for the background and data images; PLT(3)
%               is passed to PPTILTFIND; PLT(4) = 1 plots the hot data
%               image; PLT(5) = 1 plots the reconstructed spot image;
%               PLT(6) = 1 plots the 3D intensity data; PLT(7) = 1 plots
%               the emittance plots; PLT(8) = 1 plots the intensity profile
%               of the central spot.
%
%    The output variables are as follows:
%
%        PPPOSDAT - position data for pepperpot data image, including
%                   intensity information.
%
%        PPEMITDAT - emittance information, straight from PPEMITCALC.
%
%        PPPIXEMIT5D - 5-d matrix containing emittance data for ever single
%                      pixel with a spot (as defined by WIDTH).
%
%        PPPIXEMIT2D - reshaped version of PPPIXEMIT5D, containing only
%                      those pixels above threshold (PPLIM) in an N x 5
%                      matrix.
%
%        PPEMITHIST - histogram data generated using the data from
%                     PPPIXEMIT2D and randomly assigning X and Y data using
%                     the 3mm hole spacing.
%

%    clear all ; close all ;

%    Necessary variables

%    pplim = 160 ;
    pplim = 230 ;
    energy = 35e3 ;
    rotang = 0 ;
    impixels = 2048 ;
    [cent,sp2sp,imtilt] = ppfakeparams(impixels,imwidth) ;
    width = ( round( sp2sp./4 ) ).*4 - 1 ;
%    width = 41 ;
    zlen = 10 ;
    ppbgfname = [] ;
    rotang = 0 ;
    plt = [2 2 0 1 1 1 1 0] ;
    bitdiv = 1 ;

    if nargout == 5
        [ppposdat,ppemitdat,pppixemit5d,pppixemit2d,ppemithist] = ...
            ppanalyse41(ppimfname,ppbgfname,cent,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;
    elseif nargout == 4
        [ppposdat,ppemitdat,pppixemit5d,pppixemit2d] = ...
            ppanalyse41(ppimfname,ppbgfname,cent,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;
    elseif nargout == 3
        [ppposdat,ppemitdat,pppixemit5d] = ...
            ppanalyse41(ppimfname,ppbgfname,cent,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;
    elseif nargout == 2
        [ppposdat,ppemitdat] = ...
            ppanalyse41(ppimfname,ppbgfname,cent,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;
    elseif nargout == 1
        [ppposdat] = ...
            ppanalyse41(ppimfname,ppbgfname,cent,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;
    else
        ppanalyse41(ppimfname,ppbgfname,cent,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;
    end

    return


