%
% Copyright 2020 Renjie Wu
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%     http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%

function locations = scanYahooA1(folder, isWindow)
    csvFiles = dir(fullfile(folder, '*.csv'));
    locations = zeros(length(csvFiles), 1);
    
    for i = 1:length(csvFiles)
        filePath = fullfile(folder, csvFiles(i).name);
        % real_(.*).csv
        TSid = str2double(extractBetween(csvFiles(i).name, 6, length(csvFiles(i).name) - 4));
        
        TS = csvread(filePath, 1, 0);
        label = TS(:, 3);
        TS = TS(:, 2);
        
        anomaly = find(label == 1);
        if size(anomaly, 1) > 0
            if isWindow == 0
                locations(TSid) = anomaly(end) / length(TS);
            else
                windowStart = anomaly(end);
                while label(windowStart) > 0
                    windowStart = windowStart - 1;
                end
                windowStart = windowStart + 1;
                locations(TSid) = windowStart / length(TS);
            end
        else
            locations(TSid) = -1;
        end
    end
end