-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarmap.py
485 lines (385 loc) · 13.3 KB
/
starmap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
from datetime import datetime
from matplotlib.collections import LineCollection
from matplotlib.pyplot import subplots, close
from numpy import array, rollaxis, clip, amax
from pytz import timezone, utc
from skyfield import api, projections
from skyfield.data import hipparcos, stellarium
from timezonefinder import TimezoneFinder
from ttkbootstrap import (
BooleanVar,
Button,
Checkbutton,
DateEntry,
DoubleVar,
Entry,
Frame,
Label,
Labelframe,
Scale,
StringVar,
Window,
constants,
Combobox,
)
from ttkbootstrap.dialogs import Messagebox, colorchooser
from tkinter.filedialog import asksaveasfilename
ADD_CONSTELLATIONS = True
BG_COLOR = "#000000"
ALPHA = 1.0
STAR_COLOR = "#ffffff"
CONSTELLATION_COLOR = "#ffffff"
CONSTELLATION_WIDTH = 0.3
STAR_SCALING = 100
STAR_SIZE_LIMIT = 400
FIGURE_SIZE = 10
FILENAME = "starmap.png"
DPI = 500
MAGNITUDE_LEVELS = [
"0 - very few stars",
"1 ",
"2 ",
"3 - urban night sky",
"4 ",
"5 ",
"6 - rural night sky",
"7 ",
"8 ",
"9 ",
"10 - most stars are visible",
"11",
"12",
"13",
"14",
"15 - show all the stars",
]
def generate_starmap(
use_constellations: bool,
constellation_color: str,
constellation_width: float,
bg_color: str,
bg_alpha: float,
star_color: str,
time: str,
lat: str,
long: str,
star_scaling: str,
max_magnitude: str,
output: str,
dpi: str,
star_limit: str,
):
try:
lat = float(lat)
except ValueError:
Messagebox.show_error(
message="The latitude isn't a valid number.\n"
+ "A valid latitude looks like this: 12.3456"
)
return
try:
long = float(long)
except ValueError:
Messagebox.show_error(
message="The longitude isn't a valid number.\n"
+ "A valid longitude looks like this: 12.3456"
)
return
try:
star_scaling = int(star_scaling)
except ValueError:
Messagebox.show_error(
message="The maximum star size isn't a valid integer number."
)
return
try:
max_magnitude = int(max_magnitude)
if max_magnitude < 0 or max_magnitude > 15:
raise ValueError
except ValueError:
Messagebox.show_error(
message="The maximum magnitude isn't a valid integer number from 0 to 15."
)
return
try:
dpi = int(dpi)
except ValueError:
Messagebox.show_error(message="The DPI isn't a valid integer number.")
return
try:
star_limit = int(star_limit)
except ValueError:
Messagebox.show_error(
message="The star size limit isn't a valid integer number."
)
return
# de421 shows position of earth and sun in space
eph = api.load("de421.bsp")
# hipparcos dataset contains star location data
with api.load.open(hipparcos.URL) as f:
stars = hipparcos.load_dataframe(f)
url = (
"https://raw.githubusercontent.com/Stellarium/stellarium/master"
"/skycultures/modern_st/constellationship.fab"
)
with api.load.open(url) as f:
constellations = stellarium.parse_constellations(f)
# define datetime and convert to utc based on our timezone
tf = TimezoneFinder()
timezone_str = tf.timezone_at(lng=long, lat=lat)
dt = datetime.strptime(time, "%Y-%m-%d %H:%M")
local = timezone(timezone_str)
# get UTC from local timezone and datetime
local_dt = local.localize(dt, is_dst=None)
utc_dt = local_dt.astimezone(utc)
# find location of earth
earth = eph["earth"]
# define observation time from our UTC datetime
ts = api.load.timescale()
t = ts.from_datetime(utc_dt)
# define an observer using the world geodetic system data
observer = api.wgs84.latlon(latitude_degrees=lat, longitude_degrees=long).at(t)
# center the observation point in the middle of the sky
ra, dec, _ = observer.radec()
center_object = api.Star(ra=ra, dec=dec)
# find where our center object is relative to earth
# and build a projection with 180 degree view
center = earth.at(t).observe(center_object)
projection = projections.build_stereographic_projection(center)
# calculate star positions and project them onto a plain space
star_positions = earth.at(t).observe(api.Star.from_dataframe(stars))
stars["x"], stars["y"] = projection(star_positions)
edges = [edge for name, edges in constellations for edge in edges]
edges_star1 = [star1 for star1, star2 in edges]
edges_star2 = [star2 for star1, star2 in edges]
print("Max bright before magnitude: " + str(amax(stars.magnitude)))
bright_stars = stars.magnitude <= max_magnitude
magnitude = stars["magnitude"][bright_stars]
print("Max bright after magnitude: " + str(amax(magnitude)))
marker_size = star_scaling * 10 ** (magnitude / -2.5)
print("Max bright after scale: " + str(amax(marker_size)))
marker_size = clip(marker_size, a_min=None, a_max=star_limit)
fig, ax = subplots(
figsize=(FIGURE_SIZE, FIGURE_SIZE), facecolor=(bg_color, bg_alpha)
)
ax.scatter(
stars["x"][bright_stars],
stars["y"][bright_stars],
s=marker_size,
color=star_color,
marker=".",
linewidths=0,
zorder=2,
)
xy1 = stars[["x", "y"]].loc[edges_star1].values
xy2 = stars[["x", "y"]].loc[edges_star2].values
lines_xy = rollaxis(array([xy1, xy2]), 1)
if use_constellations:
ax.add_collection(
LineCollection(
lines_xy, colors=constellation_color, linewidths=constellation_width
)
)
# set the aspect ratio of the plot to be equal
ax.set_aspect("equal")
# other settings
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.axis("off")
fig.savefig(output, bbox_inches="tight", pad_inches=0, dpi=DPI)
close(fig)
Messagebox.ok(message="Done!")
def pick_color(color_chooser: colorchooser.ColorChooserDialog, color_var: StringVar):
color_chooser.show()
color_var.set(color_chooser.result.hex)
def add_color_chooser(root: Frame, button_text: str, color_value: StringVar):
# Create container frame
frame = Frame(root)
# Create color chooser
chooser = colorchooser.ColorChooserDialog()
# Create button
button = Button(
frame, text=button_text, command=lambda: pick_color(chooser, color_value)
)
button.pack(padx=5, side=constants.LEFT, fill=constants.X, expand=True)
# Create color value label
label = Label(
frame,
text=color_value.get(),
background=color_value.get(),
foreground="grey",
font="TkFixedFont",
)
label.pack(padx=5, side=constants.RIGHT)
# Ensure label is updated when color changes
color_value.trace_add(
"write",
lambda *args: label.config(
text=color_value.get(), background=color_value.get()
),
)
frame.pack(fill=constants.X, pady=5)
def add_scale(root: Frame, scale_text: str, scale_value: DoubleVar, to: float = 1.0):
# Create container frame
frame = Frame(root)
# Create label
label = Label(frame, text=scale_text)
label.pack(anchor=constants.NE, fill=constants.X, expand=True, padx=5)
# Create scale
button = Scale(frame, variable=scale_value, to=to)
button.pack(padx=5, side=constants.LEFT, fill=constants.X, expand=True)
# Create scale value label
label = Label(
frame,
text=f"{scale_value.get():.2f}",
font="TkFixedFont",
)
label.pack(padx=5, side=constants.RIGHT)
# Ensure label is updated when value changes
scale_value.trace_add(
"write",
lambda *args: label.config(text=f"{scale_value.get():.2f}"),
)
frame.pack(fill=constants.X, pady=5)
def choose_file(entry, value):
filename = asksaveasfilename()
if filename:
entry.delete(0, constants.END) # Clear the Entry widget
entry.insert(constants.END, filename)
value.set(filename)
def create_combobox(root, text: str, variable=None, values=None, state="readonly"):
frame = Frame(root)
label = Label(frame, text=text)
label.pack(anchor=constants.NW, padx=5)
combo_frame = Frame(frame)
combo = Combobox(combo_frame, textvariable=variable)
combo["values"] = values
combo["state"] = state
combo.pack(side=constants.LEFT, fill=constants.X, expand=True, padx=5)
combo_frame.pack(expand=True, fill=constants.X)
frame.pack(pady=5, expand=True, fill=constants.X)
return combo
def create_entry(root, text: str, value="", add_button=False, button_var=None):
frame = Frame(root)
label = Label(frame, text=text)
label.pack(anchor=constants.NW, padx=5)
entry_frame = Frame(frame)
entry = Entry(entry_frame)
entry.insert(0, str(value))
entry.pack(side=constants.LEFT, fill=constants.X, expand=True, padx=5)
if add_button:
# Filename chooser
filename_button = Button(
entry_frame,
text="...",
command=lambda: choose_file(entry, button_var),
)
filename_button.pack(side=constants.LEFT, padx=5)
entry_frame.pack(expand=True, fill=constants.X)
frame.pack(pady=5, expand=True, fill=constants.X)
return entry
if __name__ == "__main__":
root = Window(themename="darkly", iconphoto="assets/logo.png", title="Starmap")
top_frame = Frame(root)
# Create geospatial (time/location) settings frame #
##############################
geospatial_settings = Labelframe(
top_frame,
text="Location and time",
)
date_frame = Frame(geospatial_settings)
# Create label
date_label = Label(date_frame, text="Date and time")
date_label.pack(anchor=constants.NW, padx=5)
# Date entry
date_entry = DateEntry(date_frame, dateformat="%Y-%m-%d %H:%M")
date_entry.pack(padx=5)
date_frame.pack(pady=5)
# Latitude
latitude = create_entry(geospatial_settings, text="Latitude")
# Longitude
longitude = create_entry(geospatial_settings, text="Longitude")
geospatial_settings.pack(padx=5, pady=5, fill=constants.Y, side=constants.LEFT)
# Create color settings frame #
###############################
color_settings = Labelframe(top_frame, text="Colors")
# Star color settings
star_color = StringVar(value=STAR_COLOR)
add_color_chooser(color_settings, "Star color", star_color)
# Background color settings
bg_color = StringVar(value=BG_COLOR)
add_color_chooser(color_settings, "Background color", bg_color)
# Background transparency
bg_alpha = DoubleVar(value=ALPHA)
add_scale(color_settings, "Background opacity", bg_alpha)
# Constellation color settings
constellation_color = StringVar(value=CONSTELLATION_COLOR)
add_color_chooser(color_settings, "Constellation color", constellation_color)
# Constellation line width
constellation_width = DoubleVar(value=CONSTELLATION_WIDTH)
add_scale(color_settings, "Constellation line width", constellation_width, to=3.0)
color_settings.pack(padx=5, pady=5, fill=constants.Y, side=constants.LEFT)
# Create star settings frame #
##############################
star_settings = Labelframe(top_frame, text="Star settings")
magnitude = StringVar(value="10 - most stars are visible")
magnitude_box = create_combobox(
star_settings,
text="Magnitude filtering level",
values=MAGNITUDE_LEVELS,
variable=magnitude,
)
# Star scaling
star_scaling = create_entry(
star_settings, text="Star scaling (in %)", value=str(STAR_SCALING)
)
# Star limit
star_limit = create_entry(
star_settings, text="Star size limit", value=str(STAR_SIZE_LIMIT)
)
# DPI
dpi = create_entry(star_settings, text="DPI", value=str(DPI))
star_settings.pack(padx=5, pady=5, fill=constants.Y, side=constants.LEFT)
top_frame.pack(side=constants.TOP)
bottom_frame = Frame(root)
# Constellations checkbox
use_constellations = BooleanVar(value=ADD_CONSTELLATIONS)
constellation_box = Checkbutton(
bottom_frame, variable=use_constellations, text="Add constellations"
)
constellation_box.pack(padx=10, pady=10)
# Filename input
filename = StringVar(value=FILENAME)
filename_input = create_entry(
bottom_frame,
value=filename.get(),
text="Output filename",
add_button=True,
button_var=filename,
)
filename_input.pack(expand=True, fill=constants.X)
# Create start button
start_button = Button(
bottom_frame,
text="Start",
command=lambda: generate_starmap(
use_constellations.get(),
constellation_color=constellation_color.get(),
constellation_width=constellation_width.get(),
star_color=star_color.get(),
bg_color=bg_color.get(),
bg_alpha=bg_alpha.get(),
time=date_entry.entry.get(),
lat=latitude.get(),
long=longitude.get(),
star_scaling=star_scaling.get(),
max_magnitude=magnitude.get()[:2],
output=filename.get(),
dpi=dpi.get(),
star_limit=star_limit.get(),
),
)
start_button.pack(padx=10, pady=10)
bottom_frame.pack(side=constants.BOTTOM, fill=constants.X, expand=True)
# Start window
root.mainloop()