def extract_matching_fields(fields, subset_definitions):
    'Removes fields that are not in the subset definition. Returns a copy without modifying the input fields dict.'
    retained_fields = {x: fields[x].copy() for x in subset_definitions}
    for (key, val) in subset_definitions.items():
        retained_fields[key]['field_details'] = fields[key]['field_details'].copy()
        for option in val:
            if (option != 'fields'):
                if ('intermediate' in retained_fields[key]['field_details']):
                    retained_fields[key]['field_details']['intermediate'] = False
                    retained_fields[key]['field_details'].setdefault('description', 'Intermediate field included by adding option with subset')
                    retained_fields[key]['field_details']['level'] = 'custom'
                    cleaner.field_cleanup(retained_fields[key])
                retained_fields[key]['field_details'][option] = val[option]
        if ('fields' in fields[key]):
            if ('fields' not in val):
                raise ValueError("'fields' key expected, not found in subset for {}".format(key))
            elif isinstance(val['fields'], dict):
                retained_fields[key]['fields'] = extract_matching_fields(fields[key]['fields'], val['fields'])
            elif (val['fields'] != '*'):
                raise ValueError("Unexpected value '{}' found in 'fields' key".format(val['fields']))
        elif ('fields' in val):
            raise ValueError("'fields' key not expected, found in subset for {}".format(key))
    return retained_fields
Removes fields that are not in the subset definition. Returns a copy without modifying the input fields dict.
def extract_matching_fields(fields, subset_definitions):
    
    retained_fields = {x: fields[x].copy() for x in subset_definitions}
    for (key, val) in subset_definitions.items():
        retained_fields[key]['field_details'] = fields[key]['field_details'].copy()
        for option in val:
            if (option != 'fields'):
                if ('intermediate' in retained_fields[key]['field_details']):
                    retained_fields[key]['field_details']['intermediate'] = False
                    retained_fields[key]['field_details'].setdefault('description', 'Intermediate field included by adding option with subset')
                    retained_fields[key]['field_details']['level'] = 'custom'
                    cleaner.field_cleanup(retained_fields[key])
                retained_fields[key]['field_details'][option] = val[option]
        if ('fields' in fields[key]):
            if ('fields' not in val):
                raise ValueError("'fields' key expected, not found in subset for {}".format(key))
            elif isinstance(val['fields'], dict):
                retained_fields[key]['fields'] = extract_matching_fields(fields[key]['fields'], val['fields'])
            elif (val['fields'] != '*'):
                raise ValueError("Unexpected value '{}' found in 'fields' key".format(val['fields']))
        elif ('fields' in val):
            raise ValueError("'fields' key not expected, found in subset for {}".format(key))
    return retained_fields