% 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 ;

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

    fuldir = ([ppdir slash datdir slash]) ;
%    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 'pp_17kV_35kV_cali_0002s.tif'],[4 42],[1986 44],[4 2028],[1984 2028],126,calplt) ;

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

    ppimmat = zeros(2048,2048,2) ;
    maxpos = zeros(numfiles,2) ;

    maxspot = [520 1225] ;
    spotrad = 6 ;
    makemov = 0 ;
    alldat = 0 ;

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

    for i = 1:numfiles

%        [ppim] = pepperplotfun([fuldir datfiles(i,:)],0,[0 0]) ;
        [ppposdat,ppemitdat,ppim] = ...
            ppanalyse41([fuldir datfiles(i,:)],[],midpos,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;
        ppcutmat = ( ppim > pplim ) ;
        ppim = ( ppim - pplim ).*ppcutmat ;
        sumint(i) = sum( sum( ppim ) ) ;
        [y,ind] = max(ppim) ;
        [maxes(i),iind] = max(y) ;
        maxpos(i,:) = [iind ind(iind)] ;
        xemi(i) = ppemitdat(1) ;
        yemi(i) = ppemitdat(2) ;
        spotint(:,:,i) = ppposdat(:,:,13) ;

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

        if i == 1
            ppimmat(:,:,1) = ppim ;
        elseif i == numfiles
            ppimmat(:,:,2) = ppim ;
        end

        if makemov == 1
            ppsubim = ppim(((maxspot(2) - spotrad):(maxspot(2) + spotrad)), ((maxspot(1) - spotrad):(maxspot(1) + spotrad))) ;
            figure(i), imagesc(ppsubim,[pplim 4000]), axis image, colormap(gray) ; axis off ;
            frame(i) = getframe(i) ;
            mov = addframe(mov,frame(i)) ;
            close(i) ;
        end

    end

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

    respot = squeeze(reshape(spotint,[],1,numfiles))' ;
    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 ;

    if alldat
        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

    ppdiff = ppimmat(:,:,2) - ppimmat(:,:,1) ;
    figure, imagesc(ppdiff,[min(min(ppdiff)) max(max(ppdiff))]), axis image, colormap(gray) ;


