skymap 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ constellation,name
2
+ And,Andromeda
3
+ Ant,Antlia
4
+ Aps,Apus
5
+ Aqr,Aquarius
6
+ Aql,Aquila
7
+ Ara,Ara
8
+ Ari,Aries
9
+ Aur,Auriga
10
+ Boo,Boötes
11
+ Cae,Caelum
12
+ Cam,Camelopardalis
13
+ Cnc,Cancer
14
+ CVn,Canes Venatici
15
+ CMa,Canis Major
16
+ CMi,Canis Minor
17
+ Cap,Capricornus
18
+ Car,Carina
19
+ Cas,Cassiopeia
20
+ Cen,Centaurus
21
+ Cep,Cepheus
22
+ Cet,Cetus
23
+ Cha,Chamaeleon
24
+ Cir,Circinus
25
+ Col,Columba
26
+ Com,Coma Berenices
27
+ CrA,Corona Australis
28
+ CrB,Corona Borealis
29
+ Crv,Corvus
30
+ Crt,Crater
31
+ Cru,Crux
32
+ Cyg,Cygnus
33
+ Del,Delphinus
34
+ Dor,Dorado
35
+ Dra,Draco
36
+ Equ,Equuleus
37
+ Eri,Eridanus
38
+ For,Fornax
39
+ Gem,Gemini
40
+ Gru,Grus
41
+ Her,Hercules
42
+ Hor,Horologium
43
+ Hya,Hydra
44
+ Hyi,Hydrus
45
+ Ind,Indus
46
+ Lac,Lacerta
47
+ Leo,Leo
48
+ LMi,Leo Minor
49
+ Lep,Lepus
50
+ Lib,Libra
51
+ Lup,Lupus
52
+ Lyn,Lynx
53
+ Lyr,Lyra
54
+ Men,Mensa
55
+ Mic,Microscopium
56
+ Mon,Monoceros
57
+ Mus,Musca
58
+ Nor,Norma
59
+ Oct,Octans
60
+ Oph,Ophiuchus
61
+ Ori,Orion
62
+ Pav,Pavo
63
+ Peg,Pegasus
64
+ Per,Perseus
65
+ Phe,Phoenix
66
+ Pic,Pictor
67
+ Psc,Pisces
68
+ PsA,Piscis Austrinus
69
+ Pup,Puppis
70
+ Pyx,Pyxis
71
+ Ret,Reticulum
72
+ Sge,Sagitta
73
+ Sgr,Sagittarius
74
+ Sco,Scorpius
75
+ Scl,Sculptor
76
+ Sct,Scutum
77
+ Ser,Serpens
78
+ Sex,Sextans
79
+ Tau,Taurus
80
+ Tel,Telescopium
81
+ Tri,Triangulum
82
+ TrA,Triangulum Australe
83
+ Tuc,Tucana
84
+ UMa,Ursa Major
85
+ UMi,Ursa Minor
86
+ Vel,Vela
87
+ Vir,Virgo
88
+ Vol,Volans
89
+ Vul,Vulpecula
data/lib/skymap/canvas.rb CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  module Skymap
4
4
  Canvas = Data.define(:size, :padding) do
5
+ def initialize(size:, padding:)
6
+ unless padding >= 0 && padding < size / 2.0
7
+ raise ArgumentError,
8
+ "padding must not be negative and must be less than half the size"
9
+ end
10
+
11
+ super
12
+ end
13
+
5
14
  def center
6
15
  Point.new(x: size / 2.0, y: size / 2.0)
7
16
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csv"
4
+
5
+ module Skymap
6
+ module Catalog
7
+ class ConstellationBoundaries
8
+ include Enumerable
9
+
10
+ PATH = File.expand_path(
11
+ "../../../data/constellations/iau/boundaries.csv",
12
+ __dir__
13
+ )
14
+
15
+ def each
16
+ return enum_for(:each) unless block_given?
17
+
18
+ CSV.foreach(PATH, headers: true).chunk_while do |previous, row|
19
+ previous["border"] == row["border"]
20
+ end.each do |rows|
21
+ yield boundary_from(rows)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def boundary_from(rows)
28
+ ConstellationBoundary.new(
29
+ constellations: rows.first["constellations"].split,
30
+ points: rows.map { |row| coordinates_from(row) }
31
+ )
32
+ end
33
+
34
+ def coordinates_from(row)
35
+ Astronoby::Coordinates::Equatorial.new(
36
+ right_ascension: Astronoby::Angle.from_hours(
37
+ row["right_ascension"].to_f
38
+ ),
39
+ declination: Astronoby::Angle.from_degrees(row["declination"].to_f),
40
+ epoch: Astronoby::JulianDate::J2000
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
@@ -8,11 +8,13 @@ module Skymap
8
8
  include Enumerable
9
9
 
10
10
  PATH = File.expand_path(
11
- "../../../data/constellations/iau.csv",
11
+ "../../../data/constellations/iau/lines.csv",
12
12
  __dir__
13
13
  )
14
14
 
15
15
  def each
16
+ return enum_for(:each) unless block_given?
17
+
16
18
  CSV.foreach(PATH, headers: true) do |row|
17
19
  yield line_from(row)
18
20
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csv"
4
+
5
+ module Skymap
6
+ module Catalog
7
+ class ConstellationNames
8
+ PATH = File.expand_path(
9
+ "../../../data/constellations/iau/names.csv",
10
+ __dir__
11
+ )
12
+
13
+ def [](abbreviation)
14
+ names[abbreviation]
15
+ end
16
+
17
+ private
18
+
19
+ def names
20
+ @names ||= CSV
21
+ .read(PATH, headers: true, encoding: "UTF-8")
22
+ .to_h { |row| [row["constellation"], row["name"]] }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -10,6 +10,8 @@ module Skymap
10
10
  PATH = File.expand_path("../../../data/stars.csv", __dir__)
11
11
 
12
12
  def each
13
+ return enum_for(:each) unless block_given?
14
+
13
15
  CSV.foreach(PATH, headers: true) do |row|
14
16
  yield star_from(row)
15
17
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ ConstellationBoundary = Data.define(:constellations, :points)
5
+ end
@@ -2,29 +2,32 @@
2
2
 
3
3
  module Skymap
4
4
  class Context
5
- attr_reader :canvas
5
+ attr_reader :observer, :instant, :canvas
6
6
 
7
7
  def initialize(observer:, instant:, canvas:)
8
8
  @observer = observer
9
9
  @instant = instant
10
10
  @canvas = canvas
11
+ @transformation = Astronoby::HorizontalTransformation.new(
12
+ observer: observer,
13
+ instant: instant
14
+ )
11
15
  @horizontal = {}
12
16
  @projections = {}
13
17
  end
14
18
 
15
19
  def horizontal(star)
16
- coordinates = star.equatorial_coordinates
20
+ horizontal_coordinates(star.equatorial_coordinates)
21
+ end
22
+
23
+ def horizontal_coordinates(coordinates)
17
24
  key = [
18
25
  coordinates.right_ascension,
19
26
  coordinates.declination,
20
27
  coordinates.epoch
21
28
  ]
22
29
 
23
- @horizontal[key] ||= Astronoby::DeepSkyObject
24
- .new(equatorial_coordinates: coordinates)
25
- .at(@instant)
26
- .observed_by(@observer)
27
- .horizontal
30
+ @horizontal[key] ||= @transformation.apply(coordinates)
28
31
  end
29
32
 
30
33
  def position(star)
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ module Horizon
5
+ module_function
6
+
7
+ def direction(horizontals)
8
+ vectors = horizontals.map do |horizontal|
9
+ [
10
+ horizontal.altitude.cos * horizontal.azimuth.cos,
11
+ horizontal.altitude.cos * horizontal.azimuth.sin,
12
+ horizontal.altitude.sin
13
+ ]
14
+ end
15
+ vectors.transpose.map(&:sum)
16
+ end
17
+
18
+ def crossing(above, below)
19
+ above.zip(below).map do |from, to|
20
+ above.last * to - below.last * from
21
+ end
22
+ end
23
+
24
+ def project(context, direction)
25
+ x, y, = direction
26
+ context.project(
27
+ altitude: Astronoby::Angle.zero,
28
+ azimuth: Astronoby::Angle.from_radians(Math.atan2(y, x))
29
+ )
30
+ end
31
+
32
+ def visible_runs(context, coordinates)
33
+ horizontals = coordinates.map do |point|
34
+ context.horizontal_coordinates(point)
35
+ end
36
+ path = [position(context, horizontals.first)] +
37
+ horizontals.each_cons(2).flat_map do |from, to|
38
+ crossing_point(context, from, to) + [position(context, to)]
39
+ end
40
+
41
+ path.chunk { |point| !point.nil? }.filter_map do |visible, points|
42
+ points if visible && points.size > 1
43
+ end
44
+ end
45
+
46
+ def position(context, horizontal)
47
+ return if below?(horizontal)
48
+
49
+ context.project(
50
+ altitude: horizontal.altitude,
51
+ azimuth: horizontal.azimuth
52
+ )
53
+ end
54
+
55
+ def crossing_point(context, from, to)
56
+ return [] if below?(from) == below?(to)
57
+
58
+ above, below = below?(from) ? [to, from] : [from, to]
59
+ [
60
+ project(
61
+ context,
62
+ crossing(direction([above]), direction([below]))
63
+ )
64
+ ]
65
+ end
66
+
67
+ def below?(horizontal)
68
+ horizontal.altitude.negative?
69
+ end
70
+
71
+ private_class_method :position, :crossing_point, :below?
72
+ end
73
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ module Layers
5
+ class ConstellationBoundaries
6
+ LINE_WIDTH_RATIO = 0.0017
7
+
8
+ def initialize(boundaries:)
9
+ @boundaries = boundaries
10
+ end
11
+
12
+ def elements(context)
13
+ width = LINE_WIDTH_RATIO * context.canvas.radius
14
+
15
+ @boundaries.flat_map do |boundary|
16
+ Horizon.visible_runs(context, boundary.points).map do |points|
17
+ Renderer::Boundary.new(points: points, width: width)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -11,22 +11,24 @@ module Skymap
11
11
  end
12
12
 
13
13
  def elements(context)
14
- stars_by_hr = @stars.to_h { |star| [star.hr, star] }
14
+ stars_by_hr = Star.by_hr(@stars)
15
15
 
16
16
  @lines.filter_map do |line|
17
17
  ends = [line.from, line.to].map { |hrs| stars_by_hr.values_at(*hrs) }
18
18
  next if ends.flatten.include?(nil)
19
19
 
20
20
  directions = ends.map do |end_stars|
21
- direction(end_stars.map { |star| context.horizontal(star) })
21
+ Horizon.direction(
22
+ end_stars.map { |star| context.horizontal(star) }
23
+ )
22
24
  end
23
25
  next if directions.all? { |direction| direction.last.negative? }
24
26
 
25
27
  from, to = ends.each_with_index.map do |end_stars, index|
26
28
  if directions[index].last.negative?
27
- project_direction(
29
+ Horizon.project(
28
30
  context,
29
- horizon_crossing(directions[1 - index], directions[index])
31
+ Horizon.crossing(directions[1 - index], directions[index])
30
32
  )
31
33
  else
32
34
  Point.midpoint(end_stars.map { |star| context.position(star) })
@@ -39,33 +41,6 @@ module Skymap
39
41
  )
40
42
  end
41
43
  end
42
-
43
- private
44
-
45
- def direction(horizontals)
46
- vectors = horizontals.map do |horizontal|
47
- [
48
- horizontal.altitude.cos * horizontal.azimuth.cos,
49
- horizontal.altitude.cos * horizontal.azimuth.sin,
50
- horizontal.altitude.sin
51
- ]
52
- end
53
- vectors.transpose.map(&:sum)
54
- end
55
-
56
- def horizon_crossing(above, below)
57
- above.zip(below).map do |from, to|
58
- above.last * to - below.last * from
59
- end
60
- end
61
-
62
- def project_direction(context, direction)
63
- x, y, = direction
64
- context.project(
65
- altitude: Astronoby::Angle.zero,
66
- azimuth: Astronoby::Angle.from_radians(Math.atan2(y, x))
67
- )
68
- end
69
44
  end
70
45
  end
71
46
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ module Layers
5
+ class ConstellationNames
6
+ def initialize(lines:, stars:, names: Catalog::ConstellationNames.new)
7
+ @lines = lines
8
+ @stars = stars
9
+ @names = names
10
+ end
11
+
12
+ def elements(context)
13
+ stars_by_hr = Star.by_hr(@stars)
14
+
15
+ @lines.group_by(&:constellation).filter_map do |abbreviation, lines|
16
+ name = @names[abbreviation]
17
+ next unless name
18
+
19
+ parts = parts(lines).map { |hrs| stars_by_hr.values_at(*hrs).compact }
20
+ risen = parts.map do |stars|
21
+ stars.reject { |star| context.horizontal(star).altitude.negative? }
22
+ end
23
+ next if risen.sum(&:size) * 2 <= parts.sum(&:size)
24
+
25
+ biggest = risen.max_by(&:size)
26
+ Renderer::Caption.new(
27
+ position: Point.midpoint(biggest.map { |s| context.position(s) }),
28
+ text: name,
29
+ size: Renderer::Caption.size_for(context.canvas),
30
+ kind: :constellation
31
+ )
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def parts(lines)
38
+ lines.each_with_object([]) do |line, parts|
39
+ hrs = line.from + line.to
40
+ joined, others = parts.partition { |part| part.intersect?(hrs) }
41
+ parts.replace(others << (joined.flatten + hrs).uniq)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ module Layers
5
+ class Ecliptic
6
+ LINE_WIDTH_RATIO = 0.0025
7
+ STEP_IN_DEGREES = 1
8
+
9
+ def initialize
10
+ obliquity = Astronoby::MeanObliquity.obliquity_of_reference
11
+ points = (0...360).step(STEP_IN_DEGREES).map do |degrees|
12
+ point_at(Astronoby::Angle.from_degrees(degrees), obliquity)
13
+ end
14
+ @points = points + points.first(1)
15
+ end
16
+
17
+ def elements(context)
18
+ width = LINE_WIDTH_RATIO * context.canvas.radius
19
+
20
+ joined(Horizon.visible_runs(context, @points)).map do |points|
21
+ Renderer::Ecliptic.new(points: points, width: width)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def point_at(longitude, obliquity)
28
+ Astronoby::Coordinates::Equatorial.new(
29
+ right_ascension: Astronoby::Angle.from_radians(
30
+ Math.atan2(longitude.sin * obliquity.cos, longitude.cos)
31
+ ),
32
+ declination: Astronoby::Angle.asin(longitude.sin * obliquity.sin),
33
+ epoch: Astronoby::JulianDate::J2000
34
+ )
35
+ end
36
+
37
+ def joined(runs)
38
+ return runs unless runs.size > 1 && runs.first.first == runs.last.last
39
+
40
+ [runs.last + runs.first.drop(1)] + runs[1...-1]
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ module Layers
5
+ class Moon
6
+ MOON_RADIUS_RATIO = 0.03
7
+
8
+ def initialize(ephem:)
9
+ @ephem = ephem
10
+ end
11
+
12
+ def elements(context)
13
+ moon = Astronoby::Moon.at(context.instant, ephem: @ephem)
14
+ horizontal = moon.observed_by(context.observer).horizontal
15
+ return [] if horizontal.altitude.negative?
16
+
17
+ [
18
+ Renderer::Moon.new(
19
+ center: context.project(
20
+ altitude: horizontal.altitude,
21
+ azimuth: horizontal.azimuth
22
+ ),
23
+ radius: MOON_RADIUS_RATIO * context.canvas.radius,
24
+ illuminated_fraction: moon.illuminated_fraction,
25
+ rotation: rotation(moon, context.observer)
26
+ )
27
+ ]
28
+ end
29
+
30
+ private
31
+
32
+ def rotation(moon, observer)
33
+ from_zenith = moon.bright_limb_position_angle.degrees -
34
+ moon.parallactic_angle(observer: observer).degrees
35
+
36
+ -90 - from_zenith
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ module Layers
5
+ class Planets
6
+ DEFAULT_MAGNITUDE_LIMIT = 5
7
+ BODIES = {
8
+ mercury: Astronoby::Mercury,
9
+ venus: Astronoby::Venus,
10
+ mars: Astronoby::Mars,
11
+ jupiter: Astronoby::Jupiter,
12
+ saturn: Astronoby::Saturn,
13
+ uranus: Astronoby::Uranus,
14
+ neptune: Astronoby::Neptune
15
+ }.freeze
16
+ NAMES = BODIES.keys.to_h { |planet| [planet, planet.to_s.capitalize] }
17
+ .freeze
18
+
19
+ def initialize(
20
+ ephem:,
21
+ magnitude_limit: DEFAULT_MAGNITUDE_LIMIT,
22
+ labels: false,
23
+ names: NAMES
24
+ )
25
+ @ephem = ephem
26
+ @magnitude_limit = magnitude_limit
27
+ @labels = labels
28
+ @names = names
29
+ end
30
+
31
+ def elements(context)
32
+ star_size = StarSize.for(
33
+ canvas: context.canvas,
34
+ magnitude_limit: @magnitude_limit
35
+ )
36
+
37
+ BODIES.flat_map do |planet, body_class|
38
+ body = body_class.at(context.instant, ephem: @ephem)
39
+ magnitude = body.apparent_magnitude
40
+ next [] if magnitude > @magnitude_limit
41
+
42
+ horizontal = body.observed_by(context.observer).horizontal
43
+ next [] if horizontal.altitude.negative?
44
+
45
+ center = context.project(
46
+ altitude: horizontal.altitude,
47
+ azimuth: horizontal.azimuth
48
+ )
49
+ radius = star_size.radius(magnitude)
50
+ caption = caption(planet, center, radius, context) if @labels
51
+ [Renderer::Planet.new(center: center, radius: radius), caption]
52
+ .compact
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def caption(planet, center, radius, context)
59
+ name = @names[planet]
60
+ return unless name
61
+
62
+ size = Renderer::Caption.size_for(context.canvas)
63
+ towards_center = (center.y > context.canvas.center.y) ? -1 : 1
64
+
65
+ Renderer::Caption.new(
66
+ position: Point.new(
67
+ x: center.x,
68
+ y: center.y + towards_center * (radius + size)
69
+ ),
70
+ text: name,
71
+ size: size,
72
+ kind: :planet
73
+ )
74
+ end
75
+ end
76
+ end
77
+ end
@@ -4,10 +4,6 @@ module Skymap
4
4
  module Layers
5
5
  class Stars
6
6
  DEFAULT_MAGNITUDE_LIMIT = 5
7
- MAX_RADIUS_MAGNITUDE = -1.5
8
- MIN_STAR_RADIUS_RATIO = 0.0025
9
- MAX_STAR_RADIUS_RATIO = 0.02
10
- STAR_SIZE_EXPONENT = 1.5
11
7
 
12
8
  def initialize(stars:, magnitude_limit: DEFAULT_MAGNITUDE_LIMIT)
13
9
  @stars = stars
@@ -15,12 +11,9 @@ module Skymap
15
11
  end
16
12
 
17
13
  def elements(context)
18
- star_size = StarSize.new(
19
- brightest_magnitude: MAX_RADIUS_MAGNITUDE,
20
- faintest_magnitude: @magnitude_limit,
21
- min_radius: MIN_STAR_RADIUS_RATIO * context.canvas.radius,
22
- max_radius: MAX_STAR_RADIUS_RATIO * context.canvas.radius,
23
- exponent: STAR_SIZE_EXPONENT
14
+ star_size = StarSize.for(
15
+ canvas: context.canvas,
16
+ magnitude_limit: @magnitude_limit
24
17
  )
25
18
 
26
19
  @stars.filter_map do |star|
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ module Layers
5
+ class Sun
6
+ SUN_RADIUS_RATIO = 0.03
7
+
8
+ def initialize(ephem:)
9
+ @ephem = ephem
10
+ end
11
+
12
+ def elements(context)
13
+ sun = Astronoby::Sun.at(context.instant, ephem: @ephem)
14
+ horizontal = sun.observed_by(context.observer).horizontal
15
+ return [] if horizontal.altitude.negative?
16
+
17
+ [
18
+ Renderer::Sun.new(
19
+ center: context.project(
20
+ altitude: horizontal.altitude,
21
+ azimuth: horizontal.azimuth
22
+ ),
23
+ radius: SUN_RADIUS_RATIO * context.canvas.radius
24
+ )
25
+ ]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skymap
4
+ module Renderer
5
+ Boundary = Data.define(:points, :width)
6
+ end
7
+ end