%
% Copyright: bboom@inf.ed.ac.uk
%
% function to retrieve the number of fish and components that processed the
% videos 
%

function [fish_count components_count] = plot_number_of_fish(site,camera,species,start_date,end_date,time_step)

if strcmp(time_step,'day')
    url = 'http://groups.inf.ed.ac.uk/vision/DATASETS/FISH4KNOWLEDGE/WEBSITE/F4KDATASAMPLES/SQL/ALLYEARS/';
elseif strcmp(time_step,'10min')
    url = 'http://groups.inf.ed.ac.uk/vision/DATASETS/FISH4KNOWLEDGE/WEBSITE/F4KDATASAMPLES/SQL/FULLDAY/';
else
    disp('error: invalid timestep');
    return;
end

start_date_vec = datenum(start_date);
end_date_vec = datenum(end_date);

if start_date_vec > end_date_vec
    disp('error: the end date is earlier than the start date');
    return;
end

current_date_vec = start_date_vec;
not_finish = 1
iter = 1;
while not_finish
    date_format = datestr(current_date_vec, 'yyyymmddHHMM');
    
    file_name = ['data_site' num2str(site) '_camera' num2str(camera) '_' date_format '.csv'];
    
    [path_to_file, status] = urlwrite([url file_name],file_name);
    if status
        fish_count(iter) = 0;
        fid = fopen(path_to_file);
        first_row = textscan(fid, '%s', 1,'delimiter', ';');
        rows = textscan(fid, '%f %f %f %f %s %f %f %f %f %f %f %f %f %f %s', 'delimiter', ',','EmptyValue', -Inf,'BufSize', 4096*32);
        fclose(fid);
        % detection_id = 1, fish_id = 4, species_id = 11
        valid_components = ~isnan([rows{1,2} + rows{1,3}]);
        uniq_fish_id = unique(rows{1,4}(valid_components));
        unique_components = unique([rows{1,2}(valid_components) rows{1,3}(valid_components)],'rows');
        
        for ii = 1:size(uniq_fish_id,1);
            index_detections = find(rows{1,4} == uniq_fish_id(ii));
            fish_species = rows{1,11}(index_detections);
            uniq_fish_species = unique(fish_species);
            count = zeros(1,size(uniq_fish_species,1));
            for jj = 1:size(uniq_fish_species,1)
                count(jj) = sum(fish_species == uniq_fish_species(jj));
            end
            [sort_v sort_i] = sort(count,'descend');
            
            if uniq_fish_species(sort_i(1)) > 0
                fish_count(iter) = fish_count(iter) + sum(species == uniq_fish_species(sort_i(1)));
            elseif  size(sort_v,2) > 1
                if sort_v(1) == sort_v(2)
                    fish_count(iter) = fish_count(iter) + sum(species == uniq_fish_species(sort_i(2)));
                end
            end
        end
        components_count(iter) = size(unique_components,1);
        delete(path_to_file);
    else
        fish_count(iter) = 0;
        components_count(iter) = 0;
    end
    
    if strcmp(time_step,'day')
        current_date_vec = addtodate(current_date_vec, 1, 'day');
    elseif strcmp(time_step,'10min')
        current_date_vec = addtodate(current_date_vec, 10, 'minute');
    end
    
    
    if current_date_vec > end_date_vec
        not_finish = 0;
    end
    
    date_format2 = datestr(end_date_vec, 'yyyymmddHHMM');
    iter = iter + 1;
    disp(['reading video at ' date_format ' until ' date_format2]);
end
