function [input_file, propstruct, bcinfostruct, pbcinfo]=ImportInput(fighandle)
% [FILE,PROPS,BCINFO] = ImportInput(FIGHANDLE)
% This function accepts the handle FIGHANDLE to the main GUI figure and
% returns the string FILE with the name of the input file, the struct PROPS
% with the fields:
%   PROPS.twoDflag      = 1 or 2 for plane strain or plane stress
%   PROPS.youngmodulus  = scalar value of Young's modulus
%   PROPS.poissonratio  = scalar value of Poisson's ratio
%   PROPS.bodyforce     = [x direction body force, y direction body force]
%   PROPS.thermalcoeff  = scalar value of coefficient of thermal expansion
%   PROPS.deltatemp     = scalar value temperature change to which the body is subjected
% and the struct BCINFO with the fields:
%   BCINFO.bccodes      = [1 X # of nodal points] vector indicating type of boundary condition
%   BCINFO.bcnangles    = [1 X # of nodal points] vector with angle between local and
%                         global coordinate systems
%   BCINFO.bcboundts    = [1 X # of nodal points] vector of tangential boundary conditions
%   BCINFO.bcboundns    = [1 X # of nodal points] vector of normal boundary conditions
%   BCINFO.codes        = [1 X # of boundaries] vector indicating type of boundary condition
%   BCINFO.normbcs      = [1 X # of boundaries] vector of normal boundary conditions
%   BCINFO.tangbcs      = [1 X # of boundaries] vector of tangential boundary conditions
%   BCINFO.pointbcs     = [# of point BCs X 6] info about point BCS
%
% Before calling this program, there must be an active mesh file in the
% main GUI. 

% get handles from main GUI.
handles=guidata(fighandle);

% get the input file name from user
filterspec = '*.in';
title = 'Choose input file';
[input_file,pn] = uigetfile(filterspec, title);
loadfile=[pn,input_file];
[fid,message] = fopen(loadfile,'r');
if(fid==-1)
    disp(message);
end
ToggleStatus(1,fighandle);

% get mesh information from handles structure
meshstruct = handles.mesh;
meshdata=meshstruct.size;
numel = meshdata(1);
numnp = meshdata(2);
nnpe = meshdata(3);
numeq = meshdata(4);
ndof = meshdata(5);
x=meshstruct.xcoords;
y=meshstruct.ycoords;
np=meshstruct.connect;
edges = meshstruct.edges;


% read in all of the data from the file
ipsos = fscanf(fid,'%d',1); %is plane strain or stress
if(ipsos<=0)
    error('ipsos must be one or two')
end

E = fscanf(fid,'%lf',1); %young's mod

nu = fscanf(fid,'%lf',1); % poisson's ratio
if(nu<0.0)
    error('nu must be greater than zero')
end

alpha = fscanf(fid,'%lf',1); % coeff of thermal expansion

% if E is less than zero, the next line will contain anisotropic moduli.
% read those now.
if(E<=0.0)
    anisomods=fscanf(fid,'%lf',9);
end

bodyfx = fscanf(fid,'%lf',1); % x-component of body force
if(bodyfx<0.0)
    error('bodyfx cannot be negative')
end

bodyfy = fscanf(fid,'%lf',1); % y-component of body force
if(bodyfy<0.0)
    error('bodyfy cannot be negative')
end
bodyf=[bodyfx;bodyfy]; % combine into one body force vector

dT = fscanf(fid,'%lf',1); % temperature change

numbound = fscanf(fid,'%lf',1); % number of global boundaries
if(numbound<0.0)
    error('numbound must be greater than zero')
end


%read in boundary conditions for each global boundary
for i = 1:numbound  %for each global boundary
    bcinfo = fscanf(fid,'%lf',3);
    code(i)  =bcinfo(1);  % type of BC
    normbc(i)=bcinfo(2);  % normal component
    tangbc(i)=bcinfo(3);  % tangential component
end


npbcs = fscanf(fid,'%lf',1); % number of point boundary conditions
if(npbcs>0)
    for j=1:npbcs
        pbcinfo(j,:) = fscanf(fid,'%lf',6);
%         POS(j,:)=pbcinfo(1:2); % location of point BC
%         pcode(j)=pbcinfo(3);   % type of BC
%         px(j)   =pbcinfo(4);   % local x component
%         py(j)   =pbcinfo(5);   % local y component
%         pa(j)   =pbcinfo(6);   % angle btwn local, global coord systems
    end
else
    pbcinfo=[];
end

status = fclose(fid);

% distribute edge bcs to each node
[bccode,nangle,boundn,boundt] = assignbcs(numnp,x,y,np,edges,code,normbc,tangbc);

% generate output structures
bcinfostruct = struct('bccodes', bccode,...
    'bcnangles', nangle,...
    'bcboundts', boundt,...
    'bcboundns', boundn,...
    'codes',code,...
    'normbcs',normbc,...
    'tangbcs',tangbc);

propstruct = struct('twoDflag', ipsos,...
    'youngmodulus', E,...
    'poissonratio', nu,...
    'bodyforce', bodyf,...
    'thermalcoeff',alpha,...
    'deltatemp', dT);
if exist('anisomods')
    propstruct.anisomods=anisomods;
end

ToggleStatus(0,fighandle);
