function satlan_parse_argos(FileIn, PathOut)
% SATLAN_PARSE_ARGOS  - Reads an argos file and parses the data.
%
% Use as: satlan_parse_argos(FileIn, PathOut)
%         satlan_parse_argos(FileIn)
%      or satlan_parse_argos
%
% Inputs: FileIn = Path and/or filename of the argos file to be parsed
%         PathOut = Path to write output files to. Output files are below.
%            Default is to the pwd.
%
% Outputs: Reads in the argos data file and parses it into:
%          sat_messages.txt   - good data
%          sat_regects.txt    - Rejected data
%          sat_log.txt        - Log of all processed data
%          sat_location.txt   - File of all location messages. This is particularly 
%            useful for editing data. Messages outside of the deployment location can be ignored

% Satlantic.m
% Jeff Scrutton
% annotated by Erich Riencker 10-29-97
% 05 Mar 1999; Modified by Brian Schlining
% 30 Mar 1999; Modified output path
% 25 May 1999; Changed name from satlan1 to satlan_parse_argos
%
% Reads in argos data file and parses into good data (messages.txt)  Reject data(regects.txt)
% a log file of all of the processed data (log.txt) and a location file (location.txt)

if ~nargin | isempty(FileIn)
   [FileName,PathName] = uigetfile('\\tsunami.shore.mbari.org\oasis\eqpac\satpac\*.*','ARGOS file...');
   if FileName == 0
      fprintf(1,'\n[SATLAN1]: Program canceled by users request\n\n')
      return;
   end
   FileIn = [PathName FileName];
end

if nargin < 2
   %PathOut = PathName;
   PathOut = pwd;
end

fid = fopen(FileIn,'r'); %read the input file
if fid == -1
   fprintf(1,'[SATLAN1]: Unable to read file %s\n\n',FileIn)
   return
end

logFid  = fopen([PathOut filesep 'sat_log.txt'],'w');		 %open log file	
rejFid  = fopen([PathOut filesep 'sat_rejects.txt'],'w'); %open rejects file
keepFid = fopen([PathOut filesep 'sat_messages.txt'],'w');%open message file
locFid  = fopen([PathOut filesep 'sat_location.txt'],'w');%open location file


separator = '-----';
message   = '';
messages  = 0;
rejects   = 0;
valid     = 0;
false     = 0;
true      = 1;

while ~feof(fid)
   
   % Find start of block...
   S = fgetl(fid);
   while isempty(S)
      S = fgetl(fid);
   end
   
   %first char must be 0 to 9
   if (length(findstr(S(1),'0123456789')) == 0) % first char must be 0 to 9
      warning('Strange input file!');
   end
   
   while S(1) == ' '                              	% while line begins with a space
      S = fgetl(fid);		                        % Just get another line
      if (S == -1)
         fprintf(1,'End of input file.\n');
         fclose('all');
         return;
      end
   end                                             % while line(1)== ' '
   
   
   fprintf(locFid,'%s\r\n',S);                     % print the line to location
   fprintf(logFid,'%s\r\n',S);                   	% print the line to log
   
   
   % The first line of each block starts at the far left side of the page
   % the third value on the line tells how many lines in the block
   n = sscanf(S,'%d',4);%scan the line for doubles no bigger than 4 chars long
   nlines = n(3);				                        %Third value in is the Number of lines in this block
   
   for i = 1:nlines-1;                             % Read so many lines of data for this block
      S = fgetl(fid);
      
      if (S == -1)
         fclose('all');
         error('Premature end of input file.');
      end
      
      if (S(6) == '-');                            %then it is a ' 1997-10-11 ....' type of line
         if (length(message) > 0); 
            text = message(2:22);                  %year-month-day time [int]
            words = sscanf(message(23:length(message)),'%X'); %the rest of the line is hex data
            %the next line creates the output
            text = [text  sprintf(' %04X',words)]; %then we put it back together WITH leading zeros
            reject = false;
            
            if (length(words) ~= 16)          	   % Not a good data message
               reject = true;
            else
               bytes = [fix(words/256) fix(rem(words,256))];
               sum = 0;
               for j = 1 : 31
                  sum = sum + bytes(j);
               end
               if (rem(sum,256) ~= bytes(32))
                  reject = true;
               end
            end
            
            
            if reject
               rejects = rejects + 1;
               fprintf(rejFid,'%s\r\n',text);      %write to the rejects file
               text = [ 'X ' text];
            else
               valid = valid + 1;
               fprintf(keepFid,'%s\r\n',text);     %write the output to the keep file
               text = ['  ' text];
            end
            fprintf(logFid,'%s\r\n',text);         %Good or bad, write to file
            
         end
         message = '';
         messages = messages + 1;
      end
      message = [ message S ];
   end
   
   fprintf(logFid,'%s',separator);
   
end


disp('End of input file.');
fprintf(1,'%d messages processed (%d valid / %d rejects)\r\n',messages,valid,rejects);
fclose('all');
