import re


def parseCrysolLog(logFileName):
    """Parse Crysol log file, obtain Chi2, Rg and eDens"""
    # will not parse crysol_summary.txt, but the .log file
    # created for each individual run

    chi2 = 9999
    Rg = -9999
    eDens = -9999

    position = -1
    counter = 0
    with open(logFileName, 'r') as rf:
        for line in rf:
            counter += 1
            if re.match("(.*)Fitting parameters(.*)", line):
                print("line number: " + repr(counter))
                position = counter + 2
            if counter == position:
                if line[66:73] != "*******":
                    chi2 = float(line[66:73])
            if re.match("(.*)Rg from the slope of net intensity(.*)", line):
                Rg = float(line[59:65])
            if re.match("(.*)Average electron density(.*)", line):
                eDens = float(line[59:66])
    rf.close()
    return {'chi2': chi2, 'Rg': Rg, 'eDens': eDens}