% script to add an xy perturbation to DATA matrix
% i.e. horizontal, looking down from above
%
% function x = putxy(slice,z_index,flipz);
%
% input variables
%   slice    matrix perturbation has rows of y, and columns of x
%   z_index  vertical (depth) index of slice
%                (defaults to first level)
%   flipz    if flag is set, flip z coordinate
%                (defaults to no flip)
%
% global variables
%   DATA     input data matrix created with readmodel
%   NX       zonal model dimension
%   NY       meridional model dimension
%   NZ       vertical model dimension

function x = putxy(slice,z_index,flipz);

global DATA NX NY NZ

% check input arguments

if nargin < 3, flipz = 0; end           % default to no flip
if nargin < 2, z_index = 1; end         % default to first level
if nargin < 1
  error(['need to provide a NY=' int2str(NY) ...
          ' by NX=' int2str(NX) ' perturbation matrix']);
end
if flipz, z_index = 1+NZ-z_index; end
if(z_index > NZ | z_index < 1 | z_index ~= round(z_index))
  error('illegal Z index in putxy function');
end
[m n]=size(slice);
if m ~= NY | n ~= NX
  error(['input slice must have NY=' int2str(NY) ...
          ' by NX=' int2str(NX) ' dimensions']);
end

% DATA matrix is set up as NY*NZ rows, and NX columns

DATA([((z_index-1)*NY)+1:z_index*NY],:) = ...
    DATA([((z_index-1)*NY)+1:z_index*NY],:) + slice;
