Skip to content

Commit eecb692

Browse files
Merge pull request #1924 from OceanParcels/remove_scaling_factors
Removing scaling_factors
2 parents ca89010 + 3314d61 commit eecb692

File tree

7 files changed

+6
-107
lines changed

7 files changed

+6
-107
lines changed

docs/examples/tutorial_unitconverters.ipynb

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -351,41 +351,6 @@
351351
")\n",
352352
"print(fieldset.Ustokes2[0, 0, 40, -5])"
353353
]
354-
},
355-
{
356-
"attachments": {},
357-
"cell_type": "markdown",
358-
"metadata": {},
359-
"source": [
360-
"## Using velocities in units other than m/s\n"
361-
]
362-
},
363-
{
364-
"attachments": {},
365-
"cell_type": "markdown",
366-
"metadata": {},
367-
"source": [
368-
"Some OGCM store velocity data in units of e.g. cm/s. For these cases, Field objects have a method `set_scaling_factor()`.\n",
369-
"\n",
370-
"If your data is in cm/s and if you want to use the built-in Advection kernels, you will therefore have to use `fieldset.U.set_scaling_factor(100)` and `fieldset.V.set_scaling_factor(100)`.\n"
371-
]
372-
},
373-
{
374-
"cell_type": "code",
375-
"execution_count": null,
376-
"metadata": {},
377-
"outputs": [],
378-
"source": [
379-
"fieldset.add_field(\n",
380-
" parcels.Field(\n",
381-
" name=\"Ucm\",\n",
382-
" data=0.01 * np.ones((ydim, xdim), dtype=np.float32),\n",
383-
" grid=fieldset.U.grid,\n",
384-
" )\n",
385-
")\n",
386-
"fieldset.Ucm.set_scaling_factor(100)\n",
387-
"print(fieldset.Ucm[0, 0, 40, -5])"
388-
]
389354
}
390355
],
391356
"metadata": {

parcels/field.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ def __init__(
219219
# Hack around the fact that NaN and ridiculously large values
220220
# propagate in SciPy's interpolators
221221
self.data[np.isnan(self.data)] = 0.0
222-
self._scaling_factor = None
223222

224223
self._dimensions = kwargs.pop("dimensions", None)
225224
self._dataFiles = kwargs.pop("dataFiles", None)
@@ -575,26 +574,6 @@ def _reshape(self, data):
575574

576575
return data
577576

578-
def set_scaling_factor(self, factor):
579-
"""Scales the field data by some constant factor.
580-
581-
Parameters
582-
----------
583-
factor :
584-
scaling factor
585-
586-
587-
Examples
588-
--------
589-
For usage examples see the following tutorial:
590-
591-
* `Unit converters <../examples/tutorial_unitconverters.ipynb>`__
592-
"""
593-
if self._scaling_factor:
594-
raise NotImplementedError(f"Scaling factor for field {self.name} already defined.")
595-
self._scaling_factor = factor
596-
self.data *= factor
597-
598577
def _search_indices(self, time, z, y, x, ti, particle=None, search2D=False):
599578
if self.grid._gtype in [GridType.RectilinearSGrid, GridType.RectilinearZGrid]:
600579
return _search_indices_rectilinear(self, time, z, y, x, ti, particle=particle, search2D=search2D)
@@ -761,8 +740,6 @@ def write(self, filename, varname=None):
761740

762741
def _rescale_and_set_minmax(self, data):
763742
data[np.isnan(data)] = 0
764-
if self._scaling_factor:
765-
data *= self._scaling_factor
766743
return data
767744

768745
def ravel_index(self, zi, yi, xi):

parcels/fieldset.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,6 @@ def from_nemo(
497497
gridindexingtype="nemo",
498498
**kwargs,
499499
)
500-
if hasattr(fieldset, "W"):
501-
fieldset.W.set_scaling_factor(-1.0)
502500
return fieldset
503501

504502
@classmethod
@@ -814,8 +812,6 @@ def from_mom5(
814812
gridindexingtype="mom5",
815813
**kwargs,
816814
)
817-
if hasattr(fieldset, "W"):
818-
fieldset.W.set_scaling_factor(-1)
819815
return fieldset
820816

821817
@classmethod
@@ -905,7 +901,7 @@ def from_b_grid_dataset(
905901
Default is False if dimensions includes time, else True
906902
tracer_interp_method : str
907903
Method for interpolation of tracer fields. It is recommended to use 'bgrid_tracer' (default)
908-
Note that in the case of from_pop() and from_b_grid_dataset(), the velocity fields are default to 'bgrid_velocity'
904+
Note that in the case of from_b_grid_dataset(), the velocity fields are default to 'bgrid_velocity'
909905
**kwargs :
910906
Keyword arguments passed to the :func:`Fieldset.from_netcdf` constructor.
911907
"""

parcels/grid.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ def lat(self):
8888
def depth(self):
8989
return self._depth
9090

91-
def negate_depth(self):
92-
"""Method to flip the sign of the depth dimension of a Grid.
93-
Note that this method does _not_ change the direction of the vertical velocity;
94-
for that users need to add a fieldset.W.set_scaling_factor(-1.0)
95-
"""
96-
self._depth = -self._depth
97-
9891
@property
9992
def mesh(self):
10093
return self._mesh

parcels/kernel.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
AdvectionRK4_3D_CROCO,
1717
AdvectionRK45,
1818
)
19-
from parcels.field import Field, VectorField
19+
from parcels.field import VectorField
2020
from parcels.grid import GridType
2121
from parcels.tools.statuscodes import (
2222
StatusCode,
@@ -207,23 +207,7 @@ def check_fieldsets_in_kernels(self, pyfunc): # TODO v4: this can go into anoth
207207
This function is to be called from the derived class when setting up the 'pyfunc'.
208208
"""
209209
if self.fieldset is not None:
210-
if pyfunc is AdvectionRK4_3D:
211-
warning = False
212-
if (
213-
isinstance(self._fieldset.W, Field)
214-
and self._fieldset.W._creation_log != "from_nemo"
215-
and self._fieldset.W._scaling_factor is not None
216-
and self._fieldset.W._scaling_factor > 0
217-
):
218-
warning = True
219-
if warning:
220-
warnings.warn(
221-
"Note that in AdvectionRK4_3D, vertical velocity is assumed positive towards increasing z. "
222-
"If z increases downward and w is positive upward you can re-orient it downwards by setting fieldset.W.set_scaling_factor(-1.)",
223-
KernelWarning,
224-
stacklevel=2,
225-
)
226-
elif pyfunc is AdvectionAnalytical:
210+
if pyfunc is AdvectionAnalytical:
227211
if self.fieldset.particlefile is not None:
228212
self.fieldset.particlefile._is_analytical = True
229213
if self._fieldset.U.interp_method != "cgrid_velocity":

tests/test_grids.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ def test_time_format_in_grid():
111111
RectilinearZGrid(lon, lat, time=time)
112112

113113

114+
@pytest.mark.v4remove
115+
@pytest.mark.xfail(reason="negate_depth removed in v4")
114116
def test_negate_depth():
115117
depth = np.linspace(0, 5, 10, dtype=np.float32)
116118
fieldset = FieldSet.from_data(
@@ -975,4 +977,4 @@ def VelocityInterpolator(particle, fieldset, time): # pragma: no cover
975977
if extrapolation:
976978
assert np.allclose(pset.Wvel[0], 0, atol=1e-9)
977979
else:
978-
assert np.allclose(pset.Wvel[0], -w * convfactor)
980+
assert np.allclose(pset.Wvel[0], w * convfactor)

tests/tools/test_warnings.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import warnings
2-
31
import numpy as np
42
import pytest
53

64
from parcels import (
75
AdvectionRK4,
8-
AdvectionRK4_3D,
96
AdvectionRK45,
107
FieldSet,
118
FieldSetWarning,
@@ -51,22 +48,7 @@ def test_file_warnings(tmp_zarrfile):
5148
pset.execute(AdvectionRK4, runtime=3, dt=1, output_file=pfile)
5249

5350

54-
@pytest.mark.v4alpha
55-
@pytest.mark.xfail(reason="https://github.com/OceanParcels/Parcels/pull/1908#issuecomment-2698014941")
5651
def test_kernel_warnings():
57-
# positive scaling factor for W
58-
filenames = str(TEST_DATA / "POPtestdata_time.nc")
59-
variables = {"U": "U", "V": "V", "W": "W", "T": "T"}
60-
dimensions = {"lon": "lon", "lat": "lat", "depth": "w_deps", "time": "time"}
61-
with warnings.catch_warnings():
62-
# ignore FieldSetWarnings (tested in test_fieldset_warnings)
63-
warnings.simplefilter("ignore", FieldSetWarning)
64-
fieldset = FieldSet.from_pop(filenames, variables, dimensions, mesh="flat")
65-
fieldset.W._scaling_factor = 0.01
66-
pset = ParticleSet(fieldset=fieldset, pclass=Particle, lon=[0], lat=[0], depth=[0], time=[0])
67-
with pytest.warns(KernelWarning):
68-
pset.execute(AdvectionRK4_3D, runtime=1, dt=1)
69-
7052
# RK45 warnings
7153
lat = [0, 1, 5, 10]
7254
lon = [0, 1, 5, 10]

0 commit comments

Comments
 (0)