-
Notifications
You must be signed in to change notification settings - Fork 29
/
alt_az.py
executable file
·205 lines (169 loc) · 7.5 KB
/
alt_az.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
#!/usr/bin/python3
# alt_az.py
# -*- coding: utf-8 -*-
#
# The python script in this file makes the various parts of a model planisphere.
#
# Copyright (C) 2014-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 optional alt-az grid of the planisphere.
"""
from math import atan2
from numpy import arange
from typing import Dict, List, Tuple
from constants import radius, transform, pos
from constants import unit_deg, unit_rev, unit_mm, central_hole_size
from graphics_context import BaseComponent, GraphicsContext
from settings import fetch_command_line_arguments
from text import text
class AltAzGrid(BaseComponent):
"""
Render the optional alt-az grid of the planisphere.
"""
def default_filename(self) -> str:
"""
Return the default filename to use when saving this component.
"""
return "alt_az_grid"
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
"""
latitude: float = abs(settings['latitude'])
bounding_box: Dict[str, float] = {
'x_min': 0,
'x_max': 0,
'y_min': 0,
'y_max': 0
}
# Trace around horizon, keeping track of minimum and maximum coordinates
alt_edge: float = -12
# At latitudes very close to the equator, the point -12 degrees below the horizon is below dec -90!
if abs(latitude) < 15:
alt_edge = -9
path: List[Tuple[float, float]] = [
transform(alt=alt_edge, az=az, latitude=latitude) for az in arange(0, 360.5, 1)
]
for pt in path:
r_b: float = radius(dec=pt[1] / unit_deg, latitude=latitude)
position: Dict[str, float] = pos(r_b, pt[0])
bounding_box['x_min'] = min(bounding_box['x_min'], position['x'])
bounding_box['x_max'] = max(bounding_box['x_max'], position['x'])
bounding_box['y_min'] = min(bounding_box['y_min'], position['y'])
bounding_box['y_max'] = max(bounding_box['y_max'], position['y'])
return bounding_box
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
"""
latitude: float = abs(settings['latitude'])
language: str = settings['language']
context.set_font_size(0.9)
# Set altitude of outer edge of alt-az grid, including margin for gluing instructions
alt_edge: float = -10
azimuth_step: float = 1
# At latitudes very close to the equator, the point -12 degrees below the horizon is below dec -90!
if abs(latitude) < 15:
alt_edge = -8
azimuth_step = 0.2
# Draw horizon (altitude 0), and line to cut around edge of window (altitude alt_edge)
alt: float
for alt in (alt_edge, 0):
# Draw a line, segment by segment, taking small steps in azimuth
path: List[Tuple[float, float]] = [
transform(alt=alt, az=az, latitude=latitude) for az in arange(0, 360.5, azimuth_step)
]
# Project line from RA, Dec into planispheric coordinates
context.begin_path()
for i, p in enumerate(path):
r_b: float = radius(dec=p[1] / unit_deg, latitude=latitude)
if i == 0:
context.move_to(**pos(r_b, p[0]))
else:
context.line_to(**pos(r_b, p[0]))
context.stroke()
if alt == alt_edge:
# Draw the central hole in the middle of the viewing window
context.begin_sub_path()
context.circle(centre_x=0, centre_y=0, radius=central_hole_size)
context.stroke()
# Create clipping area, excluding central hole
context.clip()
# Draw lines of constant altitude
context.begin_path()
for alt in range(10, 85, 10):
path: List[Tuple[float, float]] = [
transform(alt=alt, az=az, latitude=latitude) for az in arange(0, 360.5, 1)
]
for i, p in enumerate(path):
r_b: float = radius(dec=p[1] / unit_deg, latitude=latitude)
if i == 0:
context.move_to(**pos(r_b, p[0]))
else:
context.line_to(**pos(r_b, p[0]))
context.stroke(color=(0.5, 0.5, 0.5, 1))
# Draw lines of constant azimuth, marking S,SSE,SE,ESE,E, etc
context.begin_path()
for az in arange(0, 359, 22.5):
path: List[Tuple[float, float]] = [
transform(alt=alt, az=az, latitude=latitude) for alt in arange(0, 90.1, 1)
]
for i, p in enumerate(path):
r_b: float = radius(dec=p[1] / unit_deg, latitude=latitude)
if i == 0:
context.move_to(**pos(r_b, p[0]))
else:
context.line_to(**pos(r_b, p[0]))
context.stroke(color=(0.5, 0.5, 0.5, 1))
# Gluing labels
def make_gluing_label(azimuth: float) -> None:
pp: Tuple[float, float] = transform(alt=0, az=azimuth - 0.01, latitude=latitude)
r: float = radius(dec=pp[1] / unit_deg, latitude=latitude)
p: Dict[str, float] = pos(r, pp[0])
pp2: Tuple[float, float] = transform(alt=0, az=azimuth + 0.01, latitude=latitude)
r2: float = radius(dec=pp2[1] / unit_deg, latitude=latitude)
p2: Dict[str, float] = pos(r2, pp2[0])
p3: List[float] = [p2[i] - p[i] for i in ('x', 'y')]
tr: float = -unit_rev / 4 - atan2(p3[0], p3[1])
context.text(text=text[language]["glue_here"],
x=p['x'], y=p['y'],
h_align=0, v_align=1, gap=unit_mm, rotation=tr)
# Write the text "Glue here" at various points around the horizon
context.set_font_style(bold=True)
context.set_color(color=(0, 0, 0, 1))
make_gluing_label(azimuth=0)
make_gluing_label(azimuth=90)
make_gluing_label(azimuth=180)
make_gluing_label(azimuth=270)
# 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=AltAzGrid().default_filename())
# Render the alt-az grid
AltAzGrid(settings={
'latitude': arguments['latitude'],
'language': 'en'
}).render_to_file(
filename=arguments['filename'],
img_format=arguments['img_format']
)