-
Notifications
You must be signed in to change notification settings - Fork 16
/
rete.py
executable file
·308 lines (250 loc) · 11.5 KB
/
rete.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
#!/usr/bin/python3
# rete.py
# -*- coding: utf-8 -*-
#
# The python script in this file makes the various parts of a model astrolabe.
#
# Copyright (C) 2010-2024 Dominic Ford <https://dcford.org.uk/>
#
# This code is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# You should have received a copy of the GNU General Public License along with
# this file; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA
# ----------------------------------------------------------------------------
"""
Render the rete of the astrolabe.
"""
from math import pi, sin, tan, cos, asin, floor
from typing import Dict
from bright_stars_process import fetch_bright_star_list
from constants import unit_deg, unit_rev, unit_mm, inclination_ecliptic, centre_scaling, r_1, d_12, line_width_base
from graphics_context import BaseComponent, GraphicsContext
from numpy import arange
from settings import fetch_command_line_arguments
from text import text
from themes import themes
class Rete(BaseComponent):
"""
Render the rete of the astrolabe.
"""
def default_filename(self) -> str:
"""
Return the default filename to use when saving this component.
"""
return "rete"
def bounding_box(self, settings: dict) -> Dict[str, float]:
"""
Return the bounding box of the canvas area used by this component.
:param settings:
A dictionary of settings required by the renderer.
:return:
Dictionary with the elements 'x_min', 'x_max', 'y_min' and 'y_max' set
"""
r_outer: float = r_1 - d_12 * 2.7
return {
'x_min': -r_outer,
'x_max': r_outer,
'y_min': -r_outer,
'y_max': r_outer
}
def do_rendering(self, settings: dict, context: GraphicsContext) -> None:
"""
This method is required to actually render this item.
:param settings:
A dictionary of settings required by the renderer.
:param context:
A GraphicsContext object to use for drawing
:return:
None
"""
is_southern: bool = settings['latitude'] < 0
language: str = settings['language']
theme: Dict[str, Tuple[float, float, float, float]] = themes[settings['theme']]
context.set_color(color=theme['lines'])
context.set_font_size(1.0)
# Define the radii of all the concentric circles drawn on front of mother
# Outer radius of the rete
r_2: float = r_1 - d_12 * 3 - unit_mm
# Radius of the hole through the centre
r_3: float = d_12 * centre_scaling
# Radius of the line denoting the equator
r_4: float = r_2 * tan((90 - inclination_ecliptic) / 2 * unit_deg)
# Radius of the line denoting the tropic of Cancer
r_5: float = r_4 * tan((90 - inclination_ecliptic) / 2 * unit_deg)
# Draw the outer edge of rete
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_2)
context.stroke()
# Draw the central hole
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_3)
context.stroke()
# Draw ecliptic
y_ecl_centre: float = (r_2 - r_5) / 2 # Ecliptic circle is centred on midpoint between +r_2 and -r_5
r_ecl_outer: float = (r_2 + r_5) / 2 # Outer radius of ecliptic circle... circle touches r_2 and -r_5
r_ecl_inner: float = r_ecl_outer * 0.9
r_ecl_centre: float = (r_ecl_outer + r_ecl_inner * 2) / 3
# Draw ecliptic as band with outer and inner edges
context.begin_path()
context.circle(centre_x=0, centre_y=(r_2 - r_5) / 2, radius=r_ecl_outer)
context.circle(centre_x=0, centre_y=(r_2 - r_5) / 2, radius=r_ecl_inner)
context.stroke(line_width=1, color=theme['lines'])
# Draw tick marks along the ecliptic at 2-degree intervals
# The maths used here is described in http://adsabs.harvard.edu/abs/1976JBAA...86..219E
theta: float
for theta in arange(0 * unit_deg, 359 * unit_deg, 2 * unit_deg):
# Sine rule
alpha: float = asin(y_ecl_centre * sin(theta) / r_ecl_outer)
# Angles in triangle add up to 180 degrees
psi: float = theta + alpha
# Decide size of tick -- every 30 degrees divide entire ecliptic band; major tick every 10 degrees;
# all other ticks are smaller
t: float = floor((theta / unit_deg) + 0.01)
if (t % 30) == 0:
r_tick_inner: float = r_ecl_inner
elif (t % 10) == 0:
r_tick_inner = (r_ecl_outer + r_ecl_inner) / 2
else:
r_tick_inner = (3 * r_ecl_outer + r_ecl_inner) / 4
# Draw tick mark
context.begin_path()
context.move_to(x=r_ecl_outer * sin(psi), y=y_ecl_centre + r_ecl_outer * cos(psi))
context.line_to(x=r_tick_inner * sin(psi), y=y_ecl_centre + r_tick_inner * cos(psi))
context.stroke()
# Write zodiacal constellation names around ecliptic. We make the text smaller in the southern hemisphere,
# because "Sagittarius" has a lot of letters to fit into a small space!
if not is_southern:
text_size: float = 1
else:
text_size = 0.7
# Write labels for the zodiacal constellations
for i, item in enumerate(text[language]["zodiacal_constellations"]):
i += 1
name: str = item['name']
if not is_southern:
theta: float = (-90 + 15 - 30 * i) * unit_deg
else:
theta = (-90 - 15 + 30 * i) * unit_deg
name = name[:8]
# Sine rule
alpha: float = asin(y_ecl_centre * sin(theta) / r_ecl_outer)
# Angles in triangle add up to 180 degrees
psi: float = -90 * unit_deg - (theta + alpha)
context.circular_text(text=name, centre_x=0, centre_y=y_ecl_centre, radius=r_ecl_centre * 1.02,
azimuth=psi / unit_deg, spacing=0.9, size=text_size)
# Set clipping region so that we don't draw stars over the top of the ecliptic belt
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_2)
context.begin_sub_path()
context.circle(centre_x=0, centre_y=0, radius=r_3)
context.begin_sub_path()
context.circle(centre_x=0, centre_y=(r_2 - r_5) / 2, radius=r_ecl_outer)
context.begin_sub_path()
context.circle(centre_x=0, centre_y=(r_2 - r_5) / 2, radius=r_ecl_inner)
context.clip()
# Draw the equator
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_4)
context.stroke()
# Draw the Tropic of Cancer
context.begin_path()
context.circle(centre_x=0, centre_y=0, radius=r_5)
context.stroke()
# Draw constellation stick figures
with open("raw_data/constellation_stick_figures.dat") as f_in:
for line in f_in:
line: str = line.strip()
# Ignore blank lines and comment lines
if (len(line) == 0) or (line[0] == '#'):
continue
# Split line into words
# These are the names of the constellations, and the start and end points for each stroke.
name: str
ra1_str: str
dec1_str: str
ra2_str: str
dec2_str: str
name, ra1_str, dec1_str, ra2_str, dec2_str = line.split()
dec1: float = float(dec1_str)
ra1: float = float(ra1_str)
dec2: float = float(dec2_str)
ra2: float = float(ra2_str)
# In the southern hemisphere, we flip the sky upside down
if is_southern:
dec1 *= -1
ra1 *= -1
dec2 *= -1
ra2 *= -1
# Convert start and end of line into a radius and an azimuth
theta_point_1: float = (90 - float(dec1)) * unit_deg / 2
r_point_1: float = r_4 * tan(theta_point_1)
if r_point_1 > r_2:
continue
theta_point_2: float = (90 - float(dec2)) * unit_deg / 2
r_point_2: float = r_4 * tan(theta_point_2)
if r_point_2 > r_2:
continue
# Draw stick figure line
context.begin_path()
context.move_to(x=r_point_1 * cos(float(ra1) * unit_deg), y=-r_point_1 * sin(float(ra1) * unit_deg))
context.line_to(x=r_point_2 * cos(float(ra2) * unit_deg), y=-r_point_2 * sin(float(ra2) * unit_deg))
context.stroke(dotted=True, line_width=1, color=theme['stick_figures'])
# Draw stars from Yale Bright Star Catalogue
for star_descriptor in fetch_bright_star_list()['stars'].values():
ra, dec, mag = star_descriptor[:3]
# Discard stars fainter than mag 4
if mag == "-" or float(mag) > 4.0:
continue
# In the southern hemisphere, we flip the sky upside down
ra = float(ra)
dec = float(dec)
if is_southern:
dec *= -1
ra *= -1
theta: float = (90 - dec) * unit_deg / 2
r: float = r_4 * tan(theta)
# Discard stars which are outside the plotted area
if r > r_2:
continue
# Draw a circle to represent this star
context.begin_path()
context.circle(centre_x=r * cos(ra * unit_deg), centre_y=-r * sin(ra * unit_deg),
radius=0.18 * unit_mm * (5 - mag))
context.fill(color=theme['lines'])
# Draw RA scale around the edge of the rete
r_tick: float = r_2 * 0.98
for ra in arange(0, 23.9, 1):
theta: float = ra / 24 * unit_rev
if is_southern:
ra = 24 - ra
context.begin_path()
context.move_to(x=-r_2 * cos(theta), y=-r_2 * sin(theta))
context.line_to(x=-r_tick * cos(theta), y=-r_tick * sin(theta))
context.stroke(dotted=False, line_width=1, color=theme['lines'])
context.text(text="{:.0f}ʰ".format(ra),
x=r_tick * cos(theta), y=-r_tick * sin(theta),
h_align=0, v_align=-1, gap=unit_mm, rotation=-pi / 2 - theta)
# Draw six small tick marks within each hour of RA
r_tick: float = r_2 * 0.99
for ra in arange(0, 23.9, 1. / 6):
theta: float = ra / 24 * unit_rev
context.begin_path()
context.move_to(x=r_2 * cos(theta), y=r_2 * sin(theta))
context.line_to(x=r_tick * cos(theta), y=r_tick * sin(theta))
context.stroke()
# Do it right away if we're run as a script
if __name__ == "__main__":
# Fetch command line arguments passed to us
arguments = fetch_command_line_arguments(default_filename=Rete().default_filename())
# Render the rete
Rete(settings={
'latitude': arguments['latitude'],
'language': 'en'
}).render_to_file(
filename=arguments['filename'],
img_format=arguments['img_format']
)