siderust-cpp
Header-only C++ wrapper for siderust
Loading...
Searching...
No Matches
altitude_events_example.cpp
Go to the documentation of this file.
1
12#include <siderust/siderust.hpp>
13
14#include <cstdio>
15#include <vector>
16
17using namespace siderust;
18using namespace qtty::literals;
19
21 return (d == CrossingDirection::Rising) ? "rising" : "setting";
22}
23
25 return (k == CulminationKind::Max) ? "max" : "min";
26}
27
28static void print_utc(const UTC& utc) {
29 std::printf("%04d-%02u-%02u %02u:%02u:%02u", utc.year, utc.month, utc.day,
30 utc.hour, utc.minute, utc.second);
31}
32
33static void print_periods(const char* title, const std::vector<Period>& periods,
34 std::size_t max_items = 4) {
35 std::printf("%s: %zu period(s)\n", title, periods.size());
36 const std::size_t n =
37 (periods.size() < max_items) ? periods.size() : max_items;
38 for (std::size_t i = 0; i < n; ++i) {
39 const auto s = periods[i].start().to_utc();
40 const auto e = periods[i].end().to_utc();
41 std::printf(" %zu) ", i + 1);
42 print_utc(s);
43 std::printf(" -> ");
44 print_utc(e);
45 std::printf(" (%.2f h)\n", periods[i].duration_days() * 24.0);
46 }
47 if (periods.size() > n) {
48 std::printf(" ... (%zu more)\n", periods.size() - n);
49 }
50}
51
52static void print_crossings(const char* title,
53 const std::vector<CrossingEvent>& events,
54 std::size_t max_items = 6) {
55 std::printf("%s: %zu event(s)\n", title, events.size());
56 const std::size_t n = (events.size() < max_items) ? events.size() : max_items;
57 for (std::size_t i = 0; i < n; ++i) {
58 const auto t = events[i].time.to_utc();
59 std::printf(" %zu) ", i + 1);
60 print_utc(t);
61 std::printf(" %s\n", crossing_direction_name(events[i].direction));
62 }
63 if (events.size() > n) {
64 std::printf(" ... (%zu more)\n", events.size() - n);
65 }
66}
67
68static void print_culminations(const char* title,
69 const std::vector<CulminationEvent>& events,
70 std::size_t max_items = 6) {
71 std::printf("%s: %zu event(s)\n", title, events.size());
72 const std::size_t n = (events.size() < max_items) ? events.size() : max_items;
73 for (std::size_t i = 0; i < n; ++i) {
74 const auto t = events[i].time.to_utc();
75 std::printf(" %zu) ", i + 1);
76 print_utc(t);
77 std::printf(" alt=%.3f deg kind=%s\n", events[i].altitude.value(),
78 culmination_kind_name(events[i].kind));
79 }
80 if (events.size() > n) {
81 std::printf(" ... (%zu more)\n", events.size() - n);
82 }
83}
84
85int main() {
86 std::printf("=== Altitude Events Example ===\n\n");
87
88 const auto obs = MAUNA_KEA;
89 const auto start = MJD::from_utc({2026, 7, 15, 0, 0, 0});
90 const auto end = start + 2.0;
91 const Period window(start, end);
92
93 std::printf("Observer: Mauna Kea (lon=%.4f lat=%.4f h=%.0f m)\n",
94 obs.lon.value(), obs.lat.value(), obs.height.value());
95 std::printf("Window MJD: %.6f -> %.6f\n\n", start.value(), end.value());
96
97 SearchOptions opts;
98 opts.with_scan_step(1.0 / 144.0).with_tolerance(1e-10);
99
100 // Sun examples.
101 const auto sun_night = sun::below_threshold(obs, window, -18.0_deg, opts);
102 const auto sun_cross = sun::crossings(obs, window, -0.833_deg, opts);
103 const auto sun_culm = sun::culminations(obs, window, opts);
104 print_periods("Sun below -18 deg (astronomical night)", sun_night);
105 print_crossings("Sun crossings at -0.833 deg", sun_cross);
106 print_culminations("Sun culminations", sun_culm);
107 std::printf("\n");
108
109 // Moon examples.
110 const auto moon_above = moon::above_threshold(obs, window, 20.0_deg, opts);
111 const auto moon_cross = moon::crossings(obs, window, 0.0_deg, opts);
112 const auto moon_culm = moon::culminations(obs, window, opts);
113 print_periods("Moon above +20 deg", moon_above);
114 print_crossings("Moon horizon crossings", moon_cross);
115 print_culminations("Moon culminations", moon_culm);
116 std::printf("\n");
117
118 // Star examples.
119 const auto& vega = VEGA;
120 const auto vega_above =
121 star_altitude::above_threshold(vega, obs, window, 25.0_deg, opts);
122 const auto vega_cross =
123 star_altitude::crossings(vega, obs, window, 0.0_deg, opts);
124 const auto vega_culm = star_altitude::culminations(vega, obs, window, opts);
125 print_periods("VEGA above +25 deg", vega_above);
126 print_crossings("VEGA horizon crossings", vega_cross);
127 print_culminations("VEGA culminations", vega_culm);
128 std::printf("\n");
129
130 // Fixed ICRS direction examples.
131 const spherical::direction::ICRS dir_icrs(279.23473_deg, 38.78369_deg);
132 const auto dir_above =
133 icrs_altitude::above_threshold(dir_icrs, obs, window, 30.0_deg, opts);
134 const auto dir_below =
135 icrs_altitude::below_threshold(dir_icrs, obs, window, 0.0_deg, opts);
136 print_periods("Fixed ICRS direction above +30 deg", dir_above);
137 print_periods("Fixed ICRS direction below horizon", dir_below);
138
139 return 0;
140}
static void print_crossings(const char *title, const std::vector< CrossingEvent > &events, std::size_t max_items=6)
static const char * crossing_direction_name(CrossingDirection d)
static void print_periods(const char *title, const std::vector< Period > &periods, std::size_t max_items=4)
static const char * culmination_kind_name(CulminationKind k)
static void print_utc(const UTC &utc)
static void print_culminations(const char *title, const std::vector< CulminationEvent > &events, std::size_t max_items=6)
std::vector< Period > below_threshold(const spherical::direction::ICRS &dir, const Geodetic &obs, const Period &window, qtty::Degree threshold, const SearchOptions &opts={})
Find periods when a fixed ICRS direction is below a threshold.
Definition altitude.hpp:580
std::vector< Period > above_threshold(const spherical::direction::ICRS &dir, const Geodetic &obs, const Period &window, qtty::Degree threshold, const SearchOptions &opts={})
Find periods when a fixed ICRS direction is above a threshold.
Definition altitude.hpp:549
std::vector< CulminationEvent > culminations(const Geodetic &obs, const Period &window, const SearchOptions &opts={})
Find culmination events for the Moon.
Definition altitude.hpp:352
std::vector< CrossingEvent > crossings(const Geodetic &obs, const Period &window, qtty::Degree threshold, const SearchOptions &opts={})
Find threshold-crossing events for the Moon.
Definition altitude.hpp:328
std::vector< Period > above_threshold(const Geodetic &obs, const Period &window, qtty::Degree threshold, const SearchOptions &opts={})
Find periods when the Moon is above a threshold altitude.
Definition altitude.hpp:280
std::vector< Period > above_threshold(const Star &s, const Geodetic &obs, const Period &window, qtty::Degree threshold, const SearchOptions &opts={})
Find periods when a star is above a threshold altitude.
Definition altitude.hpp:425
std::vector< CulminationEvent > culminations(const Star &s, const Geodetic &obs, const Period &window, const SearchOptions &opts={})
Find culmination events for a star.
Definition altitude.hpp:497
std::vector< CrossingEvent > crossings(const Star &s, const Geodetic &obs, const Period &window, qtty::Degree threshold, const SearchOptions &opts={})
Find threshold-crossing events for a star.
Definition altitude.hpp:473
std::vector< CrossingEvent > crossings(const Geodetic &obs, const Period &window, qtty::Degree threshold, const SearchOptions &opts={})
Find threshold-crossing events for the Sun.
Definition altitude.hpp:184
std::vector< Period > below_threshold(const Geodetic &obs, const Period &window, qtty::Degree threshold, const SearchOptions &opts={})
Find periods when the Sun is below a threshold altitude.
Definition altitude.hpp:160
std::vector< CulminationEvent > culminations(const Geodetic &obs, const Period &window, const SearchOptions &opts={})
Find culmination events for the Sun.
Definition altitude.hpp:208
tempoch::Period Period
Definition time.hpp:19
const Star VEGA
Definition bodies.hpp:257
const Geodetic MAUNA_KEA
Mauna Kea Observatory (Hawaii, USA).
tempoch::UTC UTC
Definition time.hpp:16
Umbrella header for the siderust C++ wrapper library.
Options for altitude search algorithms.
Definition altitude.hpp:55
SearchOptions & with_scan_step(double step)
Set a custom scan step.
Definition altitude.hpp:63
SearchOptions & with_tolerance(double tol)
Set time tolerance.
Definition altitude.hpp:70
A direction on the celestial sphere, compile-time tagged by frame.
Definition spherical.hpp:36