%simthenplotter.m   loads and plots the 3 output files from perl scrip simwaymthen.pl
clear all
close all

initialize_simradplot


%Define color map same as for BiomaperII
	colormap(cmap(1,100));         % make color map for -100 to -55 dB when
	colmap=colormap;              % datrange = 1e-4 to 1e-8
	%colmap=[colmap;ones(10,3)];   % add white for range -55 to -50dB
	m=32;
	g=[12:m-1]'/max(m-1,1);
	g=1-g;
	g=[g g g];
	colmap=[colmap;g];            % add gray-scale for range -55 to -45dB
	colmap=[colmap;zeros(20,3)];  % add black for range -45 to -40dB
	%colmap=[colmap;zeros(19,3)];  % add black for range -45 to -41.25dB
	%colmap=[colmap;ones(1,3)];  % add white for -40 dB
	colormap(colmap);
	% datrange= max and min values for 10log(sv) data (Albatross IV data series)
	%datrange =[10*log10(1e-4) 10*log10(1e-10)]; %this sets range of color bar.
	%clegend5(datrange,'sv (dB)',.87)                %see below same values applied
%End Define color map


td=length(processday);
for q=1:td
DAY=processday(q);

for freq=1:3
simfile=[path,'SIMf', num2str(DAY(q,:)), '.out', num2str(freq)];
load(simfile);
disp(freq)

switch freq
case 1, echo38=eval(simfile(11:17)); echo38=rot90(fliplr(echo38+8));
case 2, echo120=eval(simfile(11:17)); echo120=rot90(fliplr(echo120+15));
case 3, echo200=eval(simfile(11:17)); echo200=rot90(fliplr(echo200+15));
end;

end; 

simtfile=[path,'SIMt', num2str(DAY(q,:)), '.time', num2str(freq)];
load(simtfile); 
gmt=eval(simtfile(11:17));


gmt=gmt(1:length(gmt)-2,:);%Alec changed from -1

echo38=echo38(:,1:length(gmt));
echo120=echo120(:,1:length(gmt));%Alec's addition
echo200=echo200(:,1:length(gmt));%Alec's addition



[rsim, csim]=size(simfile);
[rdata, cdata]=size(echo38);

depth=([1:250]);

%do single 3 panel plot
figure(4);
%PLOT 38
subplot(3,1,1)
%GET RIGHT DEPTHS DEPENDING ON DAY
if DAY<= 227,depth38=depth;
else depth38=depth.*2;
end;   
colormap(colmap);
imagesc(gmt(1:length(echo38),1),depth38,echo38,[-100 -40])
colorbar
title(['NBP0204 Simrad EK500 38 kHz on Day:  ', num2str(DAY(q,:)) ]);
axis([mingmt maxgmt 0 max(depth38)]);
ylabel('Depth (m)')
drawnow
orient landscape

%PLOT 120
subplot(3,1,2)
colormap(colmap);
imagesc(gmt(1:length(echo120),1),depth,echo120,[-100 -40])
colorbar
title(['NBP0204 Simrad EK500 120 kHz on Day:  ', num2str(DAY(q,:)) ]);
axis([mingmt maxgmt 0 250]);
ylabel('Depth (m)')
drawnow
orient landscape

%PLOT 200
subplot(3,1,3)
colormap(colmap);
imagesc(gmt(1:length(echo200),1),depth,echo200,[-100 -40])
colorbar
title(['NBP0204 Simrad EK500 200 kHz on Day:  ', num2str(DAY(q,:)) ]);
axis([mingmt maxgmt 0 250]);
xlabel('Year Day (GMT)')
ylabel('Depth (m)')
drawnow
orient landscape


%do separate plots
if singleplots==1
figure (1)   
%GET RIGHT DEPTHS DEPENDING ON DAY
if DAY<= 227,depth38=depth;
else depth38=depth.*2;
end;   
colormap(colmap);
imagesc(gmt(1:length(echo38),1),depth38,echo38,[-100 -40])
colorbar
title(['NBP0204 Simrad EK500 38 kHz on Day:  ', num2str(DAY(q,:)) ]);
axis([mingmt maxgmt 0 max(depth38)]);
xlabel('Year Day (GMT)')
ylabel('Depth (m)')
drawnow
orient landscape

figure (2)
colormap(colmap);
imagesc(gmt(1:length(echo120),1),depth,echo120,[-100 -40])
colorbar
title(['NBP0204 Simrad EK500 120 kHz on Day:  ', num2str(DAY(q,:)) ]);
axis([mingmt maxgmt 0 250]);
xlabel('Year Day (GMT)')
ylabel('Depth (m)')
drawnow
orient landscape

figure (3)
colormap(colmap);
imagesc(gmt(1:length(echo200),1),depth,echo200,[-100 -40])
colorbar
title(['NBP0204 Simrad EK500 200 kHz on Day:  ', num2str(DAY(q,:)) ]);
axis([mingmt maxgmt 0 250]);
xlabel('Year Day (GMT)')
ylabel('Depth (m)')
drawnow
orient landscape
 
   
end;
end;

%print a jpeg of each figure window
%for figname=1:4
%pfile=([simfile(11:17),'_f',num2str(figname), '.jpg']);
%print(figname,'-djpeg99',  pfile);
%end;

save(simfile(11:17));
