%function to plot a slice of data through the model
% can have xy, xz, yz or quiver slices
%
% function plotslice(slice,slice2,ptype,index,dtype,flag,units)
%
% input variables
%   slice  slice of data to plot
%   slice2 second data slice if needed
%   ptype  'xy' (horizontal), 'xz' (zonal), 'yz' (meridional)
%          'qv' (horizontal quiver plot)
%   index  index for depth (m), latitude (N) or longitude (east)
%          negative index indicates barotropic (depth-integrated) response
%   dtype  string description of data type
%   flag   0: temperature data
%          1: data with zero mean
%          2: other data
%          3: salinity data
%   units  units for contour plotting
%
% global variables
%   DEPTHS  mit model depths (m)
%   LAT     mit model meridional grid (Latitude North)
%   LONG    mit model zonal grid (longitude east)
%   NX      zonal model dimension
%   NY      meridional model dimension
%   NZ      vertical model dimension

function plotslice(slice,slice2,ptype,index,dtype,flag,units,ci)

% check input arguments

if all(all(isnan(slice))), 
  error('all elements of slice are nans')
end
if isstr(slice2)                    % only one slice argument
  if nargin < 6, flag = ''; end
  if nargin < 5, dtype = 0; end
  if nargin < 4, index = ''; end
  if nargin < 3, error('need at least 4 input arguments'); end
  units=flag; flag=dtype; dtype=index; index=ptype; ptype=slice2;
else                                % two slice arguments
  if nargin < 7, units = ''; end
  if nargin < 6, flag = 0; end
  if nargin < 5, dtype = ''; end
  if nargin < 4, error('need at least 5 input arguments'); end
end

global DEPTHS LAT LONG NX NY NZ

% contour intervals for vertical temperature sections
vci = [0:.1:.6 .8:.2:2 2 3 4 5 10 15 20 25 30];

% contour intervals for vertical salinity sections
vsi = 31:.2:37;

%$$$ colormap([.9 .9 .9; 1 1 1]);
colormap([.9 .9 .9; 1 1 1]);

% contour plot slice

if ptype == 'xy'                         % horizontal slice
  tmp=slice;
  ix=find(~isnan(slice));
  tmp(ix)=zeros(size(ix));
  ix=find(isnan(slice));
  tmp(ix)=ones(size(ix));
  shadeland(LONG,LAT,tmp); hold on
  clear tmp ix
   
  if flag==0                            % temperature data
    c=contour(LONG,LAT,slice); clabel2(c);
  elseif flag==1                        % difference data
    cntoura(LONG,LAT,slice,0,units);
  elseif flag==2                        % other data
    c=contour(LONG,LAT,slice); clabel2(c);
  elseif flag==3                        % salinity data
    c=contour(LONG,LAT,slice); clabel2(c);
  else
    error('unrecognised flag')
  end
  hold off
  title(['horizontal slice at ' num2str(DEPTHS(index)) ' m, ' dtype]);
  xlabel('longitude east');
  ylabel('Latitude North');

elseif ptype == 'xz'                     % zonal slice

  tmp=slice;
  ix=find(~isnan(slice));
  tmp(ix)=zeros(size(ix));
  ix=find(isnan(slice));
  tmp(ix)=ones(size(ix));
  shadeland(LONG,-DEPTHS/1e3,tmp); hold on
  clear tmp ix

  if flag==0                            % temperature data
    c=contour(LONG,-DEPTHS/1e3,slice,vci); clabel2(c);
  elseif flag==1                        % difference data
    cntoura(LONG,-DEPTHS/1e3,slice,0,units);
  elseif flag==2                        % other data
    c=contour(LONG,-DEPTHS/1e3,slice); clabel2(c);
  elseif flag==3                        % other data
    c=contour(LONG,-DEPTHS/1e3,slice,vsi); clabel2(c);
  else
    error('unrecognised flag')
  end
  hold off
  title(['zonal slice at ' num2str(LAT(index)) ' deg lat north, ' dtype]);
  xlabel('longitude east');
  ylabel('depth (km)');

elseif ptype == 'yz'                     % meridional slice
  tmp=slice;
  ix=find(~isnan(slice));
  tmp(ix)=zeros(size(ix));
  ix=find(isnan(slice));
  tmp(ix)=ones(size(ix));
  shadeland(LAT,-DEPTHS/1e3,tmp); hold on
  clear tmp ix
  
  if flag==0                            % temperature data
    c=contour(LAT,-DEPTHS/1e3,slice,vci); clabel2(c);
  elseif flag==1                        % difference data
    cntoura(LAT,-DEPTHS/1e3,slice,0,units);
  elseif flag==2                        % other data
    c=contour(LAT,-DEPTHS/1e3,slice); clabel2(c);
  elseif flag==3                        % other data
    c=contour(LAT,-DEPTHS/1e3,slice,vsi); clabel2(c);
  else
    error('unrecognised flag')
  end
  hold off
  title(['meridional slice at ' num2str(LONG(index)) ' deg long east, ' dtype]);
  xlabel('Latitude North');
  ylabel('depth (km)');

elseif ptype == 'qv'                     % horizontal quiver plot
  tmp=slice;
  ix=find(~isnan(slice));
  tmp(ix)=zeros(size(ix));
  ix=find(isnan(slice));
  tmp(ix)=ones(size(ix));
  shadeland(LONG,LAT,tmp); hold on

  tmp=get(0,'defaultlinelinewidth');
  set(0,'defaultlinelinewidth',0.01)
  myquiver(LONG,LAT,real(slice),imag(slice),5);
  
  % check for existence of slice2 which would in this case contain
  % pressure in m, and contour plot the pressure on the figure

  if ~isstr(slice2)
    if index > 0, cntoura(LONG,LAT,slice2*100,0,'cm',10);
    else  cntoura(LONG,LAT,slice2,0,'m^2'); end
  end

  % put legend on quiver plot

  ix=axis; xx=.29*ix(1)+.71*ix(2); yy=.19*ix(3)+.81*ix(4);
  patch([.99*xx .99*xx 1.13*xx 1.13*xx], ...
      [.9*yy 1.1*yy 1.1*yy .9*yy],'w')
  maxvector = myround(max(abs(slice(~isnan(slice)))));
  myquiver(xx+[0 LONG(2)-LONG(1)],yy+[0 LAT(2)-LAT(1)], ...
      [maxvector nan;nan nan],[0 nan; nan nan],5);
  hold off, set(0,'defaultlinelinewidth',tmp);
  clear ix tmp

  if index > 0
    text(1.03*xx,yy,[num2str(maxvector*100) ' cm/s']);
    title(['horizontal slice at ' num2str(DEPTHS(index)) ' m, ' dtype]);
  else                  % barotropic response
    text(1.03*xx,yy,[num2str(max(abs(slice(~isnan(slice))))) ' m^2/s']);
    title(['barotropic (depth-integrated) response, ' dtype]);
  end
  xlabel('Longitude East');
  ylabel('Latitude North');

else
  error(['unrecognized ptype ''' ptype '''']);
end
