function [dataOUT,params] = correct_magnetic_baseline(dataIN,params)
%CORRECT_MAGNETIC_BASELINE UI for basline correction along field axis
%
%   Usage: [dataOUT,params] = CORRECT_MAGNETIC_BASELINE(dataIN,params), where
%   dataIN is a dataset loaded via the LOAD_MATLAB or LOAD_BRUKER function.
%
%   The function will show the input data and offer an input field, where
%   the number of field basline points should be set. It is also possible to
%   cut the field axis on both sides by the number in the second input field.
%   By pressing "Apply" the correction will be executed and the graph changes
%   to the corrected data. The number of field baseline points and points,
%   which were cut off, will also be appended to the params struct.
%   By pressing "Reset" the graph will return to show the original data.
%   Exit the function by pressing "Done".

%% creating UI
fig = figure;
fig.Name = 'Correct Magnetic Field Baseline';
fig.Units = 'Normalized';
fig.Position = [.2 .2 .6 .6];
fig.Tag = 'CorrMagBase';

%panel for plot
plotpanel = uipanel(fig);
plotpanel.Units = 'Normalized';
plotpanel.Position = [.01 .06 .98 .92];
%axes for plot
ax = axes(plotpanel);
%plot current data in figure with field as x axis
dataIN_transp = dataIN.';
plot(ax,dataIN_transp)
xlabel(ax,'Magnetic Field Points');
ylabel(ax,'EPR Signal / A. U.');
axis(ax,'tight');

%create push buttons
a = uicontrol(fig,'Style','pushbutton');
a.String = 'Apply';
a.Units = 'Normalized';
a.Position = [.52 .01 .15 .05];
a.FontUnits = 'Normalized';
a.FontSize = 0.6;
a.Tag = 'Apply';
a.Callback = @FieldApplyButtonPushed;

r = uicontrol(fig,'Style','pushbutton');
r.String = 'Reset';
r.Units = 'Normalized';
r.Position = [.68 .01 .15 .05];
r.FontUnits = 'Normalized';
r.FontSize = 0.6;
r.Tag = 'Reset';
r.Callback = @FieldResetButtonPushed;

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

%create input fields
p1 = uicontrol(fig,'Style','edit');
p1.String = 'No. points baseline';
p1.Units = 'Normalized';
p1.Position = [.01 .01 .24 .05];
p1.HorizontalAlignment = 'left';
p1.FontUnits = 'Normalized';
p1.FontSize = 0.6;
p1.Tag = 'FieldPoints';

p2 = uicontrol(fig,'Style','edit');
p2.String = 'No. points to cut before';
p2.Units = 'Normalized';
p2.Position = [.26 .01 .24 .05];
p2.HorizontalAlignment = 'left';
p2.FontUnits = 'Normalized';
p2.FontSize = 0.6;
p2.Tag = 'FieldPoints';

uicontrol(p1); %passes focus to input
uiwait(fig)

%% Callback functions
    function FieldApplyButtonPushed(~,event)
        field_baseline = str2double(get(p1,'String'));
        field_cut = str2double(get(p2,'String'));
        %field baseline CUTTING
        dataIN(:,1:field_cut) = [];
        dataIN(:,end-field_cut:end) = [];
        %field baseline CORRECTING
        data_size = size(dataIN);
        dataOUT = zeros(data_size);
        baseline_mean_right = mean(dataIN(:,1:field_baseline),2);
        baseline_mean_left = mean(dataIN(:,end-field_baseline:end),2);
        baseline_mean = (baseline_mean_right + baseline_mean_left) / 2;
        for n = 1:data_size(1)
            dataOUT(n,:) = dataIN(n,:) - baseline_mean(n);
        end
        %plotting result with field as x axis
        dataOUT_transp = dataOUT.';
        plot(ax,dataOUT_transp)
        xlabel(ax,'Magnetic Field Points');
        ylabel(ax,'EPR Signal / A. U.');
        axis(ax,'tight');
        %adjusting parameter
        params.Field_Sweep = params.Field_Sweep - params.Field_Step*2*field_cut;
        params.Field_Start = params.Field_Start + params.Field_Step*field_cut;
        params.Field_End = params.Field_End - params.Field_Step*field_cut;
        params.Field_Vector(:,1:field_cut) = [];
        params.Field_Vector(:,end-field_cut:end) = [];
        %writing parameter
        params.No_field_cut_pts = field_cut;
        params.No_field_basline_pts = field_baseline;
    end

    function FieldResetButtonPushed(~,~)
        plot(ax,dataIN_transp)
        xlabel(ax,'Magnetic Field Points');
        ylabel(ax,'EPR Signal / A. U.');
        axis(ax,'tight');
    end

    function FieldDoneButtonPushed(~,~)
        close 'Correct Magnetic Field Baseline'
    end

end

