%run_ajitterhist

asetup
load auto_refsm


if (~exist('xyt.mat', 'file'))
   
   config.ddrange = config.end_dd - config.start_dd + 1;
   config.rl_range = [1 2];                     % just to fake it out
   config.nbins = 2;                            % keep it small but more than 1
   
   
   [data, config] = agetmat(config);
   
   zzz.dday= data.dday;
   zzz.lon = data.lon;
   zzz.lat = data.lat;
   clear data
   data = zzz
   save xyt.mat data
else
   load xyt.mat
end

ii = gxytzoom(data.lon, data.lat, data.dday);

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

asetup
config.start_dd = min(data.dday(ii));
config.end_dd = max(data.dday(ii));
config.ddrange = config.end_dd - config.start_dd;


[data, config] = agetmat(config);
aflagit_setup  %because we have real reference layer now
[flagged, pfc1] = aflagit(data, config, pfc);

%ajitter_hist

%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);
badrefli = find(isnan(sum(dtfm(rlbins,:))));

rl_dtfm = ones(size(data.uabs_refl_sm));    %dot-multiply reference layer mask
rl_dtfm(badrefli) = NaN;

ddu = diff(diff(data.uabs_refl_g));
ddu = [0 ddu 0]; %to give it the same timestamp
ddu(badrefli) = NaN;
ddv = diff(diff(data.vabs_refl_g));
ddv = [0 ddv 0]; %to give it the same timestampe
ddv(badrefli) = NaN;

uvdiff =  abs(ddu + i*ddv);
uvdiff(badrefli) = NaN;

refsmdiff = (data.uabs_refl_g - data.uabs_refl_sm) + ...
    i*(data.vabs_refl_g - data.vabs_refl_sm);
refsmdiff(badrefli) = NaN;
absrefsmdiff = abs(refsmdiff);


f4
ajitter_raw

f5
ajitter_hist

f6 % show all
ajitter_bias

