function [s_f, ok] = sens_factors

ok = -1;

if exist('tmp_s_f.mat','file'), load 'tmp_s_f'; end
% If saved options are not found, set to default options:
if ~exist('s_f','var')
    s_f = [1 1 1 2];
end

% Define variables from the entries of 's_f', which will be changed through the GUI: 
s_f_1 = s_f(1);
s_f_2 = s_f(2);
s_f_3 = s_f(3);
s_f_4 = s_f(4);

% normalized scale factors (sum = 4):
norm_s_f = s_f*4/sum(s_f);

scrsz = get(0,'ScreenSize');
Xc = scrsz(3)*.4;
Yc = scrsz(4)*.4;
X0 = scrsz(3)*.25;
Y0 = scrsz(4)*.25;
clr = get(0,'DefaultUicontrolBackgroundColor');

h = dialog('Color',clr,'Position',[Xc Yc X0 Y0],'Name','Define sensitivity factors for interpolation');%,'CloseRequestFcn',@CloseCallback);

set( h, 'Units', 'character');
Size = get( h, 'Position');
Xc = Size(1);
Yc = Size(2);
X0 = 80; % width of dialog box
Y0 = 18.5; % height of dialog box
set(h,'Position',[Xc Yc X0 Y0]);

edit_x = 30; % horizontal position of left edge of edit boxes;
edit_y = 4; % vertical position of bottom edge of edit boxes;
edit_w = 20; % width of edit boxes;
edit_h = 1.5; % height of edit boxes;
norm_x = edit_x+edit_w+3; % horizontal position of left edge of boxes for normalized values
d = 3; % decimal places to display in normalized edit boxes

h_header = uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[1 Y0-5 X0-2 4.5],...
    'HorizontalAlignment','left','Style','text');

header_string = ['Enter scale factors representing the sensitivity of structural responses ' ...
    'to percent deviations in each of the following building dimensions. (These factors will be ' ...
    'normalized so that their sum is 4, so only their relative magnitudes are important.)'];

set(h_header,'String',header_string);




uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[2 edit_y+6 30 1.5],...
    'HorizontalAlignment','left','Style','text','String','Building Width, W:');

uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[edit_x edit_y+7.5 edit_w 1.5],...
    'HorizontalAlignment','left','Style','text','String','Editable values:');

uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[norm_x edit_y+7.5 edit_w 1.5],...
    'HorizontalAlignment','left','Style','text','String','Normalized values:');

W_edit = uicontrol('Parent',h,'BackgroundColor','w','Units','character','Position',[edit_x edit_y+6 edit_w edit_h],...
    'HorizontalAlignment','center','Style','edit','String',num2str(s_f_1), 'Callback', @EditCallback);

W_norm = uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[norm_x edit_y+6 edit_w edit_h],...
    'Enable','inactive','HorizontalAlignment','center','Style','edit','String',num2str(norm_s_f(1)));

uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[2 edit_y+4 X0-4 1.5],...
    'HorizontalAlignment','left','Style','text','String','Aspect Ratio, L / W :');

AR_edit = uicontrol('Parent',h,'BackgroundColor','w','Units','character','Position',[edit_x edit_y+4 edit_w edit_h],...
    'HorizontalAlignment','center','Style','edit','String',num2str(s_f_2), 'Callback', @EditCallback);

AR_norm = uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[norm_x edit_y+4 edit_w edit_h],...
    'Enable','inactive','HorizontalAlignment','center','Style','edit','String',num2str(norm_s_f(2)));


uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[2 edit_y+2 X0-4 1.5],...
    'HorizontalAlignment','left','Style','text','String','Eave Height Ratio, H / W :');

HR_edit = uicontrol('Parent',h,'BackgroundColor','w','Units','character','Position',[edit_x edit_y+2 edit_w edit_h],...
    'HorizontalAlignment','center','Style','edit','String',num2str(s_f_3), 'Callback', @EditCallback);

HR_norm = uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[norm_x edit_y+2 edit_w edit_h],...
    'Enable','inactive','HorizontalAlignment','center','Style','edit','String',num2str(norm_s_f(3)));


uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[2 edit_y X0-4 1.5],...
    'HorizontalAlignment','left','Style','text','String','Roof Slope, 2R / W :');

RS_edit = uicontrol('Parent',h,'BackgroundColor','w','Units','character','Position',[edit_x edit_y edit_w edit_h],...
    'HorizontalAlignment','center','Style','edit','String',num2str(s_f_4), 'Callback', @EditCallback);

RS_norm = uicontrol('Parent',h,'BackgroundColor',clr,'Units','character','Position',[norm_x edit_y edit_w edit_h],...
    'Enable','inactive','HorizontalAlignment','center','Style','edit','String',num2str(norm_s_f(4)));

save_button = uicontrol(h, 'Style','pushbutton','String','Save values','Units','character','Position',[X0*.5-30-4 1 30 2],...
    'Callback',@SaveCallback);
cancel_button = uicontrol(h, 'Style','pushbutton','String','Cancel','Units','character','Position',[X0*.5+4 1 30 2],...
    'Callback',@CancelCallback);


uiwait(h);


function EditCallback( src, event )
    s_f_1_str = get(W_edit,'String');
    if ~isempty(str2num(s_f_1_str))
        s_f_1 = abs(str2num(s_f_1_str));
    end
    set(W_edit,'String',num2str(s_f_1));
    
    s_f_2_str = get(AR_edit,'String');
    if ~isempty(str2num(s_f_2_str))
        s_f_2 = abs(str2num(s_f_2_str));
    end
    set(AR_edit,'String',num2str(s_f_2));
    
    s_f_3_str = get(HR_edit,'String');
    if ~isempty(str2num(s_f_3_str))
        s_f_3 = abs(str2num(s_f_3_str));
    end
    set(HR_edit,'String',num2str(s_f_3));

    s_f_4_str = get(RS_edit,'String');
    if ~isempty(str2num(s_f_4_str))
        s_f_4 = abs(str2num(s_f_4_str));
    end
    set(RS_edit,'String',num2str(s_f_4));
        
    norm_s_f = [s_f_1 s_f_2 s_f_3 s_f_4]*4/sum([s_f_1 s_f_2 s_f_3 s_f_4]);
    
    set(W_norm,'String',num2str(round(10^d*norm_s_f(1))/10^d));
    set(AR_norm,'String',num2str(round(10^d*norm_s_f(2))/10^d));
    set(HR_norm,'String',num2str(round(10^d*norm_s_f(3))/10^d));
    set(RS_norm,'String',num2str(round(10^d*norm_s_f(4))/10^d));
    
end

% TS_chkbox = uicontrol(hA, 'Style','checkbox','String','Display time series during analysis (enables storing of time series)',...
%     'Units','characters','Value',ts_plot,'Position',[2 yA-3 X0-4 1.5],'Callback',@TSdispCallback);
% % TS_txt = uicontrol(hA,'BackgroundColor',clr, 'Style', 'text', 'String','(enables optional storing of time series)',...
% %     'HorizontalAlignment','left','Units','character','Position',[7 yA-4.5 X0-8 1.5]);
% 
% 
% ObsPks_radio = uicontrol(hB, 'Style','radiobutton','String','Observed peaks (maximum and minimum of time series)',...
%     'Value',obs_pks,'Units','character','Position',[2 yB-3 X0-4 1.5],'Callback',@ObsPksCallback);
% EstPks_radio = uicontrol(hB, 'Style','radiobutton','String','Estimated peaks (expected value of peak from probability distribution)',...
%     'Value',est_pks,'Units','character','Position',[2 yB-5 X0-4 1.5],'Callback',@EstPksCallback);
% % If only one type of peak is selected, disable the corresponding radio button so that it can't be deselected:
% if obs_pks & ~est_pks
%     set(ObsPks_radio,'Enable','inactive');
% elseif est_pks & ~obs_pks
%     set(EstPks_radio,'Enable','inactive');
% end
% 
% MultPks_radio =  uicontrol(hC, 'Style','radiobutton','String','Store multiple peaks for each wind direction as well as averaged peaks',...
%     'Value',mult_pks,'Units','character','Position',[2 yB-3 X0-4 1.5],'Callback',@MultPksCallback);
% AvgPks_radio =  uicontrol(hC, 'Style','radiobutton','String','Store averaged peaks only',...
%     'Value',~mult_pks,'Units','character','Position',[2 yB-5 X0-4 1.5],'Callback',@AvgPksCallback);
% % Disable the selected radio button, so that changes can only be made by pressing the other button:
% if mult_pks
%     set(MultPks_radio,'Enable','inactive');
% else
%     set(AvgPks_radio,'Enable','inactive');
% end
% 
% PkLoads_chkbox = uicontrol(hD, 'Style','checkbox','String','Store load distributions producing peak responses',...
%     'Value',pk_loads,'Units','character','Position',[2 yD-3 X0-4 1.5],'Callback',@PkLoadsCallback);
% 
% save_button = uicontrol(hE, 'Style','pushbutton','String','Save selections','Units','character','Position',[X0*.5-30-4 yE-3 30 2],...
%     'Callback',@SaveCallback);
% cancel_button = uicontrol(hE, 'Style','pushbutton','String','Cancel','Units','character','Position',[X0*.5+4 yE-3 30 2],...
%     'Callback',@CancelCallback);
% 
% uiwait(h);
%     
% % function CloseCallback(src, evendata)
% %     % reset output options to default settings if user presses cancel or closes dialog box
% %     if ok==-1
% %         out_opt_struct = DefaultSettings;
% %     end
% % end 
% 
% function TSdispCallback(src, eventdata)
%     ts_plot = get(TS_chkbox,'Value');
% end
% function ObsPksCallback(src, eventdata)
%     obs_pks = get(ObsPks_radio,'Value');
%     if obs_pks
%         set( EstPks_radio, 'Enable', 'on');
%     else
%         set( EstPks_radio, 'Enable', 'inactive');
%     end
% end
% function EstPksCallback(src, eventdata)
%     est_pks = get(EstPks_radio,'Value');
%     if est_pks
%         set( ObsPks_radio, 'Enable', 'on');
%     else
%         set( ObsPks_radio, 'Enable', 'inactive');
%     end
% end
% 
% function AvgPksCallback(src, eventdata)
%     mult_pks = 0;
%     set(MultPks_radio, 'Value', 0,'Enable','on');
%     set(AvgPks_radio, 'Enable', 'inactive');
% end
% function MultPksCallback(src, eventdata)
%     mult_pks = 1;
%     set(AvgPks_radio, 'Value', 0,'Enable','on');
%     set(MultPks_radio,'Enable', 'inactive');
% end
% 
% function PkLoadsCallback(src, eventdata)
%     pk_loads = get(PkLoads_chkbox,'Value');
%     % 1 -> save, 0 -> don't save
% end
% 
function SaveCallback(src, eventdata)
    s_f = [s_f_1 s_f_2 s_f_3 s_f_4];
    % save scale factors to use as default values in next analysis:
    save('tmp_s_f','s_f');
    ok = 1;
    close(h);
end
function CancelCallback(src, eventdata)
    close(h);
end


end