import re


def _get_base_key(key):
  """Extracts the base key from the provided key.

  Earth Engine exports `TFRecords` containing each data variable with its
  corresponding variable name. In the case of time sequences, the name of the
  data variable is of the form `variable_1, variable_2, ..., variable_n`,
  where `variable` is the name of the variable, and n the number of elements
  in the time sequence. Extracting the base key ensures that each step of the
  time sequence goes through the same normalization steps.
  The base key obeys the following naming pattern: `([a-zA-Z]+)`
  For instance, for an input key `variable_1`, this function returns `variable`.
  For an input key `variable`, this function simply returns `variable`.

  Args:
    key: Input key.

  Returns:
    The corresponding base key.

  Raises:
    ValueError when `key` does not match the expected pattern.
  """
  match = re.fullmatch(r'([a-zA-Z]+)', key)
  if match:
    return match.group(1)
  raise ValueError(
      f'The provided key does not match the expected pattern: {key}')