siderust-cpp
Header-only C++ wrapper for siderust
Loading...
Searching...
No Matches
demo.cpp
Go to the documentation of this file.
1
10#include <cmath>
11#include <cstdio>
12#include <siderust/siderust.hpp>
13
14int main() {
15 using namespace siderust;
16 using namespace siderust::frames;
17 using namespace qtty::literals;
18
19 std::printf("=== siderust-cpp demo ===\n\n");
20
21 // --- Time ---
22 auto jd = JulianDate::from_utc({2026, 7, 15, 22, 0, 0});
23 std::printf("JD for 2026-07-15 22:00 UTC: %.6f\n", jd.value());
24 std::printf("Julian centuries since J2000: %.10f\n", jd.julian_centuries());
25
26 auto mjd = MJD::from_jd(jd);
27 std::printf("MJD: %.6f\n\n", mjd.value());
28
29 // --- Observatory ---
30 auto obs = ROQUE_DE_LOS_MUCHACHOS;
31 std::printf("Roque de los Muchachos: lon=%.4f lat=%.4f h=%.0f m\n\n",
32 obs.lon.value(), obs.lat.value(), obs.height.value());
33
34 // --- Sun altitude ---
35 qtty::Radian sun_alt = sun::altitude_at(obs, mjd);
36 std::printf("Sun altitude: %.4f rad (%.2f deg)\n\n",
37 sun_alt.value(), sun_alt.to<qtty::Degree>().value());
38
39 // --- Star catalog ---
40 const auto& vega = VEGA;
41 std::printf("Star: %s, d=%.2f ly, L=%.2f Lsun\n",
42 vega.name().c_str(), vega.distance_ly(),
43 vega.luminosity_solar());
44
45 // --- Star altitude ---
46 qtty::Radian star_alt = star_altitude::altitude_at(vega, obs, mjd);
47 std::printf("Vega altitude: %.4f rad (%.2f deg)\n\n",
48 star_alt.value(), star_alt.to<qtty::Degree>().value());
49
50 // =================================================================
51 // TYPED COORDINATE & EPHEMERIS API
52 // =================================================================
53 std::printf("--- Typed Coordinate API ---\n\n");
54
55 // Compile-time typed ICRS direction
56 spherical::direction::ICRS vega_icrs(279.23473, 38.78369);
57
58 // Template-targeted transform: ICRS → EclipticMeanJ2000
59 auto ecl = vega_icrs.to_frame<EclipticMeanJ2000>(jd);
60 std::printf("Typed ICRS (%.4f, %.4f) -> EclMeanJ2000 (%.4f, %.4f)\n",
61 vega_icrs.ra().value(), vega_icrs.dec().value(),
62 ecl.lon().value(), ecl.lat().value());
63
64 // Shorthand .to<>() syntax
65 auto eq_j2000 = vega_icrs.to<EquatorialMeanJ2000>(jd);
66 std::printf("Typed ICRS -> EquatorialJ2000 (%.4f, %.4f)\n",
67 eq_j2000.ra().value(), eq_j2000.dec().value());
68
69 // Horizontal transform
70 auto hor = vega_icrs.to_horizontal(jd, obs);
71 std::printf("Typed Horizontal: az=%.4f alt=%.4f deg\n\n",
72 hor.az().value(), hor.al().value());
73
74 // Roundtrip: ICRS → Ecliptic → ICRS
75 auto back = ecl.to_frame<ICRS>(jd);
76 std::printf("Roundtrip: (%.6f, %.6f) -> (%.6f, %.6f) -> (%.6f, %.6f)\n",
77 vega_icrs.ra().value(), vega_icrs.dec().value(),
78 ecl.lon().value(), ecl.lat().value(),
79 back.ra().value(), back.dec().value());
80
81 // qtty unit-safe angle conversion
82 qtty::Radian ra_rad = vega_icrs.ra().to<qtty::Radian>();
83 std::printf("Vega RA: %.6f deg = %.6f rad\n\n", vega_icrs.ra().value(), ra_rad.value());
84
85 // --- Typed Ephemeris ---
86 std::printf("--- Typed Ephemeris ---\n\n");
87
88 auto earth = ephemeris::earth_heliocentric(jd);
89 std::printf("Earth heliocentric (typed AU): (%.8f, %.8f, %.8f)\n",
90 earth.x().value(), earth.y().value(), earth.z().value());
91
92 // Unit conversion: AU → km
93 qtty::Kilometer x_km = earth.comp_x.to<qtty::Kilometer>();
94 qtty::Kilometer y_km = earth.comp_y.to<qtty::Kilometer>();
95 std::printf("Earth heliocentric (km): (%.2f, %.2f, ...)\n\n",
96 x_km.value(), y_km.value());
97
98 auto moon = ephemeris::moon_geocentric(jd);
99 std::printf("Moon geocentric (typed km): (%.2f, %.2f, %.2f)\n",
100 moon.x().value(), moon.y().value(), moon.z().value());
101 auto moon_r = std::sqrt(moon.x().value() * moon.x().value() + moon.y().value() * moon.y().value() + moon.z().value() * moon.z().value());
102 std::printf("Moon distance: %.2f km\n\n", moon_r);
103
104 // --- Planets ---
105 auto mars_data = MARS;
106 std::printf("Mars: mass=%.4e kg, radius=%.2f km\n",
107 mars_data.mass_kg, mars_data.radius_km);
108 std::printf(" orbit: a=%.6f AU, e=%.6f\n\n",
109 mars_data.orbit.semi_major_axis_au,
110 mars_data.orbit.eccentricity);
111
112 // --- Night periods (sun below -18°) ---
113 auto night_start = mjd;
114 auto night_end = mjd + 1.0;
115 auto nights = sun::below_threshold(obs, night_start, night_end, -18.0_deg);
116 std::printf("Astronomical night periods (sun < -18 deg):\n");
117 for (auto& p : nights) {
118 std::printf(" MJD %.6f – %.6f (%.2f hours)\n",
119 p.start_mjd(), p.end_mjd(),
120 p.duration_days() * 24.0);
121 }
122
123 std::printf("\nDone.\n");
124 return 0;
125}
Umbrella header for the siderust C++ wrapper library.
Mean ecliptic & equinox of J2000.0.
Definition frames.hpp:51
Mean equatorial of J2000.0 (FK5-aligned).
Definition frames.hpp:57
International Celestial Reference System.
Definition frames.hpp:47
A direction on the celestial sphere, compile-time tagged by frame.
Definition spherical.hpp:36
std::enable_if_t< frames::has_frame_transform_v< F, Target >, Direction< Target > > to_frame(const JulianDate &jd) const
Transform to a different reference frame.
std::enable_if_t< frames::has_horizontal_transform_v< F_ >, Direction< frames::Horizontal > > to_horizontal(const JulianDate &jd, const Geodetic &observer) const
Transform to the horizontal (alt-az) frame.
auto to(const JulianDate &jd) const -> decltype(this->template to_frame< Target >(jd))
Shorthand: .to<Target>(jd) (calls to_frame).
qtty::Degree dec() const
Definition spherical.hpp:69
qtty::Degree ra() const
Definition spherical.hpp:66