function [partmap2var_output, output_time] = partmap2variable(part_num,...
    dflow_floc, dflow_beg, dflow_end)
%Extracts information from partitioned files to variable. 
%   Information includes element ID, element domain, element x & y & z, 
%   bounding nodes x & y, element u, v, depth, tau. Data returned is from
%   the last time step of the model run by default.

% Creates last time step as the default output
dflow0part_file = strcat(dflow_floc,dflow_beg,'0000',dflow_end);
time_options = ncread(dflow0part_file,'time');
[num_time_options,~] = size(time_options);
output_time = time_options(num_time_options,1);
time_index = find(time_options == output_time);

% Calls partitioned file and pulls important data
for i = 0:part_num-1
    % Assigning the correct NetCDF file name to be opened
    file_num = sprintf('%04d',i);
    filename = strcat(dflow_beg, file_num, dflow_end);        
    
    read_file = strcat(dflow_floc,filename);
    
    % Assign element information and data for the last time step to matrix
    if exist(read_file,'file') == 2
        x_elem = ncread(read_file,'FlowElem_xcc');
        y_elem = ncread(read_file,'FlowElem_ycc');
        z_elem = ncread(read_file,'FlowElem_zcc');
        area = ncread(read_file,'FlowElem_bac');
        elem_index = double(ncread(read_file,'FlowElemGlobalNr'));
        elem_domain = double(ncread(read_file,'FlowElemDomain'));
        ucx = ncread(read_file,'ucx');
        ucy = ncread(read_file,'ucy');
        u = ucx(:,time_index);
        v = ucy(:,time_index);
        h_matrix = ncread(read_file,'waterdepth');
        depth = h_matrix(:,time_index);
        tau_matrix = ncread(read_file,'taus');
        tau = tau_matrix(:,time_index);
        
        % Because partitions include some nodes from other domains, remove
        % all elements from other domains
        elem_step_raw = [elem_index elem_domain x_elem y_elem z_elem...
            area u v depth tau];
        elem_step = elem_step_raw(elem_step_raw(:,2)==i,:);
        
        % Start matrix or append matrix with new elements
        if i == 0
            elem_matrix = elem_step;
        else
            elem_matrix = [elem_matrix; elem_step];
        end

    end

end

% Assign element matrix to output variable
partmap2var_output = elem_matrix;
