mountcontrol/cxx/mount_astrom.h
Timur A. Fatkhullin 95fcca2deb ...
2025-03-07 00:28:17 +03:00

319 lines
9.0 KiB
C++

#pragma once
/*********************************
* MOUNT CONTROL COMPONENTS *
* *
* astrometry functions *
*********************************/
#include <chrono>
#include <unordered_map>
#include <xsimd/xsimd.hpp>
#include "utils.h"
namespace mcc::traits
{
template <typename T>
concept mcc_scalar_or_simd_c = xsimd::is_batch<T>::value || std::is_arithmetic_v<T>;
template <typename T>
concept mcc_real_or_char_range =
std::floating_point<T> ||
(std::ranges::contiguous_range<T> && std::same_as<char, std::remove_cvref_t<std::ranges::range_value_t<T>>>);
} // namespace mcc::traits
namespace mcc::astro
{
// modified Julian date (based on ERFA eraCal2jd)
// template <traits::mcc_scalar_or_simd_c T>
static int mcc_julday(const std::chrono::system_clock::time_point& start_time,
const std::chrono::system_clock::duration& step,
std::vector<double, xsimd::default_allocator<double>>& mjd)
{
if (mjd.empty()) {
return -100;
}
using namespace std::literals::chrono_literals;
auto dd = std::chrono::floor<std::chrono::days>(start_time);
std::chrono::year_month_day ymd{dd};
static constexpr std::chrono::year MIN_YEAR = -4799y;
if (ymd.year() < MIN_YEAR) {
return -1;
}
if (!ymd.month().ok()) {
return -2;
}
// my = (im - 14) / 12;
// iypmy = (long) (iy + my);
int64_t my = -(14 - (unsigned)ymd.month()) / 12;
int64_t iypmy = (int)ymd.year() + my;
// (1461L * (iypmy + 4800L)) / 4L
// + (367L * (long) (im - 2 - 12 * my)) / 12L
// - (3L * ((iypmy + 4900L) / 100L)) / 4L
// + (long) id - 2432076L
// integer part of result MJD
int64_t mjd_int = 1461LL * (iypmy + 480LL) / 4LL +
(367LL * ((int64_t)(unsigned)ymd.month() - 2LL - 12LL * my)) / 12LL -
(3LL * (iypmy + 4900LL) / 100LL) / 4LL + (int64_t)(unsigned)ymd.day() - 2432076LL;
constexpr double nanosec = 1.0 / 24.0 / 3600.0 / 1.0E-9; // 1 nanosecond in days
double mjd_float = static_cast<double>(mjd_int) +
std::chrono::duration_cast<std::chrono::nanoseconds>(start_time - dd).count() * nanosec;
double d_step = std::chrono::duration_cast<std::chrono::nanoseconds>(step).count() * nanosec;
constexpr size_t reg_size = xsimd::batch<double>::size;
size_t vec_size = mjd.size() - mjd.size() % reg_size;
xsimd::batch<double> res_reg{mjd_float};
xsimd::batch<double> step_reg = [d_step]<size_t... Is>(std::index_sequence<Is...>) {
return xsimd::batch<double>{(Is * d_step)...};
}(std::make_index_sequence<reg_size>{});
// vectorized part
size_t i = 0;
for (; i < vec_size; i += vec_size) {
res_reg += step_reg;
res_reg.store_aligned(mjd.data() + i);
}
// scalar part
for (size_t j = i; j < mjd.size(); ++j) {
mjd[j] = mjd_float + j * d_step;
}
return 0;
}
/*
* angles are in degrees or sexagimal string form
*/
template <traits::mcc_real_or_char_range AT,
traits::mcc_real_or_char_range RT,
traits::mcc_real_or_char_range DT,
traits::mcc_real_or_char_range LT>
double mcc_time_to_alt_limit(const AT& alt_limit,
const RT& RA,
const DT& DEC,
const LT& LAT,
const std::chrono::system_clock::time_point& now)
{
// sin(alt) = sin(DEC)*sin(phi) + cos(DEC)*cos(phi)*cos(HA)
// HA = LST - RA
double ra, dec, lat, alt;
if constexpr (std::floating_point<AT>) {
alt = alt_limit * utils::deg2radCoeff;
} else {
alt = utils::parsAngleString(alt_limit);
alt *= utils::deg2radCoeff;
}
if constexpr (std::floating_point<RT>) {
ra = RA * utils::deg2radCoeff;
} else {
ra = utils::parsAngleString(RA, true);
ra *= utils::deg2radCoeff;
}
if constexpr (std::floating_point<DT>) {
dec = DEC * utils::deg2radCoeff;
} else {
dec = utils::parsAngleString(DEC);
dec *= utils::deg2radCoeff;
}
if constexpr (std::floating_point<LT>) {
lat = LAT * utils::deg2radCoeff;
} else {
lat = utils::parsAngleString(LAT);
lat *= utils::deg2radCoeff;
}
double cos_ha = (std::sin(alt) - std::sin(dec) * std::sin(lat)) / std::cos(dec) / std::cos(lat);
if (std::abs(cos_ha) > 1.0) { // circumpolar (it never sets below horizon)
return std::numeric_limits<double>::infinity();
}
double lst = std::acos(cos_ha) + ra;
return 0.0;
}
/* IERS bulletins
*
* BULLETIN A: https://datacenter.iers.org/data/latestVersion/bulletinA.txt
* leapseconds: https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat
*
*/
struct leapsecond_db_elem_t {
double mjd;
unsigned day, month;
int year;
double tai_utc; // TAI-UTC in seconds
};
typedef std::vector<leapsecond_db_elem_t> leapsecond_db_t;
struct earth_orient_db_elem_t {
int year;
unsigned month, day;
double mjd;
double x, y; // Polar coordinates in arcsecs
double dut1; // UT1-UTC in seconds
};
typedef std::vector<earth_orient_db_elem_t> earth_orient_db_t;
// init to some known state
static leapsecond_db_t CURRENT_LEAPSECONDS_DB = {
{41317.0, 1, 1, 1972, 10}, {41499.0, 1, 7, 1972, 11}, {41683.0, 1, 1, 1973, 12},
{42048.0, 1, 1, 1974, 13}, {42413.0, 1, 1, 1975, 14}, {42778.0, 1, 1, 1976, 15},
{43144.0, 1, 1, 1977, 16}, {43509.0, 1, 1, 1978, 17}, {43874.0, 1, 1, 1979, 18},
{44239.0, 1, 1, 1980, 19}, {44786.0, 1, 7, 1981, 20}, {45151.0, 1, 7, 1982, 21},
{45516.0, 1, 7, 1983, 22}, {46247.0, 1, 7, 1985, 23}, {47161.0, 1, 1, 1988, 24},
{47892.0, 1, 1, 1990, 25}, {48257.0, 1, 1, 1991, 26}, {48804.0, 1, 7, 1992, 27},
{49169.0, 1, 7, 1993, 28}, {49534.0, 1, 7, 1994, 29}, {50083.0, 1, 1, 1996, 30},
{50630.0, 1, 7, 1997, 31}, {51179.0, 1, 1, 1999, 32}, {53736.0, 1, 1, 2006, 33},
{54832.0, 1, 1, 2009, 34}, {56109.0, 1, 7, 2012, 35}, {57204.0, 1, 7, 2015, 36},
{57754.0, 1, 1, 2017, 37}
};
bool mcc_parse_bulletinA(std::derived_from<std::basic_istream<char>> auto& stream, char comment_sym = '*')
{
for (std::string line; std::getline(stream, line);) {
if (line.empty()) {
continue;
}
auto sv = utils::trimSpaces(line, utils::TrimType::TRIM_BOTH);
if (sv.size()) {
if (sv[0] == comment_sym) { // comment string
continue;
}
} else {
continue;
}
}
return true;
}
bool mcc_parse_leapsecs(std::derived_from<std::basic_istream<char>> auto& stream, char comment_sym = '#')
{
size_t n;
std::optional<double> vd;
std::optional<unsigned> vui;
std::optional<int> vi;
leapsecond_db_t db;
leapsecond_db_elem_t db_elem;
for (std::string line; std::getline(stream, line);) {
if (line.empty()) {
continue;
}
auto sv = utils::trimSpaces(line, utils::TrimType::TRIM_BOTH);
if (sv.size()) {
if (sv[0] == comment_sym) { // comment string
continue;
}
} else {
continue;
}
n = 0;
for (auto const& el : std::views::split(sv, std::string_view(" "))) {
if (std::ranges::size(el) == 0) {
continue;
}
switch (n) {
case 0:
vd = utils::numFromStr<double>(el);
if (!vd) {
return false;
}
db_elem.mjd = vd.value();
break;
case 1:
vui = utils::numFromStr<unsigned>(el);
if (!vui) {
return false;
}
db_elem.day = vui.value();
break;
case 2:
vui = utils::numFromStr<unsigned>(el);
if (!vui) {
return false;
}
db_elem.month = vui.value();
break;
case 3:
vi = utils::numFromStr<int>(el);
if (!vi) {
return false;
}
db_elem.year = vi.value();
break;
case 4:
vd = utils::numFromStr<double>(el);
if (!vd) {
return false;
}
db_elem.tai_utc = vd.value();
break;
default:
// just ignore??!!!
break;
}
++n;
if (n == 5) {
break;
}
}
if (n < 5) { // not enough elements
return false;
}
db.emplace_back(std::move(db_elem));
}
CURRENT_LEAPSECONDS_DB = std::move(db);
return true;
}
} // namespace mcc::astro