Пример #1
0
def test_parse_metric_exceptions(metricname):
    # create a dataset
    ds = random_ds()
    # create a metric dataset
    ds_metric = random_ds().isel(z=0, time=0).rename({"data": metricname})

    # provide dataset instead of dataarray
    with pytest.raises(ValueError):
        ds_parsed = _parse_metric(ds, ds_metric)
Пример #2
0
def test_parse_metric_exception_dim_length():
    metricname = "metric"
    # create a dataset
    ds = random_ds()
    # create a metric dataset
    ds_metric = random_ds().isel(z=0, time=0).rename({"data": metricname})
    # set attributes
    ds.attrs = {"activity_id": "a", "grid_label": "g"}

    # provide dataarray with non-matching dimensions
    with pytest.raises(ValueError) as execinfo:
        ds_parsed = _parse_metric(
            ds,
            ds_metric.isel(x=slice(0, -1), y=slice(1, None))[metricname])
    msg = "a.none.none.none.none.none.g.none.none:`metric` dimensions ['x:2', 'y:1'] do not match `ds` ['x:3', 'y:2']."
    assert execinfo.value.args[0] == msg
Пример #3
0
def test_parse_metric(metricname):
    # create a dataset
    ds = random_ds()
    # create a metric dataset
    ds_metric = random_ds().isel(z=0, time=0).rename({"data": metricname})
    metric = ds_metric[metricname]
    metric.attrs.update({"check": "carry"})

    # parse the metric
    ds_parsed = _parse_metric(ds, metric)
    assert metricname in ds_parsed.coords
    xr.testing.assert_allclose(ds_parsed[metricname].reset_coords(drop=True),
                               metric)
    assert (ds_parsed[metricname].attrs["parsed_with"] ==
            "cmip6_preprocessing/postprocessing/_parse_metric")
    # check that existing attrs are conserved
    assert ds_parsed[metricname].attrs["check"] == "carry"
Пример #4
0
def test_parse_metric_exceptions_input_name():
    # create a dataset
    ds = random_ds()
    # create a metric dataset
    ds_metric = random_ds().isel(z=0, time=0)
    # set attributes
    ds.attrs = {"activity_id": "a"}

    # provide dataarray without name
    with pytest.warns(RuntimeWarning) as warninfo:
        da_metric_nameless = ds_metric["data"]
        da_metric_nameless.name = None

        ds_parsed = _parse_metric(ds, da_metric_nameless)
    assert (
        warninfo[0].message.args[0] ==
        "a.none.none.none.none.none.none.none.none:`metric` has no name. This might lead to problems down the line."
    )
Пример #5
0
def test_parse_metric_dim_alignment():
    metricname = "metric"
    # create a dataset
    ds = random_ds(time_coords=True)
    # create a metric dataset
    ds_metric = (random_ds(time_coords=True).isel(z=0, time=slice(
        0, -1)).rename({"data": metricname}))
    # set attributes
    ds.attrs = {"activity_id": "a", "grid_label": "g"}

    print(ds)

    # Allow alignment
    with pytest.warns(UserWarning) as warninfo:
        ds_parsed = _parse_metric(ds,
                                  ds_metric[metricname],
                                  dim_length_conflict="align")

    xr.testing.assert_allclose(ds_parsed.time, ds_metric.time)

    msg = "a.none.none.none.none.none.g.none.none:`metric` dimensions ['time:5'] do not match `ds` ['time:6']. Aligning the data on `inner`"
    assert warninfo[0].message.args[0] == msg