code: def most_similar_catchments(self, subject_catchment, similarity_dist_function, records_limit=500,
                                  include_subject_catchment='auto'):
          if include_subject_catchment not in ['auto', 'force', 'exclude']:
              raise ValueError("Parameter `include_subject_catchment={}` invalid.".format(include_subject_catchment) +
                               "Must be one of `auto`, `force` or `exclude`.")
          query = (self.db_session.query(Catchment).
                   join(Catchment.descriptors).
                   join(Catchment.amax_records).
                   filter(Catchment.id != subject_catchment.id,
                          Catchment.is_suitable_for_pooling,
                          or_(Descriptors.urbext2000 < 0.03, Descriptors.urbext2000 == None),
                          AmaxRecord.flag == 0).
                   group_by(Catchment).
                   having(func.count(AmaxRecord.catchment_id) >= 10))
          catchments = query.all()
          if include_subject_catchment == 'force':
              if len(subject_catchment.amax_records) >= 10:
                  catchments.append(subject_catchment)
          elif include_subject_catchment == 'auto':
              if len(subject_catchment.amax_records) >= 10 and subject_catchment.is_suitable_for_pooling and \
                 (subject_catchment.descriptors.urbext2000 < 0.03 or subject_catchment.descriptors.urbext2000 is None):
                  catchments.append(subject_catchment)
          for catchment in catchments:
              catchment.similarity_dist = similarity_dist_function(subject_catchment, catchment)
          catchments.sort(key=attrgetter('similarity_dist'))
          amax_records_count = 0
          catchments_limited = []
          for catchment in catchments:
              catchments_limited.append(catchment)
              amax_records_count += catchment.record_length
              if amax_records_count >= records_limit:
                  break
          return catchments_limited