code: def ma_fitplane(bma, 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_f, y_f = get_xy_ma(bma_f, gt, origmask=origmask)
      #Regardless of desired output (origmask True or False), for fit, need to limit to valid pixels only 
      bma_f_mask = np.ma.getmaskarray(bma_f)
      #Create xyz stack, needed for SVD
      xyz = np.vstack((np.ma.array(x_f, mask=bma_f_mask).compressed(), \
              np.ma.array(y_f, mask=bma_f_mask).compressed(), bma_f.compressed())).T
      #coeff = fitPlaneSVD(xyz)
      coeff = fitPlaneLSQ(xyz)
      print(coeff)
      vals = coeff[0]*x_f + coeff[1]*y_f + coeff[2]
      resid = bma_f - vals
      return vals, resid, coeff
docs: Fit a plane to values in input array