%% Step 4: This scripts allows manual correction of individual click trains in which
% individual clicks have not been detected in the initial click detection
% or where echoes or noise sources have causes clicks to be falsely
% detected.
% When this script has been run all click trains of interest are ready to
% be processed and analysed further. 

clear all
clf

path = mfilename('fullpath');
hdd = path(1);
spath = [hdd,':\d3\scripts'];

global recdir prefix
%  prefix = 'hp12_272a';  ´
%  prefix = 'hp12_293a';
%  prefix = 'hp13_102a';
%  prefix = 'hp14_226b';
  prefix = 'hp15_218a'; 
%  prefix = 'hp16_264a';

cpath = fullfile(spath,'pernille',prefix,'curC');

recdir = [hdd,':\d3\data\',prefix(1:4),'\',prefix];
caldir = [hdd,':\d3\d3matlab\cal'];

deploy_name = prefix;
prhdir = [hdd,':\d3\data\metadata\prh'];
auddir = [hdd,':\d3\data\metadata\aud']; % where you can find your audits

settagpath('cal',caldir,'prh',prhdir, 'audit', auddir)

global recPieces trialInSession recoveries ttt filename
recoveries = spath;
trialInSession = 1% sequence number %The trial in the session.

ttt = 0.08; % windows size in seconds
channel = 1 ; % one of the two channels on the tag
fs = 5e5;
stwindow = 1; % which window do you want to start at
additionaltime = 0; % in seconds; if you want to look at some seconds after the sequence


addpath(spath)

load(fullfile(spath,[prefix,'_TEST.mat']),'subcl')

cd(recdir)
recPieces = [subcl(:,1) subcl(:,2)+additionaltime]*fs; 
filename = strcat('sound_tag_',  prefix,'_',num2str(round(10*subcl(trialInSession,1))),'_',num2str(round(10*subcl(trialInSession,2))),'.mat');
%  load(fullfile(cpath, strcat('sound_tag_',  prefix,'_',num2str(round(10*subcl(trialInSession,1))),'_',num2str(round(10*subcl(trialInSession,2))),'.mat')))
% %  % % % Above line is used when I want to correct from the beginning
load(fullfile(cpath, 'corrected', strcat('sound_tag_',  prefix, '_',num2str(round(10*subcl(trialInSession,1))),'_',num2str(round(10*subcl(trialInSession,2))),'.mat'))) % This line is used when I want to load the file I have already started correcting 
% plot(curC.rc{channel}(2:end,1), diff(curC.rc{channel}(:,1))/fs, 'ko')
% 
title('before correction')
%%

curC = findmissedporpoiseclicksTagD3(recdir,prefix,curC,channel,stwindow);
curCC = curC;

if exist(fullfile(cpath, 'corrected', strcat('sound_tag_', prefix, '_',num2str(round(10*subcl(trialInSession,1))),'_',num2str(round(10*subcl(trialInSession,2))), '.mat')))
    load(fullfile(cpath, 'corrected', strcat('sound_tag_',  prefix, '_',num2str(round(10*subcl(trialInSession,1))),'_',num2str(round(10*subcl(trialInSession,2))),'.mat')))
    curC.rc{channel} = curCC.rc{channel};
    disp('loaded and updated the mat file')
end
save(fullfile(cpath, 'corrected', strcat('sound_tag_', prefix, '_',num2str(round(10*subcl(trialInSession,1))),'_',num2str(round(10*subcl(trialInSession,2))))), 'curC')
% focal = curC.rc{channel}(curC.rc{channel}(:,4)==1,:);
% calf = curC.rc{channel}(curC.rc{channel}(:,4)==2,:);
figure, plot(curC.rc{channel}(2:end,1), diff(curC.rc{channel}(:,1))*1000/fs, 'ko')
title('after correction')
%%
% figure, 
% set(gcf, 'position', [ 1          41        1920        1084])
% plot((focal(2:end,1)-water)/fs, diff(focal(:,1))/fs, 'ko')
% hold on
% % plot(calf(2:end,1), diff(calf(:,1))/fs, 'ro')
% scatter((calf(2:end,1)-water)/fs, diff(calf(:,1))/fs, 25, calf(2:end,3)-max(calf(2:end,3)), 'filled')
% set(gca, 'yscale', 'log')
% set(gca, 'fontsize', 16, 'fontname', 'Arial')
% yl = get(gca, 'ylim');
% % plot([0 0], yl, 'r--', 'linewidth', 1.5)
% grid on
% xlabel('Time from release (s)', 'fontsize', 20, 'fontname', 'Arial')
% ylabel('Inter-click interval (s)', 'fontsize', 20, 'fontname', 'Arial')
% set(gca, 'position', [0.0437    0.1100    0.9372    0.8150])
% set(gca, 'xlim', [-50 900])
% lg = legend('mother', 'calf', 'location', 'northwest');
% set(lg, 'box', 'off')
% cbar = colorbar;
% set(cbar, 'fontsize', 16)
% set(get(cbar, 'ylabel'), 'String', 'Relative RL (dB re 1µPa)', 'fontsize', 13, 'color', 'w', 'fontname', 'Arial')
% set(get(cbar, 'ylabel'), 'position', [ -0.5000  -37.8139    9.1603])