def test_divergence(od, varNameList): # Add units if None not in varNameList: for varName in varNameList: od._ds[varName].attrs["units"] = "m/s" # Compute divergence dive_ds = divergence(od, iName=varNameList[0], jName=varNameList[1], kName=varNameList[2]) # sin' = cos for varName in varNameList: if varName is not None: axis = varName[-1] diveName = "d" + varName + "_" + "d" + axis var = dive_ds[diveName] coords = {coord[0]: var[coord] for coord in var.coords} coords["Z"], coords["Y"], coords["X"] = xr.broadcast( coords["Z"], coords["Y"], coords["X"]) check = np.cos(coords[axis]) mask = xr.where(np.logical_or(check.isnull(), var.isnull()), 0, 1) # Assert using numpy var = var.where(mask, drop=True).values check = check.where(mask, drop=True).values assert_array_almost_equal(var, check, 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_normal_strain(od_in): # Compute n_strain ds_out = normal_strain(od_in) assert ds_out["n_strain"].attrs["units"] == "s^-1" long_name = "normal component of strain" assert ds_out["n_strain"].attrs["long_name"] == long_name # Check values divs = divergence(od_in, iName="U", jName="V") assert_allclose((divs["dU_dX"] - divs["dV_dY"]).values, ds_out["n_strain"].values) # Test shortcut od_out = od_in.compute.normal_strain() ds_out_IN_od_out(ds_out, od_out)
def test_horizontal_divergence_velocity(od_in): # Compute hor_div_vel ds_out = horizontal_divergence_velocity(od_in) assert ds_out["hor_div_vel"].attrs["units"] == "m s^-2" long_name = "horizontal divergence of the velocity field" assert ds_out["hor_div_vel"].attrs["long_name"] == long_name # Check values hor_div = divergence(od_in, iName="U", jName="V") hor_div = hor_div["dU_dX"] + hor_div["dV_dY"] assert_allclose(hor_div.values, ds_out["hor_div_vel"].values) # Test shortcut od_out = od_in.compute.horizontal_divergence_velocity() ds_out_IN_od_out(ds_out, od_out)
def test_normal_strain(od_in): # Compute n_strain ds_out = normal_strain(od_in) assert ds_out['n_strain'].attrs['units'] == 's^-1' long_name = 'normal component of strain' assert ds_out['n_strain'].attrs['long_name'] == long_name # Check values divs = divergence(od_in, iName='U', jName='V') assert_allclose((divs['dU_dX'] - divs['dV_dY']).values, ds_out['n_strain'].values) # Test shortcut od_out = od_in.compute.normal_strain() ds_out_IN_od_out(ds_out, od_out)
def test_horizontal_divergence_velocity(od_in): # Compute hor_div_vel ds_out = horizontal_divergence_velocity(od_in) assert ds_out['hor_div_vel'].attrs['units'] == 'm s^-2' long_name = 'horizontal divergence of the velocity field' assert ds_out['hor_div_vel'].attrs['long_name'] == long_name # Check values hor_div = divergence(od_in, iName='U', jName='V') hor_div = hor_div['dU_dX'] + hor_div['dV_dY'] assert_allclose(hor_div.values, ds_out['hor_div_vel'].values) # Test shortcut od_out = od_in.compute.horizontal_divergence_velocity() ds_out_IN_od_out(ds_out, od_out)
def test_div_errors(od, iName, jName, kName): with pytest.raises(ValueError): divergence(od, iName=iName, jName=jName, kName=kName)