%
% PURPOSE:  Create both a list of triples (x,y,z) defining the test map, and
%           a 2D matrix Z from the triples suitable for plotting with surf().
%
% CHANGELOG:  1) rsm; 1kmx1km, undulating bottom with N/S wall 800 m to the East.
%
% NOTES: 1) The map coordinate system is NED at the surface, with x,y
%           at the lower left.
%

maxN      = 1000;           %meters.  N-S extent.
maxE      = 1000;          %meters.  E-W extent.
wallY     = maxE-200;      %meters.  y location of N/S wall.
wallDepth = 25;            %meters.  Depth east of wall.
depth     = 100;           %meters.  Depth at y = 1.
amplitude =  5;            %meters.  0-peak amplitude of undulations.
wavelength= 50;            %meters.  wavelength of undulations along y.
northing0 = 4061667;       %meters.  UTM nominal location at Portuguese Ledge.
easting0  = 594172;        %meters.  UTM nominal location at Portuguese Ledge.
nOff      = 200;           %meters.  Northing offset of mission start.
eOff      = 100;           %meters.  Easting offset of mission start.
fileName  = 'portWallOver';   %char.    Base name of files.
%
titleStr=[fileName, ' ', num2str(maxN), 'm x ', num2str(maxE), 'm, $\lambda$ = ',num2str(wavelength), 'm'];

%
% Allocate memory:
plg.northings = NaN(maxN,1);
plg.eastings  = NaN(maxE,1);
plg.z = NaN(maxN, maxE);
bottom = zeros(maxN*maxE,3,'double');
whos plg bottom

i = 1;

for( x = 1:maxN )
  for( y = 1:maxE )
    if( y < wallY )
      z = depth + amplitude*sin( 2*pi*y/wavelength );   %Bottom west of the wall at x=800.
    else
      z = wallDepth;                         %Bottom east of the wall.
    end
    bottom(i,:) = double([x y z]);
    i = i+1;
    plg.northings(x) = x;
    plg.eastings(y)  = y;
    plg.z( x, y ) = -z;   %Dave uses x->eastings, y->northings, z up so depth has z<0. 
  end
end
%
% Allocate the rest of the memory for "bottom":
nWallVox = maxN*(-ceil(plg.z(1,wallY-1)-wallDepth-1));
fprintf( 'Now allocating an additional %d elements for the vertical portion.\n',nWallVox);
bottom = [bottom; zeros( nWallVox, 3,'double')];
whos plg bottom

% Fill in the wall:
for( x = 1:maxN )
  for( z = wallDepth:-plg.z(x, wallY-1) )
    bottom(i,:) = double([x wallY z]);
    i = i+1;
    %plg.z( x, wallY ) = z;
  end
end

%Shift to starting waypoint at PL:

bottom(:,1) = double(bottom(:,1)+northing0- nOff);
bottom(:,2) = double(bottom(:,2)+easting0 - eOff);

plg.northings = plg.northings+northing0 - nOff;
plg.eastings = plg.eastings+easting0 - eOff;

dlmwrite([fileName,'.csv'],bottom,'precision','%.2f');
%save(fileName,'bottom','plg','x','y','z')
save(fileName);
