Skip to content

Commit

Permalink
Changes from old class test in all changed test files
Browse files Browse the repository at this point in the history
  • Loading branch information
karen-garciaperdomo committed Nov 28, 2024
1 parent 15495c1 commit 88f557d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 181 deletions.
75 changes: 2 additions & 73 deletions esmvalcore/cmor/_fixes/cmip6/cesm2_waccm_fv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .cesm2 import Fgco2 as BaseFgco2
from .cesm2 import Omon as BaseOmon
from .cesm2 import Tas as BaseTas
from .cesm2 import Pr as BasePr
from .cesm2_waccm import Cl as BaseCl
from .cesm2_waccm import Cli as BaseCli
from .cesm2_waccm import Clw as BaseClw
Expand All @@ -33,76 +34,4 @@
Tas = BaseTas


class Pr(Fix):
"""Fixes for pr."""

def fix_metadata(self, cubes):
"""Fix time coordinates.
Parameters
----------
cubes : iris.cube.CubeList
Cubes to fix
Returns
-------
iris.cube.CubeList
"""
new_list = iris.cube.CubeList()
for cube in cubes:
try:
old_time = cube.coord("time")
except iris.exceptions.CoordinateNotFoundError:
new_list.append(cube)
else:
if old_time.is_monotonic():
new_list.append(cube)
else:
time_units = old_time.units
time_data = old_time.points

# erase erroneously copy-pasted points
time_diff = np.diff(time_data)
idx_neg = np.where(time_diff <= 0.0)[0]
while len(idx_neg) > 0:
time_data = np.delete(time_data, idx_neg[0] + 1)
time_diff = np.diff(time_data)
idx_neg = np.where(time_diff <= 0.0)[0]

# create the new time coord
new_time = iris.coords.DimCoord(
time_data,
standard_name="time",
var_name="time",
units=time_units,
)

# create a new cube with the right shape
dims = (
time_data.shape[0],
cube.coord("latitude").shape[0],
cube.coord("longitude").shape[0],
)
data = cube.data
new_data = np.ma.append(
data[: dims[0] - 1, :, :], data[-1, :, :]
)
new_data = new_data.reshape(dims)

tmp_cube = iris.cube.Cube(
new_data,
standard_name=cube.standard_name,
long_name=cube.long_name,
var_name=cube.var_name,
units=cube.units,
attributes=cube.attributes,
cell_methods=cube.cell_methods,
dim_coords_and_dims=[
(new_time, 0),
(cube.coord("latitude"), 1),
(cube.coord("longitude"), 2),
],
)

new_list.append(tmp_cube)
return new_list
Pr = BasePr
2 changes: 1 addition & 1 deletion tests/integration/cmor/_fixes/cmip6/test_cesm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def pr_cubes():
)

lon_coord = iris.coords.DimCoord(
points=[0.0], var_name="lon", standard_name="longitude"
[0.0], var_name="lon", standard_name="longitude"
)

correct_coord_specs = [
Expand Down
16 changes: 15 additions & 1 deletion tests/integration/cmor/_fixes/cmip6/test_cesm2_fv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from esmvalcore.cmor._fixes.cmip6.cesm2 import Cl as BaseCl
from esmvalcore.cmor._fixes.cmip6.cesm2 import Fgco2 as BaseFgco2
from esmvalcore.cmor._fixes.cmip6.cesm2 import Tas as BaseTas
from esmvalcore.cmor._fixes.cmip6.cesm2 import Pr as BasePr
from esmvalcore.cmor._fixes.cmip6.cesm2_fv2 import (
Cl,
Cli,
Expand All @@ -11,6 +12,7 @@
Omon,
Siconc,
Tas,
Pr
)
from esmvalcore.cmor._fixes.common import SiconcFixScalarCoord
from esmvalcore.cmor._fixes.fix import GenericFix
Expand Down Expand Up @@ -76,8 +78,20 @@ def test_get_tas_fix():
"""Test getting of fix."""
fix = Fix.get_fixes("CMIP6", "CESM2-FV2", "Amon", "tas")
assert fix == [Tas(None), GenericFix(None)]

fix = Fix.get_fixes("CMIP6", "CESM2-FV2", "day", "tas")
assert fix == [Tas(None), GenericFix(None)]

def test_tas_fix():
"""Test fix for ``tas``."""
assert Tas is BaseTas


def test_get_pr_fix():
"""Test getting of fix."""
fix = Fix.get_fixes("CMIP6", "CESM2-FV2", "day", "pr")
assert fix == [Pr(None), GenericFix(None)]


def test_pr_fix():
"""Test fix for ``Pr``."""
assert Pr is BasePr
68 changes: 28 additions & 40 deletions tests/integration/cmor/_fixes/cmip6/test_cesm2_waccm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from esmvalcore.cmor._fixes.cmip6.cesm2 import Cl as BaseCl
from esmvalcore.cmor._fixes.cmip6.cesm2 import Fgco2 as BaseFgco2
from esmvalcore.cmor._fixes.cmip6.cesm2 import Tas as BaseTas
from esmvalcore.cmor._fixes.cmip6.cesm2 import Pr as BasePr
from esmvalcore.cmor._fixes.cmip6.cesm2 import Tasmin as BaseTasmin
from esmvalcore.cmor._fixes.cmip6.cesm2 import Tasmax as BaseTasmax
from esmvalcore.cmor._fixes.cmip6.cesm2_waccm import (
Cl,
Cli,
Expand All @@ -20,6 +23,8 @@
Siconc,
Tas,
Pr,
Tasmin,
Tasmax,
)
from esmvalcore.cmor._fixes.common import SiconcFixScalarCoord
from esmvalcore.cmor._fixes.fix import GenericFix
Expand Down Expand Up @@ -122,60 +127,43 @@ def test_get_tas_fix():
"""Test getting of fix."""
fix = Fix.get_fixes("CMIP6", "CESM2-WACCM", "Amon", "tas")
assert fix == [Tas(None), GenericFix(None)]
fix = Fix.get_fixes("CMIP6", "CESM2-WACCM", "day", "tas")
assert fix == [Tas(None), GenericFix(None)]


def test_tas_fix():
"""Test fix for ``tas``."""
assert Tas is BaseTas


@pytest.fixture
def pr_cubes():
correct_time_coord = iris.coords.DimCoord(
points=[1.0, 2.0, 3.0, 4.0, 5.0],
var_name="time",
standard_name="time",
units="days since 1850-01-01",
)
def test_get_pr_fix():
"""Test getting of fix."""
fix = Fix.get_fixes("CMIP6", "CESM2-WACCM", "day", "pr")
assert fix == [Pr(None), GenericFix(None)]

lat_coord = iris.coords.DimCoord(
[0.0], var_name="lat", standard_name="latitude"
)

lon_coord = iris.coords.DimCoord(
points=[0.0], var_name="lon", standard_name="longitude"
)
def test_pr_fix():
"""Test fix for ``Pr``."""
assert Pr is BasePr

correct_coord_specs = [
(correct_time_coord, 0),
(lat_coord, 1),
(lon_coord, 2),
]

correct_pr_cube = iris.cube.Cube(
np.ones((5, 1, 1)),
var_name="pr",
units="kg m-2 s-1",
dim_coords_and_dims=correct_coord_specs,
)

scalar_cube = iris.cube.Cube(0.0, var_name="ps")
def test_get_tasmin_fix():
"""Test getting of fix."""
fix = Fix.get_fixes("CMIP6", "CESM2-WACCM", "day", "tasmin")
assert fix == [Tasmin(None), GenericFix(None)]

return iris.cube.CubeList([correct_pr_cube, scalar_cube])

def test_tasmin_fix():
"""Test fix for ``Tasmin``."""
assert Tasmin is BaseTasmin

def test_get_pr_fix():
"""Test pr fix."""
fix = Fix.get_fixes("CMIP6", "CESM2", "day", "pr")
assert fix == [Pr(None), GenericFix(None)]

def test_get_tasmax_fix():
"""Test getting of fix."""
fix = Fix.get_fixes("CMIP6", "CESM2-WACCM", "day", "tasmax")
assert fix == [Tasmax(None), GenericFix(None)]

def test_pr_fix_metadata(pr_cubes):
"""Test metadata fix."""
vardef = get_var_info("CMIP6", "day", "pr")
fix = Pr(vardef)

out_cubes = fix.fix_metadata(pr_cubes)
assert out_cubes[0].var_name == "pr"
coord = out_cubes[0].coord("time")
assert pd.Series(coord.points).is_monotonic_increasing
def test_tasmax_fix():
"""Test fix for ``Tasmax``."""
assert Tasmax is BaseTasmax
74 changes: 8 additions & 66 deletions tests/integration/cmor/_fixes/cmip6/test_cesm2_waccm_fv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from esmvalcore.cmor._fixes.cmip6.cesm2 import Fgco2 as BaseFgco2
from esmvalcore.cmor._fixes.cmip6.cesm2 import Tas as BaseTas
from esmvalcore.cmor._fixes.cmip6.cesm2 import Pr as BasePr
from esmvalcore.cmor._fixes.cmip6.cesm2_waccm import Cl as BaseCl
from esmvalcore.cmor._fixes.cmip6.cesm2_waccm_fv2 import (
Cl,
Expand Down Expand Up @@ -83,80 +84,21 @@ def test_get_tas_fix():
"""Test getting of fix."""
fix = Fix.get_fixes("CMIP6", "CESM2-WACCM-FV2", "Amon", "tas")
assert fix == [Tas(None), GenericFix(None)]
fix = Fix.get_fixes("CMIP6", "CESM2-WACCM-FV2", "day", "tas")
assert fix == [Tas(None), GenericFix(None)]


def test_tas_fix():
"""Test fix for ``tas``."""
assert Tas is BaseTas


@pytest.fixture
def pr_cubes():
wrong_time_coord = iris.coords.AuxCoord(
points=[1.0, 2.0, 1.0, 2.0, 3.0],
var_name="time",
standard_name="time",
units="days since 1850-01-01",
)

correct_time_coord = iris.coords.AuxCoord(
points=[1.0, 2.0, 3.0],
var_name="time",
standard_name="time",
units="days since 1850-01-01",
)

correct_lat_coord = iris.coords.DimCoord(
[0.0], var_name="lat", standard_name="latitude"
)
wrong_lat_coord = iris.coords.DimCoord(
[0.0], var_name="latitudeCoord", standard_name="latitude"
)
correct_lon_coord = iris.coords.DimCoord(
[0.0], var_name="lon", standard_name="longitude"
)
wrong_lon_coord = iris.coords.DimCoord(
[0.0], var_name="longitudeCoord", standard_name="longitude"
)

wrong_coord_specs = [
(wrong_time_coord, 0),
(wrong_lat_coord, 1),
(wrong_lon_coord, 2),
]

correct_coord_specs = [
(correct_time_coord, 0),
(correct_lat_coord, 1),
(correct_lon_coord, 2),
]
correct_pr_cube = iris.cube.Cube(
np.ones((2, 2, 2)),
var_name="pr",
dim_coords_and_dims=correct_coord_specs,
)

wrong_pr_cube = iris.cube.Cube(
np.ones((2, 2, 2)),
var_name="ta",
dim_coords_and_dims=wrong_coord_specs,
)

return iris.cube.CubeList([correct_pr_cube, wrong_pr_cube])


def test_get_pr_fix():
"""Test pr fix."""
fix = Fix.get_fixes("CMIP6", "CESM2", "day", "pr")
"""Test getting of fix."""
fix = Fix.get_fixes("CMIP6", "CESM2-WACCM_FV2", "day", "pr")
assert fix == [Pr(None), GenericFix(None)]


def test_pr_fix_metadata(pr_cubes):
"""Test metadata fix."""
vardef = get_var_info("CMIP6", "day", "pr")
fix = Pr(vardef)

out_cubes = fix.fix_metadata(pr_cubes)
assert out_cubes[0].var_name == "pr"
coord = out_cubes[0].coord("time")
assert pd.Series(coord.points).is_monotonic_increasing
def test_pr_fix():
"""Test fix for ``Pr``."""
assert Pr is BasePr

0 comments on commit 88f557d

Please sign in to comment.