def convert(value, row):
    """AFED rules (In process rules_2 because needs astate value)
       
       1. IF ORI_invalid flag = False:
       
          a. Try to code AFED using ORI on crosswalk_ORI
          b. Try to code AFED using ORI[2:5] on crosswalk_ORI_POS_3-5
          c. Try to code AFED using ORI[7:9] on crosswalk_ORI_POS_8-9
          
       2. IF ORI_invalid flag = True:
          
          a. Try to code AFED using ARRNAME on crosswalk_ARRNAME
          b. Try to code AFED using ARRNAME on crosswalk_ARRNAME_EMBED
          
       3. IF AFED is not coded after step 2:
       
         a. If ASOURCE =2, code AFED=2.
         b. If ASOURCE not equal 2, code AFED=9.

        * All cases coded via 1b, 1c, 2b, 3a, or 3b should be reviewed 
          and ORIs added to crosswalk_1. Create a review table including 
          AORI, ARRNAME, and AFED for review and approval. Once approved, 
          add to crosswalk_1 and crosswalk_2, as appropriate.  
          Upgrade crosswalk_2 only if ORI is missing or not legitimate.
    """ 
    ori = key_as_str(row['aori'])
    arrname = key_as_str(row['arrname']) 
    c = 0
    fed = None
    
    #setup rule 1
    if not row['aori_invalid']:
        xwalk_list = [('ori', ori), ('ori_pos_3-5', ori[2:5]), ('ori_pos_8-9', ori[7:9])]  
    #setup rule 2
    else:
        xwalk_list =  [('arrname', arrname), ('arrname_embed', arrname)]
    
    #apply rule 1 or 2       
    for xwalk in xwalk_list:
        c += 1
        try:
            fed = crosswalk[xwalk[0]].convert(xwalk[1])
            break
        except ConvertError:
            continue
        #cache data for sentence segment 
    
    #rule 3
    if not fed:
        fed = 2 if row['asource'] == 2 else 9
 
    #write log if fed coded by anything other than rule 1a or 2a
    if c > 1:
        if row['aori'] == '999999999':
            log.write('afed_unknown', [row['id'], row['rapstatex'], row['astate'], row['aori'], row['arrname'], fed], 1)
        else:
            log.write('afed', [row['id'], row['rapstatex'], row['astate'], row['aori'], row['arrname'], fed], 1)
        
    return fed