%%% This script creates input files for gfw or reads the output of gfw to
%%% plot the signal distribution.

%%% Compute the energy expected from a given grating

dirname='/oxford/svn/sp_upgrade/trunk/simulation/gfw/simfiles/';

%%%create or plot
create=1; %create=1 to generate files,; create=0 to generate plots

machine=3;%1 = SOLEIL, 2 = IFIC, 3 = FACET

%%% Create html page
if (create==0)
    if (machine==1)
        %SOLEIL
    fid2=fopen([ '/Users/delerue/travail/svn/sp_upgrade/trunk/data_acquisition/gratings/gratings_SOLEIL' '.html'],'w');
    fprintf(fid2,'<HTML>\n');
    fprintf(fid2,'<Title> %s',['Gratings studies for the SOLEIL test stand ']);
    fprintf(fid2,'</Title>\n');
    fprintf(fid2,'<h1> %s',['Gratings studies for the SOLEIL test stand ']);
    fprintf(fid2,'</h1>\n');
    fprintf(fid2,'<table border=3>\n');
    elseif (machine==2)
        %IFIC
    fid2=fopen([ '/Users/delerue/travail/svn/sp_upgrade/trunk/data_acquisition/gratings/gratings_IFIC' '.html'],'w');
    fprintf(fid2,'<HTML>\n');
    fprintf(fid2,'<Title> %s',['Gratings studies for the IFIC test stand ']);
    fprintf(fid2,'</Title>\n');
    fprintf(fid2,'<h1> %s',['Gratings studies for the IFIC test stand ']);
    fprintf(fid2,'</h1>\n');
    fprintf(fid2,'<table border=3>\n');
    else
        %SLAC
    fid2=fopen([ '/Users/delerue/travail/svn/sp_upgrade/trunk/data_acquisition/gratings/gratings_SLAC' '.html'],'w');
    fprintf(fid2,'<HTML>\n');
    fprintf(fid2,'<Title> %s',['Gratings studies for E-203 at FACET ']);
    fprintf(fid2,'</Title>\n');
    fprintf(fid2,'<h1> %s',['Gratings studies for E-203 at FACET ']);
    fprintf(fid2,'</h1>\n');
    fprintf(fid2,'<table border=3>\n');
    end % machine
end

%% Reads GFW template
fid=fopen(['/oxford/cauchy/script_gfw_template.in']);
txt=fread(fid);
fclose(fid);

changing=1;
chgmax=120;
ichg=1;
idxchg=1;

while((changing==1)&&(ichg<chgmax)&&(ichg<=idxchg))
    
    if (machine==1)
        error('wrong machine');
    else (machine==3)
        %SLAC
    %order=1;
    grating_period=0.05; %mm
%    grating_period=0.25; %mm
%    grating_period=0.5; %mm
%    grating_period=1.0; %mm
%    grating_period=1.49; %mm
    grating_width=20; %mm
    blaze_angle=30; % deg
    gamma=45000;
    norm_emittance=15; %mm.mrad
    position=1; %mm
    fwhmx=0.2; %mm
    fwhmy=0.2; %mm
    charge=2e10; %e-
    bunch_length_FWHM_ps=0.1; %ps
    shape=1;
    asymmetry=1;
    phi_step_size=1;
    number_phi_step=8;
    end %machine
    
    idxchg=1; % The first change is no change
    var_chg='';
    
    grating_period_vals=[ 0.01 0.05 0.1 0.2 0.5 1 1.49 2];
    nchg=length(grating_period_vals);
    if ((ichg>idxchg)&&(ichg<=(idxchg+nchg)))
        grating_period=grating_period_vals(ichg-idxchg);
        var_chg='grating_period';
    end %if
    idxchg=idxchg+nchg;
    
    grating_width_vals=[1 2 5 7 10 15 20 25 30 40 50 60 80 ];
    nchg=length(grating_width_vals);
    if ((ichg>idxchg)&&(ichg<=(idxchg+nchg)))
        grating_width=grating_width_vals(ichg-idxchg);
        var_chg='grating_width';
    end %if
    idxchg=idxchg+nchg;
    
    blaze_angle_vals=[10 15 17.5 20 26.75 30 36 40 45 50 ];
    nchg=length(blaze_angle_vals);
    if ((ichg>idxchg)&&(ichg<=(idxchg+nchg)))
        blaze_angle=blaze_angle_vals(ichg-idxchg);
        var_chg='blaze_angle';
    end %if
    idxchg=idxchg+nchg;
    
    position_vals=[0.5 1 2 3 4 5 6 10 20];
    nchg=length(position_vals);
    if ((ichg>idxchg)&&(ichg<=(idxchg+nchg)))
        position=position_vals(ichg-idxchg);
        var_chg='position';
        
    end %if
    idxchg=idxchg+nchg;
    
    fwhmx_vals=[0.3 0.5 0.7 1];
    nchg=length(fwhmx_vals);
    if ((ichg>idxchg)&&(ichg<=(idxchg+nchg)))
        fwhmx=fwhmx_vals(ichg-idxchg);
        var_chg='FWHM X';
    end %if
    idxchg=idxchg+nchg;
    
    fwhmy_vals=[0.5 1 1.1 1.5 2];
    nchg=length(fwhmy_vals);
    if ((ichg>idxchg)&&(ichg<=(idxchg+nchg)))
        fwhmy=fwhmy_vals(ichg-idxchg);
        var_chg='FWHM Y';
    end %if
    idxchg=idxchg+nchg;
    
    charge_vals=[5e5 1e10 2e10 3e10];
    nchg=length(charge_vals);
    if ((ichg>idxchg)&&(ichg<=(idxchg+nchg)))
        charge=charge_vals(ichg-idxchg);
        var_chg='Charge';
    end %if
    idxchg=idxchg+nchg;
    
    bunch_length_FWHM_ps_vals=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0];
    nchg=length(bunch_length_FWHM_ps_vals);
    if ((ichg>idxchg)&&(ichg<=(idxchg+nchg)))
        bunch_length_FWHM_ps=bunch_length_FWHM_ps_vals(ichg-idxchg);
        var_chg='Bunch length';
    end %if
    idxchg=idxchg+nchg;
            
    fname=['gfw_SLAC_gperiod_' num2str(grating_period) 'mm_gwidth_' num2str(grating_width) 'mm_blaze_' num2str(blaze_angle) '_pos_' num2str(position) 'mm_fwhmx_' num2str(fwhmx) 'mm_fwhmy_' num2str(fwhmy) 'mm_charge_' num2str(charge) '_blength_' num2str(bunch_length_FWHM_ps) 'ps' ]
    
    if (create==1)
        
        outtxt=strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(strrep(char(txt'),...
            '@change_order@','n'),...
            '@grating_period@',num2str(grating_period)),...
            '@grating_width@',num2str(grating_width)),...
            '@blaze_angle@',num2str(blaze_angle)),...
            '@gamma@',num2str(gamma)),...
            '@norm_emittance@',num2str(norm_emittance)),...
            '@position@',num2str(position)),...
            '@fwhmx@',num2str(fwhmx)),...
            '@fwhmy@',num2str(fwhmy)),...
            '@charge@',num2str(charge)),...
            '@bunch_length_FWHM_ps@',num2str(bunch_length_FWHM_ps)),...
            '@shape@',num2str(shape)),...
            '@asymmetry@',num2str(asymmetry)),...
            '@phi_step_size@',num2str(phi_step_size)),...
            '@number_phi_step@',num2str(number_phi_step)),...
            '@save_dE_file@','y'),...
            '@file_dE_name@',[  'gfw_dE' '.gfwout' ]),...
            '@save_energy_file@','y'),...
            '@file_Energy_name@',[  'gfw_Energy' '.gfwout']),...
            '@quit@','q');
        
        %% Writes GFW file
        fid=fopen([ dirname fname '.gfwin'],'w');
        fwrite(fid,outtxt);
        fclose(fid);
        
        
    else %if not create ==1
        
        fname
        plot_gratings_energy_gfw_SOLEIL_fcn(fname)
        
        fprintf(fid2,'<tr>\n');
        fprintf(fid2,'<td>\n');
        
        if ~isempty(var_chg)
            fprintf(fid2,'Changing variable: <b>%s</b> <BR/>\n',var_chg);
        end %if
        
        fprintf(fid2,'grating_period: %s <BR/>\n',num2str(grating_period));
        fprintf(fid2,'grating_width: %s <BR/>\n',num2str(grating_width));
        fprintf(fid2,'blaze_angle: %s <BR/>\n',num2str(blaze_angle));
        fprintf(fid2,'gamma: %s <BR/>\n',num2str(gamma));
        fprintf(fid2,'norm_emittance: %s <BR/>\n',num2str(norm_emittance));
        fprintf(fid2,'position: %s <BR/>\n',num2str(position));
        fprintf(fid2,'fwhmx: %s <BR/>\n',num2str(fwhmx));
        fprintf(fid2,'fwhmy: %s <BR/>\n',num2str(fwhmy));
        fprintf(fid2,'charge: %s (%s nC) <BR/>\n',num2str(charge), num2str(charge/1.6e10) );
        fprintf(fid2,'bunch_length_FWHM_ps: %s <BR/>\n',num2str(bunch_length_FWHM_ps));
        fprintf(fid2,'shape: %s <BR/>\n',num2str(shape));
        fprintf(fid2,'asymmetry: %s <BR/>\n',num2str(asymmetry));
        fprintf(fid2,'phi_step_size: %s <BR/>\n',num2str(phi_step_size));
        fprintf(fid2,'number_phi_step: %s <BR/>\n',num2str(number_phi_step));
        %fprintf(fid2,'save_dE_file: %s <BR/>\n','y');
        %fprintf(fid2,'file_dE_name: %s <BR/>\n',[  'gfw_dE' '.gfwout' ]);
        %fprintf(fid2,'save_energy_file: %s <BR/>\n','y');
        %fprintf(fid2,'file_Energy_name: %s <BR/>\n',[  'gfw_Energy' '.gfwout']);
        
        fprintf(fid2,'</td>\n');
        
        imgname=[ 'gratings_efficiency_gfw_' fname '.png'];
        fprintf(fid2,'<td><A href="%s"><img src="%s" width=600></A></td>\n',imgname,imgname);
        
        imgname=[ 'gratings_energy_gfw_' fname '.png'];
        fprintf(fid2,'<td><A href="%s"><img src="%s" width=600></A></td>\n',imgname,imgname);
        
        fprintf(fid2,'</tr>\n');
        
        
        
        
    end %if create
    
    ichg=ichg+1;
    
    %system(['ssh pplxint6.physics.ox.ac.uk "cd cauchy ; setenv  LD_LIBRARY_PATH /system/sl4/NAG/cllux08dgl/lib/ ; ./gfw31m < ./' fname '.in "'])
end %while ichg

if (create==0)
    fprintf(fid2,'</table>\n');
    fprintf(fid2,'</html>\n');
    fclose(fid2);
end

idxchg

