dies und das
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
import math
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP, localcontext
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import astronomy
|
||||
try:
|
||||
@@ -57,6 +58,7 @@ REDSHIFT_ORDER = [
|
||||
TIME_ORDER = ["utc", "ut1", "tai", "jd", "mjd", "unix", "trueSolar", "sidereal"]
|
||||
COORDINATE_ORDER = ["ra", "dec", "galLon", "galLat"]
|
||||
ANGLE_ORDER = ["deg", "arcmin", "arcsec", "rad"]
|
||||
TOOLS_ORDER = ["utc", "altitude", "azimuth"]
|
||||
ANGLE_TO_DEGREES = {
|
||||
"deg": Decimal("1"),
|
||||
"arcmin": Decimal("0.01666666666666666666666666667"),
|
||||
@@ -215,6 +217,27 @@ def parse_utc_datetime(value: str) -> datetime | None:
|
||||
return None
|
||||
|
||||
|
||||
def parse_local_datetime(value: str, time_zone_name: str) -> datetime | None:
|
||||
text = str(value or "").strip()
|
||||
if text == "":
|
||||
return None
|
||||
|
||||
normalized = text.replace(" ", "T")
|
||||
try:
|
||||
zone = ZoneInfo(time_zone_name)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
for fmt in ("%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M"):
|
||||
try:
|
||||
parsed = datetime.strptime(normalized, fmt)
|
||||
return parsed.replace(tzinfo=zone)
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def dt_to_time(dt_utc: datetime) -> astronomy.Time:
|
||||
dt_utc = dt_utc.astimezone(timezone.utc)
|
||||
return astronomy.Time.Make(
|
||||
@@ -509,6 +532,44 @@ def convert_angle(payload: dict) -> dict:
|
||||
return {"valid": True, "fields": fields}
|
||||
|
||||
|
||||
def convert_tools(payload: dict) -> dict:
|
||||
tool = str(payload.get("tool") or "").strip().lower()
|
||||
if tool != "solar_altaz":
|
||||
raise ValueError("Unbekanntes Tool im Bereich Diverse Tools.")
|
||||
|
||||
local_dt = parse_local_datetime(str(payload.get("dateTimeLocal") or ""), str(payload.get("timeZone") or ""))
|
||||
latitude_decimal = parse_decimal_input(payload.get("latitude", ""))
|
||||
longitude_decimal = parse_decimal_input(payload.get("longitude", ""))
|
||||
|
||||
if local_dt is None or latitude_decimal is None or longitude_decimal is None:
|
||||
return {"valid": False, "fields": {key: "" for key in TOOLS_ORDER}}
|
||||
|
||||
latitude = float(latitude_decimal)
|
||||
longitude = float(longitude_decimal)
|
||||
if (
|
||||
not math.isfinite(latitude)
|
||||
or not math.isfinite(longitude)
|
||||
or latitude < -90.0
|
||||
or latitude > 90.0
|
||||
or longitude < -180.0
|
||||
or longitude > 180.0
|
||||
):
|
||||
return {"valid": False, "fields": {key: "" for key in TOOLS_ORDER}}
|
||||
|
||||
dt_utc = local_dt.astimezone(timezone.utc)
|
||||
time_value = dt_to_time(dt_utc)
|
||||
observer = astronomy.Observer(latitude, longitude, 0.0)
|
||||
equator = astronomy.Equator(astronomy.Body.Sun, time_value, observer, True, True)
|
||||
horizon = astronomy.Horizon(time_value, observer, equator.ra, equator.dec, astronomy.Refraction.Normal)
|
||||
|
||||
fields = {
|
||||
"utc": format_utc_datetime(dt_utc),
|
||||
"altitude": format_decimal_german(horizon.altitude, max_fraction_digits=4),
|
||||
"azimuth": format_decimal_german(normalize_angle_degrees(horizon.azimuth), max_fraction_digits=4),
|
||||
}
|
||||
return {"valid": True, "fields": fields}
|
||||
|
||||
|
||||
def handle_request(payload: dict) -> dict:
|
||||
kind = str(payload.get("kind") or "").strip().lower()
|
||||
if kind == "distance":
|
||||
@@ -521,6 +582,8 @@ def handle_request(payload: dict) -> dict:
|
||||
result = convert_coordinates(payload)
|
||||
elif kind == "angle":
|
||||
result = convert_angle(payload)
|
||||
elif kind == "tools":
|
||||
result = convert_tools(payload)
|
||||
else:
|
||||
raise ValueError("Unbekannter Umrechnungsbereich.")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user