clear; close all; 

vid = VideoWriter('cohFracChanges3.mp4','MPEG-4'); 
vid.FrameRate = 30;    % Default 30
vid.Quality = 100;    % Default 75
open(vid); 
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]); % White background 

myBlue = [0.4 0.44 0.73];

sigye = 2e-7;    % Vertical electron size 2 um 
epsye = 1e-14; 
sigpye = epsye/sigye; 
undLength = 2; % Length of undulator in m 
betax = undLength/(4*pi); % Ideal beta function of electron beam to match photon beta function
photonE = (0.01:0.0025:100); % Range of photon energies in keV
lambda = (1e-10*12.3984)./photonE; % Convert photon energy in keV to wavelength in m

epsxerange = (5500:-10:50);
for ii = [epsxerange]
    sigp = (1/(4*pi)).*(lambda.*undLength).^0.5; % Photon beam source size in m 
    sigpp = (lambda./(undLength)).^0.5; % Photon beam divergence in rad 
    epsxe = ii*1e-12; % Range of horizontal electron emittances 
    sigxe = (betax*epsxe)^0.5; % Range of sigma_x^e (horiz e-beam SD width) in m 
    sigpxe = epsxe/sigxe; % Value of divergence of e-beam in rad for present value of sigxe 
    pt1 = 1 + (sigxe./sigp).^2; 
    pt2 = 1 + (sigpxe./sigpp).^2; 
    pt3 = 1 + (sigye./sigp).^2; 
    pt4 = 1 + (sigpye./sigpp).^2; 
    fcoh = (pt1 .* pt2 .* pt3 .* pt4).^(-0.5); 
    diffLimEnergy = 12.3984/(4*pi*epsxe*1e10); % Diffraction-limited energy
    diffLimI = round((diffLimEnergy - 0.1)/0.0025); % Index of diffraction-limited energy in photonE

    if (ii == max(epsxerange)) 
        fcohStart = fcoh; 
    end
    loglog(0.01,1e-5,100,2,photonE,fcoh,'color',myBlue, 'LineWidth', 3.5); 
    hold on;
    loglog(0.01,1e-5,100,2,photonE,fcohStart,'color','red', 'LineWidth', 3.5); 
%     if (diffLimI >= 1)
%         plot(photonE(1,diffLimI),fcoh(1,diffLimI),'ko',...
%             'MarkerFaceColor',myBlue,'MarkerSize',10);
%     end
    
    str1 = '{\epsilon_x^e} = ';
    str2 = num2str(epsxe*1e12,'% 7.1f'); 
    str3 = ' pm.rad'; 
    strTot = [str1,str2,str3]; 
    annotation('textbox',[0.16 0.4 0.14 0.07],'String',strTot,'EdgeColor',...
        'none','FontSize',22,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    str1 = '{\sigma_x^e} = ';
    str2 = num2str(sigxe*1e6,'% 7.1f'); 
    str3 = ' {\mu}m'; 
    strTot = [str1,str2,str3]; 
    annotation('textbox',[0.16 0.35 0.14 0.07],'String',strTot,'EdgeColor',...
        'none','FontSize',22,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    str1 = '{\sigma_x^{\prime e}} = ';
    str2 = num2str(sigpxe*1e6,'% 7.3f'); 
    str3 = ' {\mu}rad'; 
    strTot = [str1,str2,str3]; 
    annotation('textbox',[0.16 0.30 0.14 0.07],'String',strTot,'EdgeColor',...
        'none','FontSize',22,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    str1 = '{L_u} = ';
    str2 = num2str(undLength,'% 7.1f'); 
    str3 = ' m'; 
    strTot = [str1,str2,str3]; 
    annotation('textbox',[0.16 0.25 0.14 0.07],'String',strTot,'EdgeColor',...
        'none','FontSize',22,'FitBoxToText','on','HorizontalAlignment',...
        'left','verticalAlignment','bottom');
    
    set(gca,'FontName','Helvetica','fontsize',22);
    set(gca,'TickLength',[0.016, 2]); 
    set(gca,'xscale','log'); 
    set(gca,'yscale','log'); 
    set(gca,'linewidth',2); 
    xlim([0.01 100]);
    ylim([1e-4 2]);
    xlabel('Photon energy [keV]');
    ylabel('Coherent fraction'); 
    
    frame = getframe(gcf); 
    writeVideo(vid,frame); 
    hold off;
    
    delete(findall(gcf,'type','annotation'));

end
% Output the movie as an avi file
close(vid); 
