Skip to content

Commit

Permalink
Unpin cf-units (#1770)
Browse files Browse the repository at this point in the history
Co-authored-by: Klaus Zimmermann <[email protected]>
  • Loading branch information
bouweandela and Klaus Zimmermann authored Nov 18, 2022
1 parent f1707f8 commit a6f8c08
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 120 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ channels:

dependencies:
- cartopy
- cf-units>=3.0.0,<3.1.0 # github.com/ESMValGroup/ESMValCore/issues/1655
- cf-units
- cftime
- dask
- compilers
Expand Down
2 changes: 1 addition & 1 deletion esmvalcore/cmor/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ def _simplify_calendar(calendar):
calendar_aliases = {
'all_leap': '366_day',
'noleap': '365_day',
'standard': 'gregorian',
'gregorian': 'standard',
}
return calendar_aliases.get(calendar, calendar)

Expand Down
2 changes: 1 addition & 1 deletion esmvalcore/preprocessor/_multimodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _unify_time_coordinates(cubes):
are no mismatches in the time arrays.
If cubes have different time units, it will reset the calendar to a
default gregorian calendar with unit "days since 1850-01-01".
the "standard" calendar with unit "days since 1850-01-01".
Might not work for (sub)daily data, because different calendars may have
different number of days in the year.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
'install': [
'cartopy',
# see https://github.com/SciTools/cf-units/issues/218
'cf-units>=3.0.0,<3.1.0,!=3.0.1.post0', # ESMValCore/issues/1655
'cf-units',
'dask[array]',
'esgf-pyclient>=0.3.1',
'esmpy!=8.1.0', # see github.com/ESMValGroup/ESMValCore/issues/1208
Expand Down
69 changes: 39 additions & 30 deletions tests/integration/cmor/_fixes/cmip5/test_access1_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,54 @@
from esmvalcore.iris_helpers import date2num


class TestAllVars(unittest.TestCase):
"""Test all vars fixes."""
@pytest.fixture
def cube():
"""Cube for testing."""
test_cube = Cube([1.0, 2.0], var_name='co2', units='J')
reference_dates = [
datetime(300, 1, 16, 12), # e.g. piControl
datetime(1850, 1, 16, 12) # e.g. historical
]
esgf_time_units = Unit(
'days since 0001-01-01',
calendar='proleptic_gregorian',
)
time_points = date2num(reference_dates, esgf_time_units)
test_cube.add_dim_coord(
DimCoord(time_points, 'time', 'time', 'time', esgf_time_units),
data_dim=0,
)
return test_cube


def setUp(self):
"""Prepare tests."""
self.cube = Cube([1.0, 2.0], var_name='co2', units='J')
reference_dates = [
datetime(300, 1, 16, 12), # e.g. piControl
datetime(1850, 1, 16, 12) # e.g. historical
]
esgf_time_units = Unit('days since 0001-01-01',
calendar='proleptic_gregorian')
time_points = date2num(reference_dates, esgf_time_units)
self.cube.add_dim_coord(
DimCoord(time_points, 'time', 'time', 'time', esgf_time_units),
data_dim=0)
self.fix = AllVars(None)
class TestAllVars:
"""Test all vars fixes."""

def test_get(self):
@staticmethod
def test_get():
"""Test getting of fix."""
self.assertListEqual(
Fix.get_fixes('CMIP5', 'ACCESS1-0', 'Amon', 'tas'),
[AllVars(None)])
assert (Fix.get_fixes('CMIP5', 'ACCESS1-0', 'Amon', 'tas')
== [AllVars(None)])

def test_fix_metadata(self):
@staticmethod
def test_fix_metadata(cube):
"""Test fix for bad calendar."""
cube = self.fix.fix_metadata([self.cube])[0]
fix = AllVars(None)
cube = fix.fix_metadata([cube])[0]
time = cube.coord('time')
dates = num2date(time.points, time.units.name, time.units.calendar)
self.assertEqual(time.units.calendar, 'gregorian')
u = Unit('days since 300-01-01 12:00:00', calendar='gregorian')
self.assertEqual(dates[0], u.num2date(15))
u = Unit('days since 1850-01-01 12:00:00', calendar='gregorian')
self.assertEqual(dates[1], u.num2date(15))
assert time.units.calendar in ('standard', 'gregorian')
u = Unit('days since 300-01-01 12:00:00', calendar='standard')
assert dates[0] == u.num2date(15)
u = Unit('days since 1850-01-01 12:00:00', calendar='standard')
assert dates[1] == u.num2date(15)

def test_fix_metadata_if_not_time(self):
@staticmethod
def test_fix_metadata_if_not_time(cube):
"""Test calendar fix do not fail if no time coord present."""
self.cube.remove_coord('time')
self.fix.fix_metadata([self.cube])
cube.remove_coord('time')
fix = AllVars(None)
fix.fix_metadata([cube])


def test_get_cl_fix():
Expand Down
71 changes: 40 additions & 31 deletions tests/integration/cmor/_fixes/cmip5/test_access1_3.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test fixes for ACCESS1-3."""
import unittest
from datetime import datetime

import pytest
from cf_units import Unit, num2date
from iris.coords import DimCoord
from iris.cube import Cube
Expand All @@ -12,45 +12,54 @@
from esmvalcore.iris_helpers import date2num


class TestAllVars(unittest.TestCase):
"""Test fixes for all vars."""
@pytest.fixture
def cube():
"""Cube for testing."""
test_cube = Cube([1.0, 2.0], var_name='co2', units='J')
reference_dates = [
datetime(300, 1, 16, 12), # e.g. piControl
datetime(1850, 1, 16, 12) # e.g. historical
]
esgf_time_units = Unit(
'days since 0001-01-01',
calendar='proleptic_gregorian',
)
time_points = date2num(reference_dates, esgf_time_units)
test_cube.add_dim_coord(
DimCoord(time_points, 'time', 'time', 'time', esgf_time_units),
data_dim=0,
)
return test_cube


def setUp(self):
"""Prepare tests."""
self.cube = Cube([1.0, 2.0], var_name='co2', units='J')
reference_dates = [
datetime(300, 1, 16, 12), # e.g. piControl
datetime(1850, 1, 16, 12) # e.g. historical
]
esgf_time_units = Unit('days since 0001-01-01',
calendar='proleptic_gregorian')
time_points = date2num(reference_dates, esgf_time_units)
self.cube.add_dim_coord(
DimCoord(time_points, 'time', 'time', 'time', esgf_time_units),
data_dim=0)
self.fix = AllVars(None)
class TestAllVars:
"""Test fixes for all vars."""

def test_get(self):
@staticmethod
def test_get():
"""Test getting of fix."""
self.assertListEqual(
Fix.get_fixes('CMIP5', 'ACCESS1-3', 'Amon', 'tas'),
[AllVars(None)])
assert (Fix.get_fixes('CMIP5', 'ACCESS1-3', 'Amon', 'tas')
== [AllVars(None)])

def test_fix_metadata(self):
@staticmethod
def test_fix_metadata(cube):
"""Test fix for bad calendar."""
cube = self.fix.fix_metadata([self.cube])[0]
fix = AllVars(None)
cube = fix.fix_metadata([cube])[0]
time = cube.coord('time')
dates = num2date(time.points, time.units.name, time.units.calendar)
self.assertEqual(time.units.calendar, 'gregorian')
u = Unit('days since 300-01-01 12:00:00', calendar='gregorian')
self.assertEqual(dates[0], u.num2date(15))
u = Unit('days since 1850-01-01 12:00:00', calendar='gregorian')
self.assertEqual(dates[1], u.num2date(15))
assert time.units.calendar in ('standard', 'gregorian')
u = Unit('days since 300-01-01 12:00:00', calendar='standard')
assert dates[0] == u.num2date(15)
u = Unit('days since 1850-01-01 12:00:00', calendar='standard')
assert dates[1] == u.num2date(15)

def test_fix_metadata_if_not_time(self):
@staticmethod
def test_fix_metadata_if_not_time(cube):
"""Test calendar fix do not fail if no time coord present."""
self.cube.remove_coord('time')
self.fix.fix_metadata([self.cube])
cube.remove_coord('time')
fix = AllVars(None)
fix.fix_metadata([cube])


def test_get_cl_fix():
Expand Down
76 changes: 41 additions & 35 deletions tests/integration/cmor/_fixes/cmip5/test_fgoals_g2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test FGOALS-g2 fixes."""
import unittest

import pytest
from cf_units import Unit
from iris.coords import DimCoord
from iris.cube import Cube
Expand All @@ -9,47 +8,54 @@
from esmvalcore.cmor.fix import Fix


class TestAll(unittest.TestCase):
@pytest.fixture
def cube():
"""Cube for testing."""
test_cube = Cube([[1.0, 2.0]], var_name='co2', units='J')
test_cube.add_dim_coord(
DimCoord(
[0.0, 1.0],
standard_name='time',
units=Unit('days since 0001-01', calendar='gregorian')),
1)
test_cube.add_dim_coord(
DimCoord(
[180],
standard_name='longitude',
units=Unit('degrees')),
0)
return test_cube


class TestAll:
"""Test fixes for all vars."""

def setUp(self):
"""Prepare tests."""
self.cube = Cube([[1.0, 2.0]], var_name='co2', units='J')
self.cube.add_dim_coord(
DimCoord(
[0.0, 1.0],
standard_name='time',
units=Unit('days since 0001-01', calendar='gregorian')),
1)
self.cube.add_dim_coord(
DimCoord(
[180],
standard_name='longitude',
units=Unit('degrees')),
0)
self.fix = AllVars(None)

def test_get(self):
@staticmethod
def test_get():
"""Test fix get."""
self.assertListEqual(
Fix.get_fixes('CMIP5', 'FGOALS-G2', 'Amon', 'tas'),
[AllVars(None)])
assert (Fix.get_fixes('CMIP5', 'FGOALS-G2', 'Amon', 'tas')
== [AllVars(None)])

def test_fix_metadata(self):
@staticmethod
def test_fix_metadata(cube):
"""Test calendar fix."""
cube = self.fix.fix_metadata([self.cube])[0]
fix = AllVars(None)
cube = fix.fix_metadata([cube])[0]

time = cube.coord('time')
self.assertEqual(time.units.origin,
'day since 1-01-01 00:00:00.000000')
self.assertEqual(time.units.calendar, 'gregorian')
assert time.units.origin == 'day since 1-01-01 00:00:00.000000'
assert time.units.calendar in ('standard', 'gregorian')

def test_fix_metadata_dont_fail_if_not_longitude(self):
@staticmethod
def test_fix_metadata_dont_fail_if_not_longitude(cube):
"""Test calendar fix."""
self.cube.remove_coord('longitude')
self.fix.fix_metadata([self.cube])
cube.remove_coord('longitude')
fix = AllVars(None)
fix.fix_metadata([cube])

def test_fix_metadata_dont_fail_if_not_time(self):
@staticmethod
def test_fix_metadata_dont_fail_if_not_time(cube):
"""Test calendar fix."""
self.cube.remove_coord('time')
self.fix.fix_metadata([self.cube])
cube.remove_coord('time')
fix = AllVars(None)
fix.fix_metadata([cube])
3 changes: 2 additions & 1 deletion tests/sample_data/multimodel_statistics/test_multimodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path
from typing import Optional

import cf_units
import iris
import numpy as np
import pytest
Expand All @@ -24,7 +25,7 @@
marks=pytest.mark.skip(
reason='Cannot calculate statistics with single cube in list')),
'365_day',
'gregorian',
'standard' if cf_units.__version__ >= '3.1' else 'gregorian',
pytest.param(
'proleptic_gregorian',
marks=pytest.mark.xfail(
Expand Down
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit a6f8c08

Please sign in to comment.