Select instrument configuration based on OB

def select_configuration(self, obresult):
        """Select instrument configuration based on OB"""

        logger = logging.getLogger(__name__)
        logger.debug('calling default configuration selector')

        # get first possible image
        ref = obresult.get_sample_frame()
        extr = self.datamodel.extractor_map['fits']
        if ref:
            # get INSCONF configuration
            result = extr.extract('insconf', ref)
            if result:
                # found the keyword, try to match
                logger.debug('found insconf config uuid=%s', result)
                # Use insconf as uuid key
                if result in self.configurations:
                    return self.configurations[result]
                else:
                    # Additional check for conf.name
                    for conf in self.configurations.values():
                        if conf.name == result:
                            return conf
                    else:
                        raise KeyError('insconf {} does not match any config'.format(result))

            # If not, try to match by DATE
            date_obs = extr.extract('observation_date', ref)
            for key, conf in self.configurations.items():
                if key == 'default':
                    # skip default
                    continue
                if conf.date_end is not None:
                    upper_t = date_obs < conf.date_end
                else:
                    upper_t = True
                if upper_t and (date_obs >= conf.date_start):
                    logger.debug('found date match, config uuid=%s', key)
                    return conf
        else:
            logger.debug('no match, using default configuration')
            return self.configurations['default']