function AuxOut = satlan_argos2out(FileIn,FileOut,Flag);
% SATLAN_ARGOS2OUT - Wrapper function for satlan_parse_argos, satlan_argosmsg2raw and satlan_raw2out
%
% SATLAN_ARGOS2OUT is a wrapper function for several data processing functions.
%  They are combined into this file to make sure that the data is always processed 
%  in a consitent order. This function takes the original argos message in 
%  \\tsunami.shore.mbari.org\oasis\eqpac\satdat and creates a processed file in 
%  \\tsunami.shore.mbari.org\oasis\eqpac\stordat\*.out
%
% Use as: aux = satlan_argos2out
%         aux = satlan_argos2out(FileIn)
%      or aux = satlan_argos2out(FileIn,FileOut,Flag)
%      or aux = satlan_argos2out([],FileOut,Flag)
% 
% Inputs: FileIn = Argos message of satlantic data to be read
%         Flag   = Processing flag
%                  0 -> Do not derive products (default)
%                  1 -> Derive products
%
% Outputs: aux = structure of auxillary data with fields
%                FileName =  Source file
%                CalibrationFile = calibration file used for processign
%                Date = Dates of lat and lon coordinates
%                Lat
%                Lon
%           
%
% Requires: SATLAN1, SATLAN2, SATLAN3, ASCIICON

% Brian Schlining
% 22 Mar 1999
% 25 Mar 1999; Actually got it to work
% 25 May 1999; Changed name form satlan2out to satlan_argos2out

% Need access to GETFNAME
%addpath('\\sunfish.shore.mbari.org\brian_mfiles\common');


%===========================================================
% Select the file to be processed if no imuts are specified
%===========================================================
if ~nargin | isempty(FileIn)
   [infile inpath] = uigetfile('*.*','Select ASCII Eqpac Data File',0,0);
   if infile == 0 
      return
   end
   FileIn = [inpath infile];
else
   [inpath infile inext] = fileparts(FileIn);
   infile = [infile inext];
end

if nargin < 2 | isempty(FileOut);
   FileOut = [pwd filesep infile '.out'];
end

if nargin < 3
   Flag = 0;
end

Aux.FileName  = infile;

%=========================================================================
% Create a temporary directory to write short-named DOS-friendly files to
%=========================================================================
PathS = ['\\tsunami.shore.mbari.org\oasis\eqpac\brian\temp'];
%mkdir('temp');
%============================================================
% Get temporary filenames that are called by other functions
%============================================================
Txt   = [PathS filesep 'sat_messages.txt'];
Raw   = [PathS filesep 'sat_data.raw'];
%Out   = [PathS filesep 'sat_data.out'];
%=========================================================
% Turn argos messages into text files with physical units
%=========================================================
satlan1(FileIn,PathS);                 % Creates a file of good argos messages
satlan2(Txt,Raw);                      % Turns argos messages into binary data
AuxFile = satlan3('\\tsunami.shore.mbari.org\oasis\eqpac\stordat\',Raw,FileOut);  % Finds the calibration file and converts binary data to text 
AuxLoc  = satlan_loc([PathS filesep 'sat_location.txt']); % Get the location and dates of messages
Aux.CalibrationFile = AuxFile.CalibrationFile;
Aux.Date = AuxLoc.Date;
Aux.Lat  = AuxLoc.Lat;
Aux.Lon  = AuxLoc.Lon;

%================================================================================
% Name the output file as the same as the input file except add extension '.out'
% Get rid of the temporary directory
%================================================================================
[trashPath FileName FileExt] = fileparts(FileIn);


%copyfile(Out,FileOut)
trash   = getfname([PathS filesep '*.*']);
[r c]   = size(trash);
for i = 3:r
   delete([PathS filesep trash(i,:)]);
end



%====================================
% Add additional products if Flagged
%====================================
if Flag
   %==============================================================
   % Read the new file into a data structure so we can process it
   %==============================================================
   OUT = readout(FileOut);
     
   OUT.FOUL_none   = OUT.LU_683./OUT.LU_555;              % fouling ration descibes amount of biofouling
   OUT             = prc_ed490(OUT);                      % Derive Ed's from Ed490
   OUT             = prc_lu0(OUT);                        % Derive Lu0+ and Luwn
   OUT.CHLOC2_ugl  = a_oc2_c(OUT.LU_490, OUT.LU_555);     % Calculate chlorphyll using OC2 eqn.
   OUT.CHLRAT_ugl  = 0.7469*(OUT.LU_443./OUT.LU_555).^-1.2797; % Chl from ratio method
   OUT.CHLCZCS_ugl = 10.^(0.053 + 1.71*log10(OUT.LUWN_555./OUT.LUWN_443)); % CZCS chl   
   
   writeout(OUT,FileOut);                                 % Write structure to a tab delimited file   
   
end

if nargout
   AuxOut = Aux;
end
