compass-lib 0.0.2__py3-none-any.whl → 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ from __future__ import annotations
2
+
3
+ import datetime
4
+
5
+ import pyIGRF14 as pyIGRF
6
+ from pydantic import BaseModel
7
+ from pydantic_extra_types.coordinate import Latitude # noqa: TC002
8
+ from pydantic_extra_types.coordinate import Longitude # noqa: TC002
9
+
10
+ from compass_lib.constants import GEOJSON_COORDINATE_PRECISION
11
+
12
+
13
+ class GeoLocation(BaseModel):
14
+ latitude: Latitude
15
+ longitude: Longitude
16
+
17
+ def as_tuple(self) -> tuple[float, float]:
18
+ """Return the latitude and longitude as a tuple.
19
+ # RFC 7946: (longitude, latitude)
20
+ """
21
+ return (
22
+ round(self.longitude, GEOJSON_COORDINATE_PRECISION),
23
+ round(self.latitude, GEOJSON_COORDINATE_PRECISION),
24
+ )
25
+
26
+
27
+ def decimal_year(dt: datetime.datetime) -> float:
28
+ dt_start = datetime.datetime( # noqa: DTZ001
29
+ year=dt.year, month=1, day=1, hour=0, minute=0, second=0
30
+ )
31
+ dt_end = datetime.datetime( # noqa: DTZ001
32
+ year=dt.year + 1, month=1, day=1, hour=0, minute=0, second=0
33
+ )
34
+ return round(
35
+ dt.year + (dt - dt_start).total_seconds() / (dt_end - dt_start).total_seconds(),
36
+ ndigits=2,
37
+ )
38
+
39
+
40
+ def get_declination(location: GeoLocation, dt: datetime.datetime) -> float:
41
+ declination, _, _, _, _, _, _ = pyIGRF.igrf_value(
42
+ location.latitude,
43
+ location.longitude,
44
+ alt=0.0,
45
+ year=decimal_year(dt),
46
+ )
47
+ return round(declination, 2)