%Comparison of blue whale spec-corr detector and manual picks (xlsx file)
%Convert all times to numbers and detection output to xls file before
%running
%Code assumes three sets of groundtruth files (high-medium-low)

clear all;
%load picking data and effort
[pnum1, txt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\Groundtruth\SOCAL46M_BmB_MarApr_jsb','Detections');
[eff1, efftxt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\Groundtruth\SOCAL46M_BmB_MarApr_jsb','MetaData','e2:f2');
[pnum2, txt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\Groundtruth\SOCAL47N_BmB_Sept_jsb_hrbins','Detections');
[eff2, efftxt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\Groundtruth\SOCAL47N_BmB_Sept_jsb_hrbins','MetaData','e2:f2');
[pnum3, txt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\Groundtruth\SOCAL47H_BmB_Dec_smk_hrbins','Detections');
[eff3, efftxt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\Groundtruth\SOCAL47H_BmB_Dec_smk_hrbins','MetaData','e2:f2');
% 
%load detections
[dnum1, txt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\ROC hrbins\th30 hrs Mar');
[dnum2, txt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\ROC hrbins\th30 hrs Sept');
[dnum3, txt] = xlsread('H:\Analysis Detectors\SOCAL 2012 - for ROC\ROC hrbins\th30 hrs Dec');

%convert to Matlab times
pmatnum1 = ones(size(pnum1)).*datenum('30-Dec-1899')+pnum1;
effmat1 = ones(size(eff1)).*datenum('30-Dec-1899')+eff1;
dmatnum1 = ones(size(dnum1)).*datenum('30-Dec-1899')+dnum1;
pmatnum2 = ones(size(pnum2)).*datenum('30-Dec-1899')+pnum2;
effmat2 = ones(size(eff2)).*datenum('30-Dec-1899')+eff2;
dmatnum2 = ones(size(dnum2)).*datenum('30-Dec-1899')+dnum2;
pmatnum3 = ones(size(pnum3)).*datenum('30-Dec-1899')+pnum3;
effmat3 = ones(size(eff3)).*datenum('30-Dec-1899')+eff3;
dmatnum3 = ones(size(dnum3)).*datenum('30-Dec-1899')+dnum3;

tincrement = datenum([0 0 0 1 0 0]);
time1veccount = datevec(effmat1(1));
time1count = datenum([time1veccount(1) time1veccount(2) time1veccount(3) time1veccount(4) 0 0]);
time2veccount = datevec(effmat2(1));
time2count = datenum([time2veccount(1) time2veccount(2) time2veccount(3) time2veccount(4) 0 0]);
time3veccount = datevec(effmat3(1));
time3count = datenum([time3veccount(1) time3veccount(2) time3veccount(3) time3veccount(4) 0 0]);

%first create matrix with first test dataset
z = 1;
while time1count<effmat1(2)
    %create vector with 3 columns: hour of det, detection (1), pick (2)
    compmat(z,1) = time1count;
    time1count = time1count+tincrement;
    z = z+1;
end

ccnt = length(compmat);
pickcount = 1;
detcount = 1;
for z = 1:ccnt-1
    if (pickcount<=length(pmatnum1))&&(pmatnum1(pickcount)>=compmat(z))&&...
            (pmatnum1(pickcount)<compmat(z+1)),
        compmat(z,3) = 1;
        pickcount = pickcount+1;
    else compmat(z,3) = 0;
    end
    if (detcount<=size(dmatnum1,1))&&(dmatnum1(detcount)>=compmat(z))&&...
            (dmatnum1(detcount)<compmat(z+1)),
        compmat(z,2) = 1;
        newind = find(dmatnum1>compmat(z+1));
        if size(newind,1)>0, detcount = newind(1);
        else detcount = NaN;
        end
    else compmat(z,2) = 0;
    end
end

%then repeat for next dataset
z = size(compmat,1)+1;
st = z;
while time2count<effmat2(2)
    %create vector with 3 columns: hour of det, detection (1), pick (2)
    compmat(z,1) = time2count;
    time2count = time2count+tincrement;
    z = z+1;
end

ccnt = length(compmat);
pickcount = 1;
detcount = 1;
for z = st:ccnt-1
    if (pickcount<=length(pmatnum2))&&(pmatnum2(pickcount)>=compmat(z))&&...
            (pmatnum2(pickcount)<compmat(z+1)),
        compmat(z,3) = 1;
        pickcount = pickcount+1;
    else compmat(z,3) = 0;
    end
    if (detcount<=size(dmatnum2,1))&&(dmatnum2(detcount)>=compmat(z))&&...
            (dmatnum2(detcount)<compmat(z+1)),
        compmat(z,2) = 1;
        newind = find(dmatnum2>compmat(z+1));
        if size(newind,1)>0, detcount = newind(1);
        else detcount = NaN;
        end
    else compmat(z,2) = 0;
    end
end

%and third dataset
z = size(compmat,1)+1;
st = z;
while time3count<effmat3(2)
    %create vector with 3 columns: hour of det, detection (1), pick (2)
    compmat(z,1) = time3count;
    time3count = time3count+tincrement;
    z = z+1;
end

ccnt = length(compmat);
pickcount = 1;
detcount = 1;
for z = st:ccnt-1
    if (pickcount<=length(pmatnum3))&&(pmatnum3(pickcount)>=compmat(z))&&...
            (pmatnum3(pickcount)<compmat(z+1)),
        compmat(z,3) = 1;
        pickcount = pickcount+1;
    else compmat(z,3) = 0;
    end
    if (detcount<=size(dmatnum3,1))&&(dmatnum3(detcount)>=compmat(z))&&...
            (dmatnum3(detcount)<compmat(z+1)),
        compmat(z,2) = 1;
        newind = find(dmatnum3>compmat(z+1));
        if size(newind,1)>0, detcount = newind(1);
        else detcount = NaN;
        end
    else compmat(z,2) = 0;
    end
end

% 
miss1 = find(compmat(:,3)&~compmat(:,2));
fals1 = find(~compmat(:,3)&compmat(:,2));

MC = length(miss1)/(ccnt-1)
FA = length(fals1)/(ccnt-1)