From db3db3eb1338f1a3d04ace254ff8599946b2d188 Mon Sep 17 00:00:00 2001 From: "Mads M. Pedersen" <mmpe@dtu.dk> Date: Wed, 1 Nov 2017 11:13:06 +0100 Subject: [PATCH] test fixes --- wetb/hawc2/pc_file.py | 7 +- wetb/hawc2/tests/test_htc_file.py | 6 +- wetb/utils/tests/test_files/test.csv | 5 ++ wetb/wind/tests/test_Shear.py | 9 +- wetb/wind/turbulence/mann_turbulence.py | 108 ++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 wetb/utils/tests/test_files/test.csv create mode 100644 wetb/wind/turbulence/mann_turbulence.py diff --git a/wetb/hawc2/pc_file.py b/wetb/hawc2/pc_file.py index 301a5b81..78dafdc5 100644 --- a/wetb/hawc2/pc_file.py +++ b/wetb/hawc2/pc_file.py @@ -16,7 +16,8 @@ standard_library.install_aliases() from wetb.hawc2.ae_file import AEFile import numpy as np -class PCFile(AEFile): + +class PCFile(object): """Read HAWC2 PC (profile coefficients) file examples @@ -37,9 +38,7 @@ class PCFile(AEFile): >>> pcfile.CM(36,10) # CM at radius=36m and AOA=10deg -0.1103 """ - def __init__(self, filename, ae_filename): - AEFile.__init__(self, ae_filename) - + def __init__(self, filename): with open (filename) as fid: lines = fid.readlines() nsets = int(lines[0].split()[0]) diff --git a/wetb/hawc2/tests/test_htc_file.py b/wetb/hawc2/tests/test_htc_file.py index 79b08927..ade4e173 100644 --- a/wetb/hawc2/tests/test_htc_file.py +++ b/wetb/hawc2/tests/test_htc_file.py @@ -151,9 +151,9 @@ class TestHtcFile(unittest.TestCase): htcfile.add_mann_turbulence(30.1, 1.1, 3.3, 102, False) s = """begin mann; create_turb_parameters\t30.1 1.1 3.3 102 0;\tL, alfaeps, gamma, seed, highfrq compensation - filename_u\t./turb/mann_l30.1_ae1.10_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102u.turb; - filename_v\t./turb/mann_l30.1_ae1.10_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102v.turb; - filename_w\t./turb/mann_l30.1_ae1.10_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102w.turb; + filename_u\t./turb/mann_l30.1_ae1.1000_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102u.turb; + filename_v\t./turb/mann_l30.1_ae1.1000_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102v.turb; + filename_w\t./turb/mann_l30.1_ae1.1000_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102w.turb; box_dim_u\t16384 0.3662; box_dim_v\t32 3.2258; box_dim_w\t32 3.2258;""" diff --git a/wetb/utils/tests/test_files/test.csv b/wetb/utils/tests/test_files/test.csv new file mode 100644 index 00000000..158e1a69 --- /dev/null +++ b/wetb/utils/tests/test_files/test.csv @@ -0,0 +1,5 @@ +0.000000000000000000e+00 5.000000000000000000e+00 +1.000000000000000000e+00 6.000000000000000000e+00 +2.000000000000000000e+00 7.000000000000000000e+00 +3.000000000000000000e+00 8.000000000000000000e+00 +4.000000000000000000e+00 9.000000000000000000e+00 diff --git a/wetb/wind/tests/test_Shear.py b/wetb/wind/tests/test_Shear.py index 2de47b7b..3cefb0fc 100644 --- a/wetb/wind/tests/test_Shear.py +++ b/wetb/wind/tests/test_Shear.py @@ -158,8 +158,8 @@ class TestShear(unittest.TestCase): def test_log_shear(self): - u = log_shear(2, 3, 9) - self.assertAlmostEqual(u, 5.49306144) + shear = log_shear(2, 3) + self.assertAlmostEqual(shear(9), 5.49306144) def test_fit_log_shear(self): zu = [(85, 8.88131), (21, 4.41832)] @@ -171,8 +171,9 @@ class TestShear(unittest.TestCase): plt.plot(log_shear(u_star, z0, z), z) plt.show() - for _zu, b in zip(zu, log_shear(u_star, z0, [85, 21])): - self.assertAlmostEqual(_zu[1], b, 4) + shear = log_shear(u_star, z0) + for z,u in zu: + self.assertAlmostEqual(u, shear(z), 4) def test_show_log_shear(self): diff --git a/wetb/wind/turbulence/mann_turbulence.py b/wetb/wind/turbulence/mann_turbulence.py new file mode 100644 index 00000000..efe42360 --- /dev/null +++ b/wetb/wind/turbulence/mann_turbulence.py @@ -0,0 +1,108 @@ +''' +Created on 20. jul. 2017 + +@author: mmpe +''' +import numpy as np + +from wetb.wind.turbulence.spectra import spectra, spectra_from_time_series +name_format = "mann_l%.1f_ae%.4f_g%.1f_h%d_%dx%dx%d_%.3fx%.2fx%.2f_s%04d%c.turb" + +def load(filename, N=(32,32)): + """Load mann turbulence box + + Parameters + ---------- + filename : str + Filename of turbulence box + N : tuple, (ny,nz) or (nx,ny,nz) + Number of grid points + + Returns + ------- + turbulence_box : nd_array + + Examples + -------- + >>> u = load('turb_u.dat') + """ + data = np.fromfile(filename, np.dtype('<f'), -1) + if len(N)==2: + ny,nz = N + nx = len(data)/(ny*nz) + assert nx==int(nx), "Size of turbulence box (%d) does not match ny x nz (%d), nx=%.2f" % (len(data),ny*nz, nx) + nx=int(nx) + else: + nx,ny,nz = N + assert len(data) == nx*ny*nz, "Size of turbulence box (%d) does not match nx x ny x nz (%d)" % (len(data),nx*ny*nz) + return data.reshape(nx , ny * nz) + +def load_uvw(filenames, N=(1024,32,32)): + """Load u, v and w turbulence boxes + + Parameters + ---------- + filenames : list or str + if list: list of u,v,w filenames + if str: filename pattern where u,v,w are replaced with '%s' + N : tuple + Number of grid point in the x, y and z direction + + Returns + ------- + u,v,w : list of np_array + + Examples + -------- + >>> u,v,w =load_uvw('turb_%s.dat') + """ + if isinstance(filenames,str): + return [load(filenames%uvw, N) for uvw in 'uvw'] + else: + return [load(f, N) for f in filenames] + +def parameters2name(no_grid_points,box_dimension,ae23,L, Gamma,high_frq_compensation, seed, folder="./turb/"): + + dxyz = tuple(np.array(box_dimension) / no_grid_points) + return ["./turb/" + name_format % ((L, ae23, Gamma, high_frq_compensation) + no_grid_points + dxyz + (seed, uvw)) for uvw in ['u', 'v', 'w']] + + +def fit_mann_parameters(spatial_resolution, u, v, w=None, plt=None): + """Fit mann parameters, ae, L, G + + Parameters + ---------- + spatial_resolution : inf, float or array_like + Distance between samples in meters + - For turbulence boxes: 1/dx = Lx/Nx where dx is distance between points, + Nx is number of points and Lx is box length in meters + - For time series: Sample frequency / U + u : array_like + The u-wind component\n + - if shape is (r,): One time series with *r* observations\n + - if shape is (r,c): *c* different time series with *r* observations\n + v : array_like, optional + The v-wind component + - if shape is (r,): One time series with *r* observations\n + - if shape is (r,c): *c* different time series with *r* observations\n + w : array_like, optional + The w-wind component + - if shape is (r,): One time series with *r* observations\n + - if shape is (r,c): *c* different time series with *r* observations\n + + Returns + ------- + ae : int or float + Alpha epsilon^(2/3) of Mann model + L : int or float + Length scale of Mann model + G : int or float + Gamma of Mann model + """ + from wetb.wind.turbulence.mann_parameters import fit_mann_model_spectra + return fit_mann_model_spectra(*spectra(spatial_resolution, u, v, w), plt=plt) + + +def fit_mann_parameters_from_time_series(sample_frq, Uvw_lst, plt=None): + from wetb.wind.turbulence.mann_parameters import fit_mann_model_spectra + return fit_mann_model_spectra(*spectra_from_time_series(sample_frq, Uvw_lst), plt=plt) \ No newline at end of file -- GitLab