def test_integral(od, varNameList):
    int_ds = integral(od, varNameList)
    w_mean_ds = weighted_mean(od, varNameList)
    int_name = [var for var in int_ds.data_vars][0]
    var = int_ds[int_name]
    check = (w_mean_ds["w_mean_" + varNameList] *
             w_mean_ds["weight_" + varNameList].sum())
    assert var.values == check.values
def test_shortcuts(od_in):

    # Only use some variables
    list_calc = [
        "Temp",
        "U",
        "V",
        "W",
        "HFacC",
        "HFacW",
        "HFacS",
        "drC",
        "drF",
        "dxC",
        "dyC",
        "dxF",
        "dyF",
        "dxG",
        "dyG",
        "dxV",
        "dyU",
        "rA",
        "rAw",
        "rAs",
        "rAz",
    ]
    od_in = od_in.subsample.cutout(varList=list_calc)

    # Gradient
    ds_out = gradient(od_in)
    od_out = od_in.compute.gradient()
    ds_out_IN_od_out(ds_out, od_out)

    # Divergence
    ds_out = divergence(od_in, iName="U", jName="V", kName="W")
    od_out = od_in.compute.divergence(iName="U", jName="V", kName="W")
    ds_out_IN_od_out(ds_out, od_out)

    # Curl
    ds_out = curl(od_in, iName="U", jName="V", kName="W")
    od_out = od_in.compute.curl(iName="U", jName="V", kName="W")
    ds_out_IN_od_out(ds_out, od_out)

    # Laplacian
    ds_out = laplacian(od_in, "Temp")
    od_out = od_in.compute.laplacian(varNameList="Temp")
    ds_out_IN_od_out(ds_out, od_out)

    # Weighted mean
    ds_out = weighted_mean(od_in)
    od_out = od_in.compute.weighted_mean()
    ds_out_IN_od_out(ds_out, od_out)

    # Integral
    ds_out = integral(od_in)
    od_out = od_in.compute.integral()
    ds_out_IN_od_out(ds_out, od_out)
def test_all_integrals(od):
    od_moor = od.subsample.mooring_array(
        Xmoor=[od.dataset["X"].min().values, od.dataset["X"].max().values],
        Ymoor=[od.dataset["Y"].min().values, od.dataset["Y"].max().values],
    )
    # Test no units
    od_moor.dataset["Temp"].attrs = []
    with pytest.warns(UserWarning):
        X = od_moor.dataset["XC"].squeeze().values
        Y = od_moor.dataset["YC"].squeeze().values
        od_surv = od.subsample.survey_stations(Xsurv=X, Ysurv=Y)
    # Test all dimension
    DIMS = []
    VARS = []
    for var in od.dataset.data_vars:
        this_dims = list(od.dataset[var].dims)
        append = True
        for dims in DIMS:
            checks = [
                set(this_dims).issubset(set(dims)),
                set(dims).issubset(set(this_dims)),
            ]
            if all(checks):
                append = False
                continue
        if append:
            VARS = VARS + [var]
            DIMS = DIMS + [list(this_dims)]
    integral(od, varNameList=VARS)
    integral(od_moor, varNameList=VARS)
    integral(od_surv, varNameList=VARS)