function gcontour

% direct derivation from webcont from webplot ...
% lat = vector of lats, longs or times for regriding and plotting
% xyt = position time vector from adcpsect 
% d   = direction index (1=longitude, 2=latitude,3=dday )
% zc, zcplot  = depths for contouring
% uv  = velocity data from adcpsect
% levs = contour levels

global XYDATA
global UVDATA
global CONTFIG

if isempty(CONTFIG.ii)
   msg = sprintf('\nNo data <Selection Cancelled?>\n\nClose & select.');
   noticeOK(msg)
   return
end

d = CONTFIG.reftype;
if d > 3, d = d - 4; end % from CONTFIG.reftypes coding for both/vectors
if d == 1    % longitude ref.
   arange = [min(XYDATA.X) max(XYDATA.X)];
elseif d == 2 % latitude ref.
   arange = [min(XYDATA.Y) max(XYDATA.Y)];   
else            % time ref.
   arange = [min(XYDATA.DDAY) max(XYDATA.DDAY)];
end
lat = [min(arange):diff(arange)/100:max(arange)];


x = XYDATA.x;
y = XYDATA.y;
dday = XYDATA.dday;
ii = CONTFIG.ii;
u = UVDATA.u;
v = UVDATA.v;

%set up xyt
xyt=[x(ii) y(ii) dday(ii)]';
% uv=0*[u(:,ii) v(:,ii)];
% uv(:,1:2:end)=u(:,ii);
% uv(:,2:2:end)=v(:,ii);
% zc = XYDATA.zc;

[depthminmax] = str2num(CONTFIG.depthstr);
dmm = find(XYDATA.zc >= depthminmax(1) & XYDATA.zc <= depthminmax(end));
zcplot = XYDATA.zc(dmm);
uv=0*[u(dmm,ii) v(dmm,ii)];
uv(:,1:2:end)=u(dmm,ii);
uv(:,2:2:end)=v(dmm,ii);
zc = zcplot; % largely redundant, but convenient

%addpath /home/noio/programs/matlab/web_adcp;  % for regrid function

u = regrid(uv(:,1:2:end),xyt(d,:),zc,lat,zc,1,3)*100;
v = regrid(uv(:,2:2:end),xyt(d,:),zc,lat,zc,1,3)*100;

set(0,'defaulttextfontsize',12) % 14
set(0,'defaultaxesfontsize',12)
if CONTFIG.bold
   set(0,'defaulttextfontweight','bold')
   set(0,'defaultaxesfontweight','bold')
end
set(gcf,'menubar','none')
set(gcf,'color',[1 1 1])
set(gcf,'units','pixels');
set(gcf,'position',[256 85 500 610]);

if isempty(CONTFIG.custlevstr) | isempty(str2num(CONTFIG.custlevstr))
   maxuv=100*max(max(abs(uv)));
   if maxuv > 300
      levs = [-500:50:500];
      %  llevs = [-200 -100 100 200];   % 0 line added below (thicker)
   elseif maxuv > 200
      levs = [-300:20:300];
      %  llevs = [-200 -100 100 200];
   elseif maxuv > 100
      levs = [-200:20:200];
      %  llevs = [-200:20:200];  %llevs = [-100  100];
   elseif maxuv > 50
      levs = [-100:10:100];
      %  llevs = [-50  50];
   else
      levs = [-50:5:50];
      %   llevs = [-40 -30 -20 -10  10 20 30 40];
   end
else
   levs = str2num(CONTFIG.custlevstr);
end
levs = levs(:)';                             % for color contour levels
llevs = levs;                                % for lines on contour borders, black
if strcmp(CONTFIG.boldlevs,'0'),    % for bold contour lines, black
   boldlevs = [0 0];
else
   boldlevs = str2num(CONTFIG.boldlevs);  
end

if CONTFIG.blkwht
   colormap('gray');
else
   colormap('jet');
end

ha1 = subplot(211);
contour_panel(lat, zcplot, u, levs, llevs, d, boldlevs);
title('Zonal Velocity')

ha2 = subplot(212);
contour_panel(lat, zcplot, v, levs, llevs, d, boldlevs);
title('Meridional Velocity')

if ~CONTFIG.showtitle % = do not omit title
   pos1 = get(ha1,'position');
   pos2 = get(ha2,'position');
   set(gcf,'position',[256 85 500 650]); % 40 pixels taller

   set(gcf,'units','normalized');
   aa = axes('position', [0 0 1 1]);
   axis('off')
   text(0.5, 0.95, CONTFIG.title, ...
      'units','normalized', ...
      'fontsize',14, ...
      'horizontalalignment','center');

   set(ha1,'position',[pos1(1)  pos1(2)*.95  pos1(3)  pos1(4)*.95]);
   set(ha2,'position',[pos2(1)  pos2(2)*.95  pos2(3)  pos2(4)*.95]);
   set(gcf,'units','pixels');
end
   
set(0,'defaulttextfontsize',10)
set(0,'defaultaxesfontsize',10)
set(0,'defaulttextfontweight','normal')
set(0,'defaultaxesfontweight','normal')

%%%%%%%%%%%%%%

function contour_panel(lat, zcplot, u, levs, llevs, d, blevs)

global XYDATA
global CONTFIG
                                       
[c,h] = contourf6(lat,zcplot,u,levs);
set(h, 'EdgeColor', 'none');
caxis([levs(1) levs(end-1)]);
dcolbar(levs, 'vert', 'cm s^{-1}');
set(gca, 'ydir', 'reverse', 'TickDir', 'out');

%ylim = get(gca, 'YLim'); %ylim(1) = 0; set(gca, 'YLim', ylim);
%ylim = get(gca, 'YLim'); %
ylim = str2num(CONTFIG.depthstr); % should be 0, but can be forced
%ylim(1) = yl(1);
set(gca, 'YLim', ylim);

% Try to prevent collisions of the tick labels after
% conversion to lat/lon by lltick; this could also be done
% by leaving the ticks in place and just deleting every other
% label after running lltick.
xt = get(gca, 'XTick');
if length(xt) > 6,
   n1 = length(num2str(xt(1))); n2 = length(num2str(xt(2)));
   if n1 > n2,
      xt = xt(2:2:end);
   else
      xt = xt(1:2:end);
   end
end

if d==1,
 xlabel('Longitude')
 lltick(gca,'x','lon')
elseif  d==2
 xlabel('Latitude')
 lltick(gca,'x','lat')
else % (d==3)
 xlabel('Decimal Days')
end

ylabel('Depth (m)')
hold on
if CONTFIG.outline % contour lines
   [c,h] = contour6(lat,zcplot,u, llevs, 'k');
   set(h, 'linewidth', 0.1);
   if ~isempty(blevs) % bold contour lines
      [c,h] = contour6(lat,zcplot,u, blevs, 'k');
      set(h, 'linewidth', 2);
      set(gca, 'Box', 'on', 'Layer', 'top');
   end
else
   if ~isempty(blevs) % only emphasized contour lines
      [c,h] = contour6(lat,zcplot,u, blevs, 'k');
      set(h, 'linewidth', 0.1);  % as fine
      set(gca, 'Box', 'on', 'Layer', 'top');
   end
end

if CONTFIG.webflag
   old_axes = gca;
   aa = axes('position', [0 0 1 1]);
   axis('off')
   ht = text(0.5, 0.005, 'Preliminary processing at Univ. of Hawaii',...     
      'units', 'normalized',...
      'VerticalAlignment', 'bottom',...
      'HorizontalAlignment' , 'center');
   set(ht, 'Interpreter', 'none') ;
   axes(old_axes);
end
   
%%%%%%%%%%%%%%%% EOF