|
1 | 1 | from datetime import datetime |
2 | 2 |
|
| 3 | +import cf_units |
3 | 4 | import numpy as np |
| 5 | +import pytest |
4 | 6 |
|
| 7 | +import iris.common.mixin |
5 | 8 | from iris.common.mixin import CfpintUnit |
6 | 9 |
|
7 | 10 |
|
@@ -32,3 +35,87 @@ def test_calendar(): |
32 | 35 | # .. but it is cf_units behaviour + currently required for correct netcdf saving |
33 | 36 | # it also means that calendar is not checked in unit/string eq (!!!) |
34 | 37 | assert str(unit) == "days since 1970-01-01" |
| 38 | + |
| 39 | + |
| 40 | +_UNKNOWN_NAMES = iris.common.mixin.CfpintUnit._IRIS_EXTRA_CATEGORIES["unknown"] |
| 41 | +_NOUNIT_NAMES = iris.common.mixin.CfpintUnit._IRIS_EXTRA_CATEGORIES["no_unit"] |
| 42 | + |
| 43 | + |
| 44 | +class TestFromUnit: |
| 45 | + """Test CfpintUnit creation from various sources.""" |
| 46 | + |
| 47 | + def test_none_unknown(self): |
| 48 | + unit = CfpintUnit.from_unit(None) |
| 49 | + assert unit.category == "unknown" |
| 50 | + assert unit.calendar is None |
| 51 | + assert unit == "unknown" |
| 52 | + |
| 53 | + @pytest.mark.parametrize("name", _UNKNOWN_NAMES) |
| 54 | + def test_str_unknown(self, name): |
| 55 | + unit = CfpintUnit.from_unit(None) |
| 56 | + assert unit.category == "unknown" |
| 57 | + assert unit.calendar is None |
| 58 | + assert all(unit == form for form in _UNKNOWN_NAMES) # string equivalence |
| 59 | + |
| 60 | + def test_cfunits_unknown(self): |
| 61 | + cfunit = cf_units.Unit(None) |
| 62 | + unit = CfpintUnit.from_unit(None) |
| 63 | + assert unit.is_unknown() |
| 64 | + |
| 65 | + @pytest.mark.parametrize("name", _NOUNIT_NAMES) |
| 66 | + def test_str_nounit(self, name): |
| 67 | + unit = CfpintUnit.from_unit(name) |
| 68 | + assert unit.category == "no_unit" |
| 69 | + assert unit.calendar is None |
| 70 | + assert all(unit == form for form in _NOUNIT_NAMES) # string equivalence |
| 71 | + |
| 72 | + def test_cfunits_nounit(self): |
| 73 | + cfunit = cf_units.Unit("no_unit") |
| 74 | + unit = CfpintUnit.from_unit(cfunit) |
| 75 | + assert unit.is_no_unit() |
| 76 | + |
| 77 | + def test_str(self): |
| 78 | + unit = CfpintUnit.from_unit("m") |
| 79 | + assert unit == "metres" # string equivalence |
| 80 | + assert unit.calendar is None |
| 81 | + assert unit.category == "regular" |
| 82 | + |
| 83 | + def test_cfunits(self): |
| 84 | + cfunit = cf_units.Unit("m") |
| 85 | + unit = CfpintUnit.from_unit(cfunit) |
| 86 | + assert unit == "metre" |
| 87 | + |
| 88 | + def test_str_date(self): |
| 89 | + unit = CfpintUnit.from_unit("days since 1970-01-01") |
| 90 | + assert unit == "days since 1970-01-01" |
| 91 | + assert unit.category == "regular" |
| 92 | + assert unit.is_datelike() |
| 93 | + assert unit.calendar == "standard" |
| 94 | + |
| 95 | + @pytest.mark.skip("from_unit does not support calendar (yet?)") |
| 96 | + def test_str_date_calendar(self): |
| 97 | + unit = CfpintUnit.from_unit("days since 1970-01-01", calendar="360_day") |
| 98 | + # YUCK!! cf_units compatibility |
| 99 | + # TODO: this needs to change |
| 100 | + assert unit == "days since 1970-01-01" |
| 101 | + assert unit.category == "regular" |
| 102 | + assert unit.is_datelike() |
| 103 | + assert unit.calendar == "360_day" |
| 104 | + |
| 105 | + def test_cfunits_date(self): |
| 106 | + cfunit = cf_units.Unit("hours since 1800-03-09 11:11") |
| 107 | + unit = CfpintUnit.from_unit(cfunit) |
| 108 | + # NB time ref is reproduced as-is |
| 109 | + # TODO: should get normalised |
| 110 | + assert unit == "hours since 1800-03-09 11:11" |
| 111 | + assert unit.is_datelike() |
| 112 | + assert unit.calendar == "standard" |
| 113 | + |
| 114 | + def test_cfunits_date_calendar(self): |
| 115 | + cfunit = cf_units.Unit("hours since 1800-03-09 11:11", calendar="365_day") |
| 116 | + unit = CfpintUnit.from_unit(cfunit) |
| 117 | + # NB time ref is reproduced as-is |
| 118 | + # TODO: should get normalised |
| 119 | + assert unit == "hours since 1800-03-09 11:11" |
| 120 | + assert unit.is_datelike() |
| 121 | + assert unit.calendar == "365_day" |
0 commit comments