dt_cmpr     = datetime(2026,6,9,'Format','uuuuMMdd');
fprintf('%s - Process started.\n',datetime('now','Format','HH:mm:ss'));
ufiles      = dirx(sprintf('/data3/xinjing/gps_pod/test_cosmic2_pod/s04_atmPhs/%s/*nc',datetime(dt_cmpr,'Format','uuuu-MM-dd')));
fprintf('%s - %d ufiles located.\n',datetime('now','Format','HH:mm:ss'),length(ufiles));
ufilesQ     = dirx(sprintf('/data3/xinjiaz/data_GPSRO/RFSI_UMD/v3.14/cosmic2/atmPrf_vicky_062426/%s/*nc',datetime(dt_cmpr,'Format','uuuu-MM-dd')));
fprintf('%s - %d ufilesQ located.\n',datetime('now','Format','HH:mm:ss'),length(ufilesQ));
pfiles      = dirx(sprintf('/data3/tcliu/gps_pod/python/cosmic2/s04_atmPhs/%s/*.nc',datetime(dt_cmpr,'Format','uuuu-MM-dd')));
fprintf('%s - %d pfiles located.\n',datetime('now','Format','HH:mm:ss'),length(pfiles));
pfilesQ     = dirx(sprintf('/data3/xinjiaz/data_GPSRO/RFSI_UMD/v3.14/cosmic2/atmPrf_tcliu_070526_UCAR_leoOrb/%s/*.nc',datetime(dt_cmpr,'Format','uuuu-MM-dd')));
fprintf('%s - %d pfilesQ located.\n',datetime('now','Format','HH:mm:ss'),length(pfilesQ));

% utoken      = regexprep(ufiles,'.*conPhs_(.*)_0001\.0001_nc','$1');
utoken      = regexprep(ufiles,'.*_STAR_(.*)_v.*','$1');
% utokenQ     = regexprep(ufilesQ,'.*atmPrf_(.*)_0001\.0001_nc','$1');
utokenQ     = regexprep(ufilesQ,'.*_STAR_(.*)_v.*','$1');
ptoken      = regexprep(pfiles,'.*_L1A_(.*)_c.*','$1');
ptokenQ     = regexprep(pfilesQ,'.*_L1A_(.*)_c.*','$1');
ubad        = uint8(zeros(size(ufiles)));
pbad        = uint8(zeros(size(pfiles)));
% ubadQ       = cellfun(@(x)str2double(ncreadatt(x,'/','bad')),ufilesQ);
ubadQ       = cellfun(@(x)ncread(x,'finalQA'),ufilesQ);
fprintf('%s - ubadQ extracted. (%d/%d)\n',datetime('now','Format','HH:mm:ss'),sum(ubadQ==0),length(ubadQ));
pbadQ       = cellfun(@(x)ncread(x,'finalQA'),pfilesQ);
fprintf('%s - pbadQ extracted. (%d/%d)\n',datetime('now','Format','HH:mm:ss'),sum(pbadQ==0),length(pbadQ));
% ufiles      = dirx(sprintf('/STARRO_data2/xinjiaz/data_GPSRO/RFSI_UMD/v3.10/planetiqrt/atmPrf_python/%s/*.nc',datetime(dt_cmpr,'Format','uuuu-MM-dd')));
% ufilesQ     = dirx(sprintf('/STARRO_data2/xinjiaz/data_GPSRO/RFSI_UMD/v3.10/planetiqrt/atmPrf_python/%s/*.nc',datetime(dt_cmpr,'Format','uuuu-MM-dd')));
% utoken      = regexprep(ufiles,'.*_STAR_(.*)_v.*','$1');
% utokenQ     = regexprep(ufilesQ,'.*_STAR_(.*)_v.*','$1');
% ubad        = uint8(zeros(size(ufiles)));
% ubadQ       = cellfun(@(x)ncread(x,'finalQA'),ufilesQ);
idx         = ismember(utoken,utokenQ);
if ~all(strcmp(utoken(idx),utokenQ)), error('UCAR mismatch!'); end
ubad(idx)   = ubadQ;
ubad(~idx)  = 3;
tempQ       = cell(size(ufiles));
tempQ(idx)  = ufilesQ;
ufilesQ     = tempQ;
idx         = ismember(ptoken,ptokenQ);
if ~all(strcmp(ptoken(idx),ptokenQ)), error('PYTHON mismatch!'); end
pbad(idx)   = pbadQ;
pbad(~idx)  = 3;
tempQ       = cell(size(pfiles));
tempQ(idx)  = pfilesQ;
pfilesQ     = tempQ;
% clear *Q

uleo        = regexprep(utoken,'(.*)\.\d{4}\..*','$1');
pleo        = regexprep(ptoken,'(C2E\d)_\d{14}_.*','$1');
ugnss       = regexprep(utoken,'.*\.([A-Z]\d\d)','$1');
pgnss       = regexprep(ptoken,'.*_([A-Z]\d\d)_A.*','$1');
udt         = datetime(regexprep(utoken,'.*\.(\d{4}\.\d{3}\.\d\d\.\d\d)\..*','$1'),'InputFormat','uuuu.DDD.HH.mm');
pdt         = datetime(regexprep(ptoken,'C2E\d_(\d{14})_.*','$1'),'InputFormat','uuuuMMddHHmmss');

leos        = unique([uleo;pleo])';
gnsses      = unique([ugnss;pgnss])';
idx_dup     = zeros(size(ufiles));
cnt         = 0;
for ll = leos
    for gg = gnsses
        uidx    = find(strcmp(uleo,ll) & strcmp(ugnss,gg));
        uu      = diff(udt(uidx))<duration(0,20,0);
        if any(uu)
            u1  = find(diff([false;uu;false])==1);
            u2  = find(diff([false;uu;false])==-1);
            for v1 = 1:length(u1)
                cnt             = cnt+1;
                vv              = u1(v1):u2(v1);
                idx_dup(uidx(vv))   = cnt;
                ubads           = ubad(uidx(vv));
                minubad         = min(ubads);
                ii              = ubads==minubad;
                idx_dup(uidx(vv(~ii)))  = -cnt;
                vv              = vv(ii);
                if ~isscalar(vv)
                    maxfdt      = 0;
                    maxpos      = NaN;
                    for ii = 1:length(vv)
                        [~,fdt] = dirx(ufiles{uidx(vv(ii))});
                        if fdt > maxfdt
                            maxfdt  = fdt;
                            if isfinite(maxpos)
                                idx_dup(uidx(vv(maxpos)))   = -cnt;
                            end
                            maxpos  = ii;
                        else
                            idx_dup(uidx(vv(ii)))       = -cnt;
                        end
                    end
                end
            end
        end
    end
end
if any(idx_dup~=0)
    fprintf('Duplicated files:\n')
    fprintf('Count Bad    Date      Time     Size                   FileName                  \n')
    fprintf('===== === ========== ======== ======== ===========================================\n')
    for idx = find(idx_dup~=0)'
        [fname,fdt,fsize] = dirx(ufiles{idx});
        fprintf('%5d %3d %s %8d %s\n',idx_dup(idx),ubad(idx),datestr(fdt,'YYYY/mm/dd HH:MM:SS'),fsize,regexprep(fname{1},'.*/',''))
    end
end
idx_rm              = idx_dup<0;
ufiles(idx_rm)      = [];
uleo(idx_rm)        = [];
ugnss(idx_rm)       = [];
udt(idx_rm)         = [];
ubad(idx_rm)        = [];

idx_dup     = zeros(size(pfiles));
cnt         = 0;
for ll = leos
    for gg = gnsses
        uidx    = find(strcmp(pleo,ll) & strcmp(pgnss,gg));
        uu      = diff(pdt(uidx))<duration(0,20,0);
        if any(uu)
            u1  = find(diff([false;uu;false])==1);
            u2  = find(diff([false;uu;false])==-1);
            for v1 = 1:length(u1)
                cnt             = cnt+1;
                vv              = u1(v1):u2(v1);
                idx_dup(uidx(vv))   = cnt;
                ubads           = pbad(uidx(vv));
                minubad         = min(ubads);
                ii              = ubads==minubad;
                idx_dup(uidx(vv(~ii)))  = -cnt;
                vv              = vv(ii);
                if ~isscalar(vv)
                    maxfdt      = 0;
                    maxpos      = NaN;
                    for ii = 1:length(vv)
                        [~,fdt] = dirx(pfiles{uidx(vv(ii))});
                        if fdt > maxfdt
                            maxfdt  = fdt;
                            if isfinite(maxpos)
                                idx_dup(uidx(vv(maxpos)))   = -cnt;
                            end
                            maxpos  = ii;
                        else
                            idx_dup(uidx(vv(ii)))       = -cnt;
                        end
                    end
                end
            end
        end
    end
end
if any(idx_dup~=0)
    fprintf('Duplicated files:\n')
    fprintf('Count Bad    Date      Time     Size                   FileName                  \n')
    fprintf('===== === ========== ======== ======== ===========================================\n')
    for idx = find(idx_dup~=0)'
        [fname,fdt,fsize] = dirx(pfiles{idx});
        fprintf('%5d %3d %s %8d %s\n',idx_dup(idx),ubad(idx),datestr(fdt,'YYYY/mm/dd HH:MM:SS'),fsize,regexprep(fname{1},'.*/',''))
    end
end
idx_rm              = idx_dup<0;
pfiles(idx_rm)      = [];
pleo(idx_rm)        = [];
pgnss(idx_rm)       = [];
pdt(idx_rm)         = [];
pbad(idx_rm)        = [];

umatch      = zeros(size(ufiles));
pmatch      = zeros(size(pfiles));
for ll = leos
    fprintf('%s - Matching for %s-',datetime('now','Format','HH:mm:ss'),ll{1});
    for gg = gnsses
        fprintf(gg{1}(1));
        uidx    = find(strcmp(uleo,ll) & strcmp(ugnss,gg));
        pidx    = find(strcmp(pleo,ll) & strcmp(pgnss,gg));
        [uu,pp] = find(abs(udt(uidx) - pdt(pidx)')<duration(0,20,0));
        umatch(uidx(uu))    = pidx(pp);
        pmatch(pidx(pp))    = uidx(uu);
    end
    fprintf('\n')
end
idx         = (pdt>dt_cmpr+1 | pdt<dt_cmpr) & pmatch==0;
pfiles(idx) = [];
ptoken(idx) = [];
pbad(idx)   = [];
pleo(idx)   = [];
pgnss(idx)  = [];
pdt(idx)    = [];
pmatch(idx) = [];

alllen                                          = length(umatch)+sum(pmatch==0);
allfiles                                        = cell(alllen,2);
allfiles(1:sum(pmatch>0),1)                     = ufiles(pmatch(pmatch>0));
allfiles(1:sum(pmatch>0),2)                     = pfiles(pmatch>0);
allfiles(sum(umatch>0)+(1:sum(umatch==0)),1)    = ufiles(umatch==0);
allfiles(length(umatch)+(1:sum(pmatch==0)),2)   = pfiles(pmatch==0);
allfilesQ                                       = cell(alllen,2);
allfilesQ(1:sum(pmatch>0),1)                    = ufilesQ(pmatch(pmatch>0));
allfilesQ(1:sum(pmatch>0),2)                    = pfilesQ(pmatch>0);
allfilesQ(sum(umatch>0)+(1:sum(umatch==0)),1)   = ufilesQ(umatch==0);
allfilesQ(length(umatch)+(1:sum(pmatch==0)),2)  = pfilesQ(pmatch==0);
allleo                                          = cell(alllen,1);
allleo(1:sum(pmatch>0))                         = uleo(pmatch(pmatch>0));
allleo(sum(umatch>0)+(1:sum(umatch==0)))        = uleo(umatch==0);
allleo(length(umatch)+(1:sum(pmatch==0)))       = pleo(pmatch==0);
allgnss                                         = cell(alllen,1);
allgnss(1:sum(pmatch>0))                        = ugnss(pmatch(pmatch>0));
allgnss(sum(umatch>0)+(1:sum(umatch==0)))       = ugnss(umatch==0);
allgnss(length(umatch)+(1:sum(pmatch==0)))      = pgnss(pmatch==0);
alldt                                           = repmat(datetime(1980,1,6),size(allfiles));
alldt(1:sum(pmatch>0),1)                        = udt(pmatch(pmatch>0));
alldt(1:sum(pmatch>0),2)                        = pdt(pmatch>0);
alldt(sum(umatch>0)+(1:sum(umatch==0)),1)       = udt(umatch==0);
alldt(length(umatch)+(1:sum(pmatch==0)),2)      = pdt(pmatch==0);
allbad                                          = nan(size(allfiles));
allbad(1:sum(pmatch>0),1)                       = ubad(pmatch(pmatch>0));
allbad(1:sum(pmatch>0),2)                       = pbad(pmatch>0);
allbad(sum(umatch>0)+(1:sum(umatch==0)),1)      = ubad(umatch==0);
allbad(length(umatch)+(1:sum(pmatch==0)),2)     = pbad(pmatch==0);
allbad(isnan(allbad))                           = -1;
fprintf('%s - Process completed!\n',datetime('now','Format','HH:mm:ss'));
save(sprintf('cmpr_ucar_%s.mat',dt_cmpr),'all*');
