%doplot.m    called by curtain2.m to make the curtain plot
%
%%%%%%%%%%%%%%%%  Do Curtain Plot of BIOMAPER data %%%%%%%%%%%%%%%%%%%%%%

%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
% Take out double %  i.e. %% to reinstate this section of code
% this section fixes gps positions 
%%  global lat lon emat trandist deltapos yint xint
%%	xint=length(eval([plot_file '(:,4)']))/yint; 
%%	lat=eval([plot_file '(:,2)']);
%%	lon=eval([plot_file '(:,3)']);

 %      length(lat)
 %      length(lon)
 %      disp('dplot 1')
 %       pause 

 	% fix gps positions
	% assumes lat and lon are in columns 2 and 3 of file sv4.
%%	newlat=lat;
%%	newlon=lon;
%%	cc=0;
%%	for nn=(yint+1):yint:length(lat)
%%		mm=nn;qq=0;
%%		if nn>cc
%%		while (lat(mm)==lat(mm-1) & lon(mm)==lon(mm-1) & mm+yint < length(lat)), mm=mm+yint;qq=qq+1; end

%%		if qq==0
%%		 newlat(nn:nn+(yint-1))=lat(nn:nn+(yint-1));
%%		 newlon(nn:nn+(yint-1))=lon(nn:nn+(yint-1));
%%		else
%%		 if mm+yint < length(lat) % this if statment put in on 24 
                             % Nov after having problems with gb25 bombing on 
                            % dlat at end of file.
%%		      mm=mm+yint;qq=qq+1;
		      %nn
%%		 end
%%	       dlat=(lat(mm)-lat(nn-1))/(qq+1);
%%		 dlon=(lon(mm)-lon(nn-1))/(qq+1);
%%		 cc=nn+(qq*yint);
%%		 for rr=nn:yint:(qq)*yint+nn
%%        		newlat(rr:rr+(yint-1))=newlat(rr-yint:rr-1)+dlat;
%%        		newlon(rr:rr+(yint-1))=newlon(rr-yint:rr-1)+dlon;
%%     	 end
%%    		end
%% 		end
%%	end
%%	lat=newlat; 
%%	lon=newlon;

  %    length(lat)
  %    length(lon)
  %     disp('dplot 2')
  %    pause
%%      clear newlat mm rr nn dlon dlat qq cc
%%      clear newlon;
%%save d:\temp\allat.mat lat
%%save d:\temp\allon.mat lon
%%tdist   % call tdist.m to get distance mattrix 
%%spdfil  %call speed filter mfile to correct for excess speed (sets points = to previous points)
%%gpsfix  %call routine a second time to interpolate results of speed filter

%%tdist   % call this a second time to get final distance mattrix for plot
%length(lat)
%length(lon)
%pause
%% 	eval([plot_file '(:,2)' '=' 'lat;']); % put smoothed lon and lat data back into file
%%	eval([plot_file '(:,3)' '=' 'lon;']);
% end GPS fix
%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

% Set up Plotting Window
kount=kount+1
H1=figure(kount);
%  	set(H1,'position',[100,300,700,480]);
  	set(H1,'position',[100,200,850,580]);
	%set(gca,'box','on','xtick',[],'ytick',[],'visible','on');
	set(H1,'menubar','none','numbertitle','off');
   %set(H1,'color',[1,0,0]);
   mindepth=eval([plot_file '(1,5)']);%sv4(1,5);
   maxdepth=eval([plot_file '(1,6)']);%sv4(1,6);
   if mindepth > 1
     excess=fix(mindepth);%(sv4(1,5));
     mindepth=mindepth - excess; %sv4(:,5)-excess; % adjust depths so that min depth is between 0 and 1 m.
     maxdepth=maxdepth - excess; %adjust maxdepth too
   end
   depthint= eval([plot_file '(1,7)']); %sv4(2,7) % size of depth interval in meters
   yint = length(eval([plot_file '(1,1:end)']))-7;% number of depth intervals use with biomaper II
   %  yint=(maxdepth/depthint)-1;   % number of depth intervals use with greene bomber
   % make depth column for plotting purposes
   depthcol= mindepth:depthint:maxdepth;
   %depthcol=depthcol';
% set up plotting variables
	  vol=eval([plot_file '(:,8:yint+7)']); % use the length of the first row of sv. 

   xkm=eval([plot_file '(:,3)']);
   %i=find(isnan(xkm)==1);xkm(i)=-999;
   xint=length(xkm);
   tmp=repmat(xkm,1,yint);
   xkm=tmp;
   %xkm=xkm';
   ykm=eval([plot_file '(:,2)']);
   tmp=repmat(ykm,1,yint);
   ykm=tmp;
   %ykm=ykm';
   tmp=repmat(depthcol,xint,1);%reshape(eval([plot_file '(:,4)']),yint,length(eval(plot_file))/yint);
   depthcol=tmp;
   %end set up plotting variables
  

%Define color map
	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',.87)                %see below same values applied
%End Define color map

% Make Plot
	hold on
	plax=axes('position', [.1 .1 .70 .85]); % Set Plotting window 
	surf(xkm,ykm,depthcol,10*log10(vol+1e-10)) %depthint = 2m, 1m, 0.5m DEPTH BINS
   set(gca,'zdir','reverse')
   ylab=ylabel('Latitude')
   set(ylab,'fontweight','bold')
   xlab=xlabel('Longitude')
   set(xlab,'fontweight','bold')
   zlab=zlabel('Depth (m)')
   set(zlab,'fontweight','bold')
   tlab=title(title_text)
   set(tlab,'fontweight','bold')
	view(az,el)
	shading('interp')
	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
 rotate3d
 set(plax,'fontweight','bold');
 set(plax,'linewidth',[1.0]);

 %next & same as color bar.
	%axis([-68 -67.3 41 41.3 0 65]);
	%set(gcf,'inverthardcopy','off')
	%print
%end %end if

