import openpyxl
import numpy as np
import matplotlib.pyplot as plt


def excel_row(filename, range):
    wb = openpyxl.load_workbook(filename)
    sheet = wb[wb.sheetnames[0]]
    return [cell.value for cell in sheet[range][0]]


led_sp = np.float32(excel_row("Spectrum.xlsx", "B2:AF2"))
# print("led=", led_sp)
cmf_x_sp = np.float32(excel_row("Spectrum.xlsx", "B3:AF3"))
cmf_y_sp = np.float32(excel_row("Spectrum.xlsx", "B4:AF4"))
cmf_z_sp = np.float32(excel_row("Spectrum.xlsx", "B5:AF5"))
sum_y = led_sp.dot(cmf_y_sp)
k = 100.0 / sum_y
# tri_x = (led_sp).dot(cmf_x_sp) * k
# tri_y = (led_sp).dot(cmf_y_sp) * k
# tri_z = (led_sp).dot(cmf_z_sp) * k
# print(tri_x, tri_y, tri_z)

for patch_no in range(1, 25):
    row = patch_no + 1
    tcc_sp = np.float32(excel_row("handy_tcc.xlsx", f"F{row}:AJ{row}")) / 100
    # print(f"tcc{patch_no}=", tcc_sp)

    tri_x = (led_sp * tcc_sp).dot(cmf_x_sp) * k
    tri_y = (led_sp * tcc_sp).dot(cmf_y_sp) * k
    tri_z = (led_sp * tcc_sp).dot(cmf_z_sp) * k
    print(patch_no, tri_x, tri_y, tri_z, sep="\t")

# wavelen = np.linspace(400, 700, 31)
# plt.plot(wavelen, led_sp, c="k")
# plt.plot(wavelen, tcc_sp, c="r")
# plt.show()
