Skip to content

Commit 4af5284

Browse files
committed
Mv StagyyData.scale to Scales.make_dimensional
1 parent 0a8a7fe commit 4af5284

File tree

4 files changed

+30
-43
lines changed

4 files changed

+30
-43
lines changed

docs/sources/apiref/phyvars.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ phyvars
5656

5757
Dictionary mapping dimension strings (**dim** fields in ``Var*``) to
5858
functions which are themselves mapping from
59-
:class:`~stagpy.stagyydata.StagyyData` to the scale for that dimension.
59+
:class:`~stagpy.dimensions.Scales` to the scale for that dimension.

stagpy/datatypes.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
class Varf:
1414
"""Metadata of scalar field.
1515
16-
`dim` is the dimension used to :func:`~stagpy.stagyydata.StagyyData.scale` to
17-
dimensional values.
16+
`dim` is the dimension used to scale to dimensional values.
1817
"""
1918

2019
description: str
@@ -33,8 +32,7 @@ class Field:
3332
class Varr:
3433
"""Metadata of radial profiles.
3534
36-
`dim` is the dimension used to :func:`~stagpy.stagyydata.StagyyData.scale` to
37-
dimensional values.
35+
`dim` is the dimension used to scale to dimensional values.
3836
"""
3937

4038
description: str
@@ -55,8 +53,7 @@ class Rprof:
5553
class Vart:
5654
"""Metadata of time series.
5755
58-
`dim` is the dimension used to :func:`~stagpy.stagyydata.StagyyData.scale` to
59-
dimensional values.
56+
`dim` is the dimension used to scale to dimensional values.
6057
"""
6158

6259
description: str

stagpy/dimensions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
from dataclasses import dataclass
55
from functools import cached_property
66

7+
from . import phyvars
8+
from .config import Scaling
9+
710
if typing.TYPE_CHECKING:
11+
from typing import TypeVar
12+
13+
from numpy.typing import NDArray
14+
815
from .parfile import StagyyPar
916
from .stagyydata import StagyyData
1017

18+
T = TypeVar("T", float, NDArray)
19+
1120

1221
@dataclass(frozen=True)
1322
class Scales:
@@ -19,6 +28,23 @@ class Scales:
1928
def par(self) -> StagyyPar:
2029
return self.sdat.par
2130

31+
def make_dimensional(self, data: T, unit: str, scaling: Scaling) -> tuple[T, str]:
32+
"""Scale quantity to obtain dimensional quantity."""
33+
if self.par.get("switches", "dimensional_units", True) or unit == "1":
34+
return data, ""
35+
scale = phyvars.SCALES[unit](self)
36+
factor = scaling.factors.get(unit, " ")
37+
if scaling.time_in_y and unit == "s":
38+
scale /= scaling.yearins
39+
unit = "yr"
40+
elif scaling.vel_in_cmpy and unit == "m/s":
41+
scale *= 100 * scaling.yearins
42+
unit = "cm/y"
43+
if factor in phyvars.PREFIXES:
44+
scale *= 10 ** (-3 * (phyvars.PREFIXES.index(factor) + 1))
45+
unit = factor + unit
46+
return data * scale, unit
47+
2248
@cached_property
2349
def length(self) -> float:
2450
"""Length in m."""

stagpy/stagyydata.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -795,42 +795,6 @@ def nfields_max(self, nfields: Optional[int]) -> None:
795795
raise error.InvalidNfieldsError(nfields)
796796
self._nfields_max = nfields
797797

798-
@typing.overload
799-
def scale(self, data: ndarray, unit: str) -> Tuple[ndarray, str]:
800-
"""Scale a ndarray."""
801-
...
802-
803-
@typing.overload
804-
def scale(self, data: float, unit: str) -> Tuple[float, str]:
805-
"""Scale a float."""
806-
...
807-
808-
def scale(
809-
self, data: Union[ndarray, float], unit: str
810-
) -> Tuple[Union[ndarray, float], str]:
811-
"""Scales quantity to obtain dimensionful quantity.
812-
813-
Args:
814-
data: the quantity that should be scaled.
815-
unit: the dimension of data as defined in phyvars.
816-
Return:
817-
scaled quantity and unit string.
818-
"""
819-
if self.par.get("switches", "dimensional_units", True) or unit == "1":
820-
return data, ""
821-
scaling = phyvars.SCALES[unit](self.scales)
822-
factor = conf.scaling.factors.get(unit, " ")
823-
if conf.scaling.time_in_y and unit == "s":
824-
scaling /= conf.scaling.yearins
825-
unit = "yr"
826-
elif conf.scaling.vel_in_cmpy and unit == "m/s":
827-
scaling *= 100 * conf.scaling.yearins
828-
unit = "cm/y"
829-
if factor in phyvars.PREFIXES:
830-
scaling *= 10 ** (-3 * (phyvars.PREFIXES.index(factor) + 1))
831-
unit = factor + unit
832-
return data * scaling, unit
833-
834798
def filename(
835799
self,
836800
fname: str,

0 commit comments

Comments
 (0)