function [params] = pre_simulation_TREPR(params)
%PRE_SIMULATION_TREPR allows manual fitting of parameters in GUI
%
%   Usage: [params] = PRE_SIMULATION_TREPR(params), where params is
%   a struct, created by the load_bruker/matlab function and preprocessed
%   by NORMALIZE_DATA, at least.
%
%   The function will show a window with a plot of the normalized mean of
%   maxima (calculated by NORMALIZE_DATA) and serveral input fields for
%   different relevant paramters of TREPR simulation.
%   By pressing "Apply" the simulation will be calculated and displayed.
%   The simulation parameters will be saved to the params struct.

%% creating UI
fig = figure;
fig.Name = 'Manual Simulation';
fig.Units = 'Normalized';
fig.Position = [.15 .2 .7 .6];
fig.Tag = 'Normalize';

%panel for plot
plotpanel = uipanel(fig);
plotpanel.Units = 'Normalized';
plotpanel.Position = [.01 .01 .7 .98];
%axes for plot
ax = axes(plotpanel);
ax.XLabel.String = 'magnetic field';
ax.YLabel.String = 'Intensity';
%plot current data in figure with field as x axis
plot(ax,0.1*params.Field_Vector,params.max_mean,'r')
axis tight

%% create push buttons
a = uicontrol(fig,'Style','pushbutton');
a.String = 'Apply';
a.Units = 'Normalized';
a.Position = [.71 .06 .28 .05];
a.FontUnits = 'Normalized';
a.FontSize = 0.6;
a.Tag = 'Apply';
a.Callback = @PreSimApplyButtonPushed;

d = uicontrol(fig,'Style','pushbutton');
d.String = 'Done';
d.Units = 'Normalized';
d.Position = [.71 .01 .28 .05];
d.FontUnits = 'Normalized';
d.FontSize = 0.6;
d.Tag = 'Done';
d.Callback = @PreSimDoneButtonPushed;

%% create input and text fields (Triplet)
textTrip = uicontrol(fig,'Style','text');
textTrip.String = 'Triplett populations';
textTrip.Units = 'Normalized';
textTrip.Position = [.71 .94 .28 .05];
textTrip.HorizontalAlignment = 'center';
textTrip.FontUnits = 'Normalized';
textTrip.FontSize = 0.6;
textTrip.FontWeight = 'bold';
textTrip.Tag = 'textTrip';

textTx = uicontrol(fig,'Style','text');
textTx.String = 'T_1 = ';
textTx.Units = 'Normalized';
textTx.Position = [.71 .89 .04333 .05];
textTx.HorizontalAlignment = 'right';
textTx.FontUnits = 'Normalized';
textTx.FontSize = 0.6;
textTx.Tag = 'textTx';

InpTx = uicontrol(fig,'Style','edit');
InpTx.String = '1';
InpTx.Units = 'Normalized';
InpTx.Position = [.75334 .89 .04333 .05];
InpTx.HorizontalAlignment = 'left';
InpTx.FontUnits = 'Normalized';
InpTx.FontSize = 0.6;
InpTx.Tag = 'InpTx';

textTy = uicontrol(fig,'Style','text');
textTy.String = 'T_2 = ';
textTy.Units = 'Normalized';
textTy.Position = [.80668 .89 .04333 .05];
textTy.HorizontalAlignment = 'right';
textTy.FontUnits = 'Normalized';
textTy.FontSize = 0.6;
textTy.Tag = 'textTy';

InpTy = uicontrol(fig,'Style','edit');
InpTy.String = '1';
InpTy.Units = 'Normalized';
InpTy.Position = [.85001 .89 .04333 .05];
InpTy.HorizontalAlignment = 'left';
InpTy.FontUnits = 'Normalized';
InpTy.FontSize = 0.6;
InpTy.Tag = 'InpTy';

textTz = uicontrol(fig,'Style','text');
textTz.String = 'T_3 = ';
textTz.Units = 'Normalized';
textTz.Position = [.90334 .89 .04333 .05];
textTz.HorizontalAlignment = 'right';
textTz.FontUnits = 'Normalized';
textTz.FontSize = 0.6;
textTz.Tag = 'textTz';

InpTz = uicontrol(fig,'Style','edit');
InpTz.String = '1';
InpTz.Units = 'Normalized';
InpTz.Position = [.94667 .89 .04333 .05];
InpTz.HorizontalAlignment = 'left';
InpTz.FontUnits = 'Normalized';
InpTz.FontSize = 0.6;
InpTz.Tag = 'InpTz';

%% create input and text fields (D & E)
textDE = uicontrol(fig,'Style','text');
textDE.String = 'D tensor';
textDE.Units = 'Normalized';
textDE.Position = [.71 .77 .28 .05];
textDE.HorizontalAlignment = 'center';
textDE.FontUnits = 'Normalized';
textDE.FontSize = 0.6;
textDE.FontWeight = 'bold';
textDE.Tag = 'textDE';

textD = uicontrol(fig,'Style','text');
textD.String = 'D = ';
textD.Units = 'Normalized';
textD.Position = [.71 .72 .0675 .05];
textD.HorizontalAlignment = 'right';
textD.FontUnits = 'Normalized';
textD.FontSize = 0.6;
textD.Tag = 'textD';

InpD = uicontrol(fig,'Style','edit');
InpD.String = '1/2 width';
InpD.Units = 'Normalized';
InpD.Position = [.7775 .72 .0675 .05];
InpD.HorizontalAlignment = 'left';
InpD.FontUnits = 'Normalized';
InpD.FontSize = 0.6;
InpD.Tag = 'InpD';

textE = uicontrol(fig,'Style','text');
textE.String = 'E = ';
textE.Units = 'Normalized';
textE.Position = [.855 .72 .0675 .05];
textE.HorizontalAlignment = 'right';
textE.FontUnits = 'Normalized';
textE.FontSize = 0.6;
textE.Tag = 'textE';

InpE = uicontrol(fig,'Style','edit');
InpE.String = '1/3 D';
InpE.Units = 'Normalized';
InpE.Position = [.9225 .72 .0675 .05];
InpE.HorizontalAlignment = 'left';
InpE.FontUnits = 'Normalized';
InpE.FontSize = 0.6;
InpE.Tag = 'InpE';

%% create input and text fields (Spin & g)
textSG = uicontrol(fig,'Style','text');
textSG.String = 'Spin and g-value';
textSG.Units = 'Normalized';
textSG.Position = [.71 .60 .28 .05];
textSG.HorizontalAlignment = 'center';
textSG.FontUnits = 'Normalized';
textSG.FontSize = 0.6;
textSG.FontWeight = 'bold';
textSG.Tag = 'textSG';

textS = uicontrol(fig,'Style','text');
textS.String = 'Spin = ';
textS.Units = 'Normalized';
textS.Position = [.71 .55 .0675 .05];
textS.HorizontalAlignment = 'right';
textS.FontUnits = 'Normalized';
textS.FontSize = 0.6;
textS.Tag = 'textS';

InpS = uicontrol(fig,'Style','edit');
InpS.String = '1';
InpS.Units = 'Normalized';
InpS.Position = [.7775 .55 .0675 .05];
InpS.HorizontalAlignment = 'left';
InpS.FontUnits = 'Normalized';
InpS.FontSize = 0.6;
InpS.Tag = 'InpS';

textG = uicontrol(fig,'Style','text');
textG.String = 'g-value = ';
textG.Units = 'Normalized';
textG.Position = [.855 .55 .0675 .05];
textG.HorizontalAlignment = 'right';
textG.FontUnits = 'Normalized';
textG.FontSize = 0.6;
textG.Tag = 'textG';

InpG = uicontrol(fig,'Style','edit');
InpG.String = '2';
InpG.Units = 'Normalized';
InpG.Position = [.9225 .55 .0675 .05];
InpG.HorizontalAlignment = 'left';
InpG.FontUnits = 'Normalized';
InpG.FontSize = 0.6;
InpG.Tag = 'InpG';

%% create input and text fields (linewidth)
textLW = uicontrol(fig,'Style','text');
textLW.String = 'Linewidth';
textLW.Units = 'Normalized';
textLW.Position = [.71 .43 .28 .05];
textLW.HorizontalAlignment = 'center';
textLW.FontUnits = 'Normalized';
textLW.FontSize = 0.6;
textLW.FontWeight = 'bold';
textLW.Tag = 'textLW';

textGauss = uicontrol(fig,'Style','text');
textGauss.String = 'lw(Gau) = ';
textGauss.Units = 'Normalized';
textGauss.Position = [.71 .38 .0675 .05];
textGauss.HorizontalAlignment = 'right';
textGauss.FontUnits = 'Normalized';
textGauss.FontSize = 0.6;
textGauss.Tag = 'textGauss';

InpGauss = uicontrol(fig,'Style','edit');
InpGauss.String = '1';
InpGauss.Units = 'Normalized';
InpGauss.Position = [.7775 .38 .0675 .05];
InpGauss.HorizontalAlignment = 'left';
InpGauss.FontUnits = 'Normalized';
InpGauss.FontSize = 0.6;
InpGauss.Tag = 'InpGauss';

textLor = uicontrol(fig,'Style','text');
textLor.String = 'lw(Lor) = ';
textLor.Units = 'Normalized';
textLor.Position = [.855 .38 .0675 .05];
textLor.HorizontalAlignment = 'right';
textLor.FontUnits = 'Normalized';
textLor.FontSize = 0.6;
textLor.Tag = 'textLor';

InpLor = uicontrol(fig,'Style','edit');
InpLor.String = '0';
InpLor.Units = 'Normalized';
InpLor.Position = [.9225 .38 .0675 .05];
InpLor.HorizontalAlignment = 'left';
InpLor.FontUnits = 'Normalized';
InpLor.FontSize = 0.6;
InpLor.Tag = 'InpLor';

uicontrol(InpTx); %passes focus to first input
uiwait(fig)

%% Callback functions
    function PreSimApplyButtonPushed(src,event)
        %get values from params
        Exp.mwFreq = params.mwFreq; % GHz
        Exp.nPoints = length(params.Field_Vector);
        Exp.CenterSweep = 0.1*[params.Field_Center params.Field_Sweep]; % mT (converted from Gauss)
        Exp.Harmonic = 0; % zeroth harmonic
        %get values from UI
        T1 = str2double(get(InpTx,'String'));
        T2 = str2double(get(InpTy,'String'));
        T3 = str2double(get(InpTz,'String'));
        Tsum = T1 + T2 + T3;
        T1n = T1/Tsum; T2n = T2/Tsum; T3n = T3/Tsum; %normalize population
        Exp.Temperature = [T1n T2n T3n]; %Triplet population
        Sys.S = str2double(get(InpS,'String')); % Total Spin
        Sys.g = str2double(get(InpG,'String')); % g-value
        D = str2double(get(InpD,'String'));
        E = str2double(get(InpE,'String'));
        Sys.D = [D E]; % MHz, D and E values
        lwGau = str2double(get(InpGauss,'String'));
        lwLor = str2double(get(InpLor,'String'));
        Sys.lw = [lwGau lwLor]; % mT, linewidth needs to be optimised
        %perform a simulation with the parameters above
        [bfield,spec] = pepper(Sys,Exp);
        spec_norm = spec/max(spec); % normalize the simulation
        %plot result
        plot(ax,0.1*params.Field_Vector,params.max_mean,'r',...
             bfield,spec_norm,'b','LineWidth',1.2)
        legend('experimental','simulation')
        axis tight
        %write manual fit to params
        params.Manual_Sys = Sys;
        params.Triplett_pop = Exp.Temperature;
    end

    function PreSimDoneButtonPushed(src,event)
        close 'Manual Simulation'
    end

end
