def add(self, x, y, z, w):
    " An incremental gridding function\n\n        Arguments:\n        x:   x-coordinates\n        y:   y-coordiantes\n        z:   z-scalar values to grid\n        w:   w-weight applied to each point (size of x or 1 for no weighting)\n             When 'type' = Nlowerthan or Ngreaterthan, w is the threshold value\n             When 'type' = distance weighted mean, distance = R^w\n        cs:  grid cell size\n        cinf: cell influence\n        type: type of grid (see below)\n\n        Output:\n        g.xx: vector of grid cell x coordinates.\n        g.yy: vector of grid cell y coordiantes.\n        g.zz: 2D matrix of grided values times their weights.\n        g.nn: 2D matrix containing the number of points in each grid cell.\n        g.ww: sum of weights of items in the grid cell\n\n        %\n        % Grid types:\n        % mean:\n        %   Average of the values. When w != 1, the mean is calculated by\n        %   multipying each value in the cell by its weight divided by the sum\n        %   of the weights in that cell.\n        %\n        % median:\n        %   Calculates the median value for each grid cell.\n        %\n        % mode:\n        %   Calculates the mode of the values for each grid cell.\n        %\n        % shoalest:\n        %   Calculates the minimum value for each grid cell.\n        %\n        % deepest:\n        %   Calculates the maximum value for each grid cell.\n        %\n        % stddev:\n        %   Calculates the standard deviation of the values in each grid cell.\n        %\n        % stderr:\n        %   Calculates the standard error of the values in each grid cell\n        %   (stddev/N, where stddev is the standard deviation and N is the number\n        %   of points falling in the cell)\n        %\n        % dwm:\n        %   Calculates the distance weighted mean where each value in the cell is\n        %   inversely weighted by the square if it's distance to the cell node.\n        %\n        % Nlowerthan:\n        %   Calculates the number of points in the grid cell lower than some value,\n        %   w.\n        %\n        % Ngreaterthan:\n        %   Calculates the number of points greater than some value w.\n        %\n        % To Do:\n        % - Rewrite mean function as a matrix operation to simplify the propagation\n        % of uncertainty calcualtion. Actually this might be make more general such\n        % that your pass a list of values, their uncertainty and weighting factors\n        % and get back a mean and propagated uncertainty. This would allow\n        % relatively simple incorporation of things like range weighting, footprint\n        % weighting, gaussian weighting, etc.\n        % - Add uncertainty to z input and propagate these through the\n        % calculations.\n        % - Add uncertainty to x and y inputs and propagate these through the\n        % calculations (more difficult)\n        % Rewrite a C mex function.\n        %\n        % Val Schmidt\n        % CCOM/JHC\n        % 2018, 2019\n        "
    if (np.isscalar(x) or np.isscalar(y) or np.isscalar(z)):
        print('X, Y, or Z is scalar - must be numpy array.')
        sys.exit()
    self._x = x.ravel()
    self._y = y.ravel()
    self._z = z.ravel()
    if (not np.isscalar(w)):
        self._w = w.ravel()
    else:
        self._w = np.array(w)
    if (self._w.size != 1):
        if sum((self._w == 0)):
            print('Found zero weights. Weights cannot be zero.')
            print('Setting to 1e-20.')
            self._w[(self._w == 0)] = 1e-20
    if (self.zw is None):
        self.create_new_grid()
    else:
        self.expand_grid()
    grows = self.yy.size
    gcols = self.xx.size
    doindices = 0
    self.sort_data_kdtree()
    if (self.type == 'dwm'):
        print('Not yet supported.')
    if (self.type == 'mean'):
        self.mean_wholegrid()
    if (self.type == 'median'):
        self.median_wholegrid()
An incremental gridding function

Arguments:
x:   x-coordinates
y:   y-coordiantes
z:   z-scalar values to grid
w:   w-weight applied to each point (size of x or 1 for no weighting)
     When 'type' = Nlowerthan or Ngreaterthan, w is the threshold value
     When 'type' = distance weighted mean, distance = R^w
cs:  grid cell size
cinf: cell influence
type: type of grid (see below)

Output:
g.xx: vector of grid cell x coordinates.
g.yy: vector of grid cell y coordiantes.
g.zz: 2D matrix of grided values times their weights.
g.nn: 2D matrix containing the number of points in each grid cell.
g.ww: sum of weights of items in the grid cell

%
% Grid types:
% mean:
%   Average of the values. When w != 1, the mean is calculated by
%   multipying each value in the cell by its weight divided by the sum
%   of the weights in that cell.
%
% median:
%   Calculates the median value for each grid cell.
%
% mode:
%   Calculates the mode of the values for each grid cell.
%
% shoalest:
%   Calculates the minimum value for each grid cell.
%
% deepest:
%   Calculates the maximum value for each grid cell.
%
% stddev:
%   Calculates the standard deviation of the values in each grid cell.
%
% stderr:
%   Calculates the standard error of the values in each grid cell
%   (stddev/N, where stddev is the standard deviation and N is the number
%   of points falling in the cell)
%
% dwm:
%   Calculates the distance weighted mean where each value in the cell is
%   inversely weighted by the square if it's distance to the cell node.
%
% Nlowerthan:
%   Calculates the number of points in the grid cell lower than some value,
%   w.
%
% Ngreaterthan:
%   Calculates the number of points greater than some value w.
%
% To Do:
% - Rewrite mean function as a matrix operation to simplify the propagation
% of uncertainty calcualtion. Actually this might be make more general such
% that your pass a list of values, their uncertainty and weighting factors
% and get back a mean and propagated uncertainty. This would allow
% relatively simple incorporation of things like range weighting, footprint
% weighting, gaussian weighting, etc.
% - Add uncertainty to z input and propagate these through the
% calculations.
% - Add uncertainty to x and y inputs and propagate these through the
% calculations (more difficult)
% Rewrite a C mex function.
%
% Val Schmidt
% CCOM/JHC
% 2018, 2019
def add(self, x, y, z, w):
    " An incremental gridding function\n\n        Arguments:\n        x:   x-coordinates\n        y:   y-coordiantes\n        z:   z-scalar values to grid\n        w:   w-weight applied to each point (size of x or 1 for no weighting)\n             When 'type' = Nlowerthan or Ngreaterthan, w is the threshold value\n             When 'type' = distance weighted mean, distance = R^w\n        cs:  grid cell size\n        cinf: cell influence\n        type: type of grid (see below)\n\n        Output:\n        g.xx: vector of grid cell x coordinates.\n        g.yy: vector of grid cell y coordiantes.\n        g.zz: 2D matrix of grided values times their weights.\n        g.nn: 2D matrix containing the number of points in each grid cell.\n        g.ww: sum of weights of items in the grid cell\n\n        %\n        % Grid types:\n        % mean:\n        %   Average of the values. When w != 1, the mean is calculated by\n        %   multipying each value in the cell by its weight divided by the sum\n        %   of the weights in that cell.\n        %\n        % median:\n        %   Calculates the median value for each grid cell.\n        %\n        % mode:\n        %   Calculates the mode of the values for each grid cell.\n        %\n        % shoalest:\n        %   Calculates the minimum value for each grid cell.\n        %\n        % deepest:\n        %   Calculates the maximum value for each grid cell.\n        %\n        % stddev:\n        %   Calculates the standard deviation of the values in each grid cell.\n        %\n        % stderr:\n        %   Calculates the standard error of the values in each grid cell\n        %   (stddev/N, where stddev is the standard deviation and N is the number\n        %   of points falling in the cell)\n        %\n        % dwm:\n        %   Calculates the distance weighted mean where each value in the cell is\n        %   inversely weighted by the square if it's distance to the cell node.\n        %\n        % Nlowerthan:\n        %   Calculates the number of points in the grid cell lower than some value,\n        %   w.\n        %\n        % Ngreaterthan:\n        %   Calculates the number of points greater than some value w.\n        %\n        % To Do:\n        % - Rewrite mean function as a matrix operation to simplify the propagation\n        % of uncertainty calcualtion. Actually this might be make more general such\n        % that your pass a list of values, their uncertainty and weighting factors\n        % and get back a mean and propagated uncertainty. This would allow\n        % relatively simple incorporation of things like range weighting, footprint\n        % weighting, gaussian weighting, etc.\n        % - Add uncertainty to z input and propagate these through the\n        % calculations.\n        % - Add uncertainty to x and y inputs and propagate these through the\n        % calculations (more difficult)\n        % Rewrite a C mex function.\n        %\n        % Val Schmidt\n        % CCOM/JHC\n        % 2018, 2019\n        "
    if (np.isscalar(x) or np.isscalar(y) or np.isscalar(z)):
        print('X, Y, or Z is scalar - must be numpy array.')
        sys.exit()
    self._x = x.ravel()
    self._y = y.ravel()
    self._z = z.ravel()
    if (not np.isscalar(w)):
        self._w = w.ravel()
    else:
        self._w = np.array(w)
    if (self._w.size != 1):
        if sum((self._w == 0)):
            print('Found zero weights. Weights cannot be zero.')
            print('Setting to 1e-20.')
            self._w[(self._w == 0)] = 1e-20
    if (self.zw is None):
        self.create_new_grid()
    else:
        self.expand_grid()
    grows = self.yy.size
    gcols = self.xx.size
    doindices = 0
    self.sort_data_kdtree()
    if (self.type == 'dwm'):
        print('Not yet supported.')
    if (self.type == 'mean'):
        self.mean_wholegrid()
    if (self.type == 'median'):
        self.median_wholegrid()