% ppalldata_05_07_07_0mm

    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 = 114 ;
    pplim = 119 ;

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

    fuldir = ([ppdir slash datdir slash scdir slash]) ;
%    fuldir= 'E:\Pepperpot\PP_05_7_07\Scintilator\';

    datfiles = ['PP_17Kv_35Kv_0mm_1446_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1447_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1454_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1455_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1456_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1458_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1459_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1500_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1501_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1502_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1503_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1504_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1505_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1506_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1507_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1508_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1509_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1510_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1511_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1512_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1513_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1514_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1515_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1516_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1517_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1518_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1519_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1520_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1521_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1522_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1523_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1524_2s.tif'; ...
        'PP_17Kv_35Kv_0mm_1525_2s.tif'] ;

    numfiles = length(datfiles) ;
    goodfiles = numfiles - 2 ;

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

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

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

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

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

    for i = 3:numfiles

        j = i - 2 ;
%        [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(j) = sum( sum( ppim ) ) ;
        [y,ind] = max(ppim) ;
        [maxes(j),iind] = max(y) ;
        maxpos(j,:) = [iind ind(iind)] ;
        xemi(j) = ppemitdat(1) ;
        yemi(j) = ppemitdat(2) ;
        spotint(:,:,j) = ppposdat(:,:,13) ;

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

        if i == 3
            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 5500]), 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,goodfiles))' ;
    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) ;


