mod_powerspec module
Powerspec is a python package created by Adekunle Ajayi and available at https://github.com/adekunleoajayi/powerspec.
The powerspec package was created for estimating wavenumber spectral density, kinetic energy spectral flux and spectral coherence of two-dimensional oceanic dataset such as SSH, vorticity.
Due to some deprecations of the package, only parts of it are used here in mod_powerspec.py.
- mod_powerspec.cross_spectra(data1, data2, navlon, navlat, window='Tukey', detrend='Both', Normalize=False)
Compute cross spectra.
- Parameters:
data1 (numpy.ndarray) – First data array.
data2 (numpy.ndarray) – Second data array.
navlon (numpy.ndarray) – Longitude.
navlat (numpy.ndarray) – Latitude.
window (str, optional) – Windowing method (default is ‘Tukey’).
detrend (str, optional) – Detrend method (default is ‘Both’).
Normalize (bool, optional) – If True, normalize the cross spectra (default is False).
- Returns:
wavenumber (numpy.ndarray) – Wavenumber values.
cross_spectra (numpy.ndarray) – Cross spectra.
- mod_powerspec.fill_nan(data)
Replaces a NaN value in a dataset with an interpolated one.
- Parameters:
data (numpy.ndarray) – Input data containing NaN values.
- Returns:
data_nonan – Data with NaN values replaced by interpolation.
- Return type:
numpy.ndarray
- mod_powerspec.interpolate(data, navlon, navlat, interp=None)
Perform a spatial interpolation if required; return x_reg, y_reg, data_reg.
- Parameters:
data (numpy.ndarray) – Raw data.
navlon (numpy.ndarray) – Longitude.
navlat (numpy.ndarray) – Latitude.
interp (str or None, optional) – If None, return data with coordinates in meters. If ‘basemap’, return interpolated data using basemap from mpl_toolkits and also coordinates in meters.
- Returns:
x2d_in (numpy.ndarray) – 2D array of x-coordinates.
y2d_in (numpy.ndarray) – 2D array of y-coordinates.
data (numpy.ndarray) – Raw data.
- mod_powerspec.isdata_contain_nan(data)
This function checks if data contains any NaN value. If yes, it replaces the NaN values with an interpolated value using the fill_nan function.
- Parameters:
data (numpy.ndarray) – Input data.
- Returns:
data_new – Data with NaN values replaced.
- Return type:
numpy.ndarray
- mod_powerspec.preprocess_data(data, detrend, window, navlon, navlat)
Preprocess data by performing interpolation, NaN handling, detrending, and windowing.
- Parameters:
data (numpy.ndarray) – Raw data.
detrend (str or None) – Detrend method.
window (str or None) – Windowing method.
navlon (numpy.ndarray) – Longitude.
navlat (numpy.ndarray) – Latitude.
- Returns:
data (numpy.ndarray) – Preprocessed data.
x (numpy.ndarray) – x-coordinates.
y (numpy.ndarray) – y-coordinates.
- mod_powerspec.spectra_flux(_u, _v, navlon, navlat, window='Tukey', detrend='Both')
Compute kinetic energy spectral flux.
- Parameters:
_u (numpy.ndarray) – Zonal velocity data.
_v (numpy.ndarray) – Meridional velocity data.
navlon (numpy.ndarray) – Longitude.
navlat (numpy.ndarray) – Latitude.
window (str, optional) – Windowing method (default is ‘Tukey’).
detrend (str, optional) – Detrend method (default is ‘Both’).
- Returns:
wavenumber (numpy.ndarray) – Wavenumber values.
flux (numpy.ndarray) – Kinetic energy spectral flux.
- mod_powerspec.wavenumber_spectra(data, navlon, navlat, window='Tukey', detrend='Both')
Compute wavenumber spectra.
- Parameters:
data (numpy.ndarray) – Raw data.
navlon (numpy.ndarray) – Longitude.
navlat (numpy.ndarray) – Latitude.
window (str or None, optional) – Windowing method (default is ‘Tukey’).
detrend (str or None, optional) – Detrend method (default is ‘Both’).
- Returns:
wavenumber (numpy.ndarray) – Wavenumber values.
psd (numpy.ndarray) – Isotropic power spectral density.
- mod_powerspec.weighted_scale(wavenumber, psd, axs=None)
Compute the weighted scale from wavenumber spectra density (like integral scale).
- Parameters:
wavenumber (numpy.ndarray) – Wavenumber values.
psd (numpy.ndarray) – Power spectral density.
axs (int or None, optional) – Axis to sum over (default is None).
- Returns:
scale – Weighted scale.
- Return type:
float or numpy.ndarray