Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcvey3 committed Mar 5, 2024
1 parent 2b98d23 commit 92d3984
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/ADCP_Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3962,7 +3962,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"TKE esimations are generally more complete than those of dissipation rates because they are found directly from the along-beam velocity measurements. Missing TKE estimations exist whenever the noise calculated by the function `doppler_noise_level` is greater than the calculated TKE, as TKE can't be less than zero. Noise levels are affected by the instrument's processor and working frequency, water waves and other sources of \"interference\", instrument motion, current speed, intricacies in the spectra calculation, the ability to see the noise floor in the spectra, etc.\n",
"TKE esimations are generally more complete than those of dissipation rates because they are found directly from the along-beam velocity measurements. Missing TKE estimations exist whenever the noise calculated by the function `calc_doppler_noise` is greater than the calculated TKE, as TKE can't be less than zero. Noise levels are affected by the instrument's processor and working frequency, water waves and other sources of \"interference\", instrument motion, current speed, intricacies in the spectra calculation, the ability to see the noise floor in the spectra, etc.\n",
"\n",
"You may also note that high TI doesn't always correlate with high TKE. TI is the ratio of flow speed standard devation to the mean, which is naturally lower when flow speeds are higher. When flow speeds are higher, they also have greater kinetic energy and thereby greater TKE.\n",
"\n",
Expand Down
7 changes: 5 additions & 2 deletions dolfyn/adp/turbulence.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,9 @@ def calc_dissipation_LT83(self, psd, U_mag, freq_range=[0.2, 0.4], noise=None):
The range over which to integrate/average the spectrum, in units
of the psd frequency vector (Hz or rad/s)
noise : float or array-like
A vector of the noise levels of the velocity data with
the same first dimension as the velocity vector (time).
Instrument noise level in same units as velocity. Typically
found from `adp.turbulence.calc_doppler_noise`.
Default: None.
Returns
-------
Expand Down Expand Up @@ -673,6 +674,8 @@ def calc_dissipation_LT83(self, psd, U_mag, freq_range=[0.2, 0.4], noise=None):
raise Exception('PSD should be 2-dimensional (time, frequency)')
if len(U_mag.shape) != 1:
raise Exception('U_mag should be 1-dimensional (time)')
if not hasattr(freq_range, "__iter__") or len(freq_range) != 2:
raise ValueError("`freq_range` must be an iterable of length 2.")
if noise is not None:
if np.shape(noise)[0] != np.shape(psd)[0]:
raise Exception(
Expand Down
14 changes: 10 additions & 4 deletions dolfyn/adv/turbulence.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class ADVBinner(VelBinner):
The length of the FFT for computing spectra (must be <= n_bin)
n_fft_coh : int (optional, default: `n_fft_coh`=`n_fft`)
Number of data points to use for coherence and cross-spectra ffts
noise : float, list or numpy.ndarray
Instrument's doppler noise in same units as velocity
noise : float or array-like
Instrument noise level in same units as velocity. Typically
found from `adv.turbulence.calc_doppler_noise`.
Default: None.
"""

def __call__(self, ds, freq_units='rad/s', window='hann'):
Expand Down Expand Up @@ -309,8 +311,9 @@ def calc_epsilon_LT83(self, psd, U_mag, freq_range=[6.28, 12.57], noise=None):
The range over which to integrate/average the spectrum, in units
of the psd frequency vector (Hz or rad/s)
noise : float or array-like
A vector of the noise levels of the velocity data with
the same first dimension as the velocity vector.
Instrument noise level in same units as velocity. Typically
found from `adv.turbulence.calc_doppler_noise`.
Default: None.
Returns
-------
Expand Down Expand Up @@ -344,6 +347,9 @@ def calc_epsilon_LT83(self, psd, U_mag, freq_range=[6.28, 12.57], noise=None):
# Ensure time has been averaged
if len(psd.time) != len(U_mag.time):
raise Exception("`U_mag` should be from ensembled-averaged dataset")
if not hasattr(freq_range, "__iter__") or len(freq_range) != 2:
raise ValueError("`freq_range` must be an iterable of length 2.")

if noise is not None:
if np.shape(noise)[0] != 3:
raise Exception(
Expand Down
4 changes: 2 additions & 2 deletions dolfyn/tests/data/Sig1000_tidal_bin.nc
Git LFS file not shown
4 changes: 2 additions & 2 deletions dolfyn/tests/data/vector_data01_bin.nc
Git LFS file not shown
10 changes: 6 additions & 4 deletions dolfyn/tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ def test_adcp_turbulence(make_data=False):
bnr = apm.ADPBinner(n_bin=20.0, fs=dat.fs, diff_style='centered')
tdat = bnr.do_avg(dat)

tdat['dudz'] = bnr.calc_dudz(tdat.vel)
tdat['dvdz'] = bnr.calc_dvdz(tdat.vel)
tdat['dwdz'] = bnr.calc_dwdz(tdat.vel)
tdat['tau2'] = bnr.calc_shear2(tdat.vel)
tdat['dudz'] = bnr.calc_dudz(tdat["vel"])
tdat['dvdz'] = bnr.calc_dvdz(tdat["vel"])
tdat['dwdz'] = bnr.calc_dwdz(tdat["vel"])
tdat['tau2'] = bnr.calc_shear2(tdat["vel"])
tdat['I'] = tdat.velds.I
tdat['ti'] = bnr.calc_ti(dat.velds.U_mag, detrend=False)
dat.velds.rotate2('beam')

tdat['psd'] = bnr.calc_psd(dat['vel'].isel(
dir=2, range=len(dat.range)//2), freq_units='Hz')
tdat['noise'] = bnr.calc_doppler_noise(tdat['psd'], pct_fN=0.8)
Expand All @@ -156,6 +157,7 @@ def test_adcp_turbulence(make_data=False):
dat, noise=tdat['noise'], orientation='up', beam_angle=25, tke_only=False)
tdat['tke'] = bnr.calc_total_tke(
dat, noise=tdat['noise'], orientation='up', beam_angle=25)
tdat['ti_noise'] = bnr.calc_ti(dat.velds.U_mag, detrend=False, noise=tdat['noise'])
# This is "negative" for this code check
tdat['wpwp'] = bnr.calc_tke(dat['vel_b5'], noise=tdat['noise'])
tdat['dissipation_rate_LT83'] = bnr.calc_dissipation_LT83(
Expand Down
23 changes: 16 additions & 7 deletions dolfyn/tools/psd.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ def psd_freq(nfft, fs, full=False):


def _getwindow(window, nfft):
if 'hann' in window:
window = np.hanning(nfft)
elif 'hamm' in window:
window = np.hamming(nfft)
elif window is None or np.sum(window == 1):
if window is None:
window = np.ones(nfft)
if len(window) != nfft:
raise ValueError("Custom window length must be equal to nfft")
elif isinstance(window, (int, float)) and window == 1:
window = np.ones(nfft)
elif isinstance(window, str):
if "hann" in window:
window = np.hanning(nfft)
elif "hamm" in window:
window = np.hamming(nfft)
else:
raise ValueError("Unsupported window type: {}".format(window))
elif isinstance(window, np.ndarray):
if len(window) != nfft:
raise ValueError("Custom window length must be equal to nfft")
else:
raise ValueError("Invalid window parameter")

return window


Expand Down
7 changes: 6 additions & 1 deletion dolfyn/velocity.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ def calc_ti(self, U_mag, noise=0, thresh=0, detrend=False):

if 'xarray' in type(U_mag).__module__:
U = U_mag.values
if "xarray" in type(noise).__module__:
noise = noise.values

if detrend:
up = self.detrend(U)
Expand Down Expand Up @@ -1064,7 +1066,8 @@ def calc_psd(self, veldat,
Specify the window function.
Options: 1, None, 'hann', 'hamm'
noise : numeric
The noise level in the same units as velocity
Instrument noise level in same units as velocity.
Default: 0 (ADCP) or [0, 0, 0] (ADV).
n_bin : int (optional)
The bin-size (default: from the binner).
n_fft : int (optional)
Expand All @@ -1088,6 +1091,8 @@ def calc_psd(self, veldat,
vel = veldat.values
if 'xarray' in type(noise).__module__:
noise = noise.values
if ("rad" not in freq_units) and ("Hz" not in freq_units):
raise ValueError("`freq_units` should be one of 'Hz' or 'rad/s'")

# Create frequency vector, also checks whether using f or omega
if 'rad' in freq_units:
Expand Down

0 comments on commit 92d3984

Please sign in to comment.