%function plotgb(emat,yint,depthint,mindepth,maxdepth,left,bottom,step,ic,tflag,mflag,moc,filename)
%plotgb2.M   called by callplot2.m which acts as this functions frontend
%This function creates a pseudocolor plot of volume backscatter data
%Original by Linda Martin modified by P. Wiebe, August 1993
% emat = file name in matlab workspace that has data to be plotted
%	 program assumes data file has six columns of data as:
%		col 1: Yearday.time
%		col 2: latitude (degrees and fractions of degrees)
%		col 3: longitude (- for Northern Hemisphere and West)
%		col 4: strata depths (M)
%		col 5: Transducer number (not used)
%		col 6: Volume backscatterwhos  ing data (sv, not logged)
% yint = equals the number of depth bins in data file and assumes each
%        bin is 2 meters in depth
% xint = equals the number of horizontal bins and is determined in the
%        the function
% figtitle = title to go on plot
%
% nplot = number of plots - max of four
%
% This function was derived from f:\gb\gb12-22\gbplot1.m
%
%set global variables
global lat lon emat trandist deltapos bottom xint yint depthint mindepth maxdepth left step ic tflag mflag moc filename plhan plax
i=1;
hold on
%emat=emat(emat(:,4)<=yint,:);
%whos
%pause
%
xint=length(emat(:,8)); %  use length of first column of sv
%pause
%reshape the backscatter data into a matrix with depth going down 
%and position going across
gresgrid=emat(:,8:length(emat(1,:)));%new format has sv values in rows after column 7

%bksctr=emat(:,8:length(emat(:,6);
% use the length of the first row of sv. 
%gresgrid=[zeros(yint,xint)];  %not needed with new format
%gresgrid(:)=bksctr;%not needed with new format

%set all bottom values (=0) to nan's
% reshape all sv into single column before searching
%  tmpsv=reshape(gresgrid, yint*xint,1);
%  index=find(tmpsv(:,1)==0);
%  for jdex=1:length(index);
%    tmpsv(index(jdex),1)=nan;
% end
 %reshape back to original shape
%  gresgrid=reshape(tmpsv, xint, yint); %xint is rows, yint is columns
% end set bottom values to nan's


%create color map and add legend
if ic==1  % do this loop only the first time into plot

colormap(cmap(1,100));         % make color map for -80 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
colormap(colmap);
save colmap colmap -ascii
% 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',.87);                %see below same values applied
%clegend5(10*log10(gresgrid+10e-10),'SV')
end

%Skip next section if doing time plot
if tflag==1 
%*************************************************************
% this section fixes gps positions (both missing points and speed filter)
clear trandist deltapos  %need to clear these before starting computation
lat=emat(:,2);
lon=emat(:,3);
gpsfix;
    
%end gpsfix

%calculate transect distances (firsttime)
%save allat.mat lat% done for curtain2.m
%save allon.mat lon % done for curtain2.m
%tdistnf
%spdfilnf  %call speed filter mfile to correct for excess speed (sets points = to previous points)
%pause
gpsfix  %call routine a second time to interpolate results of speed filter
%save allat.mat lat % done for curtain2.m
%save allon.mat lon % done for curtain2.m
tdist   % call this a second time to get final distance matrix for plot
   % must have at least 10 points
%figure(2);plot(trandist);pause
%hold on;plot(trandist);hold off;
emat(:,2)=lat;  % put smoothed lon and lat data into emat
emat(:,3)=lon;

%clear lat;
%clear lon;
%clear trandist;


%scale horizontal axis of plot when plotting distance in meters
%width=.85;
%width=(max(trandist)/5000)*.87; %scale width of plot so that 5000 m = full range
%width=(max(trandist)/8000)*.87; %scale width of plot so that 8000 m = full range
%width=(max(trandist)/9000)*.87; %scale width of plot so that 9000 m = full range
%width=(max(trandist)/10000)*.87; %scale width of plot so that 10000 m = full range
%width=(max(trandist)/11000)*.87;%Scale width of plot so that 11000 m = full range
%width=(max(trandist)/15000)*.87;%Scale width of plot so that 15000 m = full range
%width=(max(trandist)/20000)*.87;%Scale width of plot so that 20000 m = full range
%width=(max(trandist)/25000)*.87;%Scale width of plot so that 25000 m = full range
%width=(max(trandist)/30000)*.87; %scale width of plot so that 30000 m = full range
%width=(max(trandist)/32000)*.87; %scale width of plot so that 32000 m = full range
%width=(max(trandist)/70000)*.87; %Scale width of plot so that 70000 m = full range
%width=(max(trandist)/60000)*.87;%Scale width of plot so that 60000 m = full range
width=(max(trandist)/max(trandist))*.87;%Scale width of plot so that 25000 m = full range

labelx='time (julian day.time)';
%***********************************************************

else    % tflag = 2 and do time plot computations of timedist
  trandist = emat(1:length(emat(:,1)),1); %trandist = time in unit steps
  labelx='Time (year day)';
  mintime=min(emat(:,1));
  maxtime=max(emat(:,1));
  elapsetime=(maxtime-mintime)*24*60; % time of transect in minutes
  tscale=elapsetime/(24*60); % elapsetime in min divided by minutes in a day
  %scale horizontal axis of plot when plotting distance as time
 width=((max(trandist)-min(trandist))/tscale)*.87;%15 min; Scale width of plot so that 15 min = full range

end

height=(yint*depthint/maxdepth)*step-.05; % scale height of plot 
%height=(yint*depthint/200)*step-.05; % scale height of plot 
%height=(yint*depthint/75)*step-.05; % scale height of plot 
%height=(yint*depthint/40)*step-.05; % scale height of plot 


% make depth column for plotting purposes
depthcol= mindepth:depthint:maxdepth;

plax(ic)=axes('position', [left bottom .85*width height]);
set(gca,'drawmode','fast');
%if length(filename)==length('svnf120')
 %   if filename =='svnf120'
  %     tmp=10*log10(gresgrid)-4.09;
   %    pcolor(trandist', depthcol',tmp') %note value of 1.01e-10 added to vol.Scat.=0 in callplot.m
   %else
   plhan(ic)= pcolor(trandist', depthcol',10*log10(gresgrid)'); %note value of 1.01e-10 added to vol.Scat.=0 in callplot.m
    %end
   %else 
   % pcolor(trandist', depthcol',10*log10(gresgrid)') %note value of 1.01e-10 added to vol.Scat.=0 in callplot.m
 %end
 
 %set(gca,'tickdir','out')
 set(plax(ic),'tickdir','out');

%set(gca,'ydir','reverse')

set(plax(ic),'fontweight','bold');
set(plax(ic),'linewidth',[1.0]);
set(plax(ic),'ydir','reverse');
if tflag==1
set(plax(ic),'yticklabelmode','manual');
set(plax(ic),'ytick',[1 25 50 75 100 200]); 
set(plax(ic),'yticklabel',[' 1 ';' 25';' 50';' 75';'100';'200']);
set(plax(ic),'xticklabelmode','manual');
set(plax(ic),'xtick',[0 5000 10000 15000 20000 25000 30000 35000 40000 45000 50000 55000 60000]); 
set(plax(ic),'xticklabel',['  0  ';' 5000'; '10000'; '15000';'20000';'25000';'30000';'35000';'40000';'45000';'50000';'55000';'60000']);
end
%********** section to plot moc profile over acoustics section plot **********
if mflag ==1
   hold on; plmoc=plot(moc(:,2),moc(:,3),'w*',moc(:,2),moc(:,3),'w-');  % plot mocness tow ontop of acoustic section plot
   set(plmoc,'linewidth',[1.0]);
end
%*************************************************************

%if ic==1
%  xlabel(labelx)
%  ylabel('Depth (m)')
%end
shading('interp');
%shading('flat')
caxis([10*log10(1e-10) 10*log10(1e-4)]); %this makes the color plot have the
hold on                                 %the same color map from one plot to
%show contour plot for comparison       %next & same as color bar.

%figure(1)
%v29=[1e-8 5e-8 9e-8 3e-7 1e-6]
   
%clab=contour(trandist,emat(1:yint,4),10*log10(gresgrid),10*log10(v29),'w');
%clabel(clab);
%xy=[0.5,1.0; 0.35,0.55; 0.45,0.55; 0.45,0.0 ;0.55,0.0; 0.55,0.55; 0.65