function windpressure

bldg_full_filename = [];
bldg_struct.d0 = [];
d0_label = '';
HDF_pathname = [];
HDF_index = struct([]);
HDF_dev = [];
HDF_label = {};
DIF_pathname = [];
DIF_index = struct([]);
HURR_pathname = [];
HURR_struct_array = struct([]);
MRI_list = [50 100 250 500 750 1000];
for i=1:length(MRI_list)
    MRI_cell{i} = [num2str(MRI_list(i)) ' year'];
end
MRI_init_ind = [1 4];
out_opt_struct = struct([]);


% % Load saved output options if the temporary file exists:
% if exist('tmp_output_options.mat','file'), load('tmp_output_options'); end
% % If saved options are not found, set to default options:
% field_names = {'ts_plot','obs_pks','est_pks','mult_pks','worst_load'};
% if ~exist('out_opt_struct','var') || ~isstruct(out_opt_struct) || ~isempty(setdiff(field_names,fieldnames(out_opt_struct)))
%     out_opt_struct.ts_plot = 1;
%     out_opt_struct.obs_pks = 1;
%     out_opt_struct.est_pks = 1;
%     out_opt_struct.mult_pks = 1;
%     out_opt_struct.worst_load = 1;
% end

% Load saved sensitivity factors for building dimensions if the temporary file exists:
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];
    save('tmp_s_f','s_f'); % save scale factors
end

set(0,'Units','character')
scrsz = get(0,'ScreenSize');
set(0,'Units','pixels')

A = 9; % height of file panel
B = 17; % height of HDF panel
C = 29; % height of DIF panel
D = 5; % height of menu panel

dX = 200;
dY = A+B+C+D;
X0 = scrsz(3)*.5-dX/2;
Y0 = scrsz(4)*.6-dY/2;
dX1 = dX-1;

toggle_w = dX1/3-2; % width of toggle buttons on menu panel
toggle_h = D-1;

clr = get(0,'DefaultUicontrolBackgroundColor');

% ****************************** Create DIF page:

DIF_page = dialog('Color',clr,'Units', 'character','Position',[X0 Y0 dX dY],'Name','Computation and Interpolation of DIFs');

bldg_panel = uipanel('Parent',DIF_page,'BackgroundColor',clr,'Units','character','Position',[0 dY-A dX A],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Building Definition:');

uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[2 A-4 dX-4 2],...
    'HorizontalAlignment','left','Style','text','String','Building input file:');
bldg_edit = uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[2 A-5 .7*dX 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');
bldg_button = uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX A-5 20 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Select file...','Callback',@BldgBrowse);
bldg_display = uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX+22 A-5 20 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Display building','Callback',@PlotBLDG,...
    'Enable','off');

uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[2 A-7.5 35 2],...
    'HorizontalAlignment','left','Style','text','String','Building terrain:');
bldg_terrain_edit = uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[2 A-8.5 35 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');

uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[40 A-7.5 50 2],...
    'HorizontalAlignment','left','Style','text','String','Building dimensions:');
bldg_d0_edit = uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[40 A-8.5 50 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');

sens_factor_buttton = uicontrol('Parent',bldg_panel,'BackgroundColor',clr,'Units','character','Position',[94 A-8.5 40 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Interpolation Sensitivity Factors ...','Callback',@SensFactors);

HDF_panel = uipanel('Parent',DIF_page,'BackgroundColor',clr,'Units','character','Position',[0 dY-A-B dX B],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Pressure Databases');

uicontrol('Parent',HDF_panel,'BackgroundColor',clr,'Units','character','Position',[2 B-4 dX-4 2],...
    'HorizontalAlignment','left','Style','text','String','Top-level directory containing HDF pressure database files:');
HDF_edit = uicontrol('Parent',HDF_panel,'BackgroundColor',clr,'Units','character','Position',[2 B-5 .7*dX 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');
HDF_button = uicontrol('Parent',HDF_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX B-5 20 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Browse...','Callback',@HDFbrowse);
HDF_update = uicontrol('Parent',HDF_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX+22 B-5 20 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Update Index','Callback',@HDFindex,...
    'Enable','off');

B1 = 11;
HDF_list = uipanel('Parent',HDF_panel,'BackgroundColor',clr,'Units','character','Position',[0 0 dX1 B1],...
    'Title','Available HDF pressure databases within specified directory:');

uicontrol('Parent',HDF_list,'BackgroundColor',clr,'Units','character','Position',[2 B1-3.5 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','text','String','Open Terrain:');
HDF_open = uicontrol('Parent',HDF_list,'BackgroundColor','w','Units','character','Position',[2 B1-8 0.5*dX1-3 4.5],...
    'HorizontalAlignment','left','Style','listbox','String','','Enable','off');

uicontrol('Parent',HDF_list,'BackgroundColor',clr,'Units','character','Position',[0.5*dX+1 B1-3.5 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','text','String','Suburban Terrain:');
HDF_suburb = uicontrol('Parent',HDF_list,'BackgroundColor','w','Units','character','Position',[0.5*dX1+1 B1-8 0.5*dX1-3 4.5],...
    'HorizontalAlignment','left','Style','listbox','String','','Enable','off');

compute_open = uicontrol('Parent',HDF_list,'BackgroundColor',clr,'Units','character','Position',[0.25*dX1-7 B1-10 15 1.5],...
    'Style','pushbutton','String','Compute DIF','Enable','off','Callback',@ComputeOpen);

compute_suburb = uicontrol('Parent',HDF_list,'BackgroundColor',clr,'Units','character','Position',[0.75*dX-7 B1-10 15 1.5],...
    'Style','pushbutton','String','Compute DIF','Enable','off','Callback',@ComputeSuburb);


DIF_panel = uipanel('Parent',DIF_page,'BackgroundColor',clr,'Units','character','Position',[0 dY-A-B-C dX C],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Directional Influence Factors (DIFs)');

uicontrol('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[2 C-4 dX-4 2],...
    'HorizontalAlignment','left','Style','text','String','Directory for storing and retrieving DIFs:');
DIF_edit = uicontrol('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[2 C-5 .7*dX 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');
DIF_button = uicontrol('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX C-5 12 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Browse...','Callback',@DIFbrowse);
out_opt_button = uicontrol('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX+14 C-5 20 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Output options...','Callback',@OutOptCallback);

C1 = 11;
C2 = 11;
DIF_list = uipanel('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[0 C2 dX1 C1],...
    'Title','Computed DIFs for selected building in specified directory:');

uicontrol('Parent',DIF_list,'BackgroundColor',clr,'Units','character','Position',[2 C1-3.5 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','text','String','Open Terrain:');
DIF_open = uicontrol('Parent',DIF_list,'BackgroundColor','w','Units','character','Position',[2 C1-8 0.5*dX1-3 4.5],...
    'HorizontalAlignment','left','Style','listbox','String','','Enable','off','Callback',@SelectOpen);

uicontrol('Parent',DIF_list,'BackgroundColor',clr,'Units','character','Position',[0.5*dX1+1 C1-3.5 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','text','String','Suburban Terrain:');
DIF_suburb = uicontrol('Parent',DIF_list,'BackgroundColor','w','Units','character','Position',[0.5*dX1+1 C1-8 0.5*dX1-3 4.5],...
    'HorizontalAlignment','left','Style','listbox','String','','Enable','off','Callback',@SelectSuburb);

plot_open = uicontrol('Parent',DIF_list,'BackgroundColor',clr,'Units','character','Position',[0.25*dX1-15 C1-10 15 1.5],...
    'Style','pushbutton','String','Display','Enable','off','Callback',@PlotOpen);
interp_open = uicontrol('Parent',DIF_list,'BackgroundColor',clr,'Units','character','Position',[0.25*dX1+2 C1-10 15 1.5],...
    'Style','pushbutton','String','Interpolate','Enable','off','Callback',@InterpOpen);

plot_suburb = uicontrol('Parent',DIF_list,'BackgroundColor',clr,'Units','character','Position',[0.75*dX1-15 C1-10 15 1.5],...
    'Style','pushbutton','String','Display','Enable','off','Callback',@PlotSuburb);
interp_suburb = uicontrol('Parent',DIF_list,'BackgroundColor',clr,'Units','character','Position',[0.75*dX1+2 C1-10 15 1.5],...
    'Style','pushbutton','String','Interpolate','Enable','off','Callback',@InterpSuburb);

interpDIF_list = uipanel('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[0 0 dX1 C2],...
    'Title','Interpolated DIFs for selected building in specified directory:');

uicontrol('Parent',interpDIF_list,'BackgroundColor',clr,'Units','character','Position',[2 C2-3.5 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','text','String','Open Terrain:');
interpDIF_open = uicontrol('Parent',DIF_panel,'BackgroundColor','w','Units','character','Position',[2 C2-8 0.5*dX1-3 4.5],...
    'HorizontalAlignment','left','Style','listbox','String','');

uicontrol('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[0.5*dX1+1 C2-3.5 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','text','String','Suburban Terrain:');
interpDIF_suburb = uicontrol('Parent',DIF_panel,'BackgroundColor','w','Units','character','Position',[0.5*dX1+1 C2-8 0.5*dX1-3 4.5],...
    'HorizontalAlignment','left','Style','listbox','String','');

plot_interp_open = uicontrol('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[0.25*dX1-7 C2-10 15 1.5],...
    'Style','pushbutton','String','Display','Enable','off','Callback',@PlotInterpOpen);

plot_interp_suburb = uicontrol('Parent',DIF_panel,'BackgroundColor',clr,'Units','character','Position',[0.75*dX1-7 C2-10 15 1.5],...
    'Style','pushbutton','String','Display','Enable','off','Callback',@PlotInterpSuburb);

DIF_menu_panel = uipanel('Parent',DIF_page,'BackgroundColor',clr,'Units','character','Position',[0 0 dX D]);
DIF_DIF_toggle = uicontrol(DIF_menu_panel, 'Style','togglebutton','String','Directional Influence Factors',...
    'Value',1,'Enable','inactive','BackgroundColor',.87*[1 1 1],'Units','characters','Position',[dX/6-toggle_w/2 0.5 toggle_w toggle_h]);
DIF_MRI_toggle = uicontrol(DIF_menu_panel, 'Style','togglebutton','String','Mean Recurrence Intervals','Value',0,...
    'Units','characters','Position',[dX/2-toggle_w/2 0.5 toggle_w toggle_h],...
    'Callback',@MRIToggleCallback);
DIF_exit_button = uicontrol(DIF_menu_panel, 'Style','pushbutton','String','E x i t',...
    'Units','characters','Position',[dX*5/6-toggle_w/2 0.5 toggle_w toggle_h],...
    'Callback',@ExitCallback);

% ****************************** Create MRI page:

MRI_page = dialog('Color',clr,'Units', 'character','Position',[X0 Y0 dX dY],'Name','Computation of Responses with Specified MRIs');

MRI_bldg_panel = uipanel('Parent',MRI_page,'BackgroundColor',clr,'Units','character','Position',[0 dY-A dX A],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Building Definition:');

uicontrol('Parent',MRI_bldg_panel,'BackgroundColor',clr,'Units','character','Position',[2 A-4 dX-4 2],...
    'HorizontalAlignment','left','Style','text','String','Building input file:');
MRI_bldg_edit = uicontrol('Parent',MRI_bldg_panel,'BackgroundColor',clr,'Units','character','Position',[2 A-5 .7*dX 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');
MRI_bldg_button = uicontrol('Parent',MRI_bldg_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX A-5 12 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Browse...','Callback',@BldgBrowse);
MRI_bldg_display = uicontrol('Parent',MRI_bldg_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX+14 A-5 12 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Display','Callback',@PlotBLDG,...
    'Enable','off');

uicontrol('Parent',MRI_bldg_panel,'BackgroundColor',clr,'Units','character','Position',[2 A-7.5 35 2],...
    'HorizontalAlignment','left','Style','text','String','Building terrain:');
MRI_bldg_terrain_edit = uicontrol('Parent',MRI_bldg_panel,'BackgroundColor',clr,'Units','character','Position',[2 A-8.5 35 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');

uicontrol('Parent',MRI_bldg_panel,'BackgroundColor',clr,'Units','character','Position',[40 A-7.5 50 2],...
    'HorizontalAlignment','left','Style','text','String','Building dimensions:');
MRI_bldg_d0_edit = uicontrol('Parent',MRI_bldg_panel,'BackgroundColor',clr,'Units','character','Position',[40 A-8.5 50 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');


HURR_panel = uipanel('Parent',MRI_page,'BackgroundColor',clr,'Units','character','Position',[0 dY-A-B dX B],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Directional Hurricane Wind Speeds');

uicontrol('Parent',HURR_panel,'BackgroundColor',clr,'Units','character','Position',[2 B-4 dX-4 2],...
    'HorizontalAlignment','left','Style','text','String','Directory containing simulated hurricane wind speed files:');
HURR_edit = uicontrol('Parent',HURR_panel,'BackgroundColor',clr,'Units','character','Position',[2 B-5 .7*dX 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');
HURR_button = uicontrol('Parent',HURR_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX B-5 12 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Browse...','Callback',@HURRbrowse);

uicontrol('Parent',HURR_panel,'BackgroundColor',clr,'Units','character','Position',[0.25*dX-20 B-8 40 1.5],...
    'HorizontalAlignment','left','Style','text','String','Select building location:');
HURR_list = uicontrol('Parent',HURR_panel,'BackgroundColor','w','Units','character','Position',[0.25*dX-20 1 40 B-9],...
    'HorizontalAlignment','left','Style','listbox','String','','Enable','off');

uicontrol('Parent',HURR_panel,'BackgroundColor',clr,'Units','character','Position',[0.75*dX-20 B-8 40 1.5],...
    'HorizontalAlignment','left','Style','text','String','Select Mean Recurrence Intervals:');
MRI_listbox = uicontrol('Parent',HURR_panel,'BackgroundColor','w','Units','character','Position',[0.75*dX-20 1 40 B-9],...
    'HorizontalAlignment','left','Style','listbox','Enable','off');


MRI_panel = uipanel('Parent',MRI_page,'BackgroundColor',clr,'Units','character','Position',[0 dY-A-B-C dX C],...
    'FontSize',12,'FontWeight','bold','TitlePosition','centertop','Title','Computation of Responses with Specified Mean Recurrence Intervals');
C1 = 3;
C2 = 15.5;
C3 = C-C1-C2;

MRI_DIF_panel = uipanel('Parent',MRI_panel,'BackgroundColor',clr,'Units','character','Position',[0 C3 dX1 C2],...
    'Title','Select DIFs to use in computation:');

uicontrol('Parent',MRI_DIF_panel,'BackgroundColor',clr,'Units','character','Position',[2 C2-4 dX1-4 2],...
    'HorizontalAlignment','left','Style','text','String','Directory for storing and retrieving DIFs:');
MRI_edit = uicontrol('Parent',MRI_DIF_panel,'BackgroundColor',clr,'Units','character','Position',[2 C2-5 .7*dX1 1.5],...
    'HorizontalAlignment','left','Style','edit','Enable','inactive','String','');
MRI_button = uicontrol('Parent',MRI_DIF_panel,'BackgroundColor',clr,'Units','character','Position',[4+.7*dX1 C2-5 12 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Browse...','Callback',@DIFbrowse);

uicontrol('Parent',MRI_DIF_panel,'BackgroundColor',clr,'Units','character','Position',[2 C2-7 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','text','String','Open Terrain:');
DIF_all_open = uicontrol('Parent',MRI_DIF_panel,'BackgroundColor','w','Units','character','Position',[2 C2-13 0.5*dX1-3 6],...
    'HorizontalAlignment','left','Style','listbox','String','','Enable','off');
DIF_open_chkbox = uicontrol('Parent',MRI_DIF_panel,'Units','character','Position',[2 C2-14.5 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','checkbox','String','Use selected "Open" DIFs in MRI computation','Enable','off',...
    'Callback',@DIFChkboxCallback);


uicontrol('Parent',MRI_DIF_panel,'BackgroundColor',clr,'Units','character','Position',[0.5*dX1+1 C2-7 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','text','String','Suburban Terrain:');
DIF_all_suburb = uicontrol('Parent',MRI_DIF_panel,'BackgroundColor','w','Units','character','Position',[0.5*dX1+1 C2-13 0.5*dX1-3 6],...
    'HorizontalAlignment','left','Style','listbox','String','','Enable','off');
DIF_suburb_chkbox = uicontrol('Parent',MRI_DIF_panel,'Units','character','Position',[0.5*dX1+1 C2-14.5 0.5*dX1-3 1.5],...
    'HorizontalAlignment','left','Style','checkbox','String','Use selected "Suburban" DIFs in MRI computation','Enable','off',...
    'Callback',@DIFChkboxCallback);

compute_MRI_button = uicontrol('Parent',MRI_panel,'BackgroundColor',clr,'Units','character','Position',[.5*dX-25 C3-2.5 50 1.5],...
    'HorizontalAlignment','left','Style','pushbutton','String','Compute responses with specified MRIs','Enable','off',...
    'Callback',@ComputeMRIs);

MRI_menu_panel = uipanel('Parent',MRI_page,'BackgroundColor',clr,'Units','character','Position',[0 0 dX D]);
MRI_DIF_toggle = uicontrol(MRI_menu_panel, 'Style','togglebutton','String','Directional Influence Factors','Value',0,...
    'Units','characters','Position',[dX/6-toggle_w/2 0.5 toggle_w toggle_h],...
    'Callback',@DIFToggleCallback);
MRI_MRI_toggle = uicontrol(MRI_menu_panel, 'Style','togglebutton','String','Mean Recurrence Intervals','Value',1,'Enable','inactive',...
    'BackgroundColor',.87*[1 1 1],'Units','characters','Position',[dX/2-toggle_w/2 0.5 toggle_w toggle_h]);
MRI_exit_button = uicontrol(MRI_menu_panel, 'Style','pushbutton','String','E x i t',...
    'Units','characters','Position',[dX*5/6-toggle_w/2 0.5 toggle_w toggle_h],...
    'Callback',@ExitCallback);

set(MRI_page,'WindowStyle','normal','Visible','off');



% ********************************** callback functions

function BldgBrowse( src, event)
    % Load pathname for building input file used in last analysis, if available, otherwise create empty string:
    if exist('tmp_bldg_path.mat','file'), load('tmp_bldg_path'); end
    if ~exist('bldg_path','var') || ~isstr(bldg_path), bldg_path = ''; end
    % Prompt user to select building input file and extract information from file:
    [bldg_filename bldg_path]= uigetfile({'*.csv','CSV (Comma delimited)(*.csv)'},'Select building input file',bldg_path);
    if any(bldg_filename~=0)
        bldg_full_filename = fullfile( bldg_path, bldg_filename );
        bldg_struct = readBLDGfile( bldg_full_filename );
        if isfield(bldg_struct,'error')
            err = errordlg('A problem was encounted in reading the selected file.  Please modify the file or select another file.',...
                'Error reading building input file');
        else
            % Save pathname to be used as default values in next analysis:
            save('tmp_bldg_path','bldg_path');
            set( bldg_edit,'String',bldg_full_filename);
            set( bldg_display, 'Enable', 'on');
            % label building dimensions:
            d0_round = round(10*bldg_struct.d0)/10;
            d_symbol = 'WLHR';
            d0_label = [d_symbol(1) ' = ' num2str(d0_round(1)) ' ' bldg_struct.length_units];
            for i = 2:4
                d0_label = [d0_label ', ' d_symbol(i) ' = ' num2str(d0_round(i)) ' ' bldg_struct.length_units];
            end
            set( bldg_d0_edit,'String', d0_label);
            set( bldg_terrain_edit,'String',bldg_struct.terrain ); % label building terrain
            set( MRI_bldg_d0_edit,'String', d0_label);
            set( MRI_bldg_terrain_edit,'String',bldg_struct.terrain ); % label building terrain
            set( MRI_bldg_edit,'String',bldg_full_filename);
            set( MRI_bldg_display, 'Enable', 'on');
            if ~isempty(DIF_pathname)
                DIFindex;
            end
            if ~isempty(HDF_index)
                HDFlist;
            end
        end
    end
end

function PlotBLDG( src, event)
    plotBLDG(bldg_struct)
end

function SensFactors( src, event)
    [s_f, ok] = sens_factors;
end

function HDFbrowse( src, event )
    % Load pathname for HDF files used in last analysis, if available, otherwise create empty string:
    if exist('tmp_HDF_toppath.mat','file'), load('tmp_HDF_toppath'); end
    if ~exist('HDF_path','var') || ~isstr(HDF_path), HDF_path = ''; end
    % Prompt user to select pressure database file:
    HDF_path = uigetdir(HDF_path,'Select the top-level directory containing HDF pressure database files');
    if any(HDF_path~=0)
        HDF_pathname = HDF_path;
        set( HDF_edit,'String',HDF_pathname);
        save('tmp_HDF_toppath','HDF_path');
        if exist(fullfile(HDF_pathname, 'HDF_index.mat'),'file')
            S_HDF_index = load( fullfile(HDF_pathname, 'HDF_index.mat') );
            HDF_index = S_HDF_index.HDF_index;
            HDFlist;
        else
            HDFindex;
        end
        set(HDF_update,'Enable','on');
    end
end
function HDFindex( src, event )
    HDF_index = indexHDF( HDF_pathname );
    HDFlist;
end
function HDFlist( src, event )
    HDF_label = {HDF_index.label};
    if ~isempty(bldg_struct.d0)
        d0 = bldg_struct.d0;
        S = diag(s_f);
        S = S/det(S)^(1/4);
        for i=1:length(HDF_index)
            HDF_index(i).d;
            delta_d = abs((HDF_index(i).d - d0)*S);
            HDF_dev(i) = (delta_d*delta_d')^.5;
            HDF_label{i} = ['[Dev: ' num2str(round(HDF_dev(i)))  '] ' HDF_label{i}];
        end
        [HDF_dev, sort_ind] = sort(HDF_dev);
        HDF_label = HDF_label(sort_ind);
        HDF_index = HDF_index(sort_ind);
    end
    ind_O = find(strcmp('Open_Country',{HDF_index.terrain}));
    ind_S = find(strcmp('Suburban',{HDF_index.terrain}));
    set( HDF_open, 'String', HDF_label(ind_O));
    set( HDF_suburb, 'String', HDF_label(ind_S));
    if ~isempty(ind_O) && isfield(bldg_struct,'terrain') && ismember(bldg_struct.terrain,{'Open_Country','Directional'}) 
        set(HDF_open, 'Enable', 'on');
        set(compute_open, 'Enable', 'on');
    else
        set(HDF_open, 'Enable', 'off');
        set(compute_open, 'Enable', 'on');
    end
    if ~isempty(ind_S) && isfield(bldg_struct,'terrain') && ismember(bldg_struct.terrain,{'Suburban','Directional'})
        set(HDF_suburb, 'Enable', 'on');
        set(compute_suburb, 'Enable', 'on');
    else
        set(HDF_suburb, 'Enable', 'off');
        set(compute_suburb, 'Enable', 'off');
    end
end
function OutOptCallback( src, event )
    [out_opt_struct_tmp, ok] = output_options;
    if ok==1
        out_opt_struct = out_opt_struct_tmp;
    end
end

function ComputeOpen( src, event )
    sel = get( HDF_open, 'Value');
    ind_O = find(strcmp('Open_Country',{HDF_index.terrain}));
    HDF_path = HDF_index(ind_O(sel)).pathname;
    if isempty(DIF_pathname)
        DIFbrowse;
    end
    if ~isempty(DIF_pathname) & isempty(out_opt_struct)
        OutOptCallback;
    end
    if ~isempty(DIF_pathname) & ~isempty(out_opt_struct)
        DIF_struct = computeDIF( bldg_struct, HDF_path, out_opt_struct );
    else
        errordlg('Output selection cancelled by user.','Analysis cancelled');
    end
    if exist('DIF_struct') && ~isfield(DIF_struct,'error')
        status = writeDIFfile( DIF_struct, DIF_pathname );
        DIFindex;
    end
end
function ComputeSuburb( src, event )
    sel = get( HDF_suburb, 'Value');
    ind_S = find(strcmp('Suburban',{HDF_index.terrain}));
    HDF_path = HDF_index(ind_S(sel)).pathname;
    if isempty(DIF_pathname)
        DIF_browse;
    end
    if ~isempty(DIF_pathname) & isempty(out_opt_struct)
        OutOptCallback;
    end
    if ~isempty(DIF_pathname) & ~isempty(out_opt_struct)
        DIF_struct = computeDIF( bldg_struct, HDF_path, out_opt_struct );
    else
        errordlg('Output selection cancelled by user.','Analysis cancelled');
    end
    if exist('DIF_struct') && ~isfield(DIF_struct,'error')
        status = writeDIFfile( DIF_struct, DIF_pathname );
        DIFindex;
    end
end
    
function DIFbrowse( src, event )
    % Load pathname for DIF file used in last analysis, if available, otherwise create empty string:
    if exist('tmp_DIF_path.mat','file'), load('tmp_DIF_path'); end
    if ~exist('DIF_path','var') || ~isstr(DIF_path), DIF_path = ''; end
    DIF_path = uigetdir(DIF_path,'Select folder for storing DIF files');
    if any(DIF_path~=0)
        DIF_pathname = DIF_path;
        set( DIF_edit,'String',DIF_pathname);
        set( MRI_edit,'String',DIF_pathname);
        save('tmp_DIF_path','DIF_path');
        if ~isempty(bldg_struct.d0)
            DIFindex;
        end
%    else
%        errordlg('DIF folder selection cancelled by user.', 'Error selecting DIF Folder');
    end

end

function DIFindex( src, event)
    DIF_index = readDIFpath( DIF_pathname, bldg_struct );
    if ~isempty(DIF_index)
        ind_1 = find([DIF_index.panel]==1);
        ind_2 = find([DIF_index.panel]==2);
        ind_3 = find([DIF_index.panel]==3);
        ind_4 = find([DIF_index.panel]==4);
        ind_3_1 = [find([DIF_index.panel]==3) find([DIF_index.panel]==1)];
        ind_4_2 = [find([DIF_index.panel]==4) find([DIF_index.panel]==2)];
        set( DIF_open, 'String', {DIF_index(ind_1).filename});
        set( DIF_suburb, 'String', {DIF_index(ind_2).filename});
        set( interpDIF_open, 'String', {DIF_index(ind_3).filename});
        set( interpDIF_suburb, 'String', {DIF_index(ind_4).filename});
        set( DIF_all_open, 'String', {DIF_index(ind_3_1).filename});
        set( DIF_all_suburb, 'String', {DIF_index(ind_4_2).filename});
        if ~isempty(ind_1) & ismember(bldg_struct.terrain,{'Open_Country','Directional'})
            set(DIF_open, 'Enable', 'on','Max',length(ind_1),'Min',0,'Value',1);
            set(plot_open, 'Enable', 'on');
            set(DIF_all_open, 'Enable', 'on');
            set(DIF_open_chkbox,'Enable','on','Value',1);
        else
            set(DIF_open, 'Enable', 'off');
            set(plot_open, 'Enable', 'off');
            set(interp_open, 'Enable', 'off');
            set(DIF_all_open, 'Enable', 'off');
            set(DIF_open_chkbox,'Enable','off','Value',0);
        end
        if ~isempty(ind_2) & ismember(bldg_struct.terrain,{'Suburban','Directional'})
            set(DIF_suburb, 'Enable', 'on','Max',length(ind_2),'Min',0,'Value',1);
            set(plot_suburb, 'Enable', 'on');
            set(DIF_all_suburb, 'Enable', 'on');
            set(DIF_suburb_chkbox,'Enable','on','Value',1);
        else
            set(DIF_suburb, 'Enable', 'off');
            set(plot_suburb, 'Enable', 'off');
            set(interp_suburb, 'Enable', 'off');
            set(DIF_all_suburb, 'Enable', 'off');
            set(DIF_suburb_chkbox,'Enable','off','Value',0);
        end
        if ~isempty(ind_3) & ismember(bldg_struct.terrain,{'Open_Country','Directional'})
            set(interpDIF_open, 'Enable', 'on');
            set(plot_interp_open, 'Enable', 'on');
            set(DIF_all_open, 'Enable', 'on');
            set(DIF_open_chkbox,'Enable','on','Value',1);
        else
            set(interpDIF_open, 'Enable', 'off');
            set(plot_interp_open, 'Enable', 'off');
        end
        if ~isempty(ind_4) & ismember(bldg_struct.terrain,{'Suburban','Directional'})
            set(interpDIF_suburb, 'Enable', 'on');
            set(plot_interp_suburb, 'Enable', 'on');
            set(DIF_all_suburb, 'Enable', 'on');
            set(DIF_suburb_chkbox,'Enable','on','Value',1);
        else
            set(interpDIF_suburb, 'Enable', 'off');
            set(plot_interp_suburb, 'Enable', 'off');
        end
    else
        set(DIF_open, 'String','','Enable', 'off');
        set(plot_open, 'Enable', 'off');
        set(interp_open, 'Enable', 'off');
        set(DIF_suburb, 'String','','Enable', 'off');
        set(plot_suburb, 'Enable', 'off');
        set(interp_suburb, 'Enable', 'off');
        set(interpDIF_open, 'String','','Enable', 'off');
        set(plot_interp_open, 'Enable', 'off');
        set(interpDIF_suburb, 'String','','Enable', 'off');
        set(plot_interp_suburb, 'Enable', 'off');
        set(DIF_all_suburb, 'Enable', 'off');
        set(DIF_suburb_chkbox,'Enable','off','Value',0);
        set(DIF_all_open, 'Enable', 'off');
        set(DIF_open_chkbox,'Enable','off','Value',0);
    end
    if (get(DIF_open_chkbox,'Value') | get(DIF_suburb_chkbox,'Value')) & ~isempty(HURR_struct_array)
        set(compute_MRI_button,'Enable','on');
    end
end

function SelectOpen( src, event)
    sel_ind = get(DIF_open, 'Value');
    if length(sel_ind)>1
        set(interp_open,'Enable','on');
        set(plot_open,'Enable','off');
    else
        set(interp_open,'Enable','off');
        set(plot_open,'Enable','on');
    end
end
function PlotOpen( src, event )
    sel_ind = get(DIF_open, 'Value');
    if length(sel_ind)>1
        errordlg('Please select only one DIF file at a time for plotting.','Error plotting DIF results');
    else
        ind_1 = find([DIF_index.panel]==1);
        DIF_struct = readDIFfile( fullfile( DIF_pathname, DIF_index(ind_1(sel_ind)).filename) );
        plotDIF( DIF_struct);
    end
    
end
function InterpOpen( src, event )
    sel_ind = get(DIF_open, 'Value');
    if length(sel_ind)==1
        errordlg('Please select more than one DIF file for interpolating.','Error interpolating DIF results');
    else
        ind_1 = find([DIF_index.panel]==1);
        for i = 1:length(sel_ind)
            DIF_struct{i} = readDIFfile( fullfile( DIF_pathname, DIF_index(ind_1(sel_ind(i))).filename) );
        end
        interpDIF_struct = interpDIF( DIF_struct );
        if ~isfield(interpDIF_struct,'error');
            status = writeDIFfile( interpDIF_struct, [] );
            DIFindex;
        end
    end
    
end

function SelectSuburb( src, event)
    sel_ind = get(DIF_suburb, 'Value');
    if length(sel_ind)>1
        set(interp_suburb,'Enable','on');
        set(plot_suburb,'Enable','off');
    else
        set(interp_suburb,'Enable','off');
        set(plot_suburb,'Enable','on');
    end
end
function PlotSuburb( src, event )
    sel_ind = get(DIF_suburb, 'Value');
    if length(sel_ind)>1
        errordlg('Please select only one DIF file at a time for plotting.','Error plotting DIF results');
    else
        ind_2 = find([DIF_index.panel]==2);
        DIF_struct = readDIFfile( fullfile( DIF_pathname, DIF_index(ind_2(sel_ind)).filename) );
        plotDIF( DIF_struct);
    end
    
end

function InterpSuburb( src, event )
    sel_ind = get(DIF_suburb, 'Value');
    if length(sel_ind)==1
        errordlg('Please select more than one DIF file for interpolating.','Error interpolating DIF results');
    else
        ind_2 = find([DIF_index.panel]==2);
        for i = 1:length(sel_ind)
            DIF_struct{i} = readDIFfile( fullfile( DIF_pathname, DIF_index(ind_2(sel_ind(i))).filename) );
        end
        interpDIF_struct = interpDIF( DIF_struct );
        if ~isfield(interpDIF_struct,'error')
            status = writeDIFfile( interpDIF_struct, [] );
            DIFindex;
        end
    end
end


function PlotInterpOpen( src, event )
    sel_ind = get(interpDIF_open, 'Value');
    if length(sel_ind)>1
        errordlg('Please select only one DIF file at a time for plotting.','Error plotting DIF results');
    else
        ind_3 = find([DIF_index.panel]==3);
        DIF_struct = readDIFfile( fullfile( DIF_pathname, DIF_index(ind_3(sel_ind)).filename) );
        plotDIF( DIF_struct);
    end
    
end

function PlotInterpSuburb( src, event )
    sel_ind = get(interpDIF_suburb, 'Value');
    if length(sel_ind)>1
        errordlg('Please select only one DIF file at a time for plotting.','Error plotting DIF results');
    else
        ind_4 = find([DIF_index.panel]==4);
        DIF_struct = readDIFfile( fullfile( DIF_pathname, DIF_index(ind_4(sel_ind)).filename) );
        plotDIF( DIF_struct);
    end
    
end


% Callbacks for MRI page


function HURRbrowse( src, event)
    % Load hurricane pathname used in last analysis, if available, otherwise create empty string:
    if exist('tmp_HURR_path.mat','file'), load('tmp_HURR_path'); end
    if ~exist('HURR_path','var') || ~isstr(HURR_path), HURR_path = ''; end
    HURR_path = uigetdir(HURR_path,'Select folder containing hurricane wind speeds');
    if any(HURR_path~=0)
        HURR_pathname = HURR_path;
        HURR_struct_array = readHURRpath( HURR_pathname );
        if isfield( HURR_struct_array,'error')
            err = errordlg('No valid hurricane wind speed files identified in the specified directory.',...
                'Error searching hurricane path');
            HURR_struct_array = struct([]);
        else
            save('tmp_HURR_path','HURR_path'); % save pathname to be used as default value in next analysis
            set(HURR_edit,'String',HURR_pathname);
            set(HURR_list,'String',{HURR_struct_array.label},'Enable','on');
            set(MRI_listbox,'String',MRI_cell,'Min',1,'Max',length(MRI_cell),'Value',MRI_init_ind,'Enable','on');
            if (get(DIF_open_chkbox,'Value') | get(DIF_suburb_chkbox,'Value'))
                set(compute_MRI_button,'Enable','on');
            end
        end
    end
end

function DIFChkboxCallback( src, event)
    if (get(DIF_open_chkbox,'Value') | get(DIF_suburb_chkbox,'Value')) & ~isempty(HURR_struct_array)
        set(compute_MRI_button,'Enable','on');
    else
        set(compute_MRI_button,'Enable','off');
    end
end


function ComputeMRIs( src, event)
    next_ind = 1;
    if get(DIF_open_chkbox,'Value')
        ind_3_1 = [find([DIF_index.panel]==3) find([DIF_index.panel]==1)];
        open_ind = get(DIF_all_open, 'Value');
        DIF_struct_cell{1} = readDIFfile( fullfile( DIF_pathname, DIF_index(ind_3_1(open_ind)).filename) );
        next_ind = 2;
    end
    if get(DIF_suburb_chkbox,'Value')
        ind_4_2 = [find([DIF_index.panel]==4) find([DIF_index.panel]==2)];
        suburb_ind = get(DIF_all_suburb, 'Value');
        DIF_struct_cell{next_ind} = readDIFfile( fullfile( DIF_pathname, DIF_index(ind_4_2(suburb_ind)).filename) );
    end
    MRI_struct = computeMRIpeaks( bldg_struct, DIF_struct_cell, HURR_struct_array(get(HURR_list,'Value')), MRI_list(get(MRI_listbox,'Value')) );
    if ~isfield(MRI_struct,'error')
        plotMRI( MRI_struct );
        status = writeMRIfile( MRI_struct );
    end
end

function DIFToggleCallback( src, event)
    set( DIF_page, 'Visible','on','WindowStyle','modal','Position',get(MRI_page,'Position'))
    set( MRI_page, 'Visible', 'off','WindowStyle','normal')
    set( MRI_DIF_toggle, 'Value', 0);
end
function MRIToggleCallback( src, event)
    set( MRI_page, 'Visible','on','WindowStyle','modal','Position',get(DIF_page,'Position'))
    set( DIF_page, 'Visible', 'off','WindowStyle','normal')
    set( DIF_MRI_toggle, 'Value', 0);
end

function ExitCallback( src, event )
    close(DIF_page);
    close(MRI_page);
end
    



end