Beispiel #1
0
def test_encode_time_bounds():

    time = pd.date_range("2000-01-16", periods=1)
    time_bounds = pd.date_range("2000-01-01", periods=2, freq="MS")
    ds = Dataset(dict(time=time, time_bounds=time_bounds))
    ds.time.attrs = {"bounds": "time_bounds"}
    ds.time.encoding = {"calendar": "noleap", "units": "days since 2000-01-01"}

    expected = {}
    # expected['time'] = Variable(data=np.array([15]), dims=['time'])
    expected["time_bounds"] = Variable(data=np.array([0, 31]),
                                       dims=["time_bounds"])

    encoded, _ = cf_encoder(ds.variables, ds.attrs)
    assert_equal(encoded["time_bounds"], expected["time_bounds"])
    assert "calendar" not in encoded["time_bounds"].attrs
    assert "units" not in encoded["time_bounds"].attrs

    # if time_bounds attrs are same as time attrs, it doesn't matter
    ds.time_bounds.encoding = {
        "calendar": "noleap",
        "units": "days since 2000-01-01"
    }
    encoded, _ = cf_encoder({k: ds[k] for k in ds.variables}, ds.attrs)
    assert_equal(encoded["time_bounds"], expected["time_bounds"])
    assert "calendar" not in encoded["time_bounds"].attrs
    assert "units" not in encoded["time_bounds"].attrs

    # for CF-noncompliant case of time_bounds attrs being different from
    # time attrs; preserve them for faithful roundtrip
    ds.time_bounds.encoding = {
        "calendar": "noleap",
        "units": "days since 1849-01-01"
    }
    encoded, _ = cf_encoder({k: ds[k] for k in ds.variables}, ds.attrs)
    with pytest.raises(AssertionError):
        assert_equal(encoded["time_bounds"], expected["time_bounds"])
    assert "calendar" not in encoded["time_bounds"].attrs
    assert encoded["time_bounds"].attrs["units"] == ds.time_bounds.encoding[
        "units"]

    ds.time.encoding = {}
    with pytest.warns(UserWarning):
        cf_encoder(ds.variables, ds.attrs)
Beispiel #2
0
def test_encode_time_bounds():

    time = pd.date_range('2000-01-16', periods=1)
    time_bounds = pd.date_range('2000-01-01', periods=2, freq='MS')
    ds = Dataset(dict(time=time, time_bounds=time_bounds))
    ds.time.attrs = {'bounds': 'time_bounds'}
    ds.time.encoding = {'calendar': 'noleap', 'units': 'days since 2000-01-01'}

    expected = dict()
    # expected['time'] = Variable(data=np.array([15]), dims=['time'])
    expected['time_bounds'] = Variable(data=np.array([0, 31]),
                                       dims=['time_bounds'])

    encoded, _ = cf_encoder(ds.variables, ds.attrs)
    assert_equal(encoded['time_bounds'], expected['time_bounds'])
    assert 'calendar' not in encoded['time_bounds'].attrs
    assert 'units' not in encoded['time_bounds'].attrs

    # if time_bounds attrs are same as time attrs, it doesn't matter
    ds.time_bounds.encoding = {
        'calendar': 'noleap',
        'units': 'days since 2000-01-01'
    }
    encoded, _ = cf_encoder({k: ds[k] for k in ds.variables}, ds.attrs)
    assert_equal(encoded['time_bounds'], expected['time_bounds'])
    assert 'calendar' not in encoded['time_bounds'].attrs
    assert 'units' not in encoded['time_bounds'].attrs

    # for CF-noncompliant case of time_bounds attrs being different from
    # time attrs; preserve them for faithful roundtrip
    ds.time_bounds.encoding = {
        'calendar': 'noleap',
        'units': 'days since 1849-01-01'
    }
    encoded, _ = cf_encoder({k: ds[k] for k in ds.variables}, ds.attrs)
    with pytest.raises(AssertionError):
        assert_equal(encoded['time_bounds'], expected['time_bounds'])
    assert 'calendar' not in encoded['time_bounds'].attrs
    assert encoded['time_bounds'].attrs['units'] == ds.time_bounds.encoding[
        'units']  # noqa

    ds.time.encoding = {}
    with pytest.warns(UserWarning):
        cf_encoder(ds.variables, ds.attrs)