-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding sfcWind derivation from uas and vas (#2242)
Co-authored-by: Elizaveta Malinina <[email protected]> Co-authored-by: Valeriu Predoi <[email protected]> Co-authored-by: Bouwe Andela <[email protected]>
- Loading branch information
1 parent
265d2f4
commit bb7866e
Showing
4 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Derivation of variable `sfcWind`.""" | ||
|
||
from iris import NameConstraint | ||
|
||
from ._baseclass import DerivedVariableBase | ||
|
||
|
||
class DerivedVariable(DerivedVariableBase): | ||
"""Derivation of variable `sfcWind`.""" | ||
|
||
@staticmethod | ||
def required(project): | ||
"""Declare the variables needed for derivation.""" | ||
required = [ | ||
{ | ||
'short_name': 'uas' | ||
}, | ||
{ | ||
'short_name': 'vas' | ||
}, | ||
] | ||
return required | ||
|
||
@staticmethod | ||
def calculate(cubes): | ||
"""Compute near-surface wind speed. | ||
Wind speed derived from eastward and northward components. | ||
""" | ||
uas_cube = cubes.extract_cube(NameConstraint(var_name='uas')) | ||
vas_cube = cubes.extract_cube(NameConstraint(var_name='vas')) | ||
|
||
sfcwind_cube = (uas_cube**2 + vas_cube**2)**0.5 | ||
|
||
return sfcwind_cube |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Test derivation of ``sfcwind``.""" | ||
import numpy as np | ||
import pytest | ||
from iris.cube import CubeList | ||
|
||
from esmvalcore.preprocessor._derive import sfcwind | ||
|
||
from .test_shared import get_cube | ||
|
||
|
||
@pytest.fixture | ||
def cubes(): | ||
"""Input cubes for derivation of ``sfcwind``.""" | ||
uas_cube = get_cube([[[3.0]]], | ||
air_pressure_coord=False, | ||
standard_name='eastward_wind', | ||
var_name='uas', | ||
units='m s-1') | ||
vas_cube = get_cube([[[4.0]]], | ||
air_pressure_coord=False, | ||
standard_name='northward_wind', | ||
var_name='vas', | ||
units='m s-1') | ||
return CubeList([uas_cube, vas_cube]) | ||
|
||
|
||
def test_sfcwind_calculate(cubes): | ||
"""Test function ``calculate``.""" | ||
derived_var = sfcwind.DerivedVariable() | ||
required_vars = derived_var.required("CMIP5") | ||
expected_required_vars = [ | ||
{ | ||
'short_name': 'uas' | ||
}, | ||
{ | ||
'short_name': 'vas' | ||
}, | ||
] | ||
assert required_vars == expected_required_vars | ||
out_cube = derived_var.calculate(cubes) | ||
assert out_cube.shape == (1, 1, 1) | ||
assert out_cube.units == 'm s-1' | ||
assert out_cube.coords('time') | ||
assert out_cube.coords('latitude') | ||
assert out_cube.coords('longitude') | ||
np.testing.assert_allclose(out_cube.data, [[[5.0]]]) | ||
np.testing.assert_allclose(out_cube.coord('time').points, [0.0]) | ||
np.testing.assert_allclose(out_cube.coord('latitude').points, [45.0]) | ||
np.testing.assert_allclose(out_cube.coord('longitude').points, [10.0]) |