Skip to content

Commit cdcac76

Browse files
committed
Error system improvement, Zelario's exoplanet data
- [beta] label fixed - now target name is required parameter for calc.to_rgb - replacing tabs with spaces in database.py
1 parent 715e934 commit cdcac76

4 files changed

Lines changed: 2430 additions & 2302 deletions

File tree

Scripts/TCT.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def frame(num):
267267
[sg.Checkbox("sRGB", size=(16, 1), key="T2_srgb"),
268268
sg.Radio(tr.gui_interp[lang][2], "T2_interp", size=(12, 1), enable_events=True, key="T2_interp1")],
269269
[sg.Checkbox(tr.gui_autoexp[lang], size=(16, 1), key="T2_autoexp"),
270-
sg.Checkbox(tr.gui_autoalign[lang]+" [beta]", size=(16, 1), key="T2_autoalign")],
270+
sg.Checkbox(tr.gui_autoalign[lang], size=(16, 1), key="T2_autoalign")],
271271
[sg.Checkbox(tr.gui_single[lang], size=(22, 1), enable_events=True, key="T2_single")],
272272
[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")],
273273
[sg.Checkbox(tr.gui_filterset[lang], size=(26, 1), enable_events=True, key="T2_filterset")],
@@ -461,13 +461,14 @@ def frame(num):
461461
elif event.startswith("T1"):
462462

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

469470
# Spectral data import and processing
470-
T1_spectrum = db.objects[obj_list()[values["T1_list"][0]]]
471+
T1_spectrum = db.objects[obj_list()[T1_name]]
471472
T1_albedo = 0
472473
if "albedo" not in T1_spectrum:
473474
if T1_mode == "albedo":
@@ -486,7 +487,7 @@ def frame(num):
486487
except Exception:
487488
T1_phase = values["T1_slider"]
488489
T1_rgb = calc.to_rgb(
489-
T1_curve, mode=T1_mode,
490+
T1_name, T1_curve, mode=T1_mode,
490491
albedo = T1_spectrum["albedo"] or T1_albedo,
491492
phase=T1_phase,
492493
exp_bit=int(values["T1_bit_num"]),
@@ -495,18 +496,13 @@ def frame(num):
495496
srgb=values["T1_srgb"]
496497
)
497498
T1_rgb_show = calc.to_rgb(
498-
T1_curve, mode=T1_mode,
499+
T1_name, T1_curve, mode=T1_mode,
499500
albedo = T1_spectrum["albedo"] or T1_albedo,
500501
phase=T1_phase,
501502
gamma=values["T1_gamma"],
502503
srgb=values["T1_srgb"],
503504
html=True
504505
)
505-
if not np.array_equal(np.absolute(T1_rgb), T1_rgb):
506-
T1_rgb_show = "#000000"
507-
print("\n" + tr.error2[lang][0])
508-
print(tr.error2[lang][1].format(values["T1_list"][0], *T1_rgb) + "\n")
509-
#break
510506

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

558554
# Color calculation
559555
T1_rgb = calc.to_rgb(
560-
T1_curve, mode=T1_mode,
556+
name_0, T1_curve, mode=T1_mode,
561557
albedo = T1_spectrum["albedo"] or T1_albedo,
562558
exp_bit=int(values["T1_bit_num"]),
563559
gamma=values["T1_gamma"],
@@ -787,14 +783,15 @@ def frame(num):
787783
for y in range(T2_h):
788784
T2_spectrum = T2_data[:, y, x]
789785
if np.sum(T2_spectrum) > 0:
786+
T2_name = f'({x}; {y})'
790787
T2_curve = calc.polator(input_data["nm"], list(T2_spectrum), T2_nm, fast=T2_fast)
791-
T2_rgb = calc.to_rgb(T2_curve, mode="albedo", albedo=True, inp_bit=T2_input_bit, exp_bit=8, gamma=input_data["gamma"])
788+
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"])
792789
T2_draw.point((x, y), T2_rgb)
793790
if x % 32 == 0 and y % 32 == 0:
794791
T2_fig.add_trace(go.Scatter(
795792
x = T2_nm,
796793
y = T2_curve,
797-
name = f'({x}; {y})',
794+
name = T2_name,
798795
line = dict(color="rgb"+str(T2_rgb), width=2)
799796
))
800797
T2_counter += 1
@@ -915,14 +912,10 @@ def frame(num):
915912

916913
# Color calculation
917914
T3_rgb = calc.to_rgb(
918-
T3_curve, mode=T3_mode,
915+
name, T3_curve, mode=T3_mode,
919916
albedo = spectrum["albedo"] or T3_albedo,
920917
exp_bit=8, gamma=values["T3_gamma"], srgb=values["T3_srgb"]
921918
)
922-
if not np.array_equal(np.absolute(T3_rgb), T3_rgb):
923-
print("\n" + tr.error2[lang][0])
924-
print(tr.error2[lang][1].format(name, *T3_rgb) + "\n")
925-
break
926919

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

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

984977
elif event.startswith("T4"):
985978

986979
if event == "T4_maxtemp_num":
987980
window["T4_slider1"].update(range=(0, int(values["T4_maxtemp_num"])))
981+
988982
else:
989-
990983
if event == "T4_irr":
991984
window["T4_scale"].update(text_color=T4_text_colors[values["T4_irr"]])
992985
window["T4_slider4"].update(disabled=not values["T4_irr"])
@@ -999,16 +992,17 @@ def frame(num):
999992
T4_curve *= 10**(-values["T4_slider4"])
1000993
except np.core._exceptions.UFuncTypeError:
1001994
pass
995+
T4_name = f'{values["T4_slider1"]} {values["T4_slider2"]} {values["T4_slider3"]}'
1002996
T4_rgb = calc.to_rgb(
1003-
T4_curve, mode=T4_mode,
997+
T4_name, T4_curve, mode=T4_mode,
1004998
albedo=values["T4_irr"],
1005999
exp_bit=int(values["T4_bit_num"]),
10061000
gamma=values["T4_gamma"],
10071001
rnd=int(values["T4_rnd_num"]),
10081002
srgb=values["T4_srgb"]
10091003
)
10101004
T4_rgb_show = calc.to_rgb(
1011-
T4_curve, mode=T4_mode,
1005+
T4_name, T4_curve, mode=T4_mode,
10121006
albedo=values["T4_irr"],
10131007
gamma=values["T4_gamma"],
10141008
srgb=values["T4_srgb"],

Scripts/calculations.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import traceback
12
import numpy as np
23
from scipy.interpolate import Akima1DInterpolator
34
import cmf, database
@@ -205,36 +206,41 @@ def xyz_to_sRGB(xyz):
205206
def to_bit(color, bit): return color * (2**bit - 1)
206207
def to_html(color): return "#{:02x}{:02x}{:02x}".format(*rounder(to_bit(color, 8), 0))
207208

208-
def to_rgb(spectrum, mode="chromaticity", inp_bit=None, exp_bit=None, rnd=0, albedo=False, phase=0, gamma=False, srgb=False, html=False):
209-
if inp_bit:
210-
spectrum /= (2**inp_bit - 1)
211-
if srgb:
212-
xyz = np.sum(spectrum[:, np.newaxis] * cmf.xyz, axis=0)
213-
rgb = xyz_to_sRGB(xyz)
214-
rgb = rgb / rgb[1] * spectrum[38] # xyz cmf is not normalized, so result was overexposed; spectrum[38] is 550 nm
215-
else:
216-
rgb = np.sum(spectrum[:, np.newaxis] * cmf.rgb, axis=0)
217-
if mode == "albedo 0.5":
218-
if rgb[1] != 0:
219-
rgb /= 2 * rgb[1]
220-
elif mode == "albedo" and albedo:
209+
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):
210+
try:
211+
if inp_bit:
212+
spectrum /= (2**inp_bit - 1)
213+
if srgb:
214+
xyz = np.sum(spectrum[:, np.newaxis] * cmf.xyz, axis=0)
215+
rgb = xyz_to_sRGB(xyz)
216+
rgb = rgb / rgb[1] * spectrum[38] # xyz cmf is not normalized, so result was overexposed; spectrum[38] is 550 nm
217+
else:
218+
rgb = np.sum(spectrum[:, np.newaxis] * cmf.rgb, axis=0)
219+
if mode == "albedo 0.5":
220+
if rgb[1] != 0:
221+
rgb /= 2 * rgb[1]
222+
elif mode == "albedo" and albedo:
223+
if html:
224+
rgb = np.clip(rgb, 0, 1)
225+
else: # "chromaticity" and when albedo == False
226+
mx = np.max(rgb)
227+
if mx != 0:
228+
rgb /= mx
229+
if phase != 0:
230+
rgb *= lambert(phase)
231+
if gamma:
232+
rgb = gamma_correction(rgb)
233+
if rgb.min() < 0:
234+
print("NegativeColorValues:", target, rgb)
235+
rgb = np.clip(rgb, 0, None)
221236
if html:
222-
rgb = np.clip(rgb, 0, 1)
223-
else: # "chromaticity" and when albedo == False
224-
mx = np.max(rgb)
225-
if mx != 0:
226-
rgb /= mx
227-
if phase != 0:
228-
rgb *= lambert(phase)
229-
if gamma:
230-
rgb = gamma_correction(rgb)
231-
if rgb.min() < 0:
232-
# print(f'Negative RGB values were clipped: {rgb}')
233-
rgb = np.clip(rgb, 0, None)
234-
if html:
235-
return to_html(rgb)
236-
else:
237-
return tuple(rounder(rgb if not exp_bit else to_bit(rgb, exp_bit), rnd))
237+
return to_html(rgb)
238+
else:
239+
return tuple(rounder(rgb if not exp_bit else to_bit(rgb, exp_bit), rnd))
240+
except:
241+
print("ColorCalcError:", target)
242+
print(traceback.format_exc())
243+
return "#000000" if html else (0, 0, 0)
238244

239245

240246
# Pivot wavelengths and ZeroPoints of filter bandpasses

0 commit comments

Comments
 (0)