%
% Create surface plot without using the Mapping Toolbox
%

if( exist( 'plg' ) ~= 1 )
  return
end

if(~exist('position'))
  position = get(0,'screensize');
  npposition(1:2) = position(1:2) + round([position(3)*.02 position(4)*.04]);
  npposition(3:4) = round([position(3)*.95 position(4)*.90]);
  position = npposition;
end

maxEl = 2.8e6;
%
% Turn on decimation by default
if( exist( 'deci' ) ~= 1 ) 
  %
  % Compute decimation factor deci:
  deci = ceil( numel(plg.z)/maxEl);
  %
  % Round deci to the nearest multiple of 5:
  if( deci > 5 ) deci = 5*round(deci/5); end
end
fprintf('Decimation factor = %d.\n',deci);

co = [0 0 1;
      0 0.5 0;
      1 0 0;
      0 0.75 0.75;
      0.75 0 0.75;
      0.75 0.75 0;
      0.25 0.25 0.25];
set(groot,'defaultAxesColorOrder',co)

if( exist( 'surfFig' ) ~= 1 )
  surfFig = figure;
  figure(surfFig);
else
  figure(surfFig);
end
set( surfFig, 'Name', '3D Bathymetry Map Using Surf', 'DeleteFcn', 'clear surfFig' );
set(gcf,'position',position)
set(gcf,'colormap',jet)
if(~exist('fontsize')) fontsize = 20;end
if(~exist('vertEx')) vertEx = 1;end
tic
%surfhand = surf(downsample(plg.eastings,decix), downsample(plg.northings,deciy),...
%                downsample(downsample(plg.z,deciy)',decix)');
surfhand = surf(downsample(plg.eastings,deci), downsample(plg.northings,deci),...
                vertEx*downsample(downsample(plg.z,deci)',deci)');
axis equal
toc
fprintf('Finished first plot.\n');
surfaxes1 = gca;
set(gca,'xticklabel',cellstr(num2str(get(gca,'xtick')','%d')))
set(gca,'yticklabel',cellstr(num2str(get(gca,'ytick')','%d')))
if( vertEx ~= 1 )
  set(gca,'zticklabel',cellstr(num2str(get(gca,'ztick')'/vertEx,'%d')))
end
set(gca,'fontsize',fontsize)
titlestr = sprintf('%s with %d Elements', ncname, ceil(numel(plg.z)/deci));
if(deci > 1)
  titlestr = strcat(titlestr, ' After Decimation');
end
if(vertEx > 1)
  tempstr = sprintf(' Vertical Ex. = %d',vertEx);
  titlestr = strcat(titlestr, tempstr);
end

titlestr = strcat(titlestr, ['; Data Set: ', DirName]);

thand=title(titlestr,'interpreter','none');
ylabel('Northings')
xlabel('Eastings')

if(exist('dataNav'))
  hold on
  plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), -vertEx*dataNav(:,mDepth),...
	'k','linewidth',4);
  plg.groundtrace = interp2(plg.eastings,plg.northings,plg.z,dataNav(:,mPos_y), ...
			    dataNav(:,mPos_x));
  plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), vertEx*plg.groundtrace,...
	'g','linewidth',4);
end


switch(ncname)
 case('UpperMontereyCanyon10m.grd')  
  set(gca,'CameraPosition', [4.5854e+05 4.2239e+06 1.1746e+05]);
  set(gca,'CameraPositionMode','manual');
  set(gca,'CameraTarget',[5.8457e+05 4.0655e+06 576.8364]);
  set(gca,'CameraTargetMode','manual');
  set(gca,'CameraUpVectorMode','manual');
  set(gca,'CameraViewAngle',1.4247);
  set(gca,'CameraViewAngleMode','manual');
 case('MontereyCanyonShallowTopo10m.grd')  
 case('PortugueseLedgeTopo1m_v20140527.grd')  
  if(0)
    set(gca,'CameraPosition', [588629 4.05391e+06 5591.13]);
    set(gca,'CameraPositionMode','manual');
    set(gca,'CameraTarget',[594642 4.06174e+06 -111.977]);
    set(gca,'CameraTargetMode','manual');
    set(gca,'CameraUpVectorMode','manual');
    set(gca,'CameraViewAngle',1.42687);
    set(gca,'CameraViewAngleMode','manual');
  end
 case('Topo_UpperCanyonTesting_2m_Wall.grd')  

  if( 0 )
  set(gca,'CameraPosition', [627407.73    4092283.18      23218.66]);
  set(gca,'CameraPositionMode','manual');
  set(gca,'CameraTarget',[589405.77    4071071.63       -591.24]);
  set(gca,'CameraTargetMode','manual');
  set(gca,'CameraUpVectorMode','manual');
  set(gca,'CameraViewAngle',1.4191);
  set(gca,'CameraViewAngleMode','manual');
  
  set(gca,'CameraPosition', [610637.92 4101853.84 36445.39]);
  set(gca,'CameraPositionMode','manual');
  set(gca,'CameraTarget',[589426.23 4070645.44 -624.92]);
  set(gca,'CameraTargetMode','manual');
  set(gca,'CameraUpVectorMode','manual');
  set(gca,'CameraViewAngle',1.42);
  set(gca,'CameraViewAngleMode','manual');
  end

  
  cPos   = [614064.59 4091020.69 20294.52];
  cTarg  = [589369.42 4070559.44 -658.40];
  cAngle = 1.42;
  
  
  set(gca,'CameraPosition', cPos);
  set(gca,'CameraPositionMode','manual');
  set(gca,'CameraTarget',cTarg);
  set(gca,'CameraTargetMode','manual');
  set(gca,'CameraUpVectorMode','manual');
  set(gca,'CameraViewAngle',cAngle);
  set(gca,'CameraViewAngleMode','manual');
  
  
 case('MontereyCanyon2800m_1m_Geo.grd')

  if( exist( 'surfFig2' ) ~= 1 )
    surfFig2 = figure;
    figure(surfFig2);
  else
    figure(surfFig2);
  end
  set( surfFig2, 'Name', '3D Bathymetry Map Using Surf', 'DeleteFcn', 'clear surfFig2' );
  set(gcf,'position',position)
  set(gcf,'colormap',jet)
  fontsize=14;
  indxEastings = find(plg.eastings> 551700 & plg.eastings< 552300);
  indxNorthings = find(plg.northings>4051600 & plg.northings<4052000);
  tic
  surfhand2 = surf(plg.eastings(indxEastings), plg.northings(indxNorthings), ...
		   plg.z(indxNorthings,indxEastings));
  axis equal
  toc
  surfaxes2 = gca;
  set(gca,'fontsize',fontsize)
  set(gca,'xticklabel',cellstr(num2str(get(gca,'xtick')','%d')))
  set(gca,'yticklabel',cellstr(num2str(get(gca,'ytick')','%d')))
  titlestr = sprintf('%s with %d Elements', loc, ...
		     length(indxNorthings)*length(indxEastings));
  title(titlestr);
  
  % set(gca,'CameraPosition', [5.4480e+05 4.0424e+06 3.9865e+03]);
  % set(gca,'CameraPositionMode','manual');
  % set(gca,'CameraTarget',[5.5203e+05 4.0518e+06 -2.8707e+03]);
  % set(gca,'CameraTargetMode','manual');
  % set(gca,'CameraUpVectorMode','manual');
  % set(gca,'CameraViewAngle',0.8394 );
  % set(gca,'CameraViewAngleMode','manual');
  
  hold on

  if(exist('dataNav'))
    plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), -vertEx*dataNav(:,mDepth),...
          'r','linewidth',3);
    plg.groundtrace = interp2(plg.eastings,plg.northings,plg.z,dataNav(:,mPos_y), ...
                              dataNav(:,mPos_x));
    plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), vertEx*plg.groundtrace,...
          'g','linewidth',2);
  end
  
  if( exist( 'surfFig3' ) ~= 1 )
    surfFig3 = figure;
    figure(surfFig3);
  else
    figure(surfFig3);
  end
  set( surfFig3, 'Name', '3D Bathymetry Map Using Surf', 'DeleteFcn', 'clear surfFig3' );
  set(gcf,'position',position)
  set(gcf,'colormap',jet)
  fontsize=14;
  %indxEastings = find(plg.eastings> 549800 & plg.eastings< 550500);
  indxEastings = find(plg.eastings> 549800 & plg.eastings< 551800);
  indxNorthings = find(plg.northings>4051000 & plg.northings<4052000);
  tic
  surfhand3 = surf(plg.eastings(indxEastings), plg.northings(indxNorthings), ...
		   plg.z(indxNorthings,indxEastings));
  axis equal
  surfaxes3=gca;
  set(gca,'fontsize',fontsize)
  set(gca,'xticklabel',cellstr(num2str(get(gca,'xtick')','%d')))
  set(gca,'yticklabel',cellstr(num2str(get(gca,'ytick')','%d')))
  titlestr = sprintf('%s with %d Elements', loc, ...
		     length(indxNorthings)*length(indxEastings));
  title(titlestr);
  
  hold on

  if(exist('dataNav'))
    plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), -vertEx*dataNav(:,mDepth),...
          'r','linewidth',3);
    plg.groundtrace = interp2(plg.eastings,plg.northings,plg.z,dataNav(:,mPos_y), ...
                              dataNav(:,mPos_x));
    plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), vertEx*plg.groundtrace,...
          'g','linewidth',2);
  end

  toc
  
 case{'MontereyCanyon1850BendEast_MAUV1m.grd','MAUVTopo1m_CCEDeep.grd'}

  set(gca,'CameraPosition', [6.3475e+05 4.0219e+06 3.4040e+04]);
  set(gca,'CameraPositionMode','manual');
  set(gca,'CameraTarget',[5.8252e+05 4.0605e+06 -510.6184]);
  set(gca,'CameraTargetMode','manual');
  set(gca,'CameraUpVectorMode','manual');
  set(gca,'CameraViewAngle',3.2873);
  set(gca,'CameraViewAngleMode','manual');

  hold on;
  [Ms7(1) Ms7(2)]=LL2utm(-122.095852, 36.701629);
  [Bin(1) Bin(2)]=LL2utm(-122.093780, 36.701833);
  [Ms6(1) Ms6(2)]=LL2utm(-122.091713, 36.702065);  %McLane Profiler
  
  indxE  = find(plg.eastings  > Ms6(1));
  indxE6 = indxE(1);
  indxN  = find(plg.northings > Ms6(2));
  indxN6 = indxN(1);
  plot3(plg.eastings(indxE6), plg.northings(indxN6), plg.z(indxN6,indxE6)+1,'rx');
  
  indxE  = find(plg.eastings  > Ms7(1));
  indxE7 = indxE(1);
  indxN  = find(plg.northings > Ms7(2));
  indxN7 = indxN(1);
  plot3(plg.eastings(indxE7), plg.northings(indxN7), plg.z(indxN7,indxE7)+1,'rx');
  
  indxE  = find(plg.eastings  > Bin(1));
  indxEb = indxE(1);
  indxN  = find(plg.northings > Bin(2));
  indxNb = indxN(1);
  plot3(plg.eastings(indxEb), plg.northings(indxNb), plg.z(indxNb,indxEb)+1,'rx');

  % hold on
  % if(exist('dataNav'))
  %   plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), -vertEx*dataNav(:,mDepth),...
  %         'r','linewidth',3);
  %   plg.groundtrace = interp2(plg.eastings,plg.northings,plg.z,dataNav(:,mPos_y), ...
  %                             dataNav(:,mPos_x));
  %   plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), vertEx*plg.groundtrace,...
  %         'g','linewidth',2);
  % end
  
  if( exist( 'surfFig2' ) ~= 1 )
    surfFig2 = figure;
    figure(surfFig2);
  else
    figure(surfFig2);
  end
  set( surfFig2, 'Name', '3D Bathymetry Map Using Surf', 'DeleteFcn', 'clear surfFig2' );
  set(gcf,'position',position)
  set(gcf,'colormap',jet)
  fontsize=14;
  indxEastings = find(plg.eastings> 578000 & plg.eastings< 582000);
  indxNorthings = find(plg.northings>4061000 & plg.northings<4063000);
  tic
  %    surfhand2 = surf(plg.eastings(indxEastings), plg.northings(indxNorthings), ...
  %                     plg.z(indxNorthings,indxEastings));
  deci2 = 2;
  surfhand2 = surf(downsample(plg.eastings(indxEastings),deci2), ...
		   downsample(plg.northings(indxNorthings),deci2),...
		   downsample(downsample(plg.z(indxNorthings,indxEastings),deci2)',deci2)');
  axis equal
  toc
  surfaxes2 = gca;

  set(gca,'fontsize',fontsize)
  set(gca,'xticklabel',cellstr(num2str(get(gca,'xtick')','%d')))
  set(gca,'yticklabel',cellstr(num2str(get(gca,'ytick')','%d')))
  titlestr = sprintf('%s with %d Elements', loc, ...
		     length(indxNorthings)*length(indxEastings));
  title(titlestr);

  set(gca,'CameraPosition', [5.6894e+05 4.0463e+06 9.6997e+03]);
  set(gca,'CameraPositionMode','manual');
  set(gca,'CameraTarget',[5.8079e+05 4.0617e+06 -1.5302e+03]);
  set(gca,'CameraTargetMode','manual');
  set(gca,'CameraUpVectorMode','manual');
  set(gca,'CameraViewAngle',1.0227);
  set(gca,'CameraViewAngleMode','manual');

  %
  % Now add the instruments:

  hold on;
  plot3(plg.eastings(indxE6), plg.northings(indxN6), plg.z(indxN6,indxE6)+1,'rx');
  plot3(plg.eastings(indxE7), plg.northings(indxN7), plg.z(indxN7,indxE7)+1,'rx');
  plot3(plg.eastings(indxEb), plg.northings(indxNb), plg.z(indxNb,indxEb)+1,'rx');

  if(exist('dataNav'))
    plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), -vertEx*dataNav(:,mDepth),...
          'r','linewidth',3);
    plg.groundtrace = interp2(plg.eastings,plg.northings,plg.z,dataNav(:,mPos_y), ...
                              dataNav(:,mPos_x));
    plot3(gca,dataNav(:,mPos_y), dataNav(:,mPos_x), vertEx*plg.groundtrace,...
          'g','linewidth',2);
  end
  
 case('AxialEM_post2015_AUV2015Bounds_Topo5m.grd')  
  if( 0 )
    set(gca,'CameraPosition', [627407.73    4092283.18      23218.66]);
    set(gca,'CameraPositionMode','manual');
    set(gca,'CameraTarget',[589405.77    4071071.63       -591.24]);
    set(gca,'CameraTargetMode','manual');
    set(gca,'CameraUpVectorMode','manual');
    set(gca,'CameraViewAngle',1.4191);
    set(gca,'CameraViewAngleMode','manual');
  end
  
 case('MontereyCanyon_5m_WB.grd')
  %
  % Add profiler and bin locations
  
  
end

