Skip to content

Commit

Permalink
Error system improvement, Zelario's exoplanet data
Browse files Browse the repository at this point in the history
- [beta] label fixed
- now target name is required parameter for calc.to_rgb
- replacing tabs with spaces in database.py
  • Loading branch information
Askaniy committed Apr 16, 2022
1 parent 715e934 commit cdcac76
Show file tree
Hide file tree
Showing 4 changed files with 2,430 additions and 2,302 deletions.
36 changes: 15 additions & 21 deletions Scripts/TCT.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def frame(num):
[sg.Checkbox("sRGB", size=(16, 1), key="T2_srgb"),
sg.Radio(tr.gui_interp[lang][2], "T2_interp", size=(12, 1), enable_events=True, key="T2_interp1")],
[sg.Checkbox(tr.gui_autoexp[lang], size=(16, 1), key="T2_autoexp"),
sg.Checkbox(tr.gui_autoalign[lang]+" [beta]", size=(16, 1), key="T2_autoalign")],
sg.Checkbox(tr.gui_autoalign[lang], size=(16, 1), key="T2_autoalign")],
[sg.Checkbox(tr.gui_single[lang], size=(22, 1), enable_events=True, key="T2_single")],
[sg.Input(size=(32, 1), disabled=True, disabled_readonly_background_color="#3A3A3A", key="T2_path"), sg.FileBrowse(button_text=tr.gui_browse[lang], size=(10, 1), disabled=True, key="T2_browse")],
[sg.Checkbox(tr.gui_filterset[lang], size=(26, 1), enable_events=True, key="T2_filterset")],
Expand Down Expand Up @@ -461,13 +461,14 @@ def frame(num):
elif event.startswith("T1"):

if event in T1_events and values["T1_list"] != []:
T1_name = values["T1_list"][0]
T1_nm = cmf.xyz_nm if values["T1_srgb"] else cmf.rgb_nm
for i in range(3):
if values["T1_br_mode"+str(i)]:
T1_mode = br_modes[i]

# Spectral data import and processing
T1_spectrum = db.objects[obj_list()[values["T1_list"][0]]]
T1_spectrum = db.objects[obj_list()[T1_name]]
T1_albedo = 0
if "albedo" not in T1_spectrum:
if T1_mode == "albedo":
Expand All @@ -486,7 +487,7 @@ def frame(num):
except Exception:
T1_phase = values["T1_slider"]
T1_rgb = calc.to_rgb(
T1_curve, mode=T1_mode,
T1_name, T1_curve, mode=T1_mode,
albedo = T1_spectrum["albedo"] or T1_albedo,
phase=T1_phase,
exp_bit=int(values["T1_bit_num"]),
Expand All @@ -495,18 +496,13 @@ def frame(num):
srgb=values["T1_srgb"]
)
T1_rgb_show = calc.to_rgb(
T1_curve, mode=T1_mode,
T1_name, T1_curve, mode=T1_mode,
albedo = T1_spectrum["albedo"] or T1_albedo,
phase=T1_phase,
gamma=values["T1_gamma"],
srgb=values["T1_srgb"],
html=True
)
if not np.array_equal(np.absolute(T1_rgb), T1_rgb):
T1_rgb_show = "#000000"
print("\n" + tr.error2[lang][0])
print(tr.error2[lang][1].format(values["T1_list"][0], *T1_rgb) + "\n")
#break

# Output
window["T1_graph"].TKCanvas.itemconfig(T1_preview, fill=T1_rgb_show)
Expand Down Expand Up @@ -557,7 +553,7 @@ def frame(num):

# Color calculation
T1_rgb = calc.to_rgb(
T1_curve, mode=T1_mode,
name_0, T1_curve, mode=T1_mode,
albedo = T1_spectrum["albedo"] or T1_albedo,
exp_bit=int(values["T1_bit_num"]),
gamma=values["T1_gamma"],
Expand Down Expand Up @@ -787,14 +783,15 @@ def frame(num):
for y in range(T2_h):
T2_spectrum = T2_data[:, y, x]
if np.sum(T2_spectrum) > 0:
T2_name = f'({x}; {y})'
T2_curve = calc.polator(input_data["nm"], list(T2_spectrum), T2_nm, fast=T2_fast)
T2_rgb = calc.to_rgb(T2_curve, mode="albedo", albedo=True, inp_bit=T2_input_bit, exp_bit=8, gamma=input_data["gamma"])
T2_rgb = calc.to_rgb(T2_name, T2_curve, mode="albedo", albedo=True, inp_bit=T2_input_bit, exp_bit=8, gamma=input_data["gamma"])
T2_draw.point((x, y), T2_rgb)
if x % 32 == 0 and y % 32 == 0:
T2_fig.add_trace(go.Scatter(
x = T2_nm,
y = T2_curve,
name = f'({x}; {y})',
name = T2_name,
line = dict(color="rgb"+str(T2_rgb), width=2)
))
T2_counter += 1
Expand Down Expand Up @@ -915,14 +912,10 @@ def frame(num):

# Color calculation
T3_rgb = calc.to_rgb(
T3_curve, mode=T3_mode,
name, T3_curve, mode=T3_mode,
albedo = spectrum["albedo"] or T3_albedo,
exp_bit=8, gamma=values["T3_gamma"], srgb=values["T3_srgb"]
)
if not np.array_equal(np.absolute(T3_rgb), T3_rgb):
print("\n" + tr.error2[lang][0])
print(tr.error2[lang][1].format(name, *T3_rgb) + "\n")
break

# Object drawing
center_x = 100 * (1 + T3_n%T3_num)
Expand Down Expand Up @@ -977,16 +970,16 @@ def frame(num):
T3_img = T3_img.crop((0, 0, T3_w, T3_h2+50 if T3_h2 > T3_min_limit else T3_min_limit+50))
T3_img.save(f'{values["T3_folder"]}/{T3_file_name}')
# T3_img.show()
print("Done, saved as", T3_file_name)
print("Done, saved as", T3_file_name, "\n")

# ------------ Events in the tab "Blackbody & Redshifts" ------------

elif event.startswith("T4"):

if event == "T4_maxtemp_num":
window["T4_slider1"].update(range=(0, int(values["T4_maxtemp_num"])))

else:

if event == "T4_irr":
window["T4_scale"].update(text_color=T4_text_colors[values["T4_irr"]])
window["T4_slider4"].update(disabled=not values["T4_irr"])
Expand All @@ -999,16 +992,17 @@ def frame(num):
T4_curve *= 10**(-values["T4_slider4"])
except np.core._exceptions.UFuncTypeError:
pass
T4_name = f'{values["T4_slider1"]} {values["T4_slider2"]} {values["T4_slider3"]}'
T4_rgb = calc.to_rgb(
T4_curve, mode=T4_mode,
T4_name, T4_curve, mode=T4_mode,
albedo=values["T4_irr"],
exp_bit=int(values["T4_bit_num"]),
gamma=values["T4_gamma"],
rnd=int(values["T4_rnd_num"]),
srgb=values["T4_srgb"]
)
T4_rgb_show = calc.to_rgb(
T4_curve, mode=T4_mode,
T4_name, T4_curve, mode=T4_mode,
albedo=values["T4_irr"],
gamma=values["T4_gamma"],
srgb=values["T4_srgb"],
Expand Down
64 changes: 35 additions & 29 deletions Scripts/calculations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import traceback
import numpy as np
from scipy.interpolate import Akima1DInterpolator
import cmf, database
Expand Down Expand Up @@ -205,36 +206,41 @@ def xyz_to_sRGB(xyz):
def to_bit(color, bit): return color * (2**bit - 1)
def to_html(color): return "#{:02x}{:02x}{:02x}".format(*rounder(to_bit(color, 8), 0))

def to_rgb(spectrum, mode="chromaticity", inp_bit=None, exp_bit=None, rnd=0, albedo=False, phase=0, gamma=False, srgb=False, html=False):
if inp_bit:
spectrum /= (2**inp_bit - 1)
if srgb:
xyz = np.sum(spectrum[:, np.newaxis] * cmf.xyz, axis=0)
rgb = xyz_to_sRGB(xyz)
rgb = rgb / rgb[1] * spectrum[38] # xyz cmf is not normalized, so result was overexposed; spectrum[38] is 550 nm
else:
rgb = np.sum(spectrum[:, np.newaxis] * cmf.rgb, axis=0)
if mode == "albedo 0.5":
if rgb[1] != 0:
rgb /= 2 * rgb[1]
elif mode == "albedo" and albedo:
def to_rgb(target, spectrum, mode="chromaticity", inp_bit=None, exp_bit=None, rnd=0, albedo=False, phase=0, gamma=False, srgb=False, html=False):
try:
if inp_bit:
spectrum /= (2**inp_bit - 1)
if srgb:
xyz = np.sum(spectrum[:, np.newaxis] * cmf.xyz, axis=0)
rgb = xyz_to_sRGB(xyz)
rgb = rgb / rgb[1] * spectrum[38] # xyz cmf is not normalized, so result was overexposed; spectrum[38] is 550 nm
else:
rgb = np.sum(spectrum[:, np.newaxis] * cmf.rgb, axis=0)
if mode == "albedo 0.5":
if rgb[1] != 0:
rgb /= 2 * rgb[1]
elif mode == "albedo" and albedo:
if html:
rgb = np.clip(rgb, 0, 1)
else: # "chromaticity" and when albedo == False
mx = np.max(rgb)
if mx != 0:
rgb /= mx
if phase != 0:
rgb *= lambert(phase)
if gamma:
rgb = gamma_correction(rgb)
if rgb.min() < 0:
print("NegativeColorValues:", target, rgb)
rgb = np.clip(rgb, 0, None)
if html:
rgb = np.clip(rgb, 0, 1)
else: # "chromaticity" and when albedo == False
mx = np.max(rgb)
if mx != 0:
rgb /= mx
if phase != 0:
rgb *= lambert(phase)
if gamma:
rgb = gamma_correction(rgb)
if rgb.min() < 0:
# print(f'Negative RGB values were clipped: {rgb}')
rgb = np.clip(rgb, 0, None)
if html:
return to_html(rgb)
else:
return tuple(rounder(rgb if not exp_bit else to_bit(rgb, exp_bit), rnd))
return to_html(rgb)
else:
return tuple(rounder(rgb if not exp_bit else to_bit(rgb, exp_bit), rnd))
except:
print("ColorCalcError:", target)
print(traceback.format_exc())
return "#000000" if html else (0, 0, 0)


# Pivot wavelengths and ZeroPoints of filter bandpasses
Expand Down
Loading

0 comments on commit cdcac76

Please sign in to comment.