docs: def detect_plates(step, vrms_surface, fids, time):
      """detect plates using derivative of horizontal velocity"""
      vphi = step.fields['v2'][0, :, :, 0]
      ph_coord = step.geom.p_coord
  
      if step.sdat.par['boundaries']['air_layer']:
          dsa = step.sdat.par['boundaries']['air_thickness']
          # we are a bit below the surface; should check if you are in the
          # thermal boundary layer
          indsurf = np.argmin(abs((1 - dsa) - step.geom.r_coord)) - 4
      else:
          indsurf = -1
  
      vph2 = 0.5 * (vphi + np.roll(vphi, 1, 0))  # interpolate to the same phi
      # velocity derivation
      dvph2 = (np.diff(vph2[:, indsurf]) / (ph_coord[0] * 2.))
  
      io_surface(step.isnap, time, fids[6], dvph2)
      io_surface(step.isnap, time, fids[7], vph2[:-1, indsurf])
  
      # prepare stuff to find trenches and ridges
      myorder_trench = 15 if step.sdat.par['boundaries']['air_layer'] else 10
      myorder_ridge = 20  # threshold
  
      # finding trenches
      pom2 = np.copy(dvph2)
      if step.sdat.par['boundaries']['air_layer']:
          maskbigdvel = -30 * vrms_surface
      else:
          maskbigdvel = -10 * vrms_surface
      pom2[pom2 > maskbigdvel] = maskbigdvel
      argless_dv = argrelextrema(
          pom2, np.less, order=myorder_trench, mode='wrap')[0]
      trench = ph_coord[argless_dv]
      velocity_trench = vph2[argless_dv, indsurf]
      dv_trench = dvph2[argless_dv]
  
      # finding ridges
      pom2 = np.copy(dvph2)
      masksmalldvel = np.amax(dvph2) * 0.2
      pom2[pom2 < masksmalldvel] = masksmalldvel
      arggreat_dv = argrelextrema(
          pom2, np.greater, order=myorder_ridge, mode='wrap')[0]
      ridge = ph_coord[arggreat_dv]
  
      # elimination of ridges that are too close to trench
      argdel = []
      if trench.any() and ridge.any():
          for i, ridge_i in enumerate(ridge):
              mdistance = np.amin(abs(trench - ridge_i))
              if mdistance < 0.016:
                  argdel.append(i)
          if argdel:
              print('deleting from ridge', trench, ridge[argdel])
              ridge = np.delete(ridge, np.array(argdel))
              arggreat_dv = np.delete(arggreat_dv, np.array(argdel))
  
      dv_ridge = dvph2[arggreat_dv]
      if 'age' in conf.plates:
          agefld = step.fields['age'][0, :, :, 0]
          age_surface = np.ma.masked_where(agefld[:, indsurf] < 0.00001,
                                           agefld[:, indsurf])
          age_surface_dim = age_surface * vrms_surface *\
              conf.scaling.ttransit / conf.scaling.yearins / 1.e6
          agetrench = age_surface_dim[argless_dv]  # age at the trench
      else:
          agetrench = np.zeros(len(argless_dv))
  
      # writing the output into a file, all time steps are in one file
      for itrench in np.arange(len(trench)):
          fids[0].write("%7.0f %11.7f %10.6f %9.2f %9.2f \n" % (
              step.isnap,
              step.geom.ti_ad,
              trench[itrench],
              velocity_trench[itrench],
              agetrench[itrench]
          ))
  
      return trench, ridge, agetrench, dv_trench, dv_ridge