dt_rng  = datetime(2026,6,9,'Format','uuuuMMdd');
alllen  = 0;
for dt_cmpr = dt_rng
    data    = load(sprintf('cmpr_ucar_%s.mat',dt_cmpr),'alllen');
    alllen  = alllen+data.alllen;
end
allbad      = NaN(alllen,2);
allleo      = cell(alllen,1);
allgnss     = cell(alllen,1);
allfiles    = cell(alllen,2);
allfilesQ   = cell(alllen,2);
alldt       = NaT(alllen,2);
aidx        = 0;
uniq_gtype  = 'GER';
for dt_cmpr = dt_rng
    data=load(sprintf('cmpr_ucar_%s.mat',dt_cmpr),'allbad','allleo','allgnss','allfiles','allfilesQ','alllen','alldt');
    fprintf('\n===== %s =====\n',datetime(dt_cmpr,'Format','uuuu-MM-dd'));
    fprintf(' LEO  #prof      ucar          star     u0s1 u1s0\n')
    fprintf('===== ===== ============= ============= ==== ====\n')
    uniq_leo = unique(data.allleo);
    for ll = 1:length(uniq_leo)
        leo     = uniq_leo{ll};
        for gg = 1:length(uniq_gtype)
            gtype   = uniq_gtype(gg);
            idx     = strcmp(data.allleo,leo) & strncmp(data.allgnss,gtype,1);
            fprintf('%4s%1s %5d %5d(%5.1f%%) %5d(%5.1f%%) %4d %4d\n', leo, gtype, sum(idx),...
                    sum(data.allbad(idx,1)==0),sum(data.allbad(idx,1)==0)/sum(idx)*100,...
                    sum(data.allbad(idx,2)==0),sum(data.allbad(idx,2)==0)/sum(idx)*100,...
                    sum(data.allbad(idx,1)==0 & data.allbad(idx,2)~=0),...
                    sum(data.allbad(idx,1)~=0 & data.allbad(idx,2)==0))
        end
        idx     = strcmp(data.allleo,leo);
        fprintf('%4s  %5d %5d(%5.1f%%) %5d(%5.1f%%) %4d %4d\n', leo, sum(idx),...
                sum(data.allbad(idx,1)==0),sum(data.allbad(idx,1)==0)/sum(idx)*100,...
                sum(data.allbad(idx,2)==0),sum(data.allbad(idx,2)==0)/sum(idx)*100,...
                sum(data.allbad(idx,1)==0 & data.allbad(idx,2)~=0),...
                sum(data.allbad(idx,1)~=0 & data.allbad(idx,2)==0))
    end
    for gg = 1:length(uniq_gtype)
        gtype   = uniq_gtype(gg);
        idx     = strncmp(data.allgnss,gtype,1);
        fprintf('%4s%1s %5d %5d(%5.1f%%) %5d(%5.1f%%) %4d %4d\n', 'All', gtype, sum(idx),...
                sum(data.allbad(idx,1)==0),sum(data.allbad(idx,1)==0)/sum(idx)*100,...
                sum(data.allbad(idx,2)==0),sum(data.allbad(idx,2)==0)/sum(idx)*100,...
                sum(data.allbad(idx,1)==0 & data.allbad(idx,2)~=0),...
                sum(data.allbad(idx,1)~=0 & data.allbad(idx,2)==0))
    end
    idx = true(data.alllen,1);
    fprintf('%4s  %5d %5d(%5.1f%%) %5d(%5.1f%%) %4d %4d\n', 'All', sum(idx),...
            sum(data.allbad(idx,1)==0),sum(data.allbad(idx,1)==0)/sum(idx)*100,...
            sum(data.allbad(idx,2)==0),sum(data.allbad(idx,2)==0)/sum(idx)*100,...
            sum(data.allbad(idx,1)==0 & data.allbad(idx,2)~=0),...
            sum(data.allbad(idx,1)~=0 & data.allbad(idx,2)==0))
    allbad(aidx+(1:data.alllen),:)      = data.allbad;
    allleo(aidx+(1:data.alllen))        = data.allleo;
    allgnss(aidx+(1:data.alllen))       = data.allgnss;
    allfiles(aidx+(1:data.alllen),:)    = data.allfiles;
    allfilesQ(aidx+(1:data.alllen),:)   = data.allfilesQ;
    alldt(aidx+(1:data.alllen),:)       = data.alldt;
    aidx                                = aidx+data.alllen;
end
clear data
fprintf('\n===== ALL =====\n');
fprintf(' LEO  #prof      ucar          star     u0s1 u1s0\n')
fprintf('===== ===== ============= ============= ==== ====\n')
uniq_leo = unique(allleo);
for ll = 1:length(uniq_leo)
    leo     = uniq_leo{ll};
    for gg = 1:length(uniq_gtype)
        gtype   = uniq_gtype(gg);
        idx     = strcmp(allleo,leo) & strncmp(allgnss,gtype,1);
        fprintf('%4s%1s %5d %5d(%5.1f%%) %5d(%5.1f%%) %4d %4d\n', leo, gtype, sum(idx),...
                sum(allbad(idx,1)==0),sum(allbad(idx,1)==0)/sum(idx)*100,...
                sum(allbad(idx,2)==0),sum(allbad(idx,2)==0)/sum(idx)*100,...
                sum(allbad(idx,1)==0 & allbad(idx,2)~=0),...
                sum(allbad(idx,1)~=0 & allbad(idx,2)==0))
    end
    idx     = strcmp(allleo,leo);
    fprintf('%4s  %5d %5d(%5.1f%%) %5d(%5.1f%%) %4d %4d\n', leo, sum(idx),...
            sum(allbad(idx,1)==0),sum(allbad(idx,1)==0)/sum(idx)*100,...
            sum(allbad(idx,2)==0),sum(allbad(idx,2)==0)/sum(idx)*100,...
            sum(allbad(idx,1)==0 & allbad(idx,2)~=0),...
            sum(allbad(idx,1)~=0 & allbad(idx,2)==0))
end
for gg = 1:length(uniq_gtype)
    gtype   = uniq_gtype(gg);
    idx     = strncmp(allgnss,gtype,1);
    fprintf('%4s%1s %5d %5d(%5.1f%%) %5d(%5.1f%%) %4d %4d\n', 'All', gtype, sum(idx),...
            sum(allbad(idx,1)==0),sum(allbad(idx,1)==0)/sum(idx)*100,...
            sum(allbad(idx,2)==0),sum(allbad(idx,2)==0)/sum(idx)*100,...
            sum(allbad(idx,1)==0 & allbad(idx,2)~=0),...
            sum(allbad(idx,1)~=0 & allbad(idx,2)==0))
end
idx = true(aidx,1);
fprintf('%4s  %5d %5d(%5.1f%%) %5d(%5.1f%%) %4d %4d\n', 'All', sum(idx),...
        sum(allbad(idx,1)==0),sum(allbad(idx,1)==0)/sum(idx)*100,...
        sum(allbad(idx,2)==0),sum(allbad(idx,2)==0)/sum(idx)*100,...
        sum(allbad(idx,1)==0 & allbad(idx,2)~=0),...
        sum(allbad(idx,1)~=0 & allbad(idx,2)==0))

% dt_cmpr     = datetime(2026,3,5,'Format','uuuuMMdd');
% load(sprintf('cmpr_ucar_%s.mat',dt_cmpr),'all*')

% idx         = find(allbad(:,1)==0 & ismember(allbad(:,2),0:1) & ismember(allleo,{'YM08','GN05'}) & ismember(regexprep(allgnss,'\d+',''),{'E','G'}));
idx         = find(ismember(allbad(:,1),0) & ismember(allbad(:,2),1) & ismember(allleo,{'C2E4'}));
afiles      = allfiles(idx,2);
afilesQ     = allfilesQ(idx,2);
ufiles      = allfiles(idx,1);
ufilesQ     = allfilesQ(idx,1);
bads        = allbad(idx,:);
yt_list     = [-10*log10(2000),-30,-10*log10(500),-10*log10(200),-20,-10*log10(50),-10*log10(20),-10,-5,0,5,10,10*log10(20),10*log10(50),20,10*log10(200),10*log10(500),30,10*log10(2000)];
ytl_list    = {'-2000','-1000','-500','-200','-100','-50','-20','-10','-5','0','5','10','20','50','100','200','500','1000','2000'}';
clrs        = get(0,'DefaultAxesColorOrder');
aleo        = allleo(idx); %regexprep(afiles,'.*_([A-Z]\d\d\d)\..*','$1');
agnss       = allgnss(idx); %regexprep(afiles,'.*\.(...)_.*','$1');
adt         = alldt(idx,2); %datetime(regexprep(afiles,'.*\.(\d{4}\.\d{3}\.\d{2}\.\d{2})\..*','$1'),'InputFormat','uuuu.DDD.HH.mm');
aidx        = 1:length(afiles);%1:min(10,length(afiles));
plt_types   = struct('ExcessPhase',0,'Coordinate',1);
plt_typ     = 'ExcessPhase';
for aa = aidx
    afile   = afiles{aa};
    ufile   = ufiles{aa};
    ufileQ  = ufilesQ{aa};
    afileQ  = afilesQ{aa};
    % afile   = dirx(regexprep(afile,'(.*)_c.*','$1*'));
    % afile   = afile{1};
    ts_a    = ncreadatt(afile,'/','startTime');
    % coef_a  = [ncreadatt(afile,'/','coef2');ncreadatt(afile,'/','coef1');ncreadatt(afile,'/','coef0')];
    ts_u    = ncreadatt(ufile,'/','startTime'); % ncread(ufile,'startTime');
    s1_a    = ncread(afile,'pL1Snr');
    s2_a    = ncread(afile,'pL2Snr');
    s1_u    = ncread(ufile,'pL1Snr');
    s2_u    = ncread(ufile,'pL2Snr');
    t_a     = ncread(afile,'time')+(ts_a-ts_u);
    t_u     = ncread(ufile,'time');
    l1_a    = ncread(afile,'exL1');
    l2_a    = ncread(afile,'exL2');
    l1_u    = ncread(ufile,'exL1');
    l2_u    = ncread(ufile,'exL2');
    xl_a    = ncread(afile,'xLeo');
    yl_a    = ncread(afile,'yLeo');
    zl_a    = ncread(afile,'zLeo');
    xg_a    = ncread(afile,'xGps');
    yg_a    = ncread(afile,'yGps');
    zg_a    = ncread(afile,'zGps');
    [FL1,FL2]   = get_sat_freq(agnss{aa});
    C1      = FL1*FL1/(FL1*FL1-FL2*FL2);
    C2      = FL2*FL2/(FL1*FL1-FL2*FL2);
    xl_u    = ncread(ufile,'xLeo');
    yl_u    = ncread(ufile,'yLeo');
    zl_u    = ncread(ufile,'zLeo');
    xg_u    = ncread(ufile,'xGps');
    yg_u    = ncread(ufile,'yGps');
    zg_u    = ncread(ufile,'zGps');
    % tlr_u   = ncread(ufile,'orbtime')-ts_u;
    % txm_u   = ncread(ufile,'txmitLR')-ts_u;
    % xl_u    = ncread(ufile,'xLeoLR');
    % yl_u    = ncread(ufile,'yLeoLR');
    % zl_u    = ncread(ufile,'zLeoLR');
    % tg_u    = interp1(tlr_u,txm_u,t_u);
    % xg_u    = ncread(ufile,'xGnssLR');
    % yg_u    = ncread(ufile,'yGnssLR');
    % zg_u    = ncread(ufile,'zGnssLR');
    OL      = [xl_a,yl_a,zl_a];
    OG      = [xg_a,yg_a,zg_a];
    LG      = OG-OL;
    OP      = OL-LG.*(sum(OL.*LG,2) ./ sum(LG.*LG,2));
    lla     = ecef2lla(OP*1E3);
    hidxa   = lla(:,3)>30E3&lla(:,3)<90E3;
    OL      = [xl_u,yl_u,zl_u];
    OG      = [xg_u,yg_u,zg_u];
    LG      = OG-OL;
    OP      = OL-LG.*(sum(OL.*LG,2) ./ sum(LG.*LG,2));
    lla     = ecef2lla(OP*1E3);
    hidxu   = lla(:,3)>30E3&lla(:,3)<90E3;
    % alt_u   = interp1(tlr_u,lla(:,3),t_u);
    % hidxu   = alt_u>30E3&alt_u<90E3;
    clf
    switch plt_typ
    case 'ExcessPhase'
        subplotx(3,3,[1,4],2,2)
        hold on
        plot_signed_log(t_u,l1_u,1E-1,1,[-3,5],'.','MarkerSize',6,'Color',(clrs(1,:)+.5)./1.5,'DisplayName','UCAR L1')
        plot_signed_log(t_a,l1_a,1E-1,1,[-3,5],'.','MarkerSize',6,'Color',(clrs(2,:)+.5)./1.5,'DisplayName','UMD L1')
        plot_signed_log(t_u,l2_u,1E-1,1,[-3,5],'.','MarkerSize',3,'Color',clrs(1,:),'DisplayName','UCAR L2')
        plot_signed_log(t_a,l2_a,1E-1,1,[-3,5],'.','MarkerSize',3,'Color',clrs(2,:),'DisplayName','UMD L2')
        legend('Location','best')
        % subplotx(3,3,1,2,2)
        % plot(t_u,l1_u,'.','MarkerSize',3,'DisplayName','UCAR')
        % hold on
        % plot(t_a,l1_a,'.','MarkerSize',3,'DisplayName','UMD')
        % title('L1')
        % legend({'UCAR','UMD'},'Location','best')
        % set(gca,'YScale','log')
        % ylim([0.001,500])
        % subplotx(3,3,4,2,2)
        % plot(t_u,l2_u,'.','MarkerSize',3)
        % hold on
        % plot(t_a,l2_a,'.','MarkerSize',3)
        % title('L2')
        % set(gca,'YScale','log')
        % ylim([0.001,500])
        subplotx(3,3,2,2,2)
        xx  = t_u;
        yy  = interp1(t_a,l1_a,t_u)-l1_u;
        if false %max(abs(yy))>20
            idx = yy>10;
            if any(idx)
                yy(idx) = 10*log10(yy(idx));
            end
            idx = yy<-10;
            if any(idx)
                yy(idx) = -10*log10(-yy(idx));
            end
            plot(xx,yy,'.-','Color',clrs(5,:),'MarkerSize',1)
            hold on
            plot(xx(hidxu),yy(hidxu),'.-','Color',clrs(5,:))
            ytl     = get(gca,'YLim');
            yidx    = find(ytl(1)>yt_list,1):find(ytl(2)<yt_list,1,'last');
            set(gca,'YTick',yt_list(yidx),'YTickLabel',ytl_list(yidx))
        else
            plot(xx,yy,'.-','Color',clrs(5,:),'MarkerSize',1)
            hold on
            plot(xx(hidxu),yy(hidxu),'.-','Color',clrs(5,:))
            ylim([-2,2])
        end
        title('L1 diff (UMD-UCAR)')
        subplotx(3,3,7,2,2)
        plot(t_u,l2_u-l1_u,'.','MarkerSize',1)
        xl  = get(gca,'XLim');
        yl  = get(gca,'YLim');
        hold on
        plot(t_a,l2_a-l1_a,'.','Color',clrs(2,:),'MarkerSize',1)
        title('L2-L1')
        % subplotx(6,3,9,2,2)
        % plot(conv(t_u,[.5,.5],'valid'),diff(l1_u)./diff(t_u),'.','MarkerSize',1,'DisplayName','UCAR')
        % hold on
        % plot(conv(t_a,[.5,.5],'valid'),diff(l1_a)./diff(t_a),'.','MarkerSize',1,'DisplayName','UMD')
        % if l1_u(end)>l1_u(1)
        %     ylim([-10,100])
        % else
        %     ylim([-100,10])
        % end
        % title('dL1/dt')
        subplotx(3,3,5,2,2)
        xx  = t_u;
        yy  = interp1(t_a,l2_a,t_u)-l2_u;
        if false %max(abs(yy))>20
            idx = yy>10;
            if any(idx)
                yy(idx) = 10*log10(yy(idx));
            end
            idx = yy<-10;
            if any(idx)
                yy(idx) = -10*log10(-yy(idx));
            end
            plot(xx,yy,'.-','Color',clrs(5,:),'MarkerSize',1)
            hold on
            plot(xx(hidxu),yy(hidxu),'.-','Color',clrs(5,:))
            ytl     = get(gca,'YLim');
            yidx    = find(ytl(1)>yt_list,1):find(ytl(2)<yt_list,1,'last');
            set(gca,'YTick',yt_list(yidx),'YTickLabel',ytl_list(yidx))
        else
            plot(xx,yy,'.-','Color',clrs(5,:),'MarkerSize',1)
            hold on
            plot(xx(hidxu),yy(hidxu),'.-','Color',clrs(5,:))
            ylim([-2,2])
        end
        title('L2 diff (UMD-UCAR)')
        subplotx(3,3,8,2,2)
        xx  = t_u;
        yy  = interp1(t_a,l2_a-l1_a,t_u)-(l2_u-l1_u);
        plot(xx,yy,'.-','Color',clrs(5,:),'MarkerSize',1)
        hold on
        plot(xx(hidxu),yy(hidxu),'.-','Color',clrs(5,:))
        title('(L2-L1) diff (UMD-UCAR)')
        % subplotx(6,3,12,2,2)
        % plot(conv(t_u,[.5,.5],'valid'),diff(l2_u)./diff(t_u),'.','MarkerSize',1,'DisplayName','UCAR')
        % hold on
        % plot(conv(t_a,[.5,.5],'valid'),diff(l2_a)./diff(t_a),'.','MarkerSize',1,'DisplayName','UMD')
        % if l1_u(end)>l1_u(1)
        %     ylim([-10,100])
        % else
        %     ylim([-100,10])
        % end
        % title('dL2/dt')
        
        LC_u = C1*l1_u-C2*l2_u;
        LC_a = C1*l1_a-C2*l2_a;
        % % xx = t_a;
        % % yy = LC_a;
        % % if yy(1)==0, rev=true; else, rev=false; yy=yy(end:-1:1); xx=xx(end:-1:1); end
        % % ilen = find(abs(yy)>0.1,1)-1;
        % % coef = polyfit(xx(1:ilen),yy(1:ilen),1);
        % % ilen_new=find(abs(polyval(coef,xx)-yy)>0.1,1);
        % % while ilen_new>ilen
        % %     ilen = ilen_new;
        % %     coef = polyfit(xx(1:ilen),yy(1:ilen),1);
        % %     ilen_new=find(abs(polyval(coef,xx)-yy)>0.1,1)-1;
        % % end
        % % yy = yy-polyval(coef,xx);
        % if rev, yy=yy(end:-1:1); xx=xx(end:-1:1); end
        subplotx(3,3,3,2,2)
        hold on
        plot(t_u,LC_u,'.','Color',(clrs(1,:)+.5)./1.5,'MarkerSize',6,'DisplayName','UCAR')
        plot(t_a,LC_a,'.','Color',clrs(2,:),'MarkerSize',1,'DisplayName','UMD')
        plot(t_a(hidxa),LC_a(hidxa),'.','Color',clrs(2,:),'MarkerSize',3,'HandleVisibility','off')
        % % plot(xx,yy,'.','MarkerSize',1,'DisplayName','UMD-Rev')
        % % plot(xx(1:ilen),yy(1:ilen),'.','MarkerSize',1,'HandleVisibility','off')
        ylim([-4.5,4.5])
        xl  = get(gca,'XLim');
        yl  = get(gca,'YLim');
        % text(xl*[0.01;0.99],yl*[0.95;0.05],sprintf('a_2 = %.3E',coef_a(1)),'HorizontalAlignment','right','VerticalAlignment','middle')
        title('LC = ({\itf1}^2L1-{\itf2}^2L2)/({\itf1}^2-{\itf2}^2)')
        % subplotx(3,3,9,2,2)
        % plot((t_u(1:end-100)+t_u(101:end))/2,(LC_u(101:end)-LC_u(1:end-100))./(t_u(101:end)-t_u(1:end-100)),'.','MarkerSize',1,'DisplayName','UCAR')
        % hold on
        % plot((t_a(1:end-100)+t_a(101:end))/2,(LC_a(101:end)-LC_a(1:end-100))./(t_a(101:end)-t_a(1:end-100)),'.','MarkerSize',1,'DisplayName','UMD')
        % % % plot((xx(1:end-100)+xx(101:end))/2,(yy(101:end)-yy(1:end-100))./(xx(101:end)-xx(1:end-100)),'.','MarkerSize',1,'DisplayName','UMD-Rev')
        % % % plot((xx(1:ilen-100)+xx(101:ilen))/2,(yy(101:ilen)-yy(1:ilen-100))./(xx(101:ilen)-xx(1:ilen-100)),'.','MarkerSize',1,'HandleVisibility','off')
        % ylim([-.045,.045])
        % title('dLC/dt')
        subplotx(3,3,6,2,2)
        xx  = t_u;
        yy  = interp1(t_a,LC_a,t_u)-LC_u;
        plot(xx,yy,'.-','Color',clrs(5,:),'MarkerSize',1)
        hold on
        plot(xx(hidxu),yy(hidxu),'.-','Color',clrs(5,:))
        title('LC diff (UMD-UCAR)')
        xlim(xl)
        subplotx(3,6,17:18,2,2)
        % ih_u = ncread(ufileQ,'Impact_height');
        % ba_u = ncread(ufileQ,'Bend_ang');
        % % iho_u = (ncread(ufileQ,'impact_opt')-ncread(ufileQ,'roc'))/1E3;
        % % ih_a = (ncread(afileQ,'impact')-ncread(afileQ,'roc'))/1E3;
        % % bao_u = ncread(ufileQ,'bangle_opt');
        % % bao_u(bao_u<-90000000)=NaN;
        % plot(ba_u,ih_u,'.','MarkerSize',6,'Color',(clrs(1,:)+.5)./1.5)
        iho_u = (ncread(ufileQ,'impact_opt')-ncread(ufileQ,'roc'))/1E3;
        bao_u = ncread(ufileQ,'bangle_opt');
        bao_u(bao_u<-90000000)=NaN;
        plot(bao_u,iho_u,'.','MarkerSize',6,'Color',(clrs(1,:)+.5)./1.5)
        if isfile(afileQ) && ~strcmp(afile,afileQ)
            iho_a = (ncread(afileQ,'impact_opt')-ncread(afileQ,'roc'))/1E3;
            % ih_a = (ncread(afileQ,'impact')-ncread(afileQ,'roc'))/1E3;
            bao_a = ncread(afileQ,'bangle_opt');
            bao_a(bao_a<-90000000)=NaN;
            % ba_a = ncread(afileQ,'bangle');
            % ba_a(ba_a<-90000000)=NaN;
            hold on
            % plot(ba_a,ih_a,'.','MarkerSize',1)
            plot(bao_a,iho_a,'.','MarkerSize',3)
        end
        ylim([40,80])
        title('Bending Angle')
        % subplotx(3,6,18,2,2)
        % alt_u = ncread(ufileQ,'MSL_alt');
        % ref_u = ncread(ufileQ,'Ref');
        % plot(ref_u,alt_u,'.','MarkerSize',6,'Color',(clrs(1,:)+.5)./1.5)
        % if isfile(afileQ) && ~strcmp(afile,afileQ)
        %     alt_a = ncread(afileQ,'alt_refrac');
        %     ref_a = ncread(afileQ,'refrac');
        %     hold on
        %     plot(ref_a,alt_a/1E3,'.','MarkerSize',3);
        % end
        % ylim([40,80])
        % title('Refractivity')
        % sgtitle({[strrep(regexprep(ufile,'.*/',''),'_','\_'),sprintf(' (bad=%d, gnss=[',bads(aa,1)),sprintf('%d,',ncreadatt(ufile,'/','refprns')),'])'],[strrep(regexprep(afile,'.*/',''),'_','\_'),sprintf(' (bad=%d, gnss=%s)',bads(aa,2),ncreadatt(afile,'/','refsatId'))]})
        sgtitle({[strrep(regexprep(ufile,'.*/',''),'_','\_'),sprintf(' (bad=%d, gnss=%s)',bads(aa,1),ncreadatt(ufile,'/','refsatId'))],[strrep(regexprep(afile,'.*/',''),'_','\_'),sprintf(' (bad=%d, gnss=%s)',bads(aa,2),ncreadatt(afile,'/','refsatId'))]})
        % exportgraphics(gcf, sprintf('Figures/%s_%s_%s.png',aleo{aa},agnss{aa},datetime(adt(aa),'Format','uuuuMMdd_HHmm')), 'Resolution', 300)
        print('-dpng','-r300',sprintf('Figures/%s_%s_%s.png',aleo{aa},agnss{aa},datetime(adt(aa),'Format','uuuuMMdd_HHmm')))
    case 'Coordinate'
        rngl_u  = sqrt(xl_u.^2+yl_u.^2+zl_u.^2);
        rngl_a  = sqrt(xl_a.^2+yl_a.^2+zl_a.^2);
        latl_u  = asind(zl_u./rngl_u);
        latl_a  = asind(zl_a./rngl_a);
        lonl_u  = atan2d(yl_u,xl_u);
        lonl_a  = atan2d(yl_a,xl_a);
        rngg_u  = sqrt(xg_u.^2+yg_u.^2+zg_u.^2);
        rngg_a  = sqrt(xg_a.^2+yg_a.^2+zg_a.^2);
        latg_u  = asind(zg_u./rngg_u);
        latg_a  = asind(zg_a./rngg_a);
        long_u  = atan2d(yg_u,xg_u);
        long_a  = atan2d(yg_a,xg_a);
        subplotx(5,2,1,2,2)
        % plot(tlr_u,rngl_u,'o')
        plot(t_u,rngl_u,'o')
        hold on
        plot(t_a,rngl_a,'.')
        set(gca,'XTickLabel','')
        ylabel('Range (km)')
        subplotx(5,2,3,2,2)
        % plot(tlr_u,latl_u,'o')
        plot(t_u,latl_u,'o')
        hold on
        plot(t_a,latl_a,'.')
        set(gca,'XTickLabel','')
        ylabel('ECI Latitude')
        subplotx(5,2,5,2,2)
        % plot(tlr_u,lonl_u,'o')
        plot(t_u,lonl_u,'o')
        hold on
        plot(t_a,lonl_a,'.')
        set(gca,'XTickLabel','')
        ylabel('ECI Longitude')
        subplotx(5,2,7,2,2)
        % plot(tlr_u,interp1(t_a,rngl_a,tlr_u)-rngl_u,'.')
        % hold on
        % plot(tlr_u,interp1(t_a,latl_a,tlr_u)-latl_u,'.')
        % plot(tlr_u,interp1(t_a,lonl_a,tlr_u)-lonl_u,'.')
        plot(t_u,interp1(t_a,rngl_a,t_u)-rngl_u,'.')
        hold on
        plot(t_u,interp1(t_a,latl_a,t_u)-latl_u,'.')
        plot(t_u,interp1(t_a,lonl_a,t_u)-lonl_u,'.')
        ylabel('ECI difference')
        % xl_ua = interp1(t_a,xl_a,tlr_u);
        % yl_ua = interp1(t_a,yl_a,tlr_u);
        % zl_ua = interp1(t_a,zl_a,tlr_u);
        xl_ua = interp1(t_a,xl_a,t_u);
        yl_ua = interp1(t_a,yl_a,t_u);
        zl_ua = interp1(t_a,zl_a,t_u);
        
        % subplotx(5,2,9,2,2)
        % 
        % subplotx(5,2,10,2,2)

        subplotx(5,2,2,2,2)
        % plot(tlr_u,rngg_u,'o')
        plot(t_u,rngg_u,'o')
        hold on
        plot(t_a,rngg_a,'.')
        set(gca,'XTickLabel','','YAxisLocation','right')
        subplotx(5,2,4,2,2)
        % plot(tlr_u,latg_u,'o')
        plot(t_u,latg_u,'o')
        hold on
        plot(t_a,latg_a,'.')
        set(gca,'XTickLabel','','YAxisLocation','right')
        subplotx(5,2,6,2,2)
        % plot(tlr_u,long_u,'o')
        plot(t_u,long_u,'o')
        hold on
        plot(t_a,long_a,'.')
        set(gca,'XTickLabel','','YAxisLocation','right')
        subplotx(5,2,8,2,2)
        % plot(tlr_u,interp1(t_a,rngg_a,tlr_u)-rngg_u,'.')
        % hold on
        % plot(tlr_u,interp1(t_a,latg_a,tlr_u)-latg_u,'.')
        % plot(tlr_u,interp1(t_a,long_a,tlr_u)-long_u,'.')
        plot(t_u,interp1(t_a,rngg_a,t_u)-rngg_u,'.')
        hold on
        plot(t_u,interp1(t_a,latg_a,t_u)-latg_u,'.')
        plot(t_u,interp1(t_a,long_a,t_u)-long_u,'.')
        set(gca,'YAxisLocation','right')
        % sgtitle({[strrep(regexprep(ufile,'.*/',''),'_','\_'),sprintf(' (bad=%d, gnss=[',bads(aa,1)),sprintf('%d,',ncreadatt(ufile,'/','refprns')),'])'],[strrep(regexprep(afile,'.*/',''),'_','\_'),sprintf(' (bad=%d, gnss=%s)',bads(aa,2),ncreadatt(afile,'/','refsatId'))]})
        sgtitle({[strrep(regexprep(ufile,'.*/',''),'_','\_'),sprintf(' (bad=%d, gnss=%s)',bads(aa,1),ncreadatt(ufile,'/','refsatId'))],[strrep(regexprep(afile,'.*/',''),'_','\_'),sprintf(' (bad=%d, gnss=%s)',bads(aa,2),ncreadatt(afile,'/','refsatId'))]})
        print('-dpng','-r300',sprintf('Figures/%s_%s_%s_xyz.png',aleo{aa},agnss{aa},datetime(adt(aa),'Format','uuuuMMdd_HHmm')))
    end
    fprintf('%s - %s saved.\n',datetime('now','Format','HH:mm:ss'),sprintf('%s_%s_%s.png',aleo{aa},agnss{aa},datetime(adt(aa),'Format','uuuuMMdd_HHmm')));
end