siderust-cpp 0.8.0
Header-only C++ wrapper for siderust
Loading...
Searching...
No Matches
07_moon_properties.cpp

Moon phase properties and illumination-window searches.

// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Vallés Puig, Ramon
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace siderust;
using namespace qtty::literals;
template <typename T> static CivilTime to_utc_civil(const T &time) {
return time.template to<scale::UTC>().to_civil();
}
void print_periods(const std::string &label, const std::vector<Period<TT, MJD>> &periods) {
std::cout << "\n" << label << ": " << periods.size() << " period(s)" << std::endl;
for (const auto &p : periods) {
auto dur_h = p.duration<qtty::Hour>();
auto s = to_utc_civil(p.start());
auto e = to_utc_civil(p.end());
std::cout << " - " << s << " -> " << e << " (" << dur_h << ")" << std::endl;
}
}
int main() {
// Default site: Roque de los Muchachos
double lat = 28.762;
double lon = -17.892;
double h_m = 2396.0;
Geodetic site{qtty::Degree(lon), qtty::Degree(lat), qtty::Meter(h_m)};
// Use a fixed date for reproducibility: 2026-03-01 00:00 UTC
auto jd = Time<TT, JD>::from_utc({2026, 3, 1, 0, 0, 0});
auto mjd = jd.to<format::MJD>();
auto window = Period<TT, MJD>(mjd, mjd + qtty::Day(35.0));
SearchOptions opts{};
// =========================================================================
// 1) Point-in-time phase properties
// =========================================================================
auto geo = moon::phase_geocentric(jd);
auto topo = moon::phase_topocentric(jd, site);
std::cout << std::fixed;
std::cout << "Moon phase at 2026-03-01 00:00 UTC" << std::endl;
std::cout << "==================================" << std::endl;
std::cout << std::setprecision(4);
std::cout << "Site: lat=" << lat << " deg, lon=" << lon << " deg, h=" << std::setprecision(0)
<< h_m << " m" << std::endl;
std::cout << "\nGeocentric:" << std::endl;
std::cout << " label : " << moon::phase_label(geo) << std::endl;
std::cout << std::setprecision(4);
std::cout << " illuminated fraction : " << geo.illuminated_fraction << std::endl;
std::cout << std::setprecision(2);
std::cout << " illuminated percent : " << illuminated_percent(geo) << " %" << std::endl;
std::cout << " phase angle : " << geo.phase_angle.to<qtty::Degree>() << std::endl;
std::cout << " elongation : " << geo.elongation.to<qtty::Degree>() << std::endl;
std::cout << " waxing : " << std::boolalpha << geo.waxing << std::endl;
std::cout << "\nTopocentric:" << std::endl;
std::cout << " label : " << moon::phase_label(topo) << std::endl;
std::cout << std::setprecision(4);
std::cout << " illuminated fraction : " << topo.illuminated_fraction << std::endl;
std::cout << " illumination delta : " << std::showpos
<< (topo.illuminated_fraction - geo.illuminated_fraction) * 100.0 << std::noshowpos
<< " %" << std::endl;
std::cout << " elongation : " << topo.elongation.to<qtty::Degree>() << std::endl;
// =========================================================================
// 2) Principal phase events
// =========================================================================
auto events = moon::find_phase_events(window, opts);
std::cout << "\nPrincipal phase events in next 35 days: " << events.size() << std::endl;
for (const auto &ev : events) {
auto utc = to_utc_civil(ev.time);
std::cout << " - " << std::setw(13) << std::right << ev.kind << " at " << utc << " UTC"
<< std::endl;
}
// =========================================================================
// 3) Illumination range searches
// =========================================================================
auto crescent = moon::illumination_range(window, 0.05, 0.35, opts);
auto quarterish = moon::illumination_range(window, 0.45, 0.55, opts);
auto gibbous = moon::illumination_range(window, 0.65, 0.95, opts);
print_periods("Crescent-like range (5%-35%)", crescent);
print_periods("Quarter-like range (45%-55%)", quarterish);
print_periods("Gibbous-like range (65%-95%)", gibbous);
return 0;
}
tempoch::EncodedTime< Scale, Format > Time
Definition time.hpp:36
tempoch::CivilTime CivilTime
Definition time.hpp:15
tempoch::Period< Time< Scale, Format > > Period
Definition time.hpp:37
double illuminated_percent(const MoonPhaseGeometry &geom)
Get the illuminated fraction as a percentage [0, 100].
Umbrella header for the siderust C++ wrapper library.
Geodetic position (WGS84 ellipsoid).
Definition geodetic.hpp:29
Options for altitude search algorithms.
Definition altitude.hpp:58