Example #1
0
def test_isotropic_ps_slope(chunk, N=512, dL=1.0, amp=1e1, s=-3.0):
    """Test the spectral slope of isotropic power spectrum."""

    theta = synthetic_field_xr(
        N,
        dL,
        amp,
        s,
        other_dim_sizes=[10],
        dim_order=True,
    )

    if chunk:
        theta = theta.chunk({"d0": 2})

    iso_ps = xrft.isotropic_power_spectrum(
        theta, dim=["y", "x"], detrend="constant", density=True
    ).mean("d0")
    npt.assert_almost_equal(np.ma.masked_invalid(iso_ps).mask.sum(), 0.0)
    y_fit, a, b = xrft.fit_loglog(iso_ps.freq_r.values[4:], iso_ps.values[4:])
    npt.assert_allclose(a, s, atol=0.1)

    iso_ps_sequal = np.zeros((len(theta.d0), int(N / 4)))
    for i in range(len(theta.d0)):
        iso_ps_sequal[i] = xrft.isotropic_power_spectrum(
            theta.isel(d0=i), detrend="constant", density=True
        )
    npt.assert_almost_equal(iso_ps.values, iso_ps_sequal.mean(axis=0))
Example #2
0
def test_isotropic_ps():
    """Test data with extra coordinates"""
    da = xr.DataArray(np.random.rand(2,5,16,32),
                  dims=['time','z','y','x'],
                  coords={'time': np.array(['2019-04-18', '2019-04-19'],
                                          dtype='datetime64'),
                         'zz': ('z',np.arange(5)), 'z': np.arange(5),
                         'y': np.arange(16), 'x': np.arange(32)})
    with pytest.raises(ValueError):
        xrft.isotropic_power_spectrum(da, dim=['z','y','x'])
    iso_ps = xrft.isotropic_power_spectrum(da, dim=['y','x'])
    npt.assert_equal(
            np.ma.masked_invalid(iso_ps.isel(freq_r=slice(1,None))).mask.sum(),
            0.)
Example #3
0
def test_isotropic_ps():
    """Test data with extra coordinates"""
    da = xr.DataArray(
        np.random.rand(2, 5, 16, 32),
        dims=["time", "z", "y", "x"],
        coords={
            "time": np.array(["2019-04-18", "2019-04-19"], dtype="datetime64"),
            "zz": ("z", np.arange(5)),
            "z": np.arange(5),
            "y": np.arange(16),
            "x": np.arange(32),
        },
    )
    with pytest.raises(ValueError):
        xrft.isotropic_power_spectrum(da, dim=["z", "y", "x"])
    iso_ps = xrft.isotropic_power_spectrum(da, dim=["y", "x"])
    npt.assert_equal(
        np.ma.masked_invalid(iso_ps.isel(freq_r=slice(1, None))).mask.sum(),
        0.0)
Example #4
0
def test_isotropic_ps_slope(N=512, dL=1., amp=1e1, s=-3.):
    """Test the spectral slope of isotropic power spectrum."""

    theta = xr.DataArray(synthetic_field(N, dL, amp, s),
                        dims=['y', 'x'],
                        coords={'y':range(N), 'x':range(N)})
    iso_ps = xrft.isotropic_power_spectrum(theta, detrend='constant',
                                         density=True)
    npt.assert_almost_equal(np.ma.masked_invalid(iso_ps[1:]).mask.sum(), 0.)
    y_fit, a, b = xrft.fit_loglog(iso_ps.freq_r.values[4:],
                                 iso_ps.values[4:])

    npt.assert_allclose(a, s, atol=.1)