% script to extract an xy slice through a data matrix
% i.e. horizontal, looking down from above
% matrix returned has rows of y and columns of x
%
% function slc = getxy(z_index,flg);
%
% input variables
%   z_index  vertical (depth) index of slice
%   flg      0 defaults to new style DATA format, see readmodel.m
%            1 old style, do not flip z
%            2 old style, flip z
%
% required global variables
%   DATA     input data matrix created with readmodel
%   NX       zonal model dimension
%   NY       meridional model dimension
%   NZ       vertical model dimension
%
% see readmodel getxz getyz

function slc = getxy(z_index,flg);

global DATA NX NY NZ

% check input arguments

if(z_index > NZ | z_index < 1 | z_index ~= round(z_index))
  error('illegal Z index in getxy function');
end

if nargin < 2
  flg = 0;
end

if flg==0                            % new style DATA format
  
  % DATA matrix is set up as NZ rows and NY*NX columns
  slc = DATA(z_index,:);
  slc = reshape(slc,NX,NY);
  slc = slc';

else                                 % old style DATA format

  % DATA matrix is set up as NX rows and NY*NZ columns
  if flg==2, z_index=1+NZ-z_index; end        % flip z
  slc = DATA(:,[((z_index-1)*NY)+1:z_index*NY]);
  slc = slc';

end
