-
Notifications
You must be signed in to change notification settings - Fork 0
/
star_map.py
182 lines (172 loc) · 6.11 KB
/
star_map.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
import dataclasses
import logging
import math
import pathlib
import typing
import PIL.Image
import PIL.ImageDraw
import tycho2
dirname = pathlib.Path(__file__).resolve().parent
@dataclasses.dataclass
class Collection:
stars: list[tuple[typing.Any]]
color: str
size: float
@classmethod
def from_magnitude_range(
cls,
database: tycho2.Database,
minimum_magnitude: float | None,
maximum_magnitude: float,
color: str,
size: float,
):
return cls(
stars=database.find(
minimum_magnitude=minimum_magnitude, maximum_magnitude=maximum_magnitude
),
color=color,
size=size,
)
def generate_pages(
cylinder_circumference_meters: float,
paper_width_meters: float,
paper_height_meters: float,
paper_margin_meters: float,
pixels_per_meter: float,
output_directory: pathlib.Path,
collections: list[Collection],
):
ceili = lambda value: int(math.ceil(value))
roundi = lambda value: int(round(value))
output_directory.mkdir(exist_ok=True)
paper_effective_width = paper_width_meters - paper_margin_meters * 2.0
pages_count = ceili(cylinder_circumference_meters / paper_effective_width)
scale = 2.0 * math.pi / cylinder_circumference_meters
paper_effective_half_height = paper_height_meters / 2 - paper_margin_meters
dec_maximum = (
2.0
* math.atan(math.exp(paper_effective_half_height * scale))
* (180.0 / math.pi)
- 90.0
)
for index in range(0, pages_count):
image = PIL.Image.new(
mode="RGB",
size=(
roundi(paper_width_meters * pixels_per_meter),
roundi(paper_height_meters * pixels_per_meter),
),
color="#CCCCCC",
)
draw = PIL.ImageDraw.Draw(image)
if index == pages_count - 1:
draw.rectangle(
xy=(
roundi(
(
paper_margin_meters
+ (
pages_count * paper_effective_width
- cylinder_circumference_meters
)
)
* pixels_per_meter
),
roundi(paper_margin_meters * pixels_per_meter),
roundi(
(paper_width_meters - paper_margin_meters) * pixels_per_meter
),
roundi(
(paper_height_meters - paper_margin_meters) * pixels_per_meter
),
),
fill="#FFFFFF",
)
else:
draw.rectangle(
xy=(
roundi(paper_margin_meters * pixels_per_meter),
roundi(paper_margin_meters * pixels_per_meter),
roundi(
(paper_width_meters - paper_margin_meters) * pixels_per_meter
),
roundi(
(paper_height_meters - paper_margin_meters) * pixels_per_meter
),
),
fill="#FFFFFF",
)
physical_left = index * paper_effective_width
physical_right = (index + 1) * paper_effective_width
ra_minimum = physical_left / cylinder_circumference_meters * 360.0
ra_maximum = physical_right / cylinder_circumference_meters * 360.0
for collection in collections:
print(ra_minimum, ra_maximum)
radius_pixels = roundi(collection.size / 2.0 * pixels_per_meter)
for star in collection.stars:
ra = star[tycho2.Database.right_ascension]
dec = star[tycho2.Database.declination]
if all(
(
ra >= ra_minimum,
ra <= ra_maximum,
abs(dec) <= dec_maximum,
)
):
x = roundi(
(
paper_width_meters
- (
(ra - ra_minimum)
/ 360.0
* cylinder_circumference_meters
+ paper_margin_meters
)
)
* pixels_per_meter
)
y = roundi(
(
paper_height_meters
- (
math.log(math.tan((dec + 90) * (math.pi / 180) / 2))
/ scale
+ paper_margin_meters
+ paper_effective_half_height
)
)
* pixels_per_meter
)
draw.ellipse(
xy=(
x - radius_pixels,
y - radius_pixels,
x + radius_pixels,
y + radius_pixels,
),
fill=collection.color,
)
image.save(str(output_directory / f"{index}.png"))
if __name__ == "__main__":
logging.getLogger().setLevel(logging.INFO)
database = tycho2.Database()
generate_pages(
cylinder_circumference_meters=0.91,
paper_width_meters=0.2025,
paper_height_meters=0.285,
paper_margin_meters=0.025,
pixels_per_meter=300.0 * 39.3701,
output_directory=dirname / "star_map",
collections=[
Collection.from_magnitude_range(
database, None, 2.0, "#5790FC", 5.0 / 1000.0
),
Collection.from_magnitude_range(
database, 2.0, 3.0, "#F89C20", 3.0 / 1000.0
),
Collection.from_magnitude_range(
database, 3.0, 4.0, "#964A8B", 2.0 / 1000.0
),
],
)