function [dataOUT,params] = normalize_data(dataIN,params)
%NORMALISE_DATA normalizes data by max of region
%
%   Usage: [dataOUT,params] = NORMALISE_DATA(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 two input fields, where
%   the left and right borders of the maximum's region should be set.
%   By pressing "Apply" the normalization will be executed and the graph
%   changes to the normalized data. The borders of the maximum region and
%   the mean of maximums 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 = 'Normalize Data';
fig.Units = 'Normalized';
fig.Position = [.2 .2 .6 .6];
fig.Tag = 'Normalize';

%panel for plot
plotpanel = uipanel(fig);
plotpanel.Units = 'Normalized';
plotpanel.Position = [.01 .06 .98 .92];
%axes for plot
ax = axes(plotpanel);
ax.XLabel.String = 'Time';
ax.YLabel.String = 'Intensity';
%plot current data in figure with time as x axis
plot(ax,dataIN)
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 = @NormApplyButtonPushed;

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 = @NormResetButtonPushed;

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 = @NormDoneButtonPushed;

%create input fields
p1 = uicontrol(fig,'Style','edit');
p1.String = 'Left point of max region';
p1.Units = 'Normalized';
p1.Position = [.01 .01 .24 .05];
p1.HorizontalAlignment = 'left';
p1.FontUnits = 'Normalized';
p1.FontSize = 0.6;
p1.Tag = 'MaxRegionLeft';

p2 = uicontrol(fig,'Style','edit');
p2.String = 'Right point of max region';
p2.Units = 'Normalized';
p2.Position = [.25 .01 .24 .05];
p2.HorizontalAlignment = 'left';
p2.FontUnits = 'Normalized';
p2.FontSize = 0.6;
p2.Tag = 'MaxRegionRight';

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

%% Callback functions
    function NormApplyButtonPushed(src,event)
        left_point = str2double(get(p1,'String'));
        right_point = str2double(get(p2,'String'));
        %take mean of each field line between specified min and max time
        maxima_mean = mean(dataIN([left_point right_point],:));
        %normalize amplitude to 1 by dividing by max of means
        dataOUT = dataIN / max(maxima_mean);
        %plotting result with time as x axis
        plot(ax,dataOUT)
        axis tight
        %writing parameter
        params.region_of_max = [left_point right_point];
        params.max_mean = maxima_mean / max(maxima_mean);
    end

    function NormResetButtonPushed(src,event)
        plot(ax,dataIN)
        axis tight
    end

    function NormDoneButtonPushed(src,event)
        close 'Normalize Data'
    end

end

