Exemplo n.º 1
0
def test_unsupported_args(dset):
    with pytest.raises(ValueError):
        esmlab.resample(dset, freq='hr')

    with pytest.raises(ValueError):
        esmlab.climatology(dset, freq='hr')

    with pytest.raises(ValueError):
        esmlab.anomaly(dset, clim_freq='ann')
Exemplo n.º 2
0
def test_resample_mon_mean(dset):

    for method in {'left', 'right'}:
        ds = esmlab.resample(dset, freq='mon', method=method)
        assert len(ds.time) == 12

    computed_dset = esmlab.resample(dset, freq='mon')
    res = computed_dset.variable_1.data
    expected = np.full(shape=(12, 2, 2), fill_value=0.5, dtype=np.float32)
    np.testing.assert_allclose(res, expected)
Exemplo n.º 3
0
def test_resample_ann_mean(dset):
    weights = np.ones(24)
    computed_dset = esmlab.resample(dset, freq='ann', weights=weights)
    assert isinstance(computed_dset, xr.Dataset)
    assert computed_dset.time.dtype == dset.time.dtype
    expected = np.array([0.0, 0.0, 0.0, 0.0, 1, 1, 1, 1], dtype=np.float32)
    np.testing.assert_allclose(computed_dset.variable_1.values.ravel().astype(
        np.float32),
                               expected,
                               rtol=1e-6)

    computed_dset = esmlab.resample(dset, freq='ann', weights=None)
    assert isinstance(computed_dset, xr.Dataset)

    with pytest.raises(ValueError):
        esmlab.resample(dset, freq='ann', weights=[1, 2])
Exemplo n.º 4
0
def test_resample_ann_mean_values_missing(ds, weights):
    dset = open_dataset(ds, decode_times=False, decode_coords=False)
    dset.variable_2.data[0:3, :, :] = np.nan
    computed_dset = esmlab.resample(dset, freq='ann', weights=weights)
    assert isinstance(computed_dset, xr.Dataset)
    assert computed_dset.time.dtype == dset.time.dtype
    expected = np.array([1.0, 1.0, 1.0, 1.0, 0, 0, 0, 0], dtype=np.float32)
    np.testing.assert_allclose(computed_dset.variable_2.data.ravel().astype(
        np.float32),
                               expected,
                               rtol=1e-6)
Exemplo n.º 5
0
def compute_ann_mean(ds):
    """esmlab wrapper"""
    #     return esmlab.climatology.compute_ann_mean(ds)
    # ignore certain warnings
    with warnings.catch_warnings():
        warnings.filterwarnings(action="ignore", module=".*nanops")
        ds_out = resample(ds, freq="ann")

    # esmlab appears to corrupt xarray indexes wrt time
    # the following seems to reset them
    ds_out["time"] = ds_out["time"]

    # ensure time dim is first on time.bounds variable
    tb_name = ds_out.time.bounds
    if ds_out[tb_name].dims[0] != "time":
        ds_out[tb_name] = ds_out[tb_name].transpose()

    # reset time to midpoint
    ds_out = time_set_mid(ds_out, "time")

    # propagate particular encoding values
    for key in ["unlimited_dims"]:
        if key in ds.encoding:
            ds_out.encoding[key] = ds.encoding[key]

    # copy file attributes
    for key in ds.attrs:
        if key != "history":
            ds_out.attrs[key] = ds.attrs[key]

    # append to history file attribute if it already exists, otherwise set it
    key = "history"
    datestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S %Z")
    msg = (
        f"{datestamp}: created by esmlab.resample, with modifications from esmlab_wrap"
    )
    if key in ds.attrs:
        ds_out.attrs[key] = "\n".join([msg, ds.attrs[key]])
    else:
        ds_out.attrs[key] = msg

    return ds_out
Exemplo n.º 6
0
def test_datetime_cftime():
    ds = esmlab.datasets.open_dataset('mpi_som_ffn_gcb2018',
                                      decode_times=False)
    res = esmlab.resample(ds, freq='ann')
    assert isinstance(res, xr.Dataset)
Exemplo n.º 7
0
def test_datetime_cftime_exception():
    ds = esmlab.datasets.open_dataset('mpi_som_ffn_gcb2018', decode_times=True)
    with pytest.raises(TypeError):
        esmlab.resample(ds, freq='ann')
Exemplo n.º 8
0
def test_resample_mon_mean(dset):
    computed_dset = esmlab.resample(dset, freq='mon')
    res = computed_dset.variable_1.data
    expected = np.full(shape=(12, 2, 2), fill_value=0.5, dtype=np.float32)
    np.testing.assert_allclose(res, expected)