import numpy as np
import pandas as pd

df = pd.read_csv('result/res-P.csv')

mass = pd.to_numeric(df.iloc[0, 1:38])
zoom = pd.to_numeric(df.iloc[2:, 39])
omegaT = pd.to_numeric(df.iloc[1, 1:38])
omegaR = pd.to_numeric(df.iloc[2:, 38])

eDep = {}
for k in range(2, 32):
    name = df.iloc[k, 0]
    data = pd.to_numeric(df.iloc[k, 1:])

    eDep[name] = 0
    for i in range(37):
        eDep[name] += zoom[k] * data[i]

aDose = {}
for k in range(2, 32):
    name = df.iloc[k, 0]
    data = pd.to_numeric(df.iloc[k, 1:])

    aDose[name] = 0
    for i in range(37):
        aDose[name] += 30 * 86400 * zoom[k] * data[i] * 1.6 * 1e-10 / mass[i]

eqDose = {}
for k in range(2, 32):
    name = df.iloc[k, 0]
    data = pd.to_numeric(df.iloc[k, 1:])

    eqDose[name] = 0
    for i in range(37):
        eqDose[name] += 30 * 86400 * omegaR[k] * zoom[k] * data[i] * 1.6 * 1e-10 / mass[i]

efDose = {}
for k in range(2, 32):
    name = df.iloc[k, 0]
    data = pd.to_numeric(df.iloc[k, 1:])

    efDose[name] = 0
    for i in range(37):
        efDose[name] += 30 * 86400 * omegaR[k] * zoom[k] * omegaT[i] * data[i] * 1.6 * 1e-10 / mass[i]

res = pd.DataFrame({
    'energy deposition': pd.Series(eDep),
    'absorbed dose': pd.Series(aDose),
    'equivalent dose': pd.Series(eqDose),
    'effective dose': pd.Series(efDose)
})
res.index.name = 'Particle'
res.loc['Sum'] = res.apply(lambda x: x.sum())

res.to_csv('result/anl-P.csv')

# -----------------------------------------------

df = pd.read_csv('result/res-O.csv')

mass = pd.to_numeric(df.iloc[2:, 31])
zoom = pd.to_numeric(df.iloc[0, 1:31])
omegaT = pd.to_numeric(df.iloc[2:, 32])
omegaR = pd.to_numeric(df.iloc[1, 1:31])

eDep = {}
for k in range(2, 39):
    name = df.iloc[k, 0]
    data = pd.to_numeric(df.iloc[k, 1:])

    eDep[name] = 0
    for i in range(30):
        eDep[name] += zoom[i] * data[i]

aDose = {}
for k in range(2, 39):
    name = df.iloc[k, 0]
    data = pd.to_numeric(df.iloc[k, 1:])

    aDose[name] = 0
    for i in range(30):
        aDose[name] += 30 * 86400 * zoom[i] * data[i] * 1.6 * 1e-10 / mass[k]

eqDose = {}
for k in range(2, 39):
    name = df.iloc[k, 0]
    data = pd.to_numeric(df.iloc[k, 1:])

    eqDose[name] = 0
    for i in range(30):
        eqDose[name] += 30 * 86400 * omegaR[i] * zoom[i] * data[i] * 1.6 * 1e-10 / mass[k]

efDose = {}
for k in range(2, 39):
    name = df.iloc[k, 0]
    data = pd.to_numeric(df.iloc[k, 1:])

    efDose[name] = 0
    for i in range(30):
        efDose[name] += 30 * 86400 * omegaR[i] * zoom[i] * omegaT[k] * data[i] * 1.6 * 1e-10 / mass[k]

res = pd.DataFrame({
    'energy deposition': pd.Series(eDep),
    'absorbed dose': pd.Series(aDose),
    'equivalent dose': pd.Series(eqDose),
    'effective dose': pd.Series(efDose)
})
res.index.name = 'Organ'
res.loc['Sum'] = res.apply(lambda x: x.sum())

res.to_csv('result/anl-O.csv')

# -----------------------------------------------

df = pd.read_csv('result/res-O.csv')

organ = df.iloc[2:, 0].to_numpy()
index = df.columns.to_numpy()[1:-2]
mass = pd.to_numeric(df.iloc[2:, 31])
zoom = pd.to_numeric(df.iloc[0, 1:31])
omegaT = pd.to_numeric(df.iloc[2:, 32])
omegaR = pd.to_numeric(df.iloc[1, 1:31])

eDep = {}
for name in index:
    eDep[name] = np.zeros(shape=(37, ))

for k in range(2, 39):
    data = pd.to_numeric(df.iloc[k, 1:])

    for i in range(30):
        eDep[index[i]][k - 2] = zoom[i] * data[i]

res = pd.DataFrame(eDep, index=organ)
res['Sum'] = res.apply(lambda x: x.sum(), axis=1)
res.loc['Sum'] = res.apply(lambda x: x.sum())

res.to_csv('result/anl-eDep.csv')

aDose = {}
for name in index:
    aDose[name] = np.zeros(shape=(37, ))

for k in range(2, 39):
    data = pd.to_numeric(df.iloc[k, 1:])

    for i in range(30):
        aDose[index[i]][k - 2] = 30 * 86400 * zoom[i] * data[i] * 1.6 * 1e-10 / mass[k]

res = pd.DataFrame(aDose, index=organ)
res['Sum'] = res.apply(lambda x: x.sum(), axis=1)
res.loc['Sum'] = res.apply(lambda x: x.sum())

res.to_csv('result/anl-aDose.csv')

eqDose = {}
for name in index:
    eqDose[name] = np.zeros(shape=(37, ))

for k in range(2, 39):
    data = pd.to_numeric(df.iloc[k, 1:])

    for i in range(30):
        eqDose[index[i]][k - 2] = 30 * 86400 * omegaR[i] * zoom[i] * data[i] * 1.6 * 1e-10 / mass[k]

res = pd.DataFrame(eqDose, index=organ)
res['Sum'] = res.apply(lambda x: x.sum(), axis=1)
res.loc['Sum'] = res.apply(lambda x: x.sum())

res.to_csv('result/anl-eqDose.csv')

efDose = {}
for name in index:
    efDose[name] = np.zeros(shape=(37, ))

for k in range(2, 39):
    data = pd.to_numeric(df.iloc[k, 1:])

    for i in range(30):
        efDose[index[i]][k - 2] = 30 * 86400 * omegaR[i] * zoom[i] * omegaT[k] * data[i] * 1.6 * 1e-10 / mass[k]

res = pd.DataFrame(efDose, index=organ)
res['Sum'] = res.apply(lambda x: x.sum(), axis=1)
res.loc['Sum'] = res.apply(lambda x: x.sum())

res.to_csv('result/anl-efDose.csv')
