function [Z, lat, lon, levels, lineSpec, props] = ...  
         parseContourInputs(function_name, varargin)
%PARSECONTOURINPUTS Parse inputs for the contour functions.
%
%  [Z, LAT, LON, LEVELS, PROPS] = PARSECONTOURINPUTS(FUNCTION_NAME,
%  VARARGIN) parses the inputs in VARARGIN and returns members. Z is a
%  M-by-N data grid. LAT is either a M-by-N or M-by-1 coordinate grid. LON
%  is either a M-by-N or N-by-1 coordinate grid. LEVELS is a vector
%  representing the contour levels. PROPS is a cell array of the MATLAB
%  grpaphics properties for the contour  patches.
%
%  See also CONTOURM, CONTOUR3M.

%  Copyright 2004 The MathWorks, Inc.
%  $Revision: 1.1.6.2 $    $Date: 2004/12/18 07:42:57 $

% Check input arguments
checknargin(2,inf,nargin,function_name);

if (numel(varargin) == 2) || (numel(varargin{2}) == 3) || ...
   (numel(varargin{2}) == 6) 

   % (Z, R, ...)
   Z = varargin{1};
   R = varargin{2};

   % Check if R is a referencing vector or matrix.
   if numel(R) == 6
      mapgate('checkrefmat',R, function_name, 'R', 2);
   else
      mapgate('checkrefvec',R, function_name, 'R', 2);
      R = refvec2mat(R,size(Z));
   end
   [lon, lat] = pixcenters(R, size(Z),'makegrid');
   levels = [];
   varargin(1:2) = [];
else
   % (LAT, LON, Z, ...)
   lat = varargin{1};    
   lon = varargin{2};
   Z = varargin{3};
   levels = [];
   varargin(1:3) = [];	
end

if ~isempty(varargin) && ~ischar(varargin{1})
   % (LAT, LON, Z, LEVELS, ...) or (Z, R, LEVELS)
   levels = varargin{1};
   varargin(1) = [];
   if isscalar(levels) && levels > 0 && ~isequal(lower(function_name),'contourfm')
      % Compute the levels based on 1.3 (contourc) style
      levelsp1 = levels + 1;
      interval = (abs(min(Z(:)))+abs(max(Z(:))))/levelsp1;
      k=(1:levelsp1)*interval+min(Z(:));
      if numel(k) > 1
        levels = k(1:end-1);
      end
   end
end

% Set and check the MATLAB graphics property/value pairs.
lineSpec = [];
props = varargin;
if length(props) > 0
   if rem(length(props),2)
     lineSpec = props{1};
     props(1) = [];
   end
   params = props(1:2:end);
   for i=1:length(params)
     if ~ischar(params{i})
       eid = sprintf('%s:%s:invalidPropString', getcomp, function_name);
       msg = sprintf('The paramater/value pairs must be a string followed by value.');
       error(eid, '%s',msg)
     end
   end
end

% Check dimension of inputs.
if any([ndims(lat) ndims(lon) ndims(Z)] > 2)
   msg = 'Lat, Lon, and Z must be two-dimensional.';
   eid = sprintf('%s:%s:invalidDimension', getcomp, function_name);
   error(eid,'%s',msg);   
end

% Check Latitude.
if length(lat) == 2 && length(lat) ~= size(Z,1)    
   lat = linspace(min(lat),max(lat),size(Z,1));

elseif min(size(lat))==1 && length(lat) ~= size(Z,1)
   msg = 'Length of Lat must match number of rows in Z.';
   eid = sprintf('%s:%s:invalidLatLength', getcomp, function_name);
   error(eid,'%s',msg);   

elseif min(size(lat)) ~= 1 && ~isequal(size(lat),size(Z))
   msg = 'Lat and Z must be the same size.';
   eid = sprintf('%s:%s:invalidLatSize', getcomp, function_name);
   error(eid,'%s',msg);   
end

% Check Longitude.
if length(lon) == 2 && length(lon) ~= size(Z,2)    
   lon = linspace(min(lon),max(lon),size(Z,2));

elseif min(size(lon))==1 && length(lon) ~= size(Z,2)
   msg = 'Length of Lon must match number of columns in Z.';
   eid = sprintf('%s:%s:invalidLonLength', getcomp, function_name);
   error(eid,'%s',msg);   

elseif min(size(lon))~=1 && ~isequal(size(lon),size(Z))
   msg = 'Lon and Z inputs must be the same size';
   eid = sprintf('%s:%s:invalidLonSize', getcomp, function_name);
   error(eid,'%s',msg);   
end
