def test_test_files(self):
     fn = local_TestFiles_path + "wetb/utils/tests/test_files/test_file.txt"
     if os.path.isfile(fn):
         os.remove(fn)
     fn1 = get_test_file(tfp + 'test_file.txt')
     self.assertTrue(os.path.isfile(fn1))
     fn2 = get_test_file('test_file.txt')
     self.assertEqual(fn1, fn2)
 def test_fit_ae2var(self):
     u = mann_turbulence.load(
         get_test_file("h2a8192_8_8_16384_32_32_0.15_10_3.3u.dat"),
         (8192, 8, 8))
     self.assertAlmostEqual(fit_ae(spatial_resolution=2, u=u, L=10, G=3.3),
                            .15,
                            delta=.02)
 def test_time_series(self):
     constraint_file = ConstraintFile(center_gl_xyz=(-5, 4, -90), box_transport_speed=10, no_grid_points=(16, 8, 8), box_size=(30, 70, 70))
     constraint_file.load(get_test_file('constraints_test.con'))
     time,uvw = constraint_file.time_series(-4)
     u,v,w = uvw.T
     m = ~np.isnan(u)
     np.testing.assert_array_equal(time[m],[0,1,2])
     np.testing.assert_array_equal(u[m],[-2,0,2])
    def test_spectra_from_timeseries(self):
        fn_lst = [
            get_test_file('h2a8192_8_8_16384_32_32_0.15_10_3.3%s.dat' % uvw)
            for uvw in 'uvw'
        ]
        u, v, w = load_uvw(fn_lst, (8192, 8, 8))
        dx = 16384 / 8192
        fx = 1 / dx  # spatial resolution
        k1, uu, vv, ww, uw = logbin_spectra(*spectra(fx, u, v, w))

        U = u + 4
        sample_frq = 2
        k12, uu2, vv2, ww2, uw2 = logbin_spectra(
            *spectra_from_time_series(sample_frq, [(
                U_, v_, w_) for U_, v_, w_ in zip(U.T, v.T, w.T)]))
        np.testing.assert_allclose(uu, uu2, 0.02)

        U = u + 8
        sample_frq = 2
        k13, uu3, vv3, ww3, uw3 = logbin_spectra(*spectra_from_time_series(
            sample_frq, [(U_[::2], v_[::2], w_[::2])
                         for U_, v_, w_ in zip(U.T, v.T, w.T)]))
        np.testing.assert_allclose(uu[:-3], uu3[:-2], 0.1)

        # One set of time series with U=4
        U = u + 4
        Uvw_lst = [(U_, v_, w_) for U_, v_, w_ in zip(U.T, v.T, w.T)]
        # Another set of time series with U=8 i.e. only every second point to have
        # same sample_frq. (nan added to have same length)
        U = u + 4
        Uvw_lst.extend([(np.r_[U_[::2], U_[::2] + np.nan],
                         np.r_[v_[::2],
                               v_[::2] + np.nan], np.r_[w_[::2],
                                                        w_[::2] + np.nan])
                        for U_, v_, w_ in zip(U.T, v.T, w.T)])
        sample_frq = 2
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            k14, uu4, vv4, ww4, uw4 = logbin_spectra(
                *spectra_from_time_series(sample_frq, Uvw_lst))
        np.testing.assert_allclose(uu[:-3], uu3[:-2], rtol=0.1)
        if 0:
            import matplotlib.pyplot as plt
            plt.semilogx(k1, uu * k1)
            plt.semilogx(k12, uu2 * k12)
            plt.semilogx(k13, uu3 * k13)
            plt.semilogx(k14, uu4 * k14)
            plt.show()
    def test_fit_mann_parameters_turbulence_box(self):
        # for uvw in 'uvw':
        #    move2test_files(tfp + 'h2a8192_8_8_16384_32_32_0.15_10_3.3%s.dat'%uvw)
        fn_lst = [
            get_test_file('h2a8192_8_8_16384_32_32_0.15_10_3.3%s.dat' % uvw)
            for uvw in 'uvw'
        ]
        u, v, w = load_uvw(fn_lst, (8192, 8, 8))
        dx = 16384 / 8192
        fx = 1 / dx  # spatial resolution
        plt = None
        ae, L, G = fit_mann_parameters(fx, u, v, w, plt=plt)

        self.assertAlmostEqual(ae, .15, delta=0.01)
        self.assertAlmostEqual(L, 10, delta=0.3)
        self.assertAlmostEqual(G, 3.3, delta=0.06)
 def test_var2ae_U(self):
     u = mann_turbulence.load(
         get_test_file("h2a8192_8_8_16384_32_32_0.15_10_3.3u.dat"),
         (8192, 8, 8))
     dx = 2
     for U in [1, 10, 100]:
         # should be independent of U
         dt = dx / U
         T = 16384 / U
         self.assertAlmostEqual(var2ae(variance=u.var(),
                                       L=10,
                                       G=3.3,
                                       U=U,
                                       T=T,
                                       sample_frq=1 / dt),
                                .15,
                                delta=.021)
 def test_var2ae_T(self):
     u = mann_turbulence.load(
         get_test_file("h2a8192_8_8_16384_32_32_0.15_10_3.3u.dat"),
         (8192, 8, 8))
     dx = 2
     U = 10
     dt = dx / U
     for i in np.arange(6):
         # reshape to more and shorter series. Variance should decrease while ae should be roughly constant
         n = 2**i
         u_ = u.T.reshape((u.T.shape * np.array([n, 1 / n])).astype(int)).T
         var = u_.var(0).mean()
         ae = var2ae(variance=var,
                     L=10,
                     G=3.3,
                     U=U,
                     T=dx * u_.shape[0] / U,
                     sample_frq=1 / dt)
         self.assertAlmostEqual(ae, .15, delta=.025)
    def test_fit_mann_parameters_from_timeseries(self):
        fn_lst = [
            get_test_file('h2a8192_8_8_16384_32_32_0.15_10_3.3%s.dat' % uvw)
            for uvw in 'uvw'
        ]
        u, v, w = load_uvw(fn_lst, (8192, 8, 8))
        dx = 16384 / 8192
        fx = 1 / dx  # spatial resolution
        ae, L, G = fit_mann_parameters(fx, u, v, w)
        self.assertAlmostEqual(ae, .15, delta=0.01)
        self.assertAlmostEqual(L, 10, delta=0.3)
        self.assertAlmostEqual(G, 3.3, delta=0.06)

        #import matplotlib.pyplot as plt
        plt = None
        U = u + 4
        sample_frq = 2
        ae, L, G = fit_mann_parameters_from_time_series(
            sample_frq, [(U_, v_, w_) for U_, v_, w_ in zip(U.T, v.T, w.T)],
            plt=plt)
        self.assertAlmostEqual(ae, .15, delta=0.01)
        self.assertAlmostEqual(L, 10, delta=0.3)
        self.assertAlmostEqual(G, 3.3, delta=0.06)

        # One set of time series with U=4
        U = u + 4
        Uvw_lst = [(U_, v_, w_) for U_, v_, w_ in zip(U.T, v.T, w.T)]
        # Another set of time series with U=8 i.e. only every second point to have
        # same sample_frq. (nan added to have same length)
        U = u + 4
        Uvw_lst.extend([(np.r_[U_[::2], U_[::2] + np.nan],
                         np.r_[v_[::2],
                               v_[::2] + np.nan], np.r_[w_[::2],
                                                        w_[::2] + np.nan])
                        for U_, v_, w_ in zip(U.T, v.T, w.T)])
        sample_frq = 2
        ae, L, G = fit_mann_parameters_from_time_series(sample_frq,
                                                        Uvw_lst,
                                                        plt=plt)
        self.assertAlmostEqual(ae, .15, delta=0.01)
        self.assertAlmostEqual(L, 10, delta=0.3)
        self.assertAlmostEqual(G, 3.3, delta=0.06)
    def test_var2ae_dt(self):
        u = mann_turbulence.load(
            get_test_file("h2a16384_8_8_65536_32_32_0.15_40_4.0u.dat"),
            (16384, 8, 8))
        dx = 4
        U = 10
        T = u.shape[0] * dx / U
        for i in np.arange(9):
            # average every neighbouring samples to decrease dt.
            # Variance should decrease while ae should be roughly constant
            n = 2**i

            u_ = u.reshape(u.shape[0] // n, n, u.shape[1]).mean(1)
            var = u_.var(0).mean()

            ae = var2ae(variance=var,
                        L=40,
                        G=4,
                        U=U,
                        T=T,
                        sample_frq=1 / (n * dx / U),
                        plt=False)
            #print(u_.shape, var, ae)
            self.assertAlmostEqual(ae, .15, delta=.04)
 def test_load(self):
     constraint_file = ConstraintFile(center_gl_xyz=(-5, 4, -90), box_transport_speed=10, no_grid_points=(16, 8, 8), box_size=(30, 70, 70))
     constraint_file.load(get_test_file('constraints_test.con'))
     with open(get_test_file('constraints_test.con')) as fid:
         self.assertEqual(str(constraint_file), fid.read())