function Aux = satlan_raw2out(CalFile, InFile, OutFile)
% SATLAN3   - Convert raw binary satlantic files to processed text files
%
% Use As: Aux = satlan_raw2out(CalFile, InFile, OutFile)
%         Aux = satlan_raw2out(CalFile, InFile)
%         Aux = satlan_raw2out(CalFile)
%
% Inputs: CalFile =  Name of calibration file, if empty satlan3 searches for it.
%         InFile  =  name of binary satlantic to read (from satlan2) (default = sat_data.raw)
%         OutFile =  Name of file to write to (default = sat_data.out)
%
% Ouputs: Aux = auxillary data structure used by satlan_write
%
% satlan_raw2out can automatically look for serial numbers with in the InFile and 
% select the most recent version of the calibration file. To use this feature try:
%    satlan_raw2out([], InFile, OutFile) or
%    satlan_raw2out(\\tsunami.shore.mbari.org\calfiles\, InFile, OutFile)
%
% Requires: ASCIICON, GETFNAME


% Brian Schlining
% 19 Mar 1999
% 25 May 1999; Renamed satlan_raw2out form satlan3

if nargin < 3
   OutFile = 'sat_data.out';
   if nargin < 2 
      InFile = 'sat_data.raw';
   end
end

if ~nargin
   CalFile = [];
end


[CalPath CalFilename CalExt] = fileparts(CalFile);


% Get the correct calfile
if nargin < 1 | isempty(CalFilename)
   
   fid    = fopen(InFile,'r','b');
   
   if fid == -1
      error(['Unable to read file ' InFile])
   end
   
   % Bytes 13:20 contain the ascii serial number of the first record
   % Skip over to byte 7 to start reading
   status = fseek(fid, 7, 'bof');
   if status == -1
      fclose(fid);
      error(['Input File, ' InFile ', is not a satlantic binary data file'])
   end
   
   % Read all the serial numbers in the file (sometimes there is more than one)
   % Each record is 36 bytes long with the serial numbers written as ascii between
   % bytes 13 and 20 (4 ascii digits).
   OK = 1;
   n = 1;
   while OK
      SNBuffer  = max(fscanf(fid,'%4d'));
      if ~isempty(SNBuffer)
         SN(n) = SNBuffer;
      else
         SN(n) = 0;
      end
      
      status = fseek(fid, 36, 'cof');
      OK     = status + 1;
   end

   fclose(fid);

   % Find the serial number that occurs most often within the file
   SNums = unique(SN);
   for i = 1:length(SNums)
      j(i) = find(SN == SNums(i));
   end
   i = min(find(j == max(j)));   % use min in case two serial numbers occur with equal frequency
   SerialNumber = SN(i);
   
   % Find all calibration files (named datXXXXx.cal where XXXX has the serial number) with 
   % the same serial number. Then find the most recent version of that file by looking at
   % x where x = a,b,c,d,... Letter later in the alphabet indicate a more recent version 
   % (i.e a is earliest, b next then c and so on)
   if isempty(CalPath)
      CalFiles = sortrows(getfname('\\tsunami.shore.mbari.org\oasis\eqpac\stordat\dat0*.cal'));
   else 
      CalFiles = sortrows(getfname([CalFile 'dat0*.cal']));
   end
   
   
   [r c]    = size(CalFiles);
   n = 1;
   for i = 1:r
      CF(i,1) = sscanf(CalFiles(i,:),'dat%4d');      
   end
   j = find(CF == SerialNumber);
   if length(j) > 1
      V = abs(CalFiles(j,8));   
      i = max(find(V == max(V)));
      j = j(i);
   end

   if isempty(CalPath)
      CalFile = deblank(['\\tsunami.shore.mbari.org\oasis\eqpac\stordat\' CalFiles(j,:)]);
   else 
      CalFile = deblank([CalPath filesep CalFiles(j,:)]);
   end
   
end

c = asciicon(CalFile, InFile, OutFile);



%========================================
% Add Auxillary information if requested
%========================================
A.FileName        = OutFile;
A.BinarySrcFile   = InFile;
A.CalibrationFile = deblank(CalFiles(j,:));
A.SerialNumber    = SN;

if nargout
   Aux = A;
end


