def test_gradient(od, axesList):

    varNameList = ["sinZ", "sinY", "sinX", "sintime"]
    if axesList == "wrong":
        with pytest.raises(ValueError):
            gradient(od, varNameList=varNameList, axesList=axesList)
    else:
        grad_ds = gradient(od, varNameList=varNameList, axesList=axesList)
        if axesList is None:
            axesList = list(od.grid_coords.keys())

        # sin' = cos
        for varName in varNameList:
            for axis in axesList:
                gradName = "d" + varName + "_" + "d" + axis
                var = grad_ds[gradName]
                if axis not in varName:
                    assert var.min().values == grad_ds[gradName].max(
                    ).values == 0
                else:
                    check = od.dataset["cos" + axis].where(var)
                    mask = xr.where(
                        np.logical_or(check.isnull(), var.isnull()), 0, 1)
                    assert_allclose(
                        var.where(mask, drop=True).values,
                        check.where(mask, drop=True).values,
                        1.0e-3,
                    )
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_gradients(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],
    )
    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)]
    gradient(od, varNameList=VARS)
    gradient(od_moor, varNameList=VARS)
    gradient(od_surv, varNameList=VARS)
Exemple #4
0
def test_Brunt_Vaisala_frequency(od_in):

    # Compute N2
    ds_out = Brunt_Vaisala_frequency(od_in)
    assert ds_out['N2'].attrs['units'] == 's^-2'
    assert ds_out['N2'].attrs['long_name'] == 'Brunt-Väisälä Frequency'
    check_params(ds_out, 'N2', ['g', 'rho0'])

    # Check values
    dSigma0_dZ = gradient(od_in, 'Sigma0', 'Z')
    dSigma0_dZ = dSigma0_dZ['dSigma0_dZ']
    assert_allclose(-dSigma0_dZ.values
                    * od_in.parameters['g']
                    / od_in.parameters['rho0'], ds_out['N2'].values)

    # Test shortcut
    od_out = od_in.compute.Brunt_Vaisala_frequency()
    ds_out_IN_od_out(ds_out, od_out)
def test_Brunt_Vaisala_frequency(od_in):

    # Compute N2
    ds_out = Brunt_Vaisala_frequency(od_in)
    assert ds_out["N2"].attrs["units"] == "s^-2"
    assert ds_out["N2"].attrs["long_name"] == "Brunt-Väisälä Frequency"
    check_params(ds_out, "N2", ["g", "rho0"])

    # Check values
    dSigma0_dZ = gradient(od_in, "Sigma0", "Z")
    dSigma0_dZ = dSigma0_dZ["dSigma0_dZ"]
    assert_allclose(
        -dSigma0_dZ.values * od_in.parameters["g"] / od_in.parameters["rho0"],
        ds_out["N2"].values,
    )

    # Test shortcut
    od_out = od_in.compute.Brunt_Vaisala_frequency()
    ds_out_IN_od_out(ds_out, od_out)