Skip to content

Commit 1881031

Browse files
Adding a new UnitConverter Unity()
Following the discussion in #2410
1 parent 6f178dc commit 1881031

4 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/parcels/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
GeographicPolar,
1515
GeographicPolarSquare,
1616
GeographicSquare,
17-
UnitConverter,
17+
Unity,
1818
)
1919
from parcels._core.field import Field, VectorField
2020
from parcels._core.fieldset import FieldSet
@@ -66,7 +66,7 @@
6666
"GeographicPolar",
6767
"GeographicPolarSquare",
6868
"GeographicSquare",
69-
"UnitConverter",
69+
"Unity",
7070
# Status codes and errors
7171
"AllParcelsErrorCodes",
7272
"FieldInterpolationError",

src/parcels/_core/converters.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"GeographicPolarSquare",
1212
"GeographicSquare",
1313
"UnitConverter",
14+
"Unity",
1415
"_convert_to_flat_array",
1516
"_unitconverters_map",
1617
]
@@ -28,11 +29,16 @@ def _convert_to_flat_array(var: npt.ArrayLike) -> npt.NDArray:
2829

2930

3031
class UnitConverter:
31-
"""Interface class for spatial unit conversion during field sampling that performs no conversion."""
32-
3332
source_unit: str | None = None
3433
target_unit: str | None = None
3534

35+
36+
class Unity(UnitConverter):
37+
"""Interface class for spatial unit conversion during field sampling that performs no conversion."""
38+
39+
source_unit: None
40+
target_unit: None
41+
3642
def to_target(self, value, z, y, x):
3743
return value
3844

src/parcels/_core/field.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from parcels._core.converters import (
1212
UnitConverter,
13+
Unity,
1314
_unitconverters_map,
1415
)
1516
from parcels._core.index_search import GRID_SEARCH_ERROR, LEFT_OUT_OF_BOUNDS, RIGHT_OUT_OF_BOUNDS, _search_time_index
@@ -135,7 +136,7 @@ def __init__(
135136
self.igrid = -1 # Default the grid index to -1
136137

137138
if self.grid._mesh == "flat" or (self.name not in _unitconverters_map.keys()):
138-
self.units = UnitConverter()
139+
self.units = Unity()
139140
elif self.grid._mesh == "spherical":
140141
self.units = _unitconverters_map[self.name]
141142

tests-v3/test_fieldset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Variable,
1414
)
1515
from parcels.field import VectorField
16-
from parcels.tools.converters import GeographicPolar, UnitConverter
16+
from parcels.tools.converters import GeographicPolar, Unity
1717
from tests.utils import TEST_DATA
1818

1919

@@ -119,7 +119,7 @@ def test_field_from_netcdf_fieldtypes():
119119

120120
# first try without setting fieldtype
121121
fset = FieldSet.from_nemo(filenames, variables, dimensions)
122-
assert isinstance(fset.varU.units, UnitConverter)
122+
assert isinstance(fset.varU.units, Unity)
123123

124124
# now try with setting fieldtype
125125
fset = FieldSet.from_nemo(filenames, variables, dimensions, fieldtype={"varU": "U", "varV": "V"})

0 commit comments

Comments
 (0)