% eda02_04
% Plot Black Rock Forest (BRF) temperature dataset
% This script creates two figures. Fig 1 displays
% the whole dataset and Fig 2 just a portion.
% clicking on the Fig 1 steers the enlarged part
% in Fig 2.  Clicking outside the axes in Fig 1
% exits the script

% read the data
D=load('brf_temp.txt');
t=D(:,1);
d=D(:,2);
Ns=size(D);
N=Ns(1);
M=Ns(2);

% number of data points in in fig 2.  You can cane
% this to be whatever you want
width=1000;

% preliminary plot in Fig 1
figure(1);
clf;
set(gca,'LineWidth',2);
plot( t, d, 'k','LineWidth',2 );
hold on;
title('Black Rock Forest Temp');
xlabel('time in days after Jan 1, 1997');
ylabel('temperature');
a = axis;

% preliminary plot in Fig 2
figure(2);
clf;
set(gca,'LineWidth',2);
Nmin=1;
Nmax=length(D(:,1));
plot( t(1:width), d(1:width), 'k','LineWidth',2 );
hold on;
plot( t(1:width), d(1:width), 'k.','LineWidth',2 );
title('Black Rock Forest Temp');
xlabel('time in days after Jan 1, 1997');
ylabel('temperature');

% display this message to the Command Window
disp('click on figure 1 to steer figure 2');
disp('(click outside of axes exits)');

% loop over detecting mouse clicks and redrawing\
% the figures
while 1
figure(1);
[tc, dc] = ginput(1);  % detect mouse click
% exit if clich is outside of the axes
if( (tc<a(1)) || (tc>a(2)) || (dc<a(3)) || (dc>a(4)) )
    break;
end
i=find((t>tc),1 ); % find index i of x(i) corresponding to click
% note early versions of MatLab need this instead: i=find((t>=tc)); i=i(1);
% redraw Fig 1 with vertical bar marking click
clf;
set(gca,'LineWidth',2);
plot( t, d, 'k','LineWidth',2 );
hold on;
title('Black Rock Forest Temp');
xlabel('time in days after Jan 1, 1997');
ylabel('temperature');
% adjust click index so that it data always inside plot
if( (i-width/2)<Nmin )
    i=Nmin+width/2;
end
if( (i+width/2)>Nmax )
    i>Nmax-width/2
end
x = t(i);
% plot vertical bar cooresponding to click
plot( [x, x], [a(3), a(4)], 'k', 'LineWidth',2 );
% redraw Figure 2
figure(2);
clf;
plot( t(i-width/2:i+width/2), d(i-width/2:i+width/2), 'k', 'LineWidth',2 );
hold on;
plot( t(i-width/2:i+width/2), d(i-width/2:i+width/2), 'k.', 'LineWidth',2 );
title('Black Rock Forest Temp');
xlabel('t in days after Jan 1, 1997');
ylabel('temperature');
end



