%% run asetup, then use this to determine on-station profile times.  save file
%% adcpllt = dday, lon, lat, mps, osi

if (~isfield(config, 'prefix'))
   config.prefix = 'a_';
end
   
config.os_cutoff = 1;
config.ddrange = (config.end_dd - config.start_dd) + 1;

stable_cutoff = .2;      % for on-station: low jitter
closetime_cutoff = 2.5*5; % 3.5 * ensemble time
ww = 5; %(window length: stations have a minimum of 4 ens; try a std over 6 ens

load auto_refsm

if (exist('adcpllt.mat','file'))
   error('cannot create adcpllt.mat: file already exists')
else
   [data, config] = agetmat(config);
   
   
   shipspeed_std = zeros(length(data.dday)-ww + 1 ,1);
   %floating std
   for ii=1:length(shipspeed_std)
      shipspeed_std(ii) = std(data.clmps(ii+[0:ww-1]));
   end
   shipspeed_std = [zeros(floor(ww/2),1); shipspeed_std(:); zeros(floor(ww/2),1)];
   shipspeed_std=shipspeed_std(:)';


   %find not moving and slow enough and clean enough nav
   osi =  find( (data.clmps < config.os_cutoff) & (shipspeed_std < stable_cutoff));
   
   %find close in time (sequential)
   goodi = find(diff(data.dday(osi))*24*60 <= closetime_cutoff);
   osi = osi(goodi);

   
   %get a measure of jitter
   refsmdiff = (data.uabs_refl_g - data.uabs_refl_sm) + ...
                         i*(data.vabs_refl_g - data.vabs_refl_sm);
   absrefsmdiff = abs(refsmdiff);
      
   reflsm_std = zeros(length(data.dday)-ww + 1 ,1);
   %floating std
   for ii=1:length(reflsm_std)
      reflsm_std(ii) = std(absrefsmdiff(ii+[0:ww-1]));
   end
   reflsm_std = [zeros(floor(ww/2),1); reflsm_std(:); zeros(floor(ww/2),1)];
   reflsm_std=reflsm_std(:)';
   
   
   dday = data.dday;
   lat = data.lat;
   lon = data.lon;
   mps = data.clmps;
   
   save adcpllt dday lat lon mps osi absrefsmdiff  reflsm_std ww
end

