function [mesh_file, meshstruct] = MappingMethod(mainguifighandle,varargin);
% [FILE, MESH] = MappingMethod(FIGHANDLE,varargin) 
% This function accepts the handle to the main GUI figure FIGHANDLE and returns
% the string FILE with the name of the mesh file and the struct MESH. It
% loads a mesh file which is the result of running the program mesh_3tri
% (or using the DrawMesh GUI). The variable input argument list varargin can 
% include nothing, or the name of the mesh file to load.
% The struct MESH has the following fields:
%     MESH.size     = [number of elements , 
%                      number of global nodes , 
%                      number of nodes per element , 
%                      global number of degrees of freedom , 
%                      number of degrees of freedom per element] 
%     MESH.xcoords  = [1 x (number of nodes)] X coordinate of each node
%     MESH.ycoords  = [1 x (number of nodes)] Y coordinate of each node
%     MESH.connect  = [(number of elements) x 3] connectivity array
%     MESH.edges    = [7 x (number of edge elements)] : information about mesh boundaries
%                                 row 1: first node number
%                                 row 2: second node number
%                                 row 3: not used
%                                 row 4: not used
%                                 row 5: boundary number
%                                 row 6: not used 
%                                 row 7: not used

% prompt for mesh file name if need be
if nargin>1
    mesh_file = varargin{1};
else
    mesh_file = [];
end

% load the mesh file
if (mesh_file)
    [fid,message] = fopen(mesh_file,'r');
    if(fid==-1)
        disp(message);
    end
else
    filterspec = '*.dat';
    title = 'Choose mesh file';
    [mesh_file,mesh_path] = uigetfile(filterspec, title);
    [fid,message] = fopen(fullfile(mesh_path,mesh_file),'r');
    if(fid==-1)
        disp(message);
    end
end

% indicate BUSY in figure window
ToggleStatus(1,mainguifighandle);

% read in mesh data
meshdata = fscanf(fid,'%5d',5);

numel = meshdata(1);
numnp = meshdata(2);
meltyp= meshdata(3);
iband = meshdata(4);
neledg= meshdata(5);
numeq = 2*numnp;
nnpe = meltyp;
ndof = nnpe*2;

% initialize global arrays to zeros
x = zeros(1,numnp);
y = zeros(1,numnp);
np = zeros(numel,nnpe);
edges = zeros(7,neledg);

% read coordinates
for ifor = 1:numnp
    coord = fscanf(fid,'%g',2);
    x(ifor) = coord(1);
    y(ifor) = coord(2);
end

% read connectivity
for ifor = 1:numel
    node_ele = fscanf(fid,'%5d',nnpe+1);
    for jfor = 1:1:nnpe
        np(ifor,jfor) = node_ele(jfor+1);
    end
end

% read edge info
for ifor = 1:neledg
    edgeinfo = fscanf(fid,'%5d',7);
    edges(:,ifor)=edgeinfo;
end
status = fclose(fid);

% make output struct and indicate WAITING in figure window
meshstruct = struct('size',[numel numnp nnpe numeq ndof],...
    'xcoords', x,...
    'ycoords', y,...
    'connect', np,...
    'edges', edges);
ToggleStatus(0,mainguifighandle);

