%
% Overlay Portuguese Ledge map with grid
% Rob, 12 July 2018
%

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

  lstr={};
  if exist( 'plgFigPtr' ) ~= 1
    plgFigPtr= figure;
    set( plgFigPtr, 'Name', 'Portuguese Grid', 'DeleteFcn', 'clear plgFigPtr' );
    set(gcf,'position',position)
    if(exist('fontsize')) 
      MbTrnFontsize=fontsize;
    else
      MbTrnFontsize=20;
    end
    MbTrnFontweight='normal';
    set(gca,'fontsize',MbTrnFontsize,'fontweigh',MbTrnFontweight)
    set(gcf,'defaultaxesfontsize',MbTrnFontsize,'defaulttextfontsize', ...
	    MbTrnFontsize)
  else
    figure(plgFigPtr);
  end

  if( exist('plg') == 0 )
    loc = 'port';
    loadGrd;
  end
%
% Compute the tile boundaries.  The tiles are 1kmx1km, with center
% separations of 750m, which creates non-overlapped regions of
% 500x500 meters. So there are 9 tiles.
%
% Start with arrays of the lower left corners.

 llc = [594000 4061000;
        594500 4061000;
        595000 4061000;
        594000 4061500;
        594500 4061500;
        595000 4061500;
        594000 4062000;
        594500 4062000;
        595000 4062000];
%
% Find the centers:
  nTiles = length(llc);
  tc =  llc + ones(nTiles,1)*[250 250];
%
% Find the boundaries:
  tb = [tc(:,1)-500 tc(:,1)+500 tc(:,2)-500 tc(:,2)+500];
   
  tvx = tb(:,1:2);
  tvy = tb(:,3:4);

  tvx = [];
  tvy = [];
  for( i=1:nTiles )
    tvx = [ tvx; tb(i,1);tb(i,2);tb(i,2);tb(i,1)];
    tvy = [ tvy; tb(i,3);tb(i,3);tb(i,4);tb(i,4)];
  end
%
% Create "centers" file:
  tilesFid = fopen('tiles.csv','w');
  fprintf( tilesFid, 'TileName , Easting , Northing , %d\n', nTiles );
%
% Build grdcut argument strings and loop through:

  gcutmap = sprintf('/usr/local/bin/grdcut PortugueseLedge20080424TopoUTM_NoNan.grd ');
  for( i=1:nTiles )
    tileName    = sprintf('PortugueseLedgeTile%d.grd', i);
    gcutNameArg = sprintf('-G%s ', tileName);
    gcutBounds  = sprintf('-R%d/%d/%d/%d -N ',tb(i,1),tb(i,2),...
			 tb(i,3),tb(i,4));
    gcutCmd = [gcutmap gcutNameArg gcutBounds];
    fprintf('%s', gcutCmd);
    fprintf(tilesFid, '%s , %d , %d\n', tileName, tc(i,1), tc(i,2)); 
    if( ~exist(tileName) )
      system(gcutCmd)
    end
  end

  Levels = [-105:1:-50];
  tic
  fprintf('Plotting Map\n');
  [cc hh] = contourf(plg.eastings,plg.northings,plg.z,Levels);
  clabel(cc,hh);
  %contourf(plg.eastings,plg.northings,plg.z);
  axis equal
  set(gca,'ytick',[4061000 4061500 4062000 4062500]);
  toc
  grid on
  hold on

  if( exist( 'missionFileName') );
    dataWP=corridor(['~/missions/',missionFileName]);
  end
  
  linewidth=1;
  if( exist( 'dataWP' ) == 1 & min(size(dataWP)) > 1 )
    plot( dataWP(:,3), dataWP(:,2), 'r', 'linewidth', linewidth);
    lstr(length(lstr)+1,1) = {'Trackline'};
    plot( dataWP(:,3), dataWP(:,2), 'gd','linewidth', linewidth);
    lstr(length(lstr)+1,1) = {'Waypoints'};
  end
  sizeDataWP = size(dataWP);
  
  for( i=1:nTiles )
    locStr = sprintf('%d',i);
    tHandle = text(tc(i,1),tc(i,2),locStr);
    set(tHandle,'color','m');
  end
