% ppalldata_19_07_07_emit

    clear all ; close all ;

    cent = [1024 1024] ;
    width = 45 ;
    zlen = 10 ;
    rotang = 0 ;
    energy = 35e3 ;
%    plt = [2 2 1 1 1 2 1 1 1 2] ;
%        PLT - a 1D array of plotting data: PLT = [2 2 1 1 1 3 1 1 1 2]
%           PLT(1:2) is passed to PEPPERPLOTFUN for the background and data images; 
%           PLT(3) = 1 plots the hot data image;
%           PLT(4) = 1 plots the reconstructed spot image; 
%           PLT(5) = 1 plots the parts of the image above the cut level; 
%           PLT(6) = 1 plots the spot intensity data in 3D,
%                  = 2 plots the intensity data as a contour plot,
%                  = 3 plots both; 
%           PLT(7) = 1 plots the emittance plots;
%           PLT(8) = 1 plots the intensity profile of the central spot;
%           PLT(9) = 1 plots the quiver plots; 
%           PLT(10) = 1 plots the individual pixel emittance data,
%                   = 2 plots it in colour.
    plt = [0 0 0 0 0 0 1 0 1 2 2] ;
%    plt = [0 0 0 0 0 0 0 0 0 0 2] ;
    calplt = 0 ;
    bitdiv = 4 ;

%    pplim = 112 ;
%    pplim = 119 ;
    pplim = 250 ;
    histpart = 5e4 ;

    [ppdir,slash] = getppdir ;
    datdir = ('19_07_07') ;
    imdir = (['PP_emittance_scanner_comparison']) ;

    fuldir = ([ppdir slash datdir slash imdir]) ;

    datfiles = dir(fuldir) ;
    isdiryes = [datfiles.isdir]' ;
    inds = find(isdiryes == 1) ;
    datfiles(inds) = [] ;

    numfiles = length(datfiles) ;
    goodfiles = 2 ;

    [midpos,sp2sp,imtilt] = ppcalfindmm([fuldir slash 'pp_268mm_cal_2s.tif'],[19 32],[2000 32],[20 2016],[2000 2016],126,calplt) ;
%    [ppim,stdm,pphist,ppminmax] = pepperplotfun([fuldir slash 'pp_268mm_cal_2s.tif'],0,[2 2]) ;

    [ppposdat_11kV,ppemitdat_11kV,ppim_11kV,pppixemit5d_11kV,pppixemit2d_11kV] = ...
        ppanalyse41([fuldir slash datfiles(1).name],[],midpos,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;

    phospix = sp2sp./3 ;
    angres = ( tan(1./(phospix.*zlen)) ).*1e3 ;

    intsum = sum(pppixemit2d_11kV(:,5)) ;
    intdiv = intsum./histpart ;
    intens = (pppixemit2d_11kV(:,5)./intdiv) ;

    [ppemithist] = ppemithistgen(pppixemit2d_11kV(:,1),pppixemit2d_11kV(:,2),pppixemit2d_11kV(:,3), ...
        pppixemit2d_11kV(:,4),intens,3,3,angres,angres) ;

    figure, hold on ;
    ppemithistplot(ppemithist(:,1),ppemithist(:,3)) ;
    xlabel(['Horizontal position, x (mm)']), ylabel(['Horizontal divergence, x'' (mrad)']), ...
        title(['\bf Horizontal emittance, \epsilon_x, for regenerated ' ...
        num2str(energy./1e3) 'keV Pepperpot image']) ;
    hold off ;

    figure, hold on ;
    ppemithistplot(ppemithist(:,2),ppemithist(:,4)) ;
    xlabel(['Vertical position, y (mm)']), ylabel(['Vertical divergence, y'' (mrad)']), ...
        title(['\bf Vertical emittance, \epsilon_y, for regenerated ' ...
        num2str(energy./1e3) 'keV Pepperpot image']) ;
    hold off ;

    break

    [ppposdat_17kV,ppemitdat_17kV,ppim_17kV,pppixemit5d_17kV,pppixemit2d_17kV,ppemithist_17kV] = ...
        ppanalyse41([fuldir slash datfiles(2).name],[],midpos,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;

    return

