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. By pressing "Apply" the
%   correction will be executed and the graph changes to the corrected data.
%   The number of field baseline points 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 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);
ax.XLabel.String = 'Magnetic field';
ax.YLabel.String = 'Intensity';
%plot current data in figure with field as x axis
dataIN_transp = dataIN.';
plot(ax,dataIN_transp)
axis 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 field
p = uicontrol(fig,'Style','edit');
p.Units = 'Normalized';
p.Position = [.01 .01 .5 .05];
p.HorizontalAlignment = 'left';
p.FontUnits = 'Normalized';
p.FontSize = 0.6;
p.Tag = 'FieldPoints';

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

%% Callback functions
    function FieldApplyButtonPushed(src,event)
        field_baseline = str2double(get(p,'String'));
        %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)
        axis tight
        %writing parameter
        params.No_field_basline_pts = field_baseline;
    end

    function FieldResetButtonPushed(src,event)
        plot(ax,dataIN_transp)
        axis tight
    end

    function FieldDoneButtonPushed(src,event)
        close 'Correct Magnetic Baseline'
    end

end

