% ppalldata_06_07_07.m

    clear all ; close all ;

    cent = [1024 1024] ;
    width = 45 ;
    zlen = 10 ;
    rotang = 0 ;
    energy = 35e3 ;
%    plt = [2 2 1 1 1 2 1 0 1 2] ;
    plt = [0 0 0 0 0 0 0 0 0 0] ;
    calplt = 0 ;
    bitdiv = 4 ;

    pplim = 119 ;
    pplimmat = [110:10:200] ;
    pplimmat = [pplimmat [250:50:1000]] ;
    pplimmat = [pplimmat [1200:200:4000]] ;

    [ppdir,bsfs] = getppdir ;
    datdir = ('06_07_07') ;

    fuldir = ([ppdir bsfs datdir]) ;
%    fuldir= 'E:\Pepperpot\PP_06_7_07\';

    datfiles = ['pp_17kV_35kV_1642_0002s.tif'; ...
%        'pp_17kV_35kV_1643_0002s.tif'; ...
%        'pp_17kV_35kV_1644_0002s.tif'; ...
%        'pp_17kV_35kV_1645_0002s.tif'; ...
%        'pp_17kV_35kV_1646_0002s.tif'; ...
        'pp_17kV_35kV_1647_0002s.tif'; ...
%        'pp_17kV_35kV_1648_0002s.tif'; ...
%        'pp_17kV_35kV_1649_0002s.tif'; ...
%        'pp_17kV_35kV_1650_0002s.tif'; ...
%        'pp_17kV_35kV_1651_0002s.tif'; ...
        'pp_17kV_35kV_1652_0002s.tif'; ...
%        'pp_17kV_35kV_1653_0002s.tif'; ...
%        'pp_17kV_35kV_1654_0002s.tif'; ...
%        'pp_17kV_35kV_1655_0002s.tif'; ...
%        'pp_17kV_35kV_1656_0002s.tif'; ...
        'pp_17kV_35kV_1657_0002s.tif'; ...
%        'pp_17kV_35kV_1658_0002s.tif'; ...
%        'pp_17kV_35kV_1659_0002s.tif'; ...
%        'pp_17kV_35kV_1700_0002s.tif'; ...
%        'pp_17kV_35kV_1701_0002s.tif'; ...
        'pp_17kV_35kV_1702_0002s.tif'; ...
%        'pp_17kV_35kV_1703_0002s.tif'; ...
%        'pp_17kV_35kV_1704_0002s.tif'; ...
%        'pp_17kV_35kV_1705_0002s.tif'; ...
%        'pp_17kV_35kV_1706_0002s.tif'; ...
        'pp_17kV_35kV_1707_0002s.tif'; ...
%        'pp_17kV_35kV_1708_0002s.tif'; ...
%        'pp_17kV_35kV_1709_0002s.tif'; ...
%        'pp_17kV_35kV_1710_0002s.tif'; ...
%        'pp_17kV_35kV_1711_0002s.tif'; ...
        'pp_17kV_35kV_1712_0002s.tif'; ...
%        'pp_17kV_35kV_1713_0002s.tif'; ...
%        'pp_17kV_35kV_1714_0002s.tif'; ...
%        'pp_17kV_35kV_1715_0002s.tif'; ...
        'pp_17kV_35kV_1720_0002s.tif'; ...
        'pp_17kV_35kV_1725_0002s.tif'; ...
        'pp_17kV_35kV_1730_0002s.tif'; ...
        'pp_17kV_35kV_1735_0002s.tif'; ...
        'pp_17kV_35kV_1740_0002s.tif'; ...
        'pp_17kV_35kV_1745_0002s.tif'; ...
        'pp_17kV_35kV_1750_0002s.tif'; ...
        'pp_17kV_35kV_1755_0002s.tif'] ;

    numfiles = length(datfiles) ;

    [midpos,sp2sp,imtilt] = ppcalfindmm([fuldir bsfs 'pp_17kV_35kV_cali_0002s.tif'],[4 42],[1986 44],[4 2028],[1984 2028],126,calplt) ;

%    [ppimbg,stdmbg,pphistbg,ppminmaxbg] = pepperplotfun([fuldir bsfs 'back_noise' bsfs 'pp_17kV_35kV_back_020ms.tif'],0,[2 2]) ;

    alldat = 1 ;
    savedat = 1 ;

    for i = 1:numfiles

        for j = 1:length(pplimmat)

            if alldat
                [ppposdat,ppemitdat,ppim,pppixemit5d,pppixemit2d] = ...
                    ppanalyse41([fuldir bsfs datfiles(i,:)],[],midpos,imtilt,sp2sp,pplimmat(j),width,zlen,rotang,energy,plt,bitdiv) ;
                xemiall(j) = ppemitdat(1).singlepix ;
                yemiall(j) = ppemitdat(2).singlepix ;
            else
                [ppposdat,ppemitdat,ppim] = ...
                    ppanalyse41([fuldir bsfs datfiles(i,:)],[],midpos,imtilt,sp2sp,pplimmat(j),width,zlen,rotang,energy,plt,bitdiv) ;
            end

            ppcutmat = ( ppim > pplimmat(j) ) ;
            ppim = ( ppim - pplimmat(j) ).*ppcutmat ;
            sumint(j) = sum( sum( ppim ) ) ;
            maxes(j) = max(max(ppim)) ;
            xemi(j) = ppemitdat(1).avespot ;
            yemi(j) = ppemitdat(2).avespot ;

        end

        dattime(i,:) = datfiles(i,14:17) ;
        savename = (['ppdat_06_07_07_' dattime(i,:) 'cuttest.mat']) ;
        if savedat
            save(savename,'pplimmat','maxes','sumint','xemi','yemi') ;
        end

        sumintmat(i,:) = sumint ;
        maxesmat(i,:) = maxes ;
        xemimat(i,:) = xemi ;
        yemimat(i,:) = yemi ;

        if alldat
            if savedat
                save(savename,'xemiall','yemiall','-append') ;
            end
            xemiallmat(i,:) = xemiall ;
            yemiallmat(i,:) = yemiall ;
        end

    end

    if savedat
        save('ppdat_06_07_07_cuttest.mat','xemiallmat','yemiallmat','-append') ;
    end

    figure, plot(pplimmat,maxesmat) ;
    xlabel(['Cut level']), ylabel(['Pixel Intensity']), ...
        title(['\bf Light intensity of the brightest pixel']) ;

    figure, plot(pplimmat,sumintmat) ;
    xlabel(['Cut level']), ylabel(['Image Intensity']), ...
        title(['\bf Integrated light intensity for the total image (after cut)']) ;

    figure, hold on ;
    plot(pplimmat,xemimat) ;
    xlabel(['Cut level']), ylabel(['Horizontal emittance (\pi mm mrad)']), ...
        title(['\bf Horizontal emittance, \epsilon_x, for average pixel data']) ;
    legend(dattime,'Location','Best') ;
    hold off ;

    figure, hold on ;
    plot(pplimmat,yemimat) ;
    xlabel(['Cut level']), ylabel(['Vertical emittance (\pi mm mrad)']), ...
        title(['\bf Vertical emittance, \epsilon_y, for average pixel data']) ;
    legend(dattime,'Location','Best') ;
    hold off ;

    if alldat
        figure, hold on ;
        plot(pplimmat,xemiallmat) ;
        xlabel(['Cut level']), ylabel(['Horizontal emittance (\pi mm mrad)']), ...
            title(['\bf Horizontal emittance, \epsilon_x, for all pixel data']) ;
        legend(dattime,'Location','Best') ;
        hold off ;

        figure, hold on ;
        plot(pplimmat,yemiallmat) ;
        xlabel(['Cut level']), ylabel(['Vertical emittance (\pi mm mrad)']), ...
            title(['\bf Vertical emittance, \epsilon_y, for all pixel data']) ;
        legend(dattime,'Location','Best') ;
        hold off ;
    end


