diff --git a/src/unxt/_quantity/distance.py b/src/unxt/_quantity/distance.py index 7b4db86..bb2254b 100644 --- a/src/unxt/_quantity/distance.py +++ b/src/unxt/_quantity/distance.py @@ -1,7 +1,7 @@ # pylint: disable=import-error, no-member, unsubscriptable-object # b/c it doesn't understand dataclass fields -__all__ = ["AbstractDistance", "Distance", "Parallax"] +__all__ = ["AbstractDistance", "Distance", "Parallax", "DistanceModulus"] from abc import abstractmethod from dataclasses import KW_ONLY @@ -143,7 +143,35 @@ def parallax(self) -> "Parallax": @property def distance_modulus(self) -> Quantity: """The distance modulus.""" - return self.distance.distance_modulus # TODO: shortcut + return self.distance.distance_modulus # TODO: specific shortcut + + +############################################################################## + + +class DistanceModulus(AbstractDistance): + """Distance modulus quantity.""" + + def __check_init__(self) -> None: + """Check the initialization.""" + if self.unit != u.mag: + msg = "Distance modulus must have units of magnitude." + raise ValueError(msg) + + @property + def distance(self) -> Distance: + """The distance.""" + return Distance(10 ** (self.value / 5 + 1), "pc") + + @property + def parallax(self) -> Parallax: + """The parallax.""" + return self.distance.parallax # TODO: specific shortcut + + @property + def distance_modulus(self) -> "DistanceModulus": + """The distance modulus.""" + return self # ============================================================================ @@ -159,6 +187,19 @@ def constructor( return cls(xp.asarray(d.value, dtype=dtype), d.unit) +@Distance.constructor._f.register # type: ignore[no-redef] # noqa: SLF001 +def constructor( + cls: type[Distance], + value: DistanceModulus | Quantity["mag"], + /, + *, + dtype: Any = None, +) -> Distance: + """Construct a `Distance` from a mag through the dist mod.""" + d = 10 ** (value.to_units_value("mag") / 5 + 1) + return cls(xp.asarray(d, dtype=dtype), "pc") + + @Parallax.constructor._f.register # type: ignore[no-redef] # noqa: SLF001 def constructor( cls: type[Parallax], value: Distance | Quantity["length"], /, *, dtype: Any = None