function timesplitrinex(filename,seconds)
% TIMESPLITRINEX splits a RINEX GPS file by time.
% TIMESPLIT(FILENAME,t) breaks a RINEX file into shorter files of length
% t in seconds. Useful for breaking a large file into days, or a day file
% into hours, etc.
% EXAMPLE: If 'myfile.obs' is a day-long rinex file,
% TIMESPLIT('myfile.obs',3600) will make 12 files of 1 hour long segments
% with names: myfile0.obs,myfile1.obs,myfile2.obs,etc.
%
% Ian M. Howat, Applied Physics Lab, University of Washington
% ihowat@apl.washington.edu
% Version 1: 5/1/2004
%load file
outname = filename(1:findstr(filename,'.')-1);
fid = fopen(filename);
%build header
header1 = [];
header2 = [];
endheader = [];
while fid
line = fgetl(fid);
if isempty(findstr(line,'MARKER NAME'));
header1 = char(header1,line);
else
break
end
end
while fid
line = fgetl(fid);
if isempty(findstr(line,'TIME OF FIRST OBS'));
header2 = char(header2,line);
else
firstob = datenum(str2num(line(1:findstr(line,'TIME')-1)));
firstobline = line;
line = fgetl(fid);
lastob = datenum(str2num(line(1:findstr(line,'TIME')-1)));
break
end
end
while fid
line = fgetl(fid);
endheader = char(endheader,line);
if ~isempty(findstr(line,'END OF HEADER'))
break
end
end
header1(1,:) = [];
header2(1,:) = [];
endheader(1,:) = [];
body = [];
outnum = 1;
while fid
line = fgetl(fid);
if line ~= -1
if ~isempty(str2num(line(1:3)))
if str2num(line(1:3)) == 5
tmp = str2num(line);
newob = datenum([tmp(1)+2000,tmp(2:6)]);
if etime(datevec(newob),datevec(firstob)) > seconds
writeit(header1,header2,firstob,lastob,endheader,body,outname,outnum);
body = [];
firstob = newob;
outnum = outnum+1;
else
lastob = newob;
end
end
end
body = char(body,line);
else
writeit(header1,header2,firstob,lastob,endheader,body,outname,outnum);
break
end
end
fclose(fid);
function writeit(header1,header2,firstob,lastob,endheader,body,outname,outnum)
if outnum < 10
fidout = fopen([outname,'0',num2str(outnum),'.obs'],'w');
body(1,:) = [];
A = char(header1,...
[num2str([outname,'0',num2str(outnum)]),' MARKER NAME '],...
header2,...
[' ',num2str(round(datevec(firstob))),'.0000000 TIME OF FIRST OBS '],...
[' ',num2str(round(datevec(lastob))),'.0000000 TIME OF LAST OBS '],...
endheader,body);
else
fidout = fopen([outname,num2str(outnum),'.obs'],'w');
body(1,:) = [];
A = char(header1,...
[num2str([outname,num2str(outnum)]),' MARKER NAME '],...
header2,...
[' ',num2str(round(datevec(firstob))),'.0000000 TIME OF FIRST OBS '],...
[' ',num2str(round(datevec(lastob))),'.0000000 TIME OF LAST OBS '],...
endheader,body);
end;
for k = 1:size(A,1)
fprintf(fidout,'%s\n',A(k,:));
end
fclose(fidout);