def field_direction_pitch_to_cell(field, direction, pitch):
    '''Field number, left or right direction of segmentation and
    harmonic center pitch as input. Segment field cell as output.
    '''

    assert isinstance(pitch, (int, float))

    segmented_field = partition_to_avoid_octave_adjacencies(field, direction)
    center_values = segmented_field_to_center_values(segmented_field)
    #print segmented_field
    #print center_values
    #print ''

    candidate_cell = segmented_field[0]
    candidate_distance = abs(center_values[0] - pitch)

    #print 'candidate_cell now %s' % candidate_cell
    cells = segmented_field
    for cell, center_value in zip(cells, center_values):
        distance = abs(center_value - pitch)
        #print cell, distance
        if distance < candidate_distance:
            candidate_cell = cell
            candidate_distance = distance
            #print 'candidate_cell now %s' % candidate_cell

    candidate_cell = tuple(candidate_cell)
    return candidate_cell
def field_id_to_segmented_field(field_id):

    letter = field_id[0]
    if letter == 'L':
        direction = 'left'
    elif letter == 'R':
        direction = 'right'
    else:
        raise ValueError

    field_number = int(field_id[1])
    field = field_number_to_field(field_number)

    segmented_field = partition_to_avoid_octave_adjacencies(field, direction)

    return segmented_field
def make_prominence_between(start_field, start_direction, start_pitch,
   stop_field, stop_direction, stop_pitch, count):
   '''Find cell in start_segmented_field closest to start_harmonic_pitch.

    Call this start_cell.

    Find cell in stop_segmented_field closest to stop_harmonic_pitch.

    Call this stop_cell.

    Make prominence between start_cell and stop_cell.

    Return prominence as list of (field, index, cell) triples.

    So [('R1', 2, (3, 5, 11, 13, 15.5, 17.5, 19, 20)),
        ('R1', 3, (26, 32, 33, 34)),
        ...]

    Returned list of tuples will always have length two or greater.
    '''

   # check input
   assert isinstance(count, int)
   assert 0 <= count

   # empty list on zero count
   if count == 0:
      return []

   # process start field
   segmented_start_field = partition_to_avoid_octave_adjacencies(
      start_field, start_direction)

   start_cell = field_direction_pitch_to_cell(
      start_field, start_direction, start_pitch)

   start_cell_index = segmented_start_field.index(start_cell)

   start_field_number = field_to_field_number(start_field)

   start_field_letter = _direction_to_letter(start_direction)

   start_id = '%s%s' % (start_field_letter, start_field_number)

   # process stop field
   segmented_stop_field = partition_to_avoid_octave_adjacencies(
      stop_field, stop_direction)

   stop_cell = field_direction_pitch_to_cell(
      stop_field, stop_direction, stop_pitch)

   stop_cell_index = segmented_stop_field.index(stop_cell)

   stop_field_number = field_to_field_number(stop_field)

   stop_field_letter = _direction_to_letter(stop_direction)

   stop_id = '%s%s' % (stop_field_letter, stop_field_number)

   # halve start field
   start_indices = range(len(segmented_start_field))
   low_start_cells = segmented_start_field[:start_cell_index]
   low_start_indices = start_indices[:start_cell_index]
   high_start_cells = segmented_start_field[start_cell_index:]
   high_start_indices = start_indices[start_cell_index:]
   pairs = zip(low_start_indices, low_start_cells)
   low_start_triples = [(start_id, index, cell) for index, cell in pairs]
   pairs = zip(high_start_indices, high_start_cells)
   high_start_triples = [(start_id, index, cell) for index, cell in pairs]

   # halve stop field
   stop_indices = range(len(segmented_stop_field))
   low_stop_cells = segmented_stop_field[:stop_cell_index+1]
   low_stop_indices = stop_indices[:stop_cell_index+1]
   high_stop_cells = segmented_stop_field[stop_cell_index+1:]
   high_stop_indices = stop_indices[stop_cell_index+1:]
   pairs = zip(low_stop_indices, low_stop_cells)
   low_stop_triples = [(stop_id, index, cell) for index, cell in pairs]
   pairs = zip(high_stop_indices, high_stop_cells)
   high_stop_triples = [(stop_id, index, cell) for index, cell in pairs]

   # build period
   begin_period = high_start_triples + low_stop_triples
   end_period = high_stop_triples + low_start_triples

   # build prominence
   prominence = []

   # count == 1
   prominence.extend(begin_period)

   # 1 < count
   for period in range(count - 1):
      prominence.extend(end_period)
      prominence.extend(begin_period)

   # return prominence
   return prominence