-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclimate.py
executable file
·376 lines (316 loc) · 16.2 KB
/
climate.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
#!/usr/bin/python3
# climate.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 climate of the astrolabe.
"""
from math import pi, sin, tan, cos, atan2, hypot, acos
from typing import Dict
from constants import unit_deg, unit_cm, unit_mm, inclination_ecliptic, centre_scaling, r_1, d_12, tab_size
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 Climate(BaseComponent):
"""
Render the climate of the astrolabe.
"""
def default_filename(self) -> str:
"""
Return the default filename to use when saving this component.
"""
return "climate"
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.5
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
latitude: float = abs(settings['latitude'])
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(0.8)
# Define the radii of all the concentric circles drawn on front of mother
# The radius of the tab at the top of climate, relative to the centre of the astrolabe
r_tab: float = r_1 - d_12 * 2.5 - unit_mm
# Outer radius of climate
r_2: float = r_1 - d_12 * 3 - unit_mm
# Radius of central hole
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 climate, and the central hole, and use these to create a clipping region
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.stroke()
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()
# Make the tab at the top of the climate
context.begin_path()
context.arc(centre_x=0, centre_y=0, radius=r_tab,
arc_from=-tab_size - pi / 2, arc_to=tab_size - pi / 2)
context.move_to(x=r_tab * sin(tab_size), y=-r_tab * cos(tab_size))
context.line_to(x=r_2 * sin(tab_size), y=-r_2 * cos(tab_size))
context.move_to(x=-r_tab * sin(tab_size), y=-r_tab * cos(tab_size))
context.line_to(x=-r_2 * sin(tab_size), y=-r_2 * cos(tab_size))
context.stroke()
# The maths involved in drawing the climate is described in this paper:
# http://adsabs.harvard.edu/abs/1976JBAA...86..125E
# Draw lines of constant altitude
altitude: float
horizon_centre: float = 0
horizon_radius: float = 0
for altitude in [-6, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85]:
theta1: float = (-latitude - (90 - altitude)) * unit_deg
theta2: float = (-latitude + (90 - altitude)) * unit_deg
x1: float = r_4 * sin(theta1)
y1: float = r_4 * cos(theta1)
x2: float = r_4 * sin(theta2)
y2: float = r_4 * cos(theta2)
y_a: float = y1 * (r_4 / (r_4 - x1))
y_b: float = y2 * (r_4 / (r_4 - x2))
# Record centre and radius of the arc denoting the horizon
if altitude == 0:
horizon_centre = (y_a + y_b) / 2
horizon_radius = (y_b - y_a) / 2
context.set_font_style(bold=True)
context.set_color(theme['text'])
if y_b < r_2:
if (altitude % 10) == 0:
context.text(text="{:.0f}".format(float(altitude)),
x=0, y=-y_b, h_align=0, v_align=1, gap=0, rotation=0)
else:
r: float = (y_b - y_a) / 2
y: float = (y_a + y_b) / 2
start: float = 180 * unit_deg - acos(
(r ** 2 + y ** 2 - r_2 ** 2) / (2 * ((y_b - y_a) / 2) * ((y_a + y_b) / 2)))
end: float = -start
if (altitude > 0) and (altitude % 10 == 0):
context.text(text="{:.0f}".format(float(altitude)),
x=r * sin(start + (r_2 / r) * 2 * unit_deg),
y=-(y_a + y_b) / 2 - r * cos(start + (r_2 / r) * 3 * unit_deg),
h_align=0, v_align=0,
gap=0,
rotation=180 * unit_deg + (start + (r_2 / r) * 3 * unit_deg))
context.text(text="{:.0f}".format(float(altitude)),
x=r * sin(end - (r_2 / r) * 2 * unit_deg),
y=-(y_a + y_b) / 2 - r * cos(end - (r_2 / r) * 3 * unit_deg),
h_align=0, v_align=0,
gap=0,
rotation=180 * unit_deg + (end - (r_2 / r) * 3 * unit_deg))
context.begin_path()
context.circle(centre_x=0, centre_y=-(y_a + y_b) / 2, radius=(y_b - y_a) / 2)
context.stroke(dotted=(altitude < 0),
line_width=0.6 + 1.2 * int(altitude == 0),
color=theme['alt_az'] if altitude > 0 else theme['lines'])
# Find coordinates of P
theta: float = -latitude * unit_deg
p_x: float = r_4 * sin(theta)
p_y: float = r_4 * cos(theta)
# Find coordinates of Z
z_x: float = 0
z_y: float = p_y / (r_4 - p_x) * r_4
# Find midpoint between Z and H
zh_x: float = -r_4 / 2
zh_y: float = z_y / 2
# Find bearing of T from ZH (clockwise from right-going axis)
theta: float = atan2(z_x - (-r_4), z_y)
# Find coordinates of T
t_x: float = 0
t_y: float = zh_y + zh_x * tan(theta)
# Draw lines of constant azimuth. We draw 16 arcs at 11.25 degree intervals, which cut through the zenith
# and meet the horizon in two opposite compass bearings. For this reason we only draw half as many arcs as
# there are compass bearings
step_size: float = 11.25 * unit_deg
for azimuth_step in range(1, 16):
azimuth: float = -90 * unit_deg + step_size * azimuth_step
# Compass direction for the start and end of the line of constant azimuth. Each line of constant azimuth
# meets the horizon at two opposite points, with opposite compass directions.
if (azimuth_step % 2) != 0:
direction_start, direction_end = ("", "")
else:
direction_start: str = text[language]['directions'][azimuth_step // 2]
direction_end: str = text[language]['directions'][azimuth_step // 2 + 8]
# In southern hemisphere, invert directions
if is_southern:
direction_start, direction_end = (direction_end, direction_start)
t_x: float = (z_y - t_y) * tan(azimuth)
# Radius of arc of constant azimuth
t_r: float = hypot(t_x, t_y - z_y)
t_hc: float = hypot(t_x, t_y - horizon_centre) # Distance from T to centre of horizon
theta: float = acos((t_r ** 2 + t_hc ** 2 - horizon_radius ** 2) / (2 * t_r * t_hc))
phi: float = atan2(t_x, horizon_centre - t_y)
start: float = -phi - theta
end: float = -phi + theta
t_c: float = hypot(t_x, t_y) # Distance from T to centre of the astrolabe
arg: float = (t_r ** 2 + t_c ** 2 - r_2 ** 2) / (2 * t_r * t_c)
if (arg >= 1) or (arg <= -1):
start2: float = start
end2: float = end
else:
theta: float = acos((t_r ** 2 + t_c ** 2 - r_2 ** 2) / (2 * t_r * t_c))
phi: float = atan2(t_x, -t_y)
start2: float = -phi - theta
end2: float = -phi + theta
context.begin_path()
context.arc(centre_x=t_x, centre_y=-t_y, radius=t_r,
arc_from=max(start, start2) - pi / 2, arc_to=min(end, end2) - pi / 2)
context.stroke(line_width=0.5,
color=theme['alt_az'])
context.set_font_style(bold=True)
context.set_color(theme['text'])
if hypot(t_x + t_r * sin(end), t_y + t_r * cos(end)) < 0.9 * r_2:
context.text(text=direction_start,
x=t_x + t_r * sin(end), y=-t_y - t_r * cos(end),
h_align=0, v_align=1, gap=unit_mm,
rotation=end - 90 * unit_deg)
else:
context.text(text=direction_start,
x=t_x + t_r * sin(min(end, end2) - (r_2 / t_r) * 8 * unit_deg),
y=-t_y - t_r * cos(min(end, end2) - (r_2 / t_r) * 8 * unit_deg),
h_align=0, v_align=0, gap=0,
rotation=(min(end, end2) - (r_2 / t_r) * 8 * unit_deg))
if hypot(t_x + t_r * sin(start), t_y + t_r * cos(start)) < 0.9 * r_2:
context.text(text=direction_end,
x=t_x + t_r * sin(start),
y=-t_y - t_r * cos(start),
h_align=0, v_align=1, gap=unit_mm,
rotation=90 * unit_deg + start)
else:
context.text(text=direction_end,
x=t_x + t_r * sin(max(start, start2) + (r_2 / t_r) * 8 * unit_deg),
y=-t_y - t_r * cos(max(start, start2) + (r_2 / t_r) * 8 * unit_deg),
h_align=0, v_align=0, gap=0,
rotation=(max(start, start2) + (r_2 / t_r) * 8 * unit_deg))
context.text(text="N" if not is_southern else "S",
x=0, y=-horizon_centre + horizon_radius,
h_align=0, v_align=1, gap=unit_mm, rotation=0)
# Subroutine for calculating the azimuthal angle of the lines of the unequal hours
if settings['astrolabe_type'] == 'full':
# Subroutine for calculating the azimuthal angle of the lines of the unequal hours
def theta_unequal_hours(r: float) -> float:
arg: float = (r ** 2 + horizon_centre ** 2 - horizon_radius ** 2) / (2 * r * horizon_centre)
if arg <= -1:
return 180 * unit_deg
if arg >= 1:
return 0 * unit_deg
return acos(arg)
# Draw lines of unequal hours in turn
for h in range(1, 12):
for r in arange(max(r_5, horizon_radius - horizon_centre), r_2 + 0.05 * unit_mm, 0.5 * unit_mm):
r0: float = r
r1: float = min(r + 0.5 * unit_mm, r_2)
theta0: float = theta_unequal_hours(r0)
theta1: float = theta_unequal_hours(r1)
psi0: float = theta0 + (360 * unit_deg - 2 * theta0) / 12 * h
psi1: float = theta1 + (360 * unit_deg - 2 * theta1) / 12 * h
context.begin_path()
context.move_to(x=r0 * sin(psi0), y=-r0 * cos(psi0))
context.line_to(x=r1 * sin(psi1), y=-r1 * cos(psi1))
context.stroke(line_width=1, dotted=False, color=theme['lines'])
# Label the unequal hours
context.set_font_size(1.6)
r: float = r_2 - 4 * unit_mm
theta0: float = theta_unequal_hours(r)
context.set_font_style(bold=False)
for pos, hr in enumerate(["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"]):
psi0: float = theta0 + (360 * unit_deg - 2 * theta0) / 12 * (pos + 0.5)
psi0 = (psi0 - 180 * unit_deg) * 0.95 + 180 * unit_deg
context.text(text=hr,
x=r * sin(psi0), y=-r * cos(psi0),
h_align=0, v_align=0, gap=unit_mm,
rotation=180 * unit_deg + psi0)
# A space to write the owner's name
if settings['astrolabe_type'] != 'full':
arc_size: float = 40 * unit_deg
context.begin_path()
context.move_to(x=r_2 * sin(arc_size), y=r_2 * cos(arc_size))
context.arc(centre_x=0, centre_y=0,
radius=r_2 - 0.8 * unit_cm,
arc_from=90 * unit_deg - arc_size,
arc_to=90 * unit_deg + arc_size
)
context.line_to(x=-r_2 * sin(arc_size), y=r_2 * cos(arc_size))
context.stroke(line_width=1, dotted=False)
context.circular_text(text="{}:".format(text[language]['name']),
centre_x=0, centre_y=0,
radius=r_2 - 0.4 * unit_cm,
azimuth=238,
spacing=1, size=1.2)
# Draw horizontal and vertical lines through the middle of the climate
context.begin_path()
context.move_to(x=-r_2, y=0)
context.line_to(x=r_2, y=0)
context.move_to(x=0, y=r_2 if settings['astrolabe_type'] == 'full' else r_4)
context.line_to(x=0, y=-r_2)
context.stroke(line_width=1, dotted=False)
# Finish up
context.set_font_style(bold=False)
context.circular_text(text=text[language]['url'],
centre_x=0, centre_y=0, radius=r_2 - 1.6 * unit_cm,
azimuth=270, spacing=1, size=0.7)
context.circular_text(text=text[language]['copyright'],
centre_x=0, centre_y=0, radius=r_2 - 1.3 * unit_cm,
azimuth=270, spacing=1, size=0.7)
context.circular_text(text=text[language]['climate_latitude'].format(latitude, "N" if not is_southern else "S"),
centre_x=0, centre_y=0, radius=r_2 - 1.0 * unit_cm,
azimuth=270, spacing=1, size=0.7)
# 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=Climate().default_filename())
# Render the climate
Climate(settings={
'latitude': arguments['latitude'],
'language': 'en'
}).render_to_file(
filename=arguments['filename'],
img_format=arguments['img_format']
)