function [isparsingcompleted] = saveNSTB(h, filename);
%---------------------------------------------------------
% save parsed NSTB data in SGMP format and show parsing progress
% Ju-Yong Do,  Jan 27, 2009
%---------------------------------------------------------
% input  : parsed data "rx", filename, status bar handle
% output : SGMP file (.mat)
%=========================================================
%=========================================================
global rx option SEC_PER_DAY SEC_PER_FILE
isparsingcompleted  = 0; %indicator whether parsing completed or not.
issaved             = 0; %indicator whether parsed data is saved.

%=========================================================
%save data and show status (for files containing meas)
if isequal(rx.info.sitename(1:3),'Geo'),    %GEO files carry only waas messages
    if isempty(rx.aiding.waas), return;     %no data available
    else
        starttow = rx.aiding.waas(1).tow;   %start tow of this subset
        endtow   = rx.aiding.waas(end).tow; %end tow of this subset  
    end
else                                        %non-GEO files (assumed to contain meas)
    if isempty(rx.meas), return;            %no data available
    else
        starttow = rx.meas(1).tow;          %start tow of this subset
        endtow   = rx.meas(end).tow;        %end tow of this subset
    end
end
isvaliddata   = (endtow>=option.starttow);  %data is in requested time window

%=========================================================        
%save or check status for requested data
if isvaliddata,                                         %found something
    reltow   = endtow-option.starttow;                  %relative time from the beginning of file
    progress = reltow/(option.endtow-option.starttow);  %parsing progress            

    %show parsing progress in every 5 minute worth of data
    if rem(reltow+1, 300)==0,             
        waitbar(progress, h, [option.fname4plot,' (',num2str(progress*100, '%3.0f'),'% parsed)']);            
    end

    % save into .mat file and reset variable
    isendofdata     = (endtow>=option.endtow);          %reached end of data requested
    isbigenough     = (rem(reltow+1, SEC_PER_FILE)==0); %large enough data accumulated to be saved    
    if isendofdata || isbigenough,
        filecount       = floor(reltow/SEC_PER_FILE);   %index to output subfiles (0,1,2...)
        rx.info.starttow= starttow;                     %start tow of this subfile
        rx.info.endtow  = endtow;                       %end tow of this subfile

        disp(['Output file: ',option.outputdir,filename,'_',num2str(filecount,'%2.0f'),'.mat']);
        waitbar(progress, h, [option.fname4plot,' ',num2str(filecount,'%2.0f'),'.mat',' is saved']);            
        save([option.outputdir,filename,'_',num2str(filecount,'%2.0f')], 'rx');

        isparsingcompleted  = (endtow>=option.endtow);  %parsed all requested data? 
        issaved             = 1;                        %data saved?
    end
end

%reset storage if not requested data or after data saved
if  ~isvaliddata || issaved ,    
    rx.meas             =[]; 
    rx.aiding.waas      =[]; 
    rx.aiding.eph       =[]; 
    rx.aiding.iono      =[]; 
    rx.aiding.utc       =[]; 
    rx.aiding.almanac   =[];
    rx.aiding.tms       =[]; 
    rx.aiding.tus       =[]; 
    rx.aiding.sqm       =[]; 
    rx.aiding.sqminfo   =[]; 
    rx.aiding.waasstatus=[];  
end

return;
%=========================================================
%=========================================================