function out = Parser(FID)
%out = Parser(FID)
%This Function takes a line from standard out and finds any useful information
%This function returns a value of -1 if there no data (no charectors, all comments and/or whitespace)
%If there is data (seperated by whitespace) this function removes extra whitespace
%and returns a space seperated list.  This function returns -2 if the end of the file has been reached
%
%INPUT 
%FID = pointer to the file that is being read
%OUTPUT
%out = a string that is has formatted (comments removed, whitespace decreases to single spaces)
%out = -1 if there was no data on the line (all whitespace and/or comments)
%
%Written by K. Steffen (ksteffen@unm.edu) at the University of New Mexico,
%Department of Earch and Planetary Science
%Copyright 2003 Kurt Steffen
%
%Last Modified June 17, 2003 

string = fgetl(FID);
if length(string) == 0
	out = -1;
	return
end
if string == -1 %end of file
	out  = -2;
	return 
end

commentindex = find(string == '%');

if ~isempty(commentindex) %removing comments 
	string(commentindex:end) = [];
end

stringL = length(string);
if stringL == 0 %empty line exit
	out = -1;
	return
end
	
spacevector = zeros(stringL,1); %finding spaces (actually tabs or spaces)
for k = 1:stringL
	if string(k) == ' ' | string(k) == '	'
		spacevector(k) = 1;
	end 
end

if min(spacevector) == 1 %all spaces or tabs exit
	out = -1;
	return
end

test = 0;
while test == 0  %removing padding from front
	if spacevector(1) == 1
		string(1) = [];
		spacevector(1) = [];	
	else
		test = 1; 
	end
end

test = 0;
while test == 0  %removing padding from back
	if spacevector(end) == 1
		string(end) = [];
		spacevector(end) = [];	
	else
		test = 1; 
	end
end

stringL = length(string);

for k = stringL:-1:2 %removing padding from center
	if spacevector(k) + spacevector(k-1) == 2
		spacevector(k) = [];
		string(k) = [];
	end
end

string(find(string == '	')) = ' ';%converting tabs to spaces

out = string;