code: def ma_fitpoly(bma, order=1, gt=None, perc=(2,98), origmask=True):
      
      if gt is None:
          gt = [0, 1, 0, 0, 0, -1] 
      #Filter, can be useful to remove outliers
      if perc is not None:
          from pygeotools.lib import filtlib
          bma_f = filtlib.perc_fltr(bma, perc)
      else:
          bma_f = bma
      #Get indices
      x, y = get_xy_ma(bma_f, gt, origmask=origmask)
      #Fit only where we have valid data
      bma_mask = np.ma.getmaskarray(bma)
      coeff = polyfit2d(x[~bma_mask].data, y[~bma_mask].data, bma[~bma_mask].data, order=order) 
      #For 1D, these are: c, y, x, xy
      print(coeff)
      #Compute values for all x and y, unless origmask=True
      vals = polyval2d(x, y, coeff) 
      resid = bma - vals
      return vals, resid, coeff
docs: Fit a plane to values in input array