function [EPH, TOE, SV] = readnav(navfil)
%readnav     [EPH, TOE, SV] = readnav(navfil)
%
%EPH = matrix of broadcast elements, one column per satellite
%TOE = vector of ephemeris times, one per satellite
%SV  = vector of satellite PRN numbers
%
%To access the information from the nth satellite (PRN SV(n)),
%use EPH(:,n) and TOE(n)
%
%January 23, 1999.  PFC.

if nargin ~=1
  help readnav
  error('Incorrect usage.')
end

nav=fopen(navfil,'r');

if nav == -1
  error(['Could not open ',navfil,'.'])
end

%Pre-allocate some memory

	d=dir(navfil);
	estlines=d.bytes/70;
	info=setstr(repmat(32,floor(estlines),80));
	nullstr=setstr(repmat(32,1,80));

%Read information from file

	c=0;
	line=fgets(nav,81);
	if line ~=-1
		while(line~=-1)
			c=c+1;
      	info(c,:)=[strrep(line(1:end-1),'D','E'),nullstr(1:80-size(line(1:end-1),2))];
			line=fgets(nav,81);
		end
	else
 		error('Problem reading file.');
	end

%Close file, free up unused memory

	fclose(nav);
	if c<estlines
		info(c+1:end,:)=[];
	end

%Parse information

	nlines=str2num(info(1,1:10))+5;
	info(1:find(info(:,61)=='E'&info(:,62)=='N'),:)=[];
	SV=unique(str2num(info(:,1:2)));
	numsats=size(SV,1);
	satindex=1:numsats;
	EPH=zeros(nlines*4+3,numsats);
	TOE=zeros(numsats,1);

	for i=1:numsats
		place=(max(find(str2num(info(:,1:2))==SV(i)))-1)*8+1;
		q=sscanf(info(place,:),'%f');
		Y=1900+(q(2)<80)*100+q(2);
		M=q(3);
		D=q(4)+q(5)/24+q(6)/1440+q(7)/86400;
		TOE(i,1)=floor(365.25*(Y-(M<=2)))+floor(30.6001*(M+12*(M<=2)+1))+D-679019;
   	EPH(1:3,i)=q(8:10);
		for j=1:nlines
     		k=(j-1)*4+4;
     		q=sscanf(info(place+j,:),'%f');
	  		EPH(k:k+length(q)-1,i)=q;
   	end
	end



