set(0, 'defaulttextinterpreter', 'none')
mvec_scale = 35;
topo_clev = [-9000:500:0];  % Topography contour levels.

load auto_refsm

jitter_cutoff = 5;
jitter_scale = 20;  %ylim

% set for this application
config.ddrange        =       1.5;         %number of ddays to get data
config.os_cutoff = 1;
%check values in aflagit_setup for your cruise.
aflagit_setup
%turn off flagging here
pfc.cfg.jitter_cutoff = 10^10;

%%%% for ajitter_rsdiff
stable_cutoff = .2;      % for on-station: low jitter
cleaner_threshold = 1;
cleaner_runs = 10;
closeraw_cutoff = 5; %cm/s        for u and v
closediff_cutoff = 10 ;   %cm/s  - for ddu and ddv
closetime_cutoff = 2.5*5; % 3.5 * ensemble time
uvdiff_scale = 20;  

uv_lim = [-70,70];


%%%%%

start_dd = config.start_dd;
for dd = start_dd: config.ddrange : config.end_dd

   config.start_dd = dd;
   
   [data, config] = agetmat(config);
   flagged = aflagit(data, config, pfc);

   
   %dot-multiply flag mask
   dtfm = ones(size(flagged));    %ones where good
   dtfm(find(flagged ~= 0)) = NaN;  %nans where bad
   
   rlbins = config.rl_range(1) : config.rl_range(2);
   okrefli = find(~isnan(sum(dtfm(rlbins,:))));
   okosi = find(data.clmps(okrefli) < config.os_cutoff);
   okosi = okrefli(okosi);
   
   ddayi = find(   (data.dday >= min(data.dday)) & (data.dday <= max(data.dday)) );
   jitterii = find( data.ref_std(ddayi) > jitter_cutoff);
   closei = find( data.ref_std(ddayi) <= jitter_cutoff);
   
   jittery_mask = zeros(size(flagged));
   jittery_mask(:,jitterii) = pfc.flag.jitter;
      
      
   %dot-multiply flag mask
   dtfm = ones(size(flagged));    %ones where good
   dtfm(find(flagged ~= 0)) = NaN;  %nans where bad
                                    %from alookit.m

                                    
   config.flagged = flagged;
   figure(1);whitebg(gcf,'w');clf
   ajitter_uv;
   subtitle('                                                                 NO jitter flagged')


   config.flagged = flagged + jittery_mask;
   figure(2);whitebg(gcf,'w');clf
   ajitter_uv;
   subtitle('                                                                jitter flagged')

   figure(3);whitebg(gcf,'w');clf
   ajitter_raw
   
   figure(4);whitebg(gcf,'w');clf
   if (exist('plot_np0001_ladcp.m','file'))
      plot_np0001_ladcp(config.start_dd + [0 config.ddrange]);
   else
      ajitter_topo
   end
   

   
   
%   figure(5);whitebg(gcf,'w');clf
%   ajitter_bias

%   figure(2);;whitebg(gcf,'w');clf
%   ajitter_rsdiff 
   

%   figure(figbase + 4);clf
%   ajitter_refl
   paused
end

