function fig = plotDIF( DIF_struct )

% this function plots results stored in the input data structure 'DIF_struct'

% determine the following:
% - are the results from interpolation?
% - if not from interpolation, are all results available for DIF and load

length_units = DIF_struct.length_units;
ws_units = DIF_struct.ws_units;
terrain = DIF_struct.terrain;

d0 = DIF_struct.d0;

d = DIF_struct.d;
n_m = size(d,1);
if n_m>1
    interp=1;
end
d0_label{1} = [];
bnd_label = cell(1,n_m);
interp_label{1} = [];
d_label = cell(1,n_m);
d_round = round(10*d)/10;
d0_round = round(10*d0)/10;
d_symbol = 'WLHR';
for i = 1:4
    if isempty(d0_label{1})
        d0_label{1} = [d_symbol(i) '=' num2str(d0_round(i)) ' ' length_units ];
    else
        d0_label{1} = [d0_label{1} ', ' d_symbol(i) '=' num2str(d0_round(i)) ' ' length_units ];
    end
    if any(abs(d_round(:,i)-d0_round(i))>10^-3)
        if isempty(interp_label{1})
            interp_label{1} = [': ' d_symbol(i) ' = ['];
        else
            interp_label{1} = [interp_label{1} ', ' d_symbol(i) ' = ['];
        end
        for j=1:n_m
            d_label{j}
            if isempty(bnd_label{j})
                bnd_label{j} = [': ' d_symbol(i) ' = ' num2str(d_round(j,i)) ' ' length_units];
            else
                bnd_label{j} = [bnd_label{j} ', ' d_symbol(i) ' = ' num2str(d_round(j,i)) ' ' length_units];
            end
            if interp_label{1}(end)=='[' %]
                interp_label{1} = [interp_label{1} num2str(d_round(j,i)) ' ' length_units];
            else
                interp_label{1} = [interp_label{1} ', ' num2str(d_round(j,i)) ' ' length_units];
            end
        end
        interp_label{1} = [interp_label{1} ']' ];
    end
end
loadinterp_label{1} = ['Interpolated' interp_label{1}];
for i = 1:n_m
    loadinterp_label{i+1} = ['Model ' num2str(i) bnd_label{i}];
    for j = 1:4
        if isempty(d_label{i})
            d_label{i} = [d_symbol(j) '=' num2str(d_round(i,j)) ' ' length_units ];
        else
            d_label{i} = [d_label{i} ', ' d_symbol(j) '=' num2str(d_round(i,j)) ' ' length_units ];
        end
    end
end


frame_loc = DIF_struct.frame_coords(:,2);
n_f = length(frame_loc);
for i = 1:n_f
    frame_label{i} = ['Frame ' num2str(i) ': y = ' num2str(frame_loc(i)) ' ' length_units];
end

resp_names = DIF_struct.resp_names;
resp_units = DIF_struct.resp_units;
n_r = length(resp_names);
for i = 1:n_r
    resp_label{i} = ['Response ' num2str(i) ': ' resp_names{i} ' (' resp_units{i} ')'];
end

theta = DIF_struct.theta;
n_q = length(theta);
for i=1:n_q
    theta_label{i} = [num2str(theta(i)) ' degrees'];
end

% initialize plot selections:
f = 1; % frame number
r = 1; % response number
q = 1; % wind direction number




%attach_loc = DIF_struct.

scrsz = get(0,'ScreenSize');
X0 = scrsz(3)*.05;
Y0 = scrsz(4)*.1;
dX = scrsz(3)*.9;
dY = scrsz(4)*.8;

clr = get(0,'DefaultUicontrolBackgroundColor');

fig = figure('Color',clr,'Position',[X0 Y0 dX dY],'Name','DIF Results Plotting');

set( fig, 'Units', 'character');

Size = get( fig, 'Position');


X0 = Size(3);
Y0 = Size(4);

mw = 70; % menu width

Y_load = 0.5*Y0;
Y_DIF = 0.5*Y0;

load_panel = uipanel('Parent',fig,'BackgroundColor',clr,'Units','character','Position',[0 0 X0-mw Y_load],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Load Distribution');
if isfield( DIF_struct,'load_max' ) && ~isempty(DIF_struct.load_max)
    attach_pts = DIF_struct.attach_pts;
    load_axes = axes('Parent',load_panel);
end

DIF_panel = uipanel('Parent',fig,'BackgroundColor',clr,'Units','character','Position',[0 Y_load X0-mw Y_DIF],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Directional Influence Factors (DIFs)');
DIF_axes = axes('Parent',DIF_panel);


Y_sum = 10.5+1.5*n_m;
Y_menu = Y0-Y_sum;


sum_panel = uipanel('Parent',fig,'BackgroundColor',clr,'Units','character','Position',[X0-mw Y_menu mw Y_sum-.05],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Results Summary');
uicontrol('Parent',sum_panel,'Style','text','Units','character','HorizontalAlignment','left',...
    'Position', [2 Y_sum-4 mw-2 2],'String','Building Dimensions:');
uicontrol('Parent',sum_panel,'Style','text','Units','character','HorizontalAlignment','left',...
    'Position', [4 Y_sum-5.5 mw-2 2],'String',d0_label{1});
uicontrol('Parent',sum_panel,'Style','text','Units','character','HorizontalAlignment','left',...
    'Position', [2 Y_sum-7 mw-2 2],'String','Model Dimensions:');
for i = 1:n_m
    uicontrol('Parent',sum_panel,'Style','text','Units','character','HorizontalAlignment','left',...
        'Position', [4 Y_sum-7-1.5*i mw-2 2],'String',d_label{i});
end
uicontrol('Parent',sum_panel,'Style','text','Units','character','HorizontalAlignment','left',...
    'Position', [2 Y_sum-1.5*n_m-8.5 mw-2 2],'String',['Terrain: ' terrain]);
uicontrol('Parent',sum_panel,'Style','text','Units','character','HorizontalAlignment','left',...
    'Position', [2 Y_sum-1.5*n_m-10 mw-2 2],'String',['Wind Speed: 1 ' ws_units]);


menu_panel = uipanel('Parent',fig,'BackgroundColor',clr,'Units','character','Position',[X0-mw 0 mw Y_menu],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Plotting Options');

mt = 2;
Ym = min([70 Y0-mt]);

A = 9;
B = 19.5;
C = 17;
D = 3;

menuA = uipanel('Parent',menu_panel,'BackgroundColor',clr,'Units','character','Position',[0 Y_menu-2-A mw-1 A],...
    'BorderType','none');
menuB = uipanel('Parent',menu_panel,'BackgroundColor',clr,'Units','character','Position',[0 Y_menu-2-(A+B) mw-1 B],...
    'FontSize',10,'FontWeight','bold','Title','Directional Influence Factors');
menuC = uipanel('Parent',menu_panel,'BackgroundColor',clr,'Units','character','Position',[0 Y_menu-2-(A+B+C) mw-1 C],...
    'FontSize',10,'FontWeight','bold','Title','Load Distributions');
menuD = uipanel('Parent',menu_panel,'BackgroundColor',clr,'Units','character','Position',[0 Y_menu-2-(A+B+C+D) mw-1 D],'BorderType','none');

%mb = 1;

frame_txt = uicontrol(menuA,'BackgroundColor',clr, 'Style', 'text', 'String','Select frame:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 A-2 mw-4 1.5]);
frame_popup = uicontrol(menuA, 'Style', 'popupmenu', 'String',frame_label,'Value',1,...
    'Units','characters','Position',[2 A-3.5 mw-4 1.5],'BackgroundColor','w','Callback',@FrameSelectCallback );
resp_txt = uicontrol(menuA,'BackgroundColor',clr, 'Style', 'text', 'String','Select response:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 A-6 mw-4 1.5]);
resp_popup = uicontrol(menuA, 'Style', 'popupmenu', 'String',resp_label,'Value',1,...
    'Units','characters','Position',[2 A-7.5 mw-4 1.5],'BackgroundColor','w','Callback',@RespSelectCallback );



maxmin_txt = uicontrol(menuB,'BackgroundColor',clr, 'Style', 'text', 'String','Type of peak:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 B-3.5 mw-4 1.5]);
max_radio = uicontrol(menuB, 'Style','radiobutton','String','Maximum values','Value',1,...
    'Units','characters','Position',[4 B-5 mw-6 1.5],'Callback',@MaxCallback);
min_radio = uicontrol(menuB, 'Style','radiobutton','String','Minimum values','Value',1,...
    'Units','characters','Position',[4 B-6.5 mw-6 1.5],'Callback',@MinCallback);

pk_txt = uicontrol(menuB,'BackgroundColor',clr, 'Style', 'text', 'String','Peak evaluation method:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 B-8.5 mw-4 1.5]);
obs_radio = uicontrol(menuB, 'Style','radiobutton','String','Observed peaks (sample max / min)','Value',1,...
    'Units','characters','Position',[4 B-10 mw-6 1.5],'Callback',@ObsPksCallback);
est_radio = uicontrol(menuB, 'Style','radiobutton','String','Estimated peaks (from probability distribution)','Value',0,...
    'Units','characters','Position',[4 B-11.5 mw-6 1.5],'Callback',@EstPksCallback);

sym_txt = uicontrol(menuB,'BackgroundColor',clr, 'Style', 'text', 'String','Results obtained by symmetry:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 B-13.5 mw-4 1.5]);
ave_radio = uicontrol(menuB, 'Style','radiobutton','String','Averaged results (one for each wind direction)','Value',1,...
    'Enable','inactive','Units','characters','Position',[4 B-15 mw-6 1.5],'Callback',@AveCallback);
all_radio = uicontrol(menuB, 'Style','radiobutton','String','All results (Wind directions A, B, C, and D)','Value',0,...
    'Units','characters','Position',[4 B-16.5 mw-6 1.5],'Callback',@AllCallback);

interp_txt = uicontrol(menuB,'BackgroundColor',clr, 'Style', 'text', 'String','Results obtained by interpolation:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 B-13.5 mw-4 1.5]);
interp_radio = uicontrol(menuB, 'Style','radiobutton','String','Interpolated results','Value',1,...
    'Units','characters','Position',[4 B-15 mw-6 1.5],'Callback',@InterpCallback);
intbnd_radio = uicontrol(menuB, 'Style','radiobutton','String','Results from bounding models','Value',0,...
    'Units','characters','Position',[4 B-16.5 mw-6 1.5],'Callback',@IntBndCallback);


if ~isfield(DIF_struct,'X_max_obs') || isempty(DIF_struct.X_max_obs)
    set(obs_radio, 'Value', 0);
    set(obs_radio, 'Enable', 'off');
    set(est_radio, 'Enable', 'inactive');
else % observed peaks available:
    X_max_obs = DIF_struct.X_max_obs;
    X_min_obs = DIF_struct.X_min_obs;
    if ~isfield(DIF_struct,'X_max_obs_all') || isempty(DIF_struct.X_max_obs_all)
        set(all_radio, 'Enable', 'off');
        set(ave_radio, 'Enable', 'inactive');
    else % symmetric results available:
        X_max_obs_all = DIF_struct.X_max_obs_all;
        X_min_obs_all = DIF_struct.X_min_obs_all;
    end
    if ~isfield(DIF_struct,'X_max_obs_bnd') || isempty(DIF_struct.X_max_obs_bnd)
        set(interp_txt, 'Visible','off','Enable','inactive');
        set(interp_radio, 'Value',0, 'Visible','off','Enable','inactive');
        set(intbnd_radio, 'Visible','off','Enable','inactive');
    else % bounding results available:
        X_max_obs_bnd = DIF_struct.X_max_obs_bnd;
        X_min_obs_bnd = DIF_struct.X_min_obs_bnd;
        set(sym_txt, 'Visible','off','Enable','inactive');
        set(ave_radio, 'Value',0, 'Visible','off','Enable','inactive');
        set(all_radio, 'Visible','off','Enable','inactive');
        symbol_list = {'+','x','s','d','*','^','v','>','<'};
    end
end    
if ~isfield(DIF_struct,'X_max_est') || isempty(DIF_struct.X_max_est)
    set(est_radio, 'Enable', 'off');
    set(obs_radio, 'Enable', 'inactive');
else % estimated peaks available:
    X_max_est = DIF_struct.X_max_est;
    X_min_est = DIF_struct.X_min_est;
    if ~isfield(DIF_struct,'X_max_est_all') || isempty(DIF_struct.X_max_est_all)
        set(all_radio, 'Enable', 'off');
        set(ave_radio, 'Enable', 'inactive');
    else % symmetric results available:
        X_max_est_all = DIF_struct.X_max_est_all;
        X_min_est_all = DIF_struct.X_min_est_all;
    end    
    if ~isfield(DIF_struct,'X_max_est_bnd') || isempty(DIF_struct.X_max_est_bnd)
        set(interp_txt, 'Visible','off','Enable','inactive');
        set(interp_radio, 'Value',0, 'Visible','off','Enable','inactive');
        set(intbnd_radio, 'Visible','off','Enable','inactive');
    else % bounding results available:
        X_max_est_bnd = DIF_struct.X_max_est_bnd;
        X_min_est_bnd = DIF_struct.X_min_est_bnd;
        set(sym_txt, 'Visible','off','Enable','inactive');
        set(ave_radio, 'Value',0, 'Visible','off','Enable','inactive');
        set(all_radio, 'Visible','off','Enable','inactive');
        symbol_list = {'+','x','s','d','*','^','v','>','<'};
    end
end


% % determine whether to display interpolation or symmetry plotting options:
% if n_m==1
%     set(interp_txt, 'Visible','off','Enable','inactive');
%     set(interp_radio, 'Value',0);
%     set(interp_radio, 'Visible','off','Enable','inactive');
%     set(intbnd_radio, 'Visible','off','Enable','inactive');
%     if ~isfield( DIF_struct, 'X_max_obs_all' )
%         set(all_radio, 'Enable','off');
%     end
% else
%     X_max_obs_bnd = DIF_struct.X_max_obs_bnd;
%     X_min_obs_bnd = DIF_struct.X_min_obs_bnd;
%     set(sym_txt, 'Visible','off','Enable','inactive');
%     set(ave_radio, 'Value',0);
%     set(ave_radio, 'Visible','off','Enable','inactive');
%     set(all_radio, 'Visible','off','Enable','inactive');
%     symbol_list = {'+','x','s','d','*','^','v','>','<'};
% %    symbol_list = {'b.','r.','g.','c.','m.','y.'};
% end

legend_ckbox = uicontrol(menuB, 'Style','checkbox','String','Show legend with plot',...
    'Units','characters','Value',1,'Position',[2 B-18.5 mw-4 1.5],'Callback',@FrameSelectCallback);

theta_txt = uicontrol(menuC,'BackgroundColor',clr, 'Style', 'text', 'String','Select wind direction:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 C-3.5 mw-4 1.5]);
theta_popup = uicontrol(menuC, 'Style', 'popupmenu', 'String',theta_label,'Value',q,...
    'Units','characters','Position',[2 C-5 mw-4 1.5],'BackgroundColor','w','Callback',@WindDirCallback );

loadsym_label{1} = 'Averaged loading';
if isfield(DIF_struct,'load_max_all') && ~isempty(DIF_struct.load_max_all)
    load_max_all = DIF_struct.load_max_all;
    load_min_all = DIF_struct.load_min_all;
    theta_all = DIF_struct.theta_all;
    n_qa = length(theta_all);
    ind_a = 1:n_qa/4; 
    ind_b = n_qa/4+1:n_qa/2; 
    ind_c = n_qa/2+1:n_qa*3/4;
    ind_d = n_qa*3/4+1:n_qa;
    theta_a = theta_all(ind_a);
    theta_b = theta_all(ind_b);
    theta_c = theta_all(ind_c);
    theta_d = theta_all(ind_d);

    loadsym_ind = find(theta_all==theta(q));
    for i = 1:length(loadsym_ind)
        if loadsym_ind(i)<=n_qa/4
            loadsym_label{i+1} = 'Wind direction A';
        elseif loadsym_ind(i)<=n_qa/2
            loadsym_label{i+1} = 'Wind direction B';
        elseif loadsym_ind(i)<=n_qa*3/4
            loadsym_label{i+1} = 'Wind direction C';
        else
            loadsym_label{i+1} = 'Wind direction D';
        end
    end
end

loadsym_txt = uicontrol(menuC,'BackgroundColor',clr, 'Style', 'text', 'String','Results obtained by exploiting model symmetry:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 C-7.5 mw-4 1.5]);
loadsym_popup = uicontrol(menuC, 'Style', 'popupmenu', 'String',loadsym_label,'Value',1,...
    'Units','characters','Position',[2 C-9 mw-4 1.5],'BackgroundColor','w','Callback',@FrameSelectCallback );

loadinterp_txt = uicontrol(menuC,'BackgroundColor',clr, 'Style', 'text', 'String','Results obtained by interpolation:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 C-7.5 mw-4 1.5]);
loadinterp_popup = uicontrol(menuC, 'Style', 'popupmenu', 'String',loadinterp_label,'Value',1,...
    'Units','characters','Position',[2 C-9 mw-4 1.5],'BackgroundColor','w','Callback',@FrameSelectCallback );

% determine whether to display interpolation or symmetry plotting options:
if n_m==1
    set(loadinterp_txt, 'Visible','off','Enable','inactive');
    set(loadinterp_popup, 'Visible','off','Enable','inactive');
else        
    set(loadsym_txt, 'Visible','off','Enable','inactive');
    set(loadsym_popup, 'Visible','off','Enable','inactive');
    load_max_bnd = DIF_struct.load_max_bnd;
    load_min_bnd = DIF_struct.load_min_bnd;
end

loadpk_txt = uicontrol(menuC,'BackgroundColor',clr, 'Style', 'text', 'String','Load distribution producing:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 C-11 mw-4 1.5]);
loadmax_radio = uicontrol(menuC, 'Style','radiobutton','String','Maximum value','Value',1,...
    'Units','characters','Position',[4 C-12.5 mw-6 1.5],'Callback',@LoadMaxCallback);
loadmin_radio = uicontrol(menuC, 'Style','radiobutton','String','Minimum value','Value',0,...
    'Units','characters','Position',[4 C-14 mw-6 1.5],'Callback',@LoadMinCallback);

loadcirc_ckbox = uicontrol(menuC, 'Style','checkbox','String','Circle peak on DIF plot',...
    'Units','characters','Value',1,'Position',[2 C-16 mw-4 1.5],'Callback',@FrameSelectCallback);


if ~isfield( DIF_struct, 'load_max' ) || isempty(DIF_struct.load_max)
    set(load_panel,'Title', 'Load Distribution (none available)');
    set(menuC,'Title', 'Load Distributions (none available)');
    set(theta_txt, 'Enable','off');
    set(theta_popup,'Value',1);
    set(theta_popup, 'String',' ');
    set(theta_popup, 'Enable','off');
    set(loadsym_txt, 'Enable','off');
    set(loadsym_popup, 'String',' ');
    set(loadsym_popup, 'Enable','off');
    set(loadinterp_txt, 'Enable','off');
    set(loadinterp_popup, 'String',' ');
    set(loadinterp_popup, 'Enable','off');
    set(loadpk_txt, 'Enable','off');
    set(loadmax_rad,'Value',0);
    set(loadmax_rad,'Enable','off');
    set(loadmin_rad,'Enable','off');
else
    load_max = DIF_struct.load_max;
    load_min = DIF_struct.load_min;
end

close_button = uicontrol(menuD, 'Style','pushbutton','String','Close',...
    'Units','characters','Position',[.4*mw .5 13 1.5],'Callback','close');

UpdateDIFplot;
UpdateLoadPlot;

uiwait(fig);

% callback functions:

function FrameSelectCallback( src, event )
    f = get( frame_popup, 'Value');
    UpdateDIFplot;
    UpdateLoadPlot;
end

function RespSelectCallback( src, event )
    r = get( resp_popup, 'Value');
    UpdateDIFplot;
    UpdateLoadPlot;
end

function MaxCallback( src, event )
    max_val = get( max_radio, 'Value');
    if max_val==0
        set( min_radio, 'Enable', 'inactive');
    else
        set( min_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end
function MinCallback( src, event )
    min_val = get( min_radio, 'Value');
    if min_val==0
        set( max_radio, 'Enable', 'inactive');
    else
        set( max_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end

function AveCallback( src, event )
    ave_val = get( ave_radio, 'Value');
    if ave_val==0
        set( all_radio, 'Enable', 'inactive');
    else
        set( all_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end
function AllCallback( src, event )
    all_val = get( all_radio, 'Value');
    if all_val==0
        set( ave_radio, 'Enable', 'inactive');
    else
        set( ave_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end

function InterpCallback( src, event )
    interp_val = get( interp_radio, 'Value');
    if interp_val==0
        set( intbnd_radio, 'Enable', 'inactive');
    else
        set( intbnd_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end
function IntBndCallback( src, event )
    intbnd_val = get( intbnd_radio, 'Value');
    if intbnd_val==0
        set( interp_radio, 'Enable', 'inactive');
    else
        set( interp_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end

function ObsPksCallback( src, event )
    obs_val = get( obs_radio, 'Value');
    if obs_val==0
        set( est_radio, 'Enable', 'inactive');
    else
        set( est_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end
function EstPksCallback( src, event )
    est_val = get( est_radio, 'Value');
    if est_val==0
        set( obs_radio, 'Enable', 'inactive');
    else
        set( obs_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end

function WindDirCallback( src, event )
    % wind direction selection change:
    % check which symmetric wind directions are available
    q = get(theta_popup, 'Value');
    if strcmp(get(loadsym_popup,'Enable'),'on')
        loadsym_ind = find(theta_all==theta(q));
        loadsym_label = cell(1,length(loadsym_ind));
        loadsym_label{1} = 'Averaged Loading';
        for i = 1:length(loadsym_ind)
            if loadsym_ind(i)<=n_qa/4
                loadsym_label{i+1} = 'Wind Direction A';
            elseif loadsym_ind(i)<=n_qa/2
                loadsym_label{i+1} = 'Wind Direction B';
            elseif loadsym_ind(i)<=n_qa*3/4
                loadsym_label{i+1} = 'Wind Direction C';
            else
                loadsym_label{i+1} = 'Wind Direction D';
            end
        end
        current_label = get(loadsym_popup, 'String');
        if ~isempty(setxor(current_label,loadsym_label)) | length(current_label)~=length(loadsym_label)
            set( loadsym_popup, 'Value',1);
        end
        set( loadsym_popup, 'String',loadsym_label);
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end


function LoadMaxCallback( src, event )
    loadmax_val = get( loadmax_radio, 'Value');
    if loadmax_val==1
        set( loadmin_radio, 'Value', 0);
    else
        set( loadmin_radio, 'Value', 1);
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end

function LoadMinCallback( src, event )
    loadmin_val = get( loadmin_radio, 'Value');
    if loadmin_val==1
        set( loadmax_radio, 'Value', 0);
    else
        set( loadmax_radio, 'Value', 1);
    end
    UpdateDIFplot;
    UpdateLoadPlot;
end


function UpdateDIFplot(src, event)
    axes(DIF_axes);
    cla reset;
    DIF_legend = {};
    count = 1;
    if get(obs_radio, 'Value')
        if get(ave_radio,'Value')
            if get(max_radio, 'Value') & get(min_radio, 'Value')
                plot(DIF_axes,[theta; theta],[X_max_obs(r,:,f) X_min_obs(r,:,f)],'k.'); hold on;
                DIF_legend{count} = 'Obs Max/Min'; count = count+1;
            elseif get(max_radio, 'Value')
                plot(DIF_axes,theta,X_max_obs(r,:,f),'k.'); hold on;
                DIF_legend{count} = 'Obs Max'; count = count+1;
            elseif get(min_radio, 'Value')
                plot(DIF_axes,theta,X_min_obs(r,:,f),'k.'); hold on;
                DIF_legend{count} = 'Obs Min'; count = count+1;
            end
        end
        if get(all_radio,'Value')
            if get(max_radio, 'Value') & get(min_radio, 'Value')
                plot(DIF_axes,[theta_a; theta_a],[X_max_obs_all(r,ind_a,f) X_min_obs_all(r,ind_a,f)],'k+'); hold on;
                DIF_legend{count} = 'Obs Max/Min A';  count = count+1;
                plot(DIF_axes,[theta_b; theta_b],[X_max_obs_all(r,ind_b,f) X_min_obs_all(r,ind_b,f)],'ks'); hold on;
                DIF_legend{count} = 'Obs Max/Min B';  count = count+1;
                plot(DIF_axes,[theta_c; theta_c],[X_max_obs_all(r,ind_c,f) X_min_obs_all(r,ind_c,f)],'kx'); hold on;
                DIF_legend{count} = 'Obs Max/Min C';  count = count+1;
                plot(DIF_axes,[theta_d; theta_d],[X_max_obs_all(r,ind_d,f) X_min_obs_all(r,ind_d,f)],'kd'); hold on;
                DIF_legend{count} = 'Obs Max/Min D';  count = count+1;
            elseif get(max_radio, 'Value')
                plot(DIF_axes,theta_a,X_max_obs_all(r,ind_a,f),'k+'); hold on;
                DIF_legend{count} = 'Obs Max A';  count = count+1;
                plot(DIF_axes,theta_b,X_max_obs_all(r,ind_b,f),'ks'); hold on;
                DIF_legend{count} = 'Obs Max B';  count = count+1;
                plot(DIF_axes,theta_c,X_max_obs_all(r,ind_c,f),'kx'); hold on;
                DIF_legend{count} = 'Obs Max C';  count = count+1;
                plot(DIF_axes,theta_d,X_max_obs_all(r,ind_d,f),'kd'); hold on;
                DIF_legend{count} = 'Obs Max D';  count = count+1;
            elseif get(min_radio, 'Value')
                plot(DIF_axes,theta_a,X_min_obs_all(r,ind_a,f),'k+'); hold on;
                DIF_legend{count} = 'Obs Min A';  count = count+1;
                plot(DIF_axes,theta_b,X_min_obs_all(r,ind_b,f),'ks'); hold on;
                DIF_legend{count} = 'Obs Min B';  count = count+1;
                plot(DIF_axes,theta_c,X_min_obs_all(r,ind_c,f),'kx'); hold on;
                DIF_legend{count} = 'Obs Min C';  count = count+1;
                plot(DIF_axes,theta_d,X_min_obs_all(r,ind_d,f),'kd'); hold on;
                DIF_legend{count} = 'Obs Min D';  count = count+1;
            end
        end
        if get(interp_radio,'Value')
            if get(max_radio, 'Value') & get(min_radio, 'Value')
                plot(DIF_axes,[theta; theta],[X_max_obs(r,:,f) X_min_obs(r,:,f)],'k.'); hold on;
                DIF_legend{count} = 'Obs Max/Min (interp)'; count = count+1;
            elseif get(max_radio, 'Value')
                plot(DIF_axes,theta,X_max_obs(r,:,f),'k.'); hold on;
                DIF_legend{count} = 'Obs Max (interp)'; count = count+1;
            elseif get(min_radio, 'Value')
                plot(DIF_axes,theta,X_min_obs(r,:,f),'k.'); hold on;
                DIF_legend{count} = 'Obs Min (interp)'; count = count+1;
            end
        end
        if get(intbnd_radio,'Value')
            if get(max_radio, 'Value') & get(min_radio, 'Value')
                for i=1:n_m
                    ind_i = (i-1)*n_q+(1:n_q);
                    plot(DIF_axes,[theta; theta],[X_max_obs_bnd(r,ind_i,f) X_min_obs_bnd(r,ind_i,f)],['k' symbol_list{i}]); hold on;
                    DIF_legend{count} = ['Obs Max/Min' bnd_label{i}];  count = count+1;
                end
            elseif get(max_radio, 'Value')
                for i=1:n_m
                    ind_i = (i-1)*n_q+(1:n_q);
                    plot(DIF_axes,theta,X_max_obs_bnd(r,ind_i,f),['k' symbol_list{i}]); hold on;
                    DIF_legend{count} = ['Obs Max' bnd_label{i}];  count = count+1;
                end
            elseif get(min_radio, 'Value')
                for i=1:n_m
                    ind_i = (i-1)*n_q+(1:n_q);
                    plot(DIF_axes,theta,X_min_obs_bnd(r,ind_i,f),['k' symbol_list{i}]); hold on;
                    DIF_legend{count} = ['Obs Min' bnd_label{i}];  count = count+1;
                end
            end
        end
    end
    if get(est_radio, 'Value')
        if get(ave_radio,'Value')
            if get(max_radio, 'Value') & get(min_radio, 'Value')
                plot(DIF_axes,[theta; theta],[X_max_est(r,:,f) X_min_est(r,:,f)],'b.'); hold on;
                DIF_legend{count} = 'Est Max/Min'; count = count+1;
            elseif get(max_radio, 'Value')
                plot(DIF_axes,theta,X_max_est(r,:,f),'b.'); hold on;
                DIF_legend{count} = 'Est Max'; count = count+1;
            elseif get(min_radio, 'Value')
                plot(DIF_axes,theta,X_min_est(r,:,f),'b.'); hold on;
                DIF_legend{count} = 'Est Min'; count = count+1;
            end
        end
        if get(all_radio,'Value')
            if get(max_radio, 'Value') & get(min_radio, 'Value')
                plot(DIF_axes,[theta_a; theta_a],[X_max_est_all(r,ind_a,f) X_min_est_all(r,ind_a,f)],'b+'); hold on;
                DIF_legend{count} = 'Est Max/Min A';  count = count+1;
                plot(DIF_axes,[theta_b; theta_b],[X_max_est_all(r,ind_b,f) X_min_est_all(r,ind_b,f)],'bs'); hold on;
                DIF_legend{count} = 'Est Max/Min B';  count = count+1;
                plot(DIF_axes,[theta_c; theta_c],[X_max_est_all(r,ind_c,f) X_min_est_all(r,ind_c,f)],'bx'); hold on;
                DIF_legend{count} = 'Est Max/Min C';  count = count+1;
                plot(DIF_axes,[theta_d; theta_d],[X_max_est_all(r,ind_d,f) X_min_est_all(r,ind_d,f)],'bd'); hold on;
                DIF_legend{count} = 'Est Max/Min D';  count = count+1;
            elseif get(max_radio, 'Value')
                plot(DIF_axes,theta_a,X_max_est_all(r,ind_a,f),'b+'); hold on;
                DIF_legend{count} = 'Est Max A';  count = count+1;
                plot(DIF_axes,theta_b,X_max_est_all(r,ind_b,f),'bs'); hold on;
                DIF_legend{count} = 'Est Max B';  count = count+1;
                plot(DIF_axes,theta_c,X_max_est_all(r,ind_c,f),'bx'); hold on;
                DIF_legend{count} = 'Est Max C';  count = count+1;
                plot(DIF_axes,theta_d,X_max_est_all(r,ind_d,f),'bd'); hold on;
                DIF_legend{count} = 'Est Max D';  count = count+1;
            elseif get(min_radio, 'Value')
                plot(DIF_axes,theta_a,X_min_est_all(r,ind_a,f),'b+'); hold on;
                DIF_legend{count} = 'Est Min A';  count = count+1;
                plot(DIF_axes,theta_b,X_min_est_all(r,ind_b,f),'bs'); hold on;
                DIF_legend{count} = 'Est Min B';  count = count+1;
                plot(DIF_axes,theta_c,X_min_est_all(r,ind_c,f),'bx'); hold on;
                DIF_legend{count} = 'Est Min C';  count = count+1;
                plot(DIF_axes,theta_d,X_min_est_all(r,ind_d,f),'bd'); hold on;
                DIF_legend{count} = 'Est Min D';  count = count+1;
            end
        end
        if get(interp_radio,'Value')
            if get(max_radio, 'Value') & get(min_radio, 'Value')
                plot(DIF_axes,[theta; theta],[X_max_est(r,:,f) X_min_est(r,:,f)],'b.'); hold on;
                DIF_legend{count} = 'Est Max/Min (interp)'; count = count+1;
            elseif get(max_radio, 'Value')
                plot(DIF_axes,theta,X_max_est(r,:,f),'b.'); hold on;
                DIF_legend{count} = 'Est Max (interp)'; count = count+1;
            elseif get(min_radio, 'Value')
                plot(DIF_axes,theta,X_min_est(r,:,f),'b.'); hold on;
                DIF_legend{count} = 'Est Min (interp)'; count = count+1;
            end
        end
        if get(intbnd_radio,'Value')
            if get(max_radio, 'Value') & get(min_radio, 'Value')
                for i=1:n_m
                    ind_i = (i-1)*n_q+(1:n_q);
                    plot(DIF_axes,[theta; theta],[X_max_est_bnd(r,ind_i,f) X_min_est_bnd(r,ind_i,f)],['b' symbol_list{i}]); hold on;
                    DIF_legend{count} = ['Est Max/Min' bnd_label{i}];  count = count+1;
                end
            elseif get(max_radio, 'Value')
                for i=1:n_m
                    ind_i = (i-1)*n_q+(1:n_q);
                    plot(DIF_axes,theta,X_max_est_bnd(r,ind_i,f),['b' symbol_list{i}]); hold on;
                    DIF_legend{count} = ['Est Max' bnd_label{i}];  count = count+1;
                end
            elseif get(min_radio, 'Value')
                for i=1:n_m
                    ind_i = (i-1)*n_q+(1:n_q);
                    plot(DIF_axes,theta,X_min_est_bnd(r,ind_i,f),['b' symbol_list{i}]); hold on;
                    DIF_legend{count} = ['Est Min' bnd_label{i}];  count = count+1;
                end
            end
        end
    end
    hold off;
    xlabel('Wind Direction (degrees)');
    ylabel([resp_names{r} ' (' resp_units{r} ')']);
    title(['Peak values of Response ' num2str(r) ' (' resp_names{r} ') for Frame ' ...
        num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']);
    v = axis;
    axis([0 360 v(3) v(4)]);
    set(DIF_axes,'XTick',[0:45:360]);
    if get(legend_ckbox, 'Value')
        legend(DIF_legend,'Location','EastOutside');
    end
end

function UpdateLoadPlot(src,event)
    axes(load_axes);
    cla reset;
    if strcmp(get(loadsym_popup,'Enable'),'on')
        if get(loadsym_popup, 'Value')==1
            if get(loadmax_radio,'Value')==1
                frame_vector_plot( load_max(:,r, q, f), attach_pts, d0(1), d0(3), d0(4), 2, 'k' );
                xlabel(['x-coordinate (' length_units ')']);
                ylabel(['y-coordinate (' length_units ')']);
                title({['Wind Direction = ' num2str(theta(q)) ' degrees; Averaged loading'],[],...
                    ['Loading producing maximum value (' num2str(X_max_obs(r,q,f)) ' ' resp_units{r} ...
                    ') of Response ' num2str(r) ' (' resp_names{r} ...
                    ') in Frame ' num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']});
                if get( loadcirc_ckbox, 'Value')
                    axes(DIF_axes); hold on;
                    load_circle = plot( theta(q), X_max_obs(r,q,f),'ro'); hold off;
                end
             else
                frame_vector_plot( load_min(:,r, q, f), attach_pts, d0(1), d0(3), d0(4), 2, 'k' );
                xlabel(['x-coordinate (' length_units ')']);
                ylabel(['y-coordinate (' length_units ')']);
                title({['Wind Direction = ' num2str(theta(q)) ' degrees; Averaged loading'],[],...
                    ['Loading producing minimum value (' num2str(X_min_obs(r,q,f)) ' ' resp_units{r} ...
                    ') of Response ' num2str(r) ' (' resp_names{r} ...
                    ') in Frame ' num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']});
                if get( loadcirc_ckbox, 'Value')
                    axes(DIF_axes); hold on;
                    load_circle = plot( theta(q), X_min_obs(r,q,f),'ro'); hold off;
                end
            end
        else % if not averaged loading
            ind = find(theta_all==theta(q));
            qa_ind = ind( get(loadsym_popup, 'Value')-1);
            if qa_ind<=n_qa/4
                wdir_label = 'Wind Direction A';
            elseif qa_ind<=n_qa/2
                wdir_label = 'Wind Direction B';
            elseif qa_ind<=n_qa*3/4
                wdir_label = 'Wind Direction C';
            else
                wdir_label = 'Wind Direction D';
            end
            if get(loadmax_radio,'Value')==1
                frame_vector_plot( load_max_all(:,r, qa_ind, f), attach_pts, d0(1), d0(3), d0(4), 2, 'k' );
                xlabel(['x-coordinate (' length_units ')']);
                ylabel(['y-coordinate (' length_units ')']);
                title({[wdir_label ' = ' num2str(theta_all(qa_ind)) ' degrees'],[],...
                    ['Loading producing maximum value (' num2str(X_max_obs_all(r,qa_ind,f)) ' ' resp_units{r} ...
                    ') of Response ' num2str(r) ' (' resp_names{r} ...
                    ') in Frame ' num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']});
                if get( loadcirc_ckbox, 'Value')
                    axes(DIF_axes); hold on;
                    load_circle = plot( theta_all(qa_ind), X_max_obs_all(r,qa_ind,f),'ro'); hold off;
                end
            else
                frame_vector_plot( load_min_all(:,r, qa_ind, f), attach_pts, d0(1), d0(3), d0(4), 2, 'k' );
                xlabel(['x-coordinate (' length_units ')']);
                ylabel(['y-coordinate (' length_units ')']);
                title({[wdir_label ' = ' num2str(theta_all(qa_ind)) ' degrees'],[],...
                    ['Loading producing minimum value (' num2str(X_min_obs_all(r,qa_ind,f)) ' ' resp_units{r} ...
                    ') of Response ' num2str(r) ' (' resp_names{r} ...
                    ') in Frame ' num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']});
                if get( loadcirc_ckbox, 'Value')
                    axes(DIF_axes); hold on;
                    load_circle = plot( theta_all(qa_ind), X_min_obs_all(r,qa_ind,f),'ro'); hold off;
                end
            end
        end
    elseif strcmp(get(loadinterp_popup,'Enable'),'on') % if interpolated loading
        if get(loadinterp_popup, 'Value')==1
            if get(loadmax_radio,'Value')==1
                frame_vector_plot( load_max(:,r, q, f), attach_pts, d0(1), d0(3), d0(4), 2, 'k' );
                xlabel(['x-coordinate (' length_units ')']);
                ylabel(['y-coordinate (' length_units ')']);
                title({['Wind Direction = ' num2str(theta(q)) ' degrees ' loadinterp_label{1}],[],...
                    ['Loading producing maximum value (' num2str(X_max_obs(r,q,f)) ' ' resp_units{r} ...
                    ') of Response ' num2str(r) ' (' resp_names{r} ...
                    ') in Frame ' num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']});
                if get( loadcirc_ckbox, 'Value')
                    axes(DIF_axes); hold on;
                    load_circle = plot( theta(q), X_max_obs(r,q,f),'ro'); hold off;
                end
            else
                frame_vector_plot( load_min(:,r, q, f), attach_pts, d0(1), d0(3), d0(4), 2, 'k' );
                xlabel(['x-coordinate (' length_units ')']);
                ylabel(['y-coordinate (' length_units ')']);
                title({['Wind Direction = ' num2str(theta(q)) ' degrees ' loadinterp_label{1}],[],...
                    ['Loading producing minimum value (' num2str(X_min_obs(r,q,f)) ' ' resp_units{r} ...
                    ') of Response ' num2str(r) ' (' resp_names{r} ...
                    ') in Frame ' num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']});
                if get( loadcirc_ckbox, 'Value')
                    axes(DIF_axes); hold on;
                    load_circle = plot( theta(q), X_min_obs(r,q,f),'ro'); hold off;
                end
            end
        else % if not averaged loading
            m = get(loadinterp_popup, 'Value')-1; % model number
            ind = (m-1)*n_q+q;
            if get(loadmax_radio,'Value')==1
                frame_vector_plot( load_max_bnd(:,r, ind, f), attach_pts, d0(1), d0(3), d0(4), 2, 'k' );
                xlabel(['x-coordinate (' length_units ')']);
                ylabel(['y-coordinate (' length_units ')']);
                title({['Wind Direction = ' num2str(theta(q)) ' degrees; ' loadinterp_label{m+1}],[],...
                    ['Loading producing maximum value (' num2str(X_max_obs_bnd(r,ind,f)) ' ' resp_units{r} ...
                    ') of Response ' num2str(r) ' (' resp_names{r} ...
                    ') in Frame ' num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']});
                if get( loadcirc_ckbox, 'Value')
                    axes(DIF_axes); hold on;
                    load_circle = plot( theta(q), X_max_obs_bnd(r,ind,f),'ro'); hold off;
                end
            else
                frame_vector_plot( load_min_bnd(:,r, ind, f), attach_pts, d0(1), d0(3), d0(4), 2, 'k' );
                xlabel(['x-coordinate (' length_units ')']);
                ylabel(['y-coordinate (' length_units ')']);
                title({['Wind Direction = ' num2str(theta(q)) ' degrees; ' loadinterp_label{m+1}],[],...
                    ['Loading producing minimum value (' num2str(X_min_obs_bnd(r,ind,f)) ' ' resp_units{r} ...
                    ') of Response ' num2str(r) ' (' resp_names{r} ...
                    ') in Frame ' num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']});
                if get( loadcirc_ckbox, 'Value')
                    axes(DIF_axes); hold on;
                    load_circle = plot( theta(q), X_min_obs_bnd(r,ind,f),'ro'); hold off;
                end
            end
        end
        
        
        
    end
end




end