function m=ReadDMCModelTextFile(filename)
%function m=ReadDMCModelTextFile(filename)
%	Read an ASCII file into a model structure
%	If the file does not exist returns [].

	% test for proper filename
	if ~isstr(filename)
		error('ReadDMCModelTextFile: Filename must be a string argument!');
	end

	% open the file 
	fid = fopen(filename,'rt');
	if fid == (-1)
		m = [];
		error(['ReadDMCModelTextFile: Could not open file:' filename ]);
	end

	% Read list format
	line = fgets(fid); 	% get the 1st line, if any...

	% read till eof
	while(line ~= [ -1 ])
      [T,R] = strtok(line);
      
      if strcmp(T,'NumPhotons:')
         m.NumPhotons = str2double(strtok(R));
      elseif strcmp(T,'source:')
         m.source = str2double(strtok(R));
      elseif strcmp(T,'mua:')
         m.mua= str2double(strtok(R));
      elseif strcmp(T,'mus:')
         m.mus = str2double(strtok(R));
      elseif strcmp(T,'g:')
         m.g= str2double(strtok(R));
      elseif strcmp(T,'BoundaryShape:')
         m.BoundaryShape= str2double(strtok(R));
      elseif strcmp(T,'radial_size:')
         m.radial_size= str2double(strtok(R));
      elseif strcmp(T,'BoundaryReflect:')
         m.BoundaryReflect = str2double(strtok(R));
      elseif strcmp(T,'BoundaryRefType:')
         m.BoundaryRefType = str2double(strtok(R));
      elseif strcmp(T,'Iwantcutboundary:')
         m.Iwantcutboundary = str2double(strtok(R));
      elseif strcmp(T,'CutSizeAngle:')
         m.CutSizeAngle = str2double(strtok(R))*pi/180;   %make degrees into radians
      elseif strcmp(T,'NumBins:')
         m.NumBins = str2double(strtok(R));
      elseif strcmp(T,'dr:')
         m.dr= str2double(strtok(R));
      elseif strcmp(T,'Diameter:')
         m.Diameter = str2double(strtok(R));
      elseif strcmp(T,'AcceptanceAngle:')
         m.AcceptanceAngle = str2double(strtok(R))*pi/180;  %make deg into radians
      elseif strcmp(T,'GridBinsz:')
         m.GridBinsz = str2double(strtok(R));
      elseif strcmp(T,'GridRange1:')
         m.GridRange1 = str2double(strtok(R));
      elseif strcmp(T,'GridRange2:')
         m.GridRange2 = str2double(strtok(R));
      elseif strcmp(T,'dgz:')
         m.dgz = str2double(strtok(R));
      elseif strcmp(T,'GridBinsx:')
         m.GridBinsx = str2double(strtok(R));
      elseif strcmp(T,'GridWidth:')
         m.GridWidth = str2double(strtok(R));
      elseif strcmp(T,'dgx:')
         m.dgx = str2double(strtok(R));
      else
         if ~(T(1)=='%')
            warning(['didn''t recognise token ',T]);
         end
      end
      
      line = fgets(fid);
   end
   
   %computed values
   if ~(m.dr == m.radial_size/(m.NumBins-1))
      disp(['Warning: Had to change m.dr from ',num2str(m.dr),' to ',num2str(m.radial_size/(m.NumBins-1))]);
      m.dr = m.radial_size/(m.NumBins-1);
   end
  
   if ~(m.dgz == (m.GridRange2-m.GridRange1)/(m.GridBinsz-1))
      disp(['Warning: Had to change m.dgz from ',num2str(m.dgz),' to ',num2str((m.GridRange2-m.GridRange1)/(m.GridBinsz-1))]);
      m.dgz = (m.GridRange2-m.GridRange1)/(m.GridBinsz-1);
   end
    if ~(m.dgx == (m.GridWidth/(m.GridBinsx-1)))
      disp(['Warning: Had to change m.dgx from ',num2str(m.dgx),' to ',num2str(m.GridWidth/(m.GridBinsx-1))]);
      m.dgx = m.GridWidth/(m.GridBinsx-1);
   end
	% close file
	fclose(fid);
