docs: def extractbpflags(calpath, deststream):
      """Make a flags file out of a bandpass calibration table
  
      calpath (str)
        The path to the bandpass calibration table
      deststream (stream-like object, e.g. an opened file)
        Where to write the flags data
  
      Below is documentation written for the command-line interface to this
      functionality:
  
      """
      tb = util.tools.table()
      tb.open(b(os.path.join(calpath, 'ANTENNA')))
      antnames = tb.getcol(b'NAME')
      tb.close()
  
      tb.open(b(calpath))
      try:
          t = tb.getkeyword(b'VisCal')
      except RuntimeError:
          raise PKError('no "VisCal" keyword in %s; it doesn\'t seem to be a '
                         'bandpass calibration table', calpath)
  
      if t != 'B Jones':
          raise PKError('table %s doesn\'t seem to be a bandpass calibration '
                         'table; its type is "%s"', calpath, t)
  
      def emit(antidx, spwidx, chanstart, chanend):
          # Channel ranges are inclusive, unlike Python syntax.
          print("antenna='%s&*' spw='%d:%d~%d' reason='BANDPASS_FLAGGED'" % \
                (antnames[antidx], spwidx, chanstart, chanend), file=deststream)
  
      for row in range(tb.nrows()):
          ant = tb.getcell(b'ANTENNA1', row)
          spw = tb.getcell(b'SPECTRAL_WINDOW_ID', row)
          flag = tb.getcell(b'FLAG', row)
  
          # This is the logical 'or' of the two polarizations: i.e., anything that
          # is flagged in either poln is flagged in this.
          sqflag = ~((~flag).prod(axis=0, dtype=np.bool))
  
          runstart = None
  
          for i in range(sqflag.size):
              if sqflag[i]:
                  # This channel is flagged. Start a run if not already in one.
                  if runstart is None:
                      runstart = i
              elif runstart is not None:
                  # The current run just ended.
                  emit(ant, spw, runstart, i - 1)
                  runstart = None
  
          if runstart is not None:
              emit(ant, spw, runstart, i)
  
      tb.close()