% ppalldata_24_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 = 112 ;
    pplim = 117 ;

    [ppdir,slash] = getppdir ;
    datdir = ('24_07_07') ;
    imdir = (['Light_output_decay_study']) ;

    caldir = ([ppdir slash datdir slash imdir]) ;
    fuldir = ([ppdir slash datdir slash imdir slash 'pp_images']) ;

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

    numfiles = length(datfiles) ;
    goodfiles = numfiles ;

    [midpos,sp2sp,imtilt] = ppcalfindmm([caldir slash 'PP_cal_2s.tif'],[27 39],[2008 38],[30 2021],[2008 2019],126,calplt) ;
%    [ppim,stdm,pphist,ppminmax] = pepperplotfun([caldir slash 'PP_cal_2s.tif'],0,[2 2]) ;

%    pepperplotfun([caldir slash 'background' slash 'PP_bg_image_2s.tif'],0,[2 2]) ;
%    [ppimbg,stdmbg,pphistbg,ppminmaxbg] = pepperplotfun([caldir slash 'background' slash 'PP_bg_image_2s.tif'],0,[2 2]) ;
%    pepperplotfun([caldir slash 'background' slash 'bg.tif'],0,[2 2]) ;
%    [ppimbg,stdmbg,pphistbg,ppminmaxbg] = pepperplotfun([caldir slash 'background' slash 'bg.tif'],0,[2 2]) ;

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

    maxspot = [443 739] ;
    spotrad = 6 ;
    makemov = 0 ;
    alldat = 1 ;

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

    for i = 1:numfiles

        j = i ; %- 2 ;

        [ppposdat,ppemitdat,ppim] = ...
            ppanalyse41([fuldir slash datfiles(i).name],[],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 slash datfiles(i).name],[],midpos,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,bitdiv) ;
            xemiall(j) = ppemitdat(1) ;
            yemiall(j) = ppemitdat(2) ;
        end

        if j == 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 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) ;


