
%this script can be configured to load proto session data and convert to
%pds.  It assumes the darks have been processed and stored in the
%"gooddarks.mat" file and that the glucometer data has been stored in the
%"refmeter.mat" file.

dirlist{01}=  'Mike\051724';user{01}='mike';
dirlist{02}=  'Mike\051824';user{02}='mike';
dirlist{03}= 'Pam\20240619';user{03}= 'pam';
dirlist{04}='Jake\20240620';user{04}='jake';
dirlist{05}='Anna\20240621';user{05}='anna';
dirlist{06}= 'Pam\20240622';user{06}= 'pam';
dirlist{07}='Mike\20240705';user{07}='mike';
dirlist{08}= 'Deb\20240706';user{08}= 'deb';
dirlist{09}='Mike\20240706';user{09}='mike';
dirlist{10}= 'Pam\20240707';user{10}= 'pam';
dirlist{11}= 'Deb\20240708';user{11}= 'deb';
dirlist{12}='Mike\20240708';user{12}='mike';

load meandarks meandark darkfileinfo
load refmeter refmeter

session=struct;
splinappweight=5e7;
refmeternames={{'CGM'},{'Ultra'},{'Lite'},{'Oxi'},{'Verio'}};

for k=1:length(dirlist)

    cd(dirlist{k})

    d=dir([user{k},'*.csv']);
    
    session(k).user=user{k};
    session(k).folder=dirlist{k};
    session(k).date=datetime(d(1).datenum,'convertfrom','datenum','format','yyyyMMdd');
    session(k).starttime=d(1).datenum;
    session(k).endtime=d(end).datenum;
    session(k).processingscript.name='sit2pdsloopcenterbin2.m';
    session(k).processingscript.samplerate=125;
    session(k).processingscript.recordtime=10;
    session(k).processingscript.avptime=1;
    session(k).processingscript.avpwavelength=4;

    refmeteruse(1)=sum(((refmeter.cgm(:,1)>=session(k).starttime)  +  (refmeter.cgm(:,1)<=session(k).endtime))==2)>0;
    refmeteruse(2)=sum(((refmeter.ultra(:,1)>=session(k).starttime)+(refmeter.ultra(:,1)<=session(k).endtime))==2)>0;
    refmeteruse(3)=sum(((refmeter.lite(:,1)>=session(k).starttime) + (refmeter.lite(:,1)<=session(k).endtime))==2)>0;
    refmeteruse(4)=sum(((refmeter.oxi(:,1)>=session(k).starttime)  +  (refmeter.oxi(:,1)<=session(k).endtime))==2)>0;
    refmeteruse(5)=sum(((refmeter.verio(:,1)>=session(k).starttime)+(refmeter.verio(:,1)<=session(k).endtime))==2)>0;

    session(k).refmeteruse=refmeteruse;
    session(k).refmeternames=refmeternames;

    for i=1:length(d)

        disp(['Processing Session ',dirlist{k},'\',user{k},' Sitting ',num2str(i)])

        darkid=find(abs(meandark(:,end)-d(i).datenum)==min(abs(meandark(:,end)-d(i).datenum)));
        session(k).sitting(i).dark=meandark(darkid,:);
        session(k).sitting(i).darkinfo=darkfileinfo(darkid);

        dark2skintimedelta=abs(meandark(darkid,end)-d(i).datenum)*24*60;
        session(k).sitting(i).darkinfo.dark2skintimedelta=dark2skintimedelta;

        if dark2skintimedelta>30
            warning(['Dark ',num2str(dark2skintimedelta,'%03.0f'),' Minutes From ',dirlist{k},'\',d(i).name,...
                ' Session ',int2str(k),' Sitting ',int2str(i,'%03.0f')]);
        end

        skin=load(d(i).name);
        session(k).sitting(i).skininfo.fileinfo=d(i);
        session(k).sitting(i).skininfo.spectracount=size(skin,1);

        if std(skin(:,265))<.00065
            sd=skin(:, 9:264)-ones(size(skin,1),1)*meandark(darkid,1:256);
            session(k).sitting(i).skininfo.firstpixel=9;
        else
            sd=skin(:,10:265)-ones(size(skin,1),1)*meandark(darkid,1:256);
            session(k).sitting(i).skininfo.firstpixel=10;
        end

        [sitdatapersec,pds,pds01]=sit2pdsloopcenterbin2(sd,...
            session(k).processingscript.samplerate,...
            session(k).processingscript.recordtime,...
            session(k).processingscript.avptime,...
            session(k).processingscript.avpwavelength);
        
        session(k).sitting(i).sitdatapersec=sitdatapersec;
        session(k).sitting(i).pds=[pds,ones(6,1)*[k,i],(1:6)',...
            d(i).datenum + (-55:10:-5)'/(24*60*60)];
        session(k).sitting(i).pds01=[pds01,ones(6,1)*[k,i],(1:6)',...
            d(i).datenum + (-55:10:-5)'/(24*60*60)];

    end

    clear darkid dark2skintimedelta skin sd sitdatapersec pds pds01

    session(k).sitabs=cat(1,session(k).sitting.pds01);
    session(k).sitabsorg=cat(1,session(k).sitting.pds);

    st=session(k).sitabs(:,end);
    session(k).spectimes=st;
    session(k).glu(1:length(st),length(refmeteruse))=zeros(length(st),1);

    for j=1:length(refmeteruse)
        switch j
            case 1
                fs=refmeter.cgm;
            case 2
                fs=refmeter.ultra;
            case 3
                fs=refmeter.lite;
            case 4
                fs=refmeter.oxi;
            case 5
                fs=refmeter.verio;
        end

        if refmeteruse(j)
            fsstartid=find(fs(:,1)>=(st(1)-15/24/60),1,'first');
            fsendid=find(fs(:,1)<=(st(end)+15/24/60),1,'last');
            fsid=(fsstartid:fsendid)';

            session(k).glu(:,j)=splinapp(fs(fsid,1),fs(fsid,2),ones(size(fsid,1),1)*splinappweight,st);
            session(k).glufitinfo(j).fsstartid=fsstartid;
            session(k).glufitinfo(j).fsendid=fsendid;
            session(k).glufitinfo(j).fsid=fsid;
            session(k).glufitinfo(j).splinappweight=splinappweight;            
        end

        clear fsstartid fsendid fsid fs
    end

    clear st refmeteruse

    cd('..\..')
end

clear i j k dirlist d splinappweight user
save protosessions 