Skip to content

Commit 7e66512

Browse files
committed
phyvars.Var* are NamedTuples
This is cleaner and allows for type annotations.
1 parent d34c655 commit 7e66512

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

docs/sources/apiref/phyvars.rst

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ phyvars
33

44
.. automodule:: stagpy.phyvars
55

6-
.. class:: Varf
7-
8-
:class:`collections.namedtuple` whose instances hold metadata of
9-
scalar fields. It defines the following fields:
10-
11-
- **description** (*str* or *func*): short description of the variable if
12-
it is output by StagYY, function to compute it otherwise.
13-
- **dim** (*str*): dimension used to
14-
:func:`~stagpy.stagyydata.StagyyData.scale` to dimensional values.
6+
.. autoclass:: Varf
157

168
.. data:: FIELD
179
:annotation: = {fieldvar: Varf()}
@@ -31,17 +23,7 @@ phyvars
3123
Dictionary of surface scalar fields output by StagYY. Keys are the
3224
variable names, values are :class:`Varf` instances.
3325

34-
.. class:: Varr
35-
36-
:class:`collections.namedtuple` whose instances hold metadata of
37-
radial profiles. It defines the following fields:
38-
39-
- **description** (*str* or *func*): short description of the variable if
40-
it is output by StagYY, function to compute it otherwise.
41-
- **kind** (*str*): shorter description to group similar variables under
42-
the same label.
43-
- **dim** (*str*): dimension used to
44-
:func:`~stagpy.stagyydata.StagyyData.scale` to dimensional values.
26+
.. autoclass:: Varr
4527

4628
.. data:: RPROF
4729
:annotation: = {rprofvar: Varr()}
@@ -61,17 +43,7 @@ phyvars
6143
Dictionary of radial profiles of the reference state. Keys are the
6244
variable names, values are :class:`Varr` instances.
6345

64-
.. class:: Vart
65-
66-
:class:`collections.namedtuple` whose instances hold metadata of
67-
time series. It defines the following fields:
68-
69-
- **description** (*str* or *func*): short description of the variable if
70-
it is output by StagYY, function to compute it otherwise.
71-
- **kind** (*str*): shorter description to group similar variables under
72-
the same label.
73-
- **dim** (*str*): dimension used to
74-
:func:`~stagpy.stagyydata.StagyyData.scale` to dimensional values.
46+
.. autoclass:: Vart
7547

7648
.. data:: TIME
7749
:annotation: = {timevar: Vart()}

stagpy/phyvars.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,34 @@
55
be computed from other variables.
66
"""
77

8-
from collections import namedtuple
8+
from __future__ import annotations
99
from operator import attrgetter
1010
from types import MappingProxyType
11+
from typing import NamedTuple, TYPE_CHECKING
1112

1213
from . import processing
1314

14-
Varf = namedtuple('Varf', ['description', 'dim'])
15+
if TYPE_CHECKING:
16+
from typing import Union, Callable, Tuple
17+
from numpy import ndarray
18+
from ._step import Step
19+
from .stagyydata import StagyyData
20+
21+
22+
class Varf(NamedTuple):
23+
"""Metadata of scalar fields.
24+
25+
Attributes:
26+
description: short description of the variable if it is output by
27+
StagYY, function to compute it otherwise.
28+
dim: dimension used to :func:`~stagpy.stagyydata.StagyyData.scale` to
29+
dimensional values.
30+
"""
31+
32+
description: Union[str, Callable[[Step], ndarray]]
33+
dim: str
34+
35+
1536
FIELD = MappingProxyType({
1637
'T': Varf('Temperature', 'K'),
1738
'v1': Varf('x Velocity', 'm/s'),
@@ -129,7 +150,24 @@
129150
'CrustThickness': ['crust'],
130151
})
131152

132-
Varr = namedtuple('Varr', ['description', 'kind', 'dim'])
153+
154+
class Varr(NamedTuple):
155+
"""Metadata of radial profiles.
156+
157+
Attributes:
158+
description: short description of the variable if it is output by
159+
StagYY, function to compute it otherwise.
160+
kind: shorter description to group similar variables under the same
161+
label.
162+
dim: dimension used to :func:`~stagpy.stagyydata.StagyyData.scale` to
163+
dimensional values.
164+
"""
165+
166+
description: Union[str, Callable[[Step], Tuple[ndarray, ndarray]]]
167+
kind: str
168+
dim: str
169+
170+
133171
RPROF = MappingProxyType({
134172
'r': Varr('Radial coordinate', 'Radius', 'm'),
135173
'Tmean': Varr('Temperature', 'Temperature', 'K'),
@@ -212,7 +250,24 @@
212250
'advth': Varr(processing.advth, 'Heat Flux', 'W/m2'),
213251
})
214252

215-
Vart = namedtuple('Vart', ['description', 'kind', 'dim'])
253+
254+
class Vart(NamedTuple):
255+
"""Metadata of time series.
256+
257+
Attributes:
258+
description: short description of the variable if it is output by
259+
StagYY, function to compute it otherwise.
260+
kind: shorter description to group similar variables under the same
261+
label.
262+
dim: dimension used to :func:`~stagpy.stagyydata.StagyyData.scale` to
263+
dimensional values.
264+
"""
265+
266+
description: Union[str, Callable[[StagyyData], Tuple[ndarray, ndarray]]]
267+
kind: str
268+
dim: str
269+
270+
216271
TIME = MappingProxyType({
217272
't': Vart('Time', 'Time', 's'),
218273
'ftop': Vart('Heat flux at top', 'Heat flux', 'W/m2'),

0 commit comments

Comments
 (0)