%%% Plot the relation between grating width and polarisation signal

%basedir='/Users/delerue/Downloads/gfw_tmp/'
basedir='/oxford/gfw_svn/simfiles/'

%basename='gfw_FACET_gperiod_0.05mm_gwidth_*mm_blaze_30_pos_1mm_fwhmx_0.2mm_fwhmy_0.2mm_charge_20000000000_blength_0.1ps.gfwout'
%basename='gfw_FACET_gperiod_0.25mm_gwidth_*mm_blaze_30_pos_1mm_fwhmx_0.2mm_fwhmy_0.2mm_charge_20000000000_blength_0.1ps.gfwout'
%basename='gfw_FACET_gperiod_0.5mm_gwidth_*mm_blaze_30_pos_1mm_fwhmx_0.2mm_fwhmy_0.2mm_charge_20000000000_blength_0.1ps.gfwout'
%basename='gfw_FACET_gperiod_1mm_gwidth_*mm_blaze_30_pos_1mm_fwhmx_0.2mm_fwhmy_0.2mm_charge_20000000000_blength_0.1ps.gfwout'
basename='gfw_FACET_gperiod_1.49mm_gwidth_*mm_blaze_30_pos_1mm_fwhmx_0.2mm_fwhmy_0.2mm_charge_20000000000_blength_0.1ps.gfwout'

%basename='gfw_SOLEIL_gperiod_1mm_gwidth_*mm_blaze_30_pos_2mm_fwhmx_0.5mm_fwhmy_1.1mm_charge_600000000_blength_2ps.gfwout'
%basename='gfw_SOLEIL_gperiod_2mm_gwidth_*mm_blaze_30_pos_2mm_fwhmx_0.5mm_fwhmy_1.1mm_charge_600000000_blength_2ps.gfwout'
%basename='gfw_SOLEIL_gperiod_3mm_gwidth_*mm_blaze_30_pos_2mm_fwhmx_0.5mm_fwhmy_1.1mm_charge_600000000_blength_2ps.gfwout'
outdir='/Users/delerue/travail/smith-purcell/plots/'

files=ls([basedir basename])

eolines=strfind(files,char(10));

energyarray=zeros(length(eolines),141)
energyarray_p1=zeros(length(eolines),141)
energyarray_p2=zeros(length(eolines),141)

energychgarray=zeros(length(eolines),141)
energychgarray_p1=zeros(length(eolines),141)
energychgarray_p2=zeros(length(eolines),141)

widtharray=zeros(length(eolines),1);

start=1;
for iloop=1:length(eolines)
    thisfile=files(start:eolines(iloop)-1);
    thisbasefile=strrep(strrep(thisfile,basedir,''),'.gfwout','');
    [angle_vals,energy_vals,energy_p1,energy_p2]=plot_gratings_energy_gfw_fcn(thisbasefile,basedir,outdir)
    size(energy_vals)
    energyarray(iloop,1:length(energy_vals))=energy_vals;
    energyarray_p1(iloop,1:length(energy_vals))=energy_p1;
    energyarray_p2(iloop,1:length(energy_vals))=energy_p2;
    widthpos=strfind(thisbasefile,'gwidth')
    widthstr=thisbasefile(widthpos+7:end)
    widthstr=widthstr(1:strfind(widthstr,'mm')-1)
    widtharray(iloop,1)=str2num(widthstr)
    start=eolines(iloop)+1;
    
    if(widtharray(iloop,1)==10)
        angle_vals10=angle_vals;
        energy_v10=energy_vals;
        energy_p1_10=energy_p1;
        energy_p2_10=energy_p2;
    end % 10mm
end %for

[sorted,idx]=sort(widtharray);


for iloop=1:length(eolines)
    if (iloop==1)
        energychgarray_p1(idx(iloop),:)=energyarray_p1(idx(iloop),:);
        energychgarray_p2(idx(iloop),:)=energyarray_p2(idx(iloop),:);
    else
        energychgarray_p1(idx(iloop),:)=energyarray_p1(idx(iloop),:)-energyarray_p1(idx(iloop-1),:);
        energychgarray_p2(idx(iloop),:)=energyarray_p2(idx(iloop),:)-energyarray_p2(idx(iloop-1),:);
    end %if
end


iangle60=angle_vals(min(find(angle_vals>=60)));
iangle90=angle_vals(min(find(angle_vals>=90)));
iangle120=angle_vals(min(find(angle_vals>=120)));


figure(11);
clf;
hold on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energyarray_p1(idx,iangle60),'--or')
plot(widtharray(idx,1),energyarray_p2(idx,iangle60),'--*b')
hold off


figure(12);
clf;
hold on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energyarray_p1(idx,iangle90),'--or')
plot(widtharray(idx,1),energyarray_p2(idx,iangle90),'--*b')
hold off


figure(13);
clf;
hold on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energyarray_p1(idx,iangle120),'-or')
plot(widtharray(idx,1),energyarray_p2(idx,iangle120),'--*b')
hold off



figure(21);
clf;
hold on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energychgarray_p1(idx,iangle60),'-or')
plot(widtharray(idx,1),energychgarray_p2(idx,iangle60),'--*b')
hold off


figure(22);
clf;
hold on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energychgarray_p1(idx,iangle90),'-or')
plot(widtharray(idx,1),energychgarray_p2(idx,iangle90),'--*b')
hold off


figure(23);
clf;
hold on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energychgarray_p1(idx,iangle120),'-or')
plot(widtharray(idx,1),energychgarray_p2(idx,iangle120),'--*b')
hold off

figure(31)
clf;

subplot(2,2,1)
hold on
grid on
plot(angle_vals10,energy_p1_10,'-or');
plot(angle_vals10,energy_p2_10,'--*b');
plot(angle_vals10,energy_p1_10+energy_p2_10,'-.k');
legend('p1','p2','Total');
title(strrep(strrep(basename,'_',' '),'_gwidth_*mm',''));
hold off

subplot(2,2,2)
hold on
grid on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energychgarray_p1(idx,iangle60),'-og','LineWidth',2)
plot(widtharray(idx,1),energychgarray_p2(idx,iangle60),'--*b','LineWidth',2)
xlabel('Grating width [mm]')
ylabel('Signal intensity (differential) [AU]')
title('60 degree')
xlim([0 45])
hold off


subplot(2,2,3)
hold on
grid on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energychgarray_p1(idx,iangle90),'-og','LineWidth',2)
plot(widtharray(idx,1),energychgarray_p2(idx,iangle90),'--*b','LineWidth',2)
xlabel('Grating width [mm]')
ylabel('Signal intensity (differential) [AU]')
title('90 degree')
xlim([0 45])
hold off


subplot(2,2,4)
hold on
grid on
%plot(widtharray(:,1),energyarray(:,myangle),'.r')
plot(widtharray(idx,1),energychgarray_p1(idx,iangle120),'-og','LineWidth',2)
plot(widtharray(idx,1),energychgarray_p2(idx,iangle120),'--*b','LineWidth',2)
xlabel('Grating width [mm]')
ylabel('Signal intensity (differential) [AU]')
title('120 degree')
xlim([0 45])
hold off

print('-dpng',[outdir 'gwidth_study_' strrep(strrep(basename,'_gwidth_*mm',''),'.gfwout','') '.png'])
