function HDF_index = indexHDF( HDF_pathname )

HDF_index = struct([]);

if ~exist('HDF_pathname','var') || isempty(HDF_pathname)
    % 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 folder:
    HDF_path = uigetdir(HDF_path,'Select the top-level directory containing HDF pressure database files');
    if any(HDF_path~=0)
        save('tmp_HDF_toppath','HDF_path');
        HDF_pathname = HDF_path;
    else
        err = errordlg('HDF pressure database folder selection cancelled', 'Analysis cancelled');
        uiwait(err); return;
    end
end
% if ~exist('bldg_struct','var') || isempty(bldg_struct)
%     % 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 = readbldginfo( bldg_full_filename );
%         if isfield(bldg_struct,'error')
%             return;
%         end
%         % Save pathname to be used as default values in next analysis:
%         save('tmp_bldg_path','bldg_path');
%     else
%         err = errordlg('Building input file selection cancelled by user', 'Analysis cancelled');
%         uiwait(err); return;
%     end
% end
% length_units = bldg_struct.length_units;
% d0 = bldg_struct.d0;
% sf = bldg_struct.sf;
% S = diag(sf);
% S = S/det(S)^(1/4);

clr = get(0,'DefaultUicontrolBackgroundColor'); % Background color for figures and dialog boxes
h1 = waitbar(0, 'Please wait, searching directories... ','Color',clr,'WindowStyle','modal',...
    'Name','Creating index of HDF files...');

folder_list{1} = HDF_pathname;
i=1; % initialize counter for number of folders
n=0; % initialize counter for number of folders containing valid HDF files
while i<=length(folder_list)
    waitbar((i-1)/length(folder_list), h1, ['Please wait, searching directory ' num2str(i) ' of ' num2str(length(folder_list))]); drawnow; % update waitbar
    dir_listing = dir(folder_list{i});
    dir_folders = dir_listing(find([dir_listing(:).isdir]==1)); % list of subfolders
    sub_folders = setdiff({dir_folders.name},{'.','..'});
    if ~isempty(sub_folders)
        for j = 1:length(sub_folders)
            sub_folders{j} = fullfile(folder_list{i},sub_folders{j});
        end
        folder_list(length(folder_list)+1:length(folder_list)+length(sub_folders)) = sub_folders;
    end
    dir_files = dir_listing(find([dir_listing(:).isdir]==0)); % list of files
    filenames = {dir_files.name};
    if ~isempty(filenames)
        dir_count = 0; % initialize counter for the number of wind directions found in the current folder
        for j = 1:length(filenames)
            filename_j = filenames{j};
            hdf_ext_ind = strfind( lower(filename_j), '.hdf' );
            if ~isempty(hdf_ext_ind) && hdf_ext_ind(1)==length(filename_j)-3 && length(filename_j)>8
                dir_count=dir_count+1;
                hdf_filename_root{dir_count} = filename_j(1:hdf_ext_ind-5);
                theta_a(dir_count,1) = str2num( filename_j(hdf_ext_ind-4:hdf_ext_ind-1) )/10;
                dir_ind(dir_count) = j;
            end
        end
        if dir_count ==0
            i=i+1; continue;
        else
            hdf_filename_root = unique(hdf_filename_root);
        end
        if length(hdf_filename_root)>1
            warn = warndlg({'The following directory contains inconsistent file names: ', '', ['    ' folder_list{i}] , '',...
                ['All HDF file names in the selected directory must be identical '...
                'except for the wind direction, specified by the last 4 characters before the file extension. ' ...
                'Files in this directory will not be available for analysis.' ]}, ...
                'Error in pressure database folder contents');
            uiwait(warn); i=i+1; continue;
        end
        HDF_full_filename = fullfile(folder_list{i},filenames{dir_ind(1)});
        HDF_struct = load_hdf_coords( HDF_full_filename );
        required_fieldnames = {'Building_Width_Ft','Building_Length_Ft','Building_Height_Ft','Roof_Slope_in_12',...
            'A_Tap_Coordinates_3D_Units'};
        if ~isempty(setdiff(required_fieldnames,fieldnames(HDF_struct)))
            warn = warndlg({['The following HDF pressure database file is missing required field(s): '],...
                '', HDF_full_filename , '','The following fields are required:','',...
                'Building_Width_Ft','Building_Length_Ft','Building_Height_Ft','Roof_Slope_in_12',...
            'A_Tap_Coordinates_3D_Units'}, 'Error in pressure database file contents');
            uiwait(warn); i=i+1; continue;
        elseif HDF_struct.A_Tap_Coordinates_3D_Units ~= 'FS_Feet'
            warn = warndlg({['The following HDF pressure database file contains unrecognized length units ("' HDF_struct.A_Tap_Coordinates_3D_Units  '"): '],...
                '', HDF_full_filename , '',['(Expected units are "FS_Feet".) Files in this directory will not be available for analysis.' ]}, ...
                'Error in pressure database file contents');
            uiwait(warn); i=i+1; continue;
        elseif ~ismember(HDF_struct.Exposure_Name, {'Open_Country', 'Suburban' } )
            warn = warndlg({['The following HDF pressure database file contains unrecognized terrain conditions ("' HDF_struct.Exposure_Name '"): '],...
                '', HDF_full_filename , '',...
                ['(Expected terrain conditions are "Open_Country" or "Suburban".) Files in this directory will not be available for analysis.' ]}, ...
                'Error in pressure database file contents');
            uiwait(warn); i=i+1; continue;
        end
        n=n+1; % increment counter for number of folders containing valid HDF files
        % extract full-scale dimensions of building:
        W = HDF_struct.Building_Width_Ft; L = HDF_struct.Building_Length_Ft; H = HDF_struct.Building_Height_Ft;
        R = round(HDF_struct.Roof_Slope_in_12*W/2*1000)/1000; 
        d = [W L H R];
%        % scaled deviation of model dimensions from building dimensions:
%        delta_d = abs((d - d0)*S);
%        % norm of scaled deviation:
%        dev = (delta_d*delta_d')^.5;

        d_round = round(10*d)/10;
        d_symbol = 'WLHR';
        HDF_label = []; %['[Dev: ' num2str(round(dev))  '] '];
        for k = 1:4
            HDF_label = [HDF_label d_symbol(k) '=' num2str(d_round(k)) ', '];
        end
        current_folder = folder_list{i};
        current_rel_path = current_folder(length(HDF_pathname)+1:end);
        HDF_label = [HDF_label HDF_struct.Exposure_Name(1) ' (...' current_rel_path ')'];
        HDF_index(n).label = HDF_label;
        HDF_index(n).d = d;
%        HDF_index(n).dev = dev;
        HDF_index(n).terrain = HDF_struct.Exposure_Name;
%         if strcmp(HDF_struct.Exposure_Name,'Open_Country')
%             HDF_index(n).panel = 1;
%         else
%             HDF_index(n).panel = 2;
%         end
        HDF_index(n).pathname = folder_list{i};
        HDF_index(n).theta_a = theta_a;
        
        clear dir_ind theta_a hdf_filename_root;
    end
    i=i+1;
end
delete(h1);

if ~isempty(HDF_index)
%    dev = [HDF_index.dev];
%    [dev, ind] = sort(dev);
%    HDF_index = HDF_index(ind);
    save( fullfile(HDF_pathname, 'HDF_index'), 'HDF_index' )
end