import pandas as pd
import colour

# Step 1: Load the file and extract spectral data from columns 4 to 39
file_path = r'upload/criTT.txt'  # Adjust the path to the location of your file

# Read the tab-separated file using pandas
data = pd.read_csv(file_path, sep="_", header=None)

# Step 2: Extract spectral data from columns 4 to 39 (for 380nm to 730nm at 10nm intervals)
# The data you provided is in the columns from index 3 to 38 (0-based indexing in pandas)
spectral_values = data.iloc[0, 4:40].values
print("spectral_values[4]:", spectral_values)
# Define the wavelengths for 380nm to 730nm at 10nm intervals
wavelengths = list(range(380, 740, 10))

# Create a spectral distribution
spectral_data = dict(zip(wavelengths, spectral_values ))
spd = colour.SpectralDistribution(spectral_data)

cri_results = colour.quality.colour_rendering_index(spd)
#cri_results = colour.quality.colour_rendering_index(spd, additional_data=True)
# Print the structure of the results to understand what is returned

print("CRI Results:", cri_results)  # Inspect the output structure

output_lines = []
output_lines.append(f"General CRI (Ra): {cri_results}\n")

output_file = 'critest.txt'
with open(output_file, 'w') as file:
    file.writelines(output_lines)

print(f"CRI results have been saved to {output_file}")

tional_data = cri_results  # Assuming the rest is still valid