% ppalldata_05_07_07_emicuttest

    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 2] ;
    plt = [0 0 0 0 0 0 0 0 0 0 0] ;
%    plt = [0 0 0 0 0 0 1 0 1 2] ;
    calplt = 0 ;
    bitdiv = 4 ;

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

    [ppdir,slash] = getppdir ;
    datdir = ('05_07_07') ;
    scdir = ('Scintilator') ;
    ltdir = ('light_test') ;

    matdir = ([ppdir slash datdir]) ;
    fuldir = ([ppdir slash datdir slash scdir]) ;

    datfile = ['PP_17Kv_35Kv_0mm_1454_2s.tif'] ;
%    datfile = ['PP_17Kv_35Kv_0mm_1525_2s.tif'] ;

%    [midpos,sp2sp,imtilt] = ppcalfindmm([fuldir slash 'PP_17Kv_35Kv_0mm_cal_200ms.tif'],[29 46],[2011 47],[29 2031],[2009 2029],126,calplt) ;
    [midpos,sp2sp,imtilt] = ppcalfindmm([],[29 46],[2011 47],[29 2031],[2009 2029],126,calplt) ;

%    pepperplotfun([fuldir slash 'PP_17Kv_35Kv_0mm_background_200ms.tif'],0,[2 2]) ;
%    [ppimbg,stdmbg,pphistbg,ppminmaxbg] = pepperplotfun([fuldir slash 'PP_17Kv_35Kv_0mm_background_2s.tif'],0,[2 2]) ;

    if 0

        load([matdir slash 'ppdat_05_07_07.mat']) ;

        figure, plot(respot) ;
        xlabel(['File number']), ylabel(['Integrated Spot Intensity']), ...
            title(['\bf Integrated light intensity for every single spot']) ;

        figure, plot(maxes,'x-k') ;
        xlabel(['File number']), ylabel(['Pixel Intensity']), ...
            title(['\bf Light intensity of the brightest pixel']) ;

        figure, plot(sumint,'x-k') ;
        xlabel(['File number']), ylabel(['Image Intensity']), ...
            title(['\bf Integrated light intensity for the total image (after cut)']) ;

        figure, hold on ;
        plot(xemi,'x-k') ; plot(yemi,'x-r') ;
        xlabel(['File number']), ylabel(['Horizontal/Vertical emittance (\pi mm mrad)']), ...
            title(['\bf Horizontal and Vertical emittance, \epsilon_x and \epsilon_y, for average pixel data']) ;
        legend(['\epsilon_x'],['\epsilon_y'],'Location','Best') ;
        hold off ;

        figure, hold on ;
        plot(xemiall,'x-k') ; plot(yemiall,'x-r') ;
        xlabel(['File number']), ylabel(['Horizontal/Vertical emittance (\pi mm mrad)']), ...
            title(['\bf Horizontal and Vertical emittance, \epsilon_x and \epsilon_y, for all pixel data']) ;
        legend(['\epsilon_x'],['\epsilon_y'],'Location','Best') ;
        hold off ;

    end

    maxspot = [976 890] ;
    alldat = 1 ;
    makemov = 0 ;

    if ( makemov == 1 ) && ( alldat == 1 )
        mov = avifile('C:\Documents and Settings\jolly\Desktop\cuttest_05_07_07_xemi.avi','fps',2,'quality',100) ;
    end

    for i = 1:length(pplimmat)

        if alldat == 1
%            [ppposdat,ppemitdat,ppim,pppixemit5d,pppixemit2d,ppemithist] = ...
            [ppposdat,ppemitdat,ppim,pppixemit5d,pppixemit2d] = ...
                ppanalyse41([fuldir slash datfile],[],midpos,imtilt,sp2sp,pplimmat(i),width,zlen,rotang,energy,plt,bitdiv) ;
            xemiall(i) = ppemitdat(1).singlepix ;
            yemiall(i) = ppemitdat(2).singlepix ;

            if makemov == 1
                figure(i), 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']) ;
%                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']) ;
                legend(['Cut level = ' num2str(pplimmat(i))],'Location','NorthWest') ;
                hold off ;

                frame(i) = getframe(i) ;
                mov = addframe(mov,frame(i)) ;
                close(i) ;
            end

        else
            [ppposdat,ppemitdat,ppim] = ...
                ppanalyse41([fuldir slash datfile],[],midpos,imtilt,sp2sp,pplimmat(i),width,zlen,rotang,energy,plt,bitdiv) ;
        end

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

    end

    if ( makemov == 1 ) && ( alldat == 1 )
        mov = close(mov) ;
    end

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

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

    figure, hold on ;
    plot(pplimmat,xemi,'x-k') ; plot(pplimmat,yemi,'x-r') ;
    xlabel(['Cut level']), ylabel(['Horizontal/Vertical emittance (\pi mm mrad)']), ...
        title(['\bf Horizontal and Vertical emittance, \epsilon_x and \epsilon_y, for average pixel data']) ;
    legend(['\epsilon_x'],['\epsilon_y'],'Location','Best') ;
    hold off ;

    if alldat
        figure, hold on ;
        plot(pplimmat,xemiall,'x-k') ; plot(pplimmat,yemiall,'x-r') ;
        xlabel(['Cut level']), ylabel(['Horizontal/Vertical emittance (\pi mm mrad)']), ...
            title(['\bf Horizontal and Vertical emittance, \epsilon_x and \epsilon_y, for all pixel data']) ;
        legend(['\epsilon_x'],['\epsilon_y'],'Location','Best') ;
        hold off ;
    end

    return



