Rose
SatelliteModel.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include "Utilities.h"
11 #include "WebCache.h"
12 #include "Plan13.h"
13 #include "Math.h"
14 #include <memory>
15 #include <algorithm>
16 #include <chrono>
17 
18 namespace rose {
19 
20 // static constexpr std::chrono::milliseconds LunarMonthMilliseconds{2551442976};
21  static constexpr std::chrono::milliseconds LunarMonthMilliseconds{2551442125};
22  static constexpr std::time_t LunarNewMoonEpoch{1618194720};
23 
28  inline double MoonPhase() {
29  std::chrono::system_clock::time_point newEpoch = std::chrono::system_clock::from_time_t(LunarNewMoonEpoch);
30  std::chrono::system_clock::time_point epoch = std::chrono::system_clock::now();
31  auto moonAge = std::chrono::duration_cast<std::chrono::milliseconds>(epoch - newEpoch);
32  auto phase = static_cast<double>((moonAge % LunarMonthMilliseconds).count()) /
33  static_cast<double>(LunarMonthMilliseconds.count()) * 2. * M_PI;
34  return phase;
35  }
36 
37  class ClearSkyEphemeris : public WebCache {
38  public:
39  ClearSkyEphemeris() = delete;
40 
41  ~ClearSkyEphemeris() override = default;
42 
50  ClearSkyEphemeris(const std::string &rootUri, const path &xdgDir, const std::string &storeRoot,
51  std::chrono::system_clock::duration duration) :
52  WebCache(rootUri, xdgDir, storeRoot, duration) {}
53 
54  template<typename It>
55  ClearSkyEphemeris(const std::string &rootUri, const path &xdgDir, const std::string &storeRoot,
56  std::chrono::system_clock::duration duration, It first, It last)
57  : WebCache(rootUri, xdgDir, storeRoot, duration) {
58  setCacheItem(first, last);
59  }
60 
61  std::string constructUrl(const local_id_t &localId) override {
62  if (localId == "Amateur")
63  return mRootURI + "esats.pl?getall=";
64  else if (localId == "Moon")
65  return mRootURI + "esats.pl?tlename=Moon";
66  return mRootURI;
67  }
68  };
69 
70  static constexpr std::array<WebCacheItem,2> CS_Ephem{
71  WebCacheItem{0, "Moon"},
72  WebCacheItem{1, "Amateur"},
73  };
74 
79  class Ephemeris : public std::map<std::string_view, std::array<std::string_view,3>> {
80  protected:
81  std::string mEphemerisSet{};
82 
83  public:
84  using iterator = std::map<std::string_view, std::array<std::string_view,3>>::iterator;
85 
86  Ephemeris() = default;
87 
88  explicit Ephemeris(const std::filesystem::path& filePath);
89 
90  void readFile(const std::filesystem::path &filePath);
91 
92  };
93 
99  protected:
100  std::unique_ptr<ClearSkyEphemeris> mEphemerisCache{};
101 
102  WebCacheProtocol::slot_type mCacheLoaded{};
103 
104  Ephemeris mEphemeris{};
105 
106  SatelliteModel();
107 
108  public:
109  static SatelliteModel& getModel() {
110  static SatelliteModel instance{};
111  return instance;
112  }
113 
114  SatelliteModel(const SatelliteModel&) = delete;
115 
116  SatelliteModel(SatelliteModel &&) = delete;
117 
118  SatelliteModel& operator=(const SatelliteModel&) = delete;
119 
120  SatelliteModel& operator=(SatelliteModel&&) = delete;
121 
122  auto begin() {
123  return mEphemeris.begin();
124  }
125 
126  auto end() {
127  return mEphemeris.end();
128  }
129 
130  [[nodiscard]] auto begin() const {
131  return mEphemeris.cbegin();
132  }
133 
134  [[nodiscard]] auto end() const {
135  return mEphemeris.cend();
136  }
137  };
138 
140  protected:
141  Observer mObserver{};
142 
143  std::vector<Satellite> mConstellation{};
144 
145  public:
146  SatelliteObservation() = default;
147 
148  explicit SatelliteObservation(const Observer &observer);
149 
150  SatelliteObservation(const Observer &observer, const std::string& object);
151 
152  void predict(const DateTime &dateTime);
153 
154  void passPrediction(uint maxCount, const std::string &favorite);
155 
156  [[nodiscard]] const Observer& observer() const {
157  return mObserver;
158  }
159 
160  [[nodiscard]] auto empty() const noexcept {
161  return mConstellation.empty();
162  }
163 
164  [[nodiscard]] auto size() const noexcept {
165  return mConstellation.size();
166  }
167 
168  [[nodiscard]] auto front() const noexcept {
169  return mConstellation.front();
170  }
171  };
172 
173  static constexpr long COARSE_DT = 90L;
174  static constexpr long FINE_DT = (-2L);
175  static constexpr double SAT_MIN_EL = 1.;
176 
178  Satellite satellite{};
179  bool riseOk{false}, setOk{false}, everUp{false}, everDown{false};
180  long deltaTime{COARSE_DT};
181  double altitude{}, azimuth{}, range{}, rangeRate{}, latRad{}, lonRad{}, periodDays{},
182  maxAltitude{}, prevAltitude{0}, setAz{0}, riseAz{0};
183 
184  DateTime srchTime{}, riseTime{}, setTime{};
185 
186  SatellitePassData() = default;
187 
199  [[nodiscard]] std::string passTimeString(time_t relative = 0) const;
200 
202  bool search(DateTime& now) const noexcept {
203  return (!setOk || !riseOk) && srchTime < now + 2.0F && (srchTime > now || altitude > -1.);
204  }
205 
206  [[nodiscard]] bool goodPass(double minAltitude) const noexcept {
207  return riseOk && setOk && maxAltitude >= minAltitude;
208  }
209 
210  void setTopo(const Observer& observer) {
211  auto topo = satellite.topo(observer);
212  altitude = std::get<0>(topo);
213  azimuth = std::get<1>(topo);
214  range = std::get<2>(topo);
215  rangeRate = std::get<3>(topo);
216  }
217 
218  void setGeo() {
219  auto geo = satellite.geo();
220  latRad = std::get<0>(geo);
221  lonRad = std::get<1>(geo);
222  }
223  };
224 }
225 
Encapsulate the day-in-space for orbital mechanics computations.
Definition: Plan13.h:141
Definition: SatelliteModel.h:37
Definition: SatelliteModel.h:139
std::shared_ptr< Slot< Args... > > slot_type
Composed Slot type.
Definition: Signals.h:123
Fetch web resources caching them in the local filesystem following XDG specifications.
Definition: WebCache.h:40
ClearSkyEphemeris(const std::string &rootUri, const path &xdgDir, const std::string &storeRoot, std::chrono::system_clock::duration duration)
Constructor.
Definition: SatelliteModel.h:50
Definition: SatelliteModel.h:79
std::string constructUrl(const local_id_t &localId) override
Construct the appropriate URL for the item.
Definition: SatelliteModel.h:61
std::string mRootURI
The root URI for items in the cache.
Definition: WebCache.h:81
void setCacheItem(key_t key, local_id_t localId)
Add or change a cache item.
Definition: WebCache.h:141
double MoonPhase()
Calculate the current phase of the moon in days between [0..2*M_PI].
Definition: SatelliteModel.h:28
bool search(DateTime &now) const noexcept
Return true if pass not found and search time not exceeded.
Definition: SatelliteModel.h:202
Fetching and caching web resources.
Data specifying an observer for computing relative visibility data.
Definition: Plan13.h:254
Definition: WebCache.h:27
Satellite orbital mechanics.
Definition: Plan13.h:294
Definition: SatelliteModel.h:98
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
Definition: SatelliteModel.h:177