function satlan2(FileIn, FileOut)
% SATLAN2  - Converts argos telemetry from a STOR-DAT into an ASCIICON compatible file
%
% This function reads data from the file sat_messages.txt and writes to the file
% data.raw
%
% Use as: satlan2(FileIn, FileOut)
% Inputs: FileIn = path and name of the file containing argos messages (cf. SATLAN1)
%         FileOut = path and name of file to write to. (default: sat_data.raw)
%         If these aren't specified, it defaults to:
%           FileIn = sat_messages.txt in the current directory
%           FileOut = sat_data.raw in the current directory
%
% Note: For ASCIICON to corectly process this file

% Satlan2.m
%
% Converts ARGOS telemetry from a STOR-DAT into an ASCIICON compatible file.
%
% Version: 1.0, 1997-10-24, Jeff Scrutton, Satlantic
%
% Input:
%	reads the file sat_messages.txt
%
% Output:
%	write the file sat_data.raw
%
% Notes:
%	* This script does not put out decent time stamps
%	* Channel selection is currently set for MBARI units as deployed by Pete Strutton.
% 
% 05 Mar 1999; Brian Schlining; Modified file and output
% 17 Mar 1999; Added time stamp from argos messages to FileOut

if ~nargin
   FileIn  = 'sat_messages.txt';
   FileOut = 'sat_data.raw';
end

if nargin < 2
   %[PathOut FileTrash Ext] = fileparts(FileIn);
   PathOut = pwd;
   FileOut = [PathOut filesep 'sat_data.raw'];
end


channels = [ 1 2 3 4 5 6 7 8 9 10 11 ];	% Select desired channels (should match cal file...)

fin      = fopen(FileIn,'r');
if fin == -1
   fprintf(1,'[SATLAN2]: Unable to read %s \n\n',FileIn)
   return
end

fout = fopen(FileOut,'w','b'); % ASCIICON expects the motorola format
if fout == -1
   fprintf(1,'[SATLAN2]: Unable to open %s for writing\n\n',FileOut)
   fclose(fin)
   return
end

rows = 1;
while ~feof(fin)
   
   S = fgetl(fin); % get a line
   
   % Parse the fields
   time  = S(1:19);
   data  = S(22:length(S));
   itime = S(length(S)-3:length(S)-2);   %instrument time stamp
   word  = sscanf(data,'%X'); %sscanf with X(hex with uppercase letters) convert to decimal
   
   % Grab the serial number		
   serialnumber = word(1);
   
   % Grab the 16-bit channels
   chan(1:8) = word(2:9);		
   
   % Grab the 12-bit channels
   S          = sprintf('%04X',word(10:15)); 
   chan(9:16) = sscanf(S,'%3X',8);
   
   % write record header
   fprintf(fout,'SATDAT%04d',serialnumber);
   rec = sprintf('SATDAT%04d',serialnumber); %write the S/N as a string to rec
   
   % write data
   for c = channels
      rec     = [rec sprintf('%6d',chan(c)) ]; 
      %chan(c) = fix(chan(c)/256) + rem(chan(c),256)*256;	% swap bytes intel->motorola only works for 2byte data
      fwrite(fout,chan(c),'int16');		                  
   end
   
   % write time stamp, this should be in time_t format, writing 0 for now.
   %
   % Matlab 4 has no support to deal with time_t formats. The ARGOS time tag
   % is probably a good candidate to write but there is no way to convert it.
   %
   % The most significant byte of word(16) is the actual instrument's
   % time stamp but due to the limited size of the telemetry frame it was
   % truncated to one byte. That stamp is the time of day, referenced to
   % midnight (=0) and a day being 240 "ticks" long.
   %
   % Remember the data is in motorola format if adding this field for real.
   %
   
   Year   = sscanf(time(1:4),'%i');
   Month  = max(sscanf(time(6:7),'%i'));
   Day    = max(sscanf(time(9:10),'%i'));
   Hour   = max(sscanf(time(12:13),'%i'));
   Minute = max(sscanf(time(15:16),'%i'));
   Second = max(sscanf(time(18:19),'%i'));
   % Convert to unix format (seconds since january 1, 1970)
   D      = (datenum(Year,Month,Day,Hour,Minute,Second) - datenum(1970,1,1,0,0,0))*86400; 

   fwrite(fout,D,'int32');
   
   % write milli second stamp

   fwrite(fout,0,'int16');
   
   % display record in a readable form
   rec          = [rec ' ' time];
   time1(rows,:)= [time,' ', itime];
   rows         = rows+1;
   %fprintf(1,'%s\r\n',rec);
end


fclose('all');

