%process the calibration sittings into pds and produce dark corrected
%bandpass filtered spectra

dfiles=dir('Dark*');
dftimes=cat(1,dfiles.datenum);
sfiles=dir('pam*');

pp=gcp('nocreate');
if isempty(pp)
    parpool
end

tic

for i=1:length(sfiles)
    s=load(sfiles(i).name);
    df=find(abs(dftimes-sfiles(i).datenum)==min(abs(dftimes-sfiles(i).datenum)));
    d=load(dfiles(df).name);
    sn=stdp(s(:,9),50);
    dn=stdp(s(:,9),50);

    if sn>.0007
%     if s(5,9)>2
        s=s(:,9:264);
    else
        s=s(:,10:265);
    end
    if sn>.003
        warning(['Check ',sfiles(i).name,' for Array Noise'])
    end

    if abs(dfiles(df).datenum - sfiles(i).datenum)>5/1440
        warning(['Dark File for ',sfiles(i).name,' exceeds 5 minutes'])
    end



%     if d(5,9)<1.258
    if dn<.0007
        d=d(:,9:264);
    else
        d=d(:,10:265);
    end
    if dn>.003
        warning(['Check ',dfiles(df).name,' for Array Noise'])
    end




%     sd=s-ones(size(s,1),1)*mean(d(3:end,:));
    sd=s-ones(size(s,1),1)*mean(d(5:end,:));
    clear s d
%     [pds,pds2,~,sdf]=signaltopds(sd,400,10.24,6,0.5,7.5);
%     save(['sit2_',num2str(i,'%03.0f')],'pds','pds2','sdf','sd','sfiles','dfiles')
    [pds,pds2,~,sdf]=signaltopds(sd,400,25.0025,6,0.25,7.5,0);
    save(['sitnofilt_',num2str(i,'%03.0f')],'pds','pds2','sdf','sd','sfiles','dfiles')
    clear pds sdf sd
    toc
end
cd ..
cd 20240121
% dfiles=dir('Dark*');
% sfiles=dir('pam*');
dfiles=dir('Dark*');
dftimes=cat(1,dfiles.datenum);
sfiles=dir('pam*');

% for i=1:length(dfiles)
%     s=load(sfiles(i).name);
%     d=load(dfiles(i).name);
%     if s(5,9)>2
%         s=s(:,9:264);
%     else
%         s=s(:,10:265);
%     end
% 
%     if d(5,9)<1.258
%         d=d(:,9:264);
%     else
%         d=d(:,10:265);
%     end
% 
%     sd=s-ones(size(s,1),1)*mean(d(3:end,:));
%     clear s d
%     [pds,pds2,~,sdf]=signaltopds(sd,400,10.24,6,0.5,7.5);
%     save(['sit_',num2str(i,'%03.0f')],'pds','sdf','sd','sfiles','dfiles')
%     pds=pds2;
%     save(['sit2_',num2str(i,'%03.0f')],'pds','sdf','sd','sfiles','dfiles')
%     clear pds pds2 sdf sd
%     disp([num2str(toc),' ',int2str(i),' ',datestr(now)])
% end
for i=1:length(sfiles)
    s=load(sfiles(i).name);
    df=find(abs(dftimes-sfiles(i).datenum)==min(abs(dftimes-sfiles(i).datenum)));
    d=load(dfiles(df).name);
    sn=stdp(s(:,9),50);
    dn=stdp(s(:,9),50);

    if sn>.0007
%     if s(5,9)>2
        s=s(:,9:264);
    else
        s=s(:,10:265);
    end
    if sn>.003
        warning(['Check ',sfiles(i).name,' for Array Noise'])
    end

    if abs(dfiles(df).datenum - sfiles(i).datenum)>5/1440
        warning(['Dark File for ',sfiles(i).name,' exceeds 5 minutes'])
    end



%     if d(5,9)<1.258
    if dn<.0007
        d=d(:,9:264);
    else
        d=d(:,10:265);
    end
    if dn>.003
        warning(['Check ',dfiles(df).name,' for Array Noise'])
    end




%     sd=s-ones(size(s,1),1)*mean(d(3:end,:));
    sd=s-ones(size(s,1),1)*mean(d(5:end,:));
    clear s d
%     [pds,pds2,~,sdf]=signaltopds(sd,400,10.24,6,0.5,7.5);
%     save(['sit2_',num2str(i,'%03.0f')],'pds','pds2','sdf','sd','sfiles','dfiles')
    [pds,pds2,~,sdf]=signaltopds(sd,400,25.0025,6,0.25,7.5,0);
    save(['sitnofilt_',num2str(i,'%03.0f')],'pds','pds2','sdf','sd','sfiles','dfiles')
    
    clear pds sdf sd
    toc
end
