import re


def AtConc_to_ZWeightPer(row):
    """Function to convert spent fuel inventory from nuclidewise atom concentration
    to elementwise weight percentage. 
    The function expects a pandas row from the MVA dataset.
    1, calculates the mass per cm3 for each isotope
    2, changes the ZAID into elements and sums the mass of each element
    3, returns the normalized mass (ie w%) for each element
    
    Parameters
    ----------
    row : pandas dataframe
        Row in dataframe describing the fuel inventory
    
    Returns
    -------
    masspervolume : dict
        Dictionary which stores the mass percentage for each element (which are the keys).
    """
    
    #precondition row into dictionary
    inventory = row.to_dict()
    inventory.pop('Unnamed: 0') #note this is highly specific
    inventory.pop('BU')
    inventory.pop('CT')
    inventory.pop('IE')
    inventory.pop('fuelType')
    inventory.pop('TOT_SF')
    inventory.pop('TOT_GSRC')
    inventory.pop('TOT_A')
    inventory.pop('TOT_H')
    
    NA = 6.022140857E23
    masspervolume = {}
    
    
    for iso in inventory:
        isoText = re.findall('\D+', iso)
        isoNum =  re.findall('\d+', iso)
        Z=isoText[0]
        A=float(isoNum[0])
            
        massconci = A*((inventory[iso]*1e24)/NA)  #this gives the mass of that isotope in g/cm3
        
        if Z in masspervolume:
            masspervolume[Z]=masspervolume[Z]+massconci
        else:
            masspervolume[Z]=massconci
            
        
    #getting weight%
    summass=sum(masspervolume.values())
    for element in masspervolume:
        masspervolume[element]=masspervolume[element]/summass
    return masspervolume