% areflsm.m
%
% reads smoothr and refabs output (as for reference layer plots),
% combines what we want, and writes to "auto_refsm.mat" in the current
% directory (presumably cruiseid/edit)

%WARNING:  because the *.bin file from smoothr is binary, it MUST have 
% been written by the same kind of machine you're running this program on 
% (PC or SUN). Otherwise, you'll just get a Nx1 vector.

% Jules 2000/11/27


%%%% goodies are set by run_areflsm.m the this is called

%---------- end of section to edit ----------

time_range = sprintf(' %f %f ', config.start_dd, config.end_dd + 1);

if ~isempty(refabsfile)
  ref_matname = [ navpath outfile 'ref.mat' ];
  ref_ncol = 9;                  % # of columns to be retrieved
  tmpcmd = fullfile(binpath, 'ref2mat');
  cmd = [ '!'  tmpcmd ' '  navpath refabsfile ' ',...
          ref_matname, time_range sprintf('%d', ref_ncol) ]
  eval(cmd)
else
  ref_matname = '';
end

if ~isempty(smoothrfile)
  sm_matname  = [ navpath outfile 'sm.mat' ];
  sm_ncol = 8;                   % # of columns to be retrieved
  step = 1;                      % default 1
  tmpcmd = fullfile(binpath, 'sm2mat');
  cmd = [ '!' tmpcmd ' ' navpath smoothrfile ' ',...
         sm_matname time_range sprintf('%d %d', step, sm_ncol) ]
  eval(cmd)
else
  sm_matname = '';
end;


% now load the data
load(ref_matname); ref = ref';
if (length(ref) == ref_ncol)
   fprintf('ERROR: your smoothr.bin file was probably written by another\n')
   fprintf('machine type.  You are on a %s: check the smoothr control \n',...
           computer)
   fprintf('used and rerun if necessary')
   error('WARNING')
end


load(sm_matname);  sm = sm';

load([ navpath navfile ]);
eval(sprintf('nav = %s;', navfile(1:end-4)))


%now the next part is straight from refplot: -----------------------------

[ref_nrow ref_ncol] = size(ref);
[sm_nrow sm_ncol] = size(sm);
if (ref_nrow == 0)
  ref = [NaN NaN NaN NaN NaN NaN NaN NaN NaN];
end
if (sm_nrow == 0)
  sm  = [NaN NaN NaN NaN NaN NaN NaN NaN];
end
% refabs and smoothr column indices:
j_reftime  = 1;
j_lon      = 2;
j_lat      = 3;
j_absu     = 4;
j_absv     = 5;
j_dfixtime = 6;
j_gapu     = 7;
j_gapv     = 8;
j_gaptime  = 9;

j_smtime   = 1;
j_sm_u     = 2;
j_sm_v     = 3;
j_smlon    = 6;
j_smlat    = 7;
if (~isnan(sm(1,1)))
  i_goodsm = find(~isnan(sm(:,j_smlon)));
  if isempty(i_goodsm), sm(1,1) = NaN; end
end


if (isnan(ref(1,1)) & (isnan(sm(1,1)) | (size(find(sm(:,2) ~=NaN)) == 0)))
  fprintf('No data available.\n')
  return
end

% edit the refabs input as in smoothr:
minv = ones(size(ref(:, j_reftime))) * min_speed;  % generate a row min_speed
maxv = ones(size(ref(:, j_reftime))) * max_speed;  %    and max_speed
sped = sqrt((ref(:,j_gapu).*ref(:,j_gapu))+(ref(:,j_gapv).* ref(:,j_gapv)));
speed= max(sped, minv);
ratio=(speed .* ref(:,j_gaptime) )./ (maxv .* ref(:,j_dfixtime) );
% ratio = (gap velocity * gaptime) / (maximum velocity * interval of fix );

% EDITING
range = find(ratio <  max_gap_ratio); % find data within  max_gap ratio range

if (length(range) ~= 0)
   ref_u = ref(range, j_absu);     % abs_u
   ref_v = ref(range, j_absv);     % abs_v
   %dt    = ref(range, j_dfixtime); % (fix1_time - fix2_time)
   ref_dday     = ref(range, j_reftime);  % fix time (t)  
end

sm_dday = sm(:,j_smtime);
sm_u = sm(:,j_sm_u);
sm_v = sm(:,j_sm_v);

%really sorry: leaving these as m/s
nav_dday = nav(:,1);
nav_umeas = nav(:,2);
nav_vmeas = nav(:,3);


%sorry: I'm going to make ocean velocities cm/s
ref_u = ref_u*100;
ref_v = ref_v*100;
sm_u = sm_u*100;
sm_v = sm_v*100;

%get rid of bad data
badi = find(nav_umeas == 1E38);
nav_dday(badi)  = [];
nav_umeas(badi) = [];
nav_vmeas(badi) = [];

%I'm also going to get rid of NaNs
badi = find(isnan(ref_u + ref_v + ref_dday));
ref_dday(badi) = [];
ref_u(badi) = [];
ref_v(badi) = [];

%I'm also going to get rid of NaNs
badi = find(isnan(sm_u + sm_v + sm_dday));
sm_dday(badi) = [];
sm_u(badi) = [];
sm_v(badi) = [];


save auto_refsm ref_u ref_v ref_dday sm_dday sm_u sm_v nav_dday nav_umeas nav_vmeas;
