mod_spectral module

mod_spectral.apply_window(da, dims, window_type='hanning')

Create windows in dimensions dims.

Parameters:
  • da (xarray.DataArray) – Data array.

  • dims (list of str) – Dimensions.

  • window_type (str, optional) – Windowing type (default is ‘hanning’).

Returns:

da – Data array with applied windowing.

Return type:

xarray.DataArray

mod_spectral.compute_crossing(array, wavenumber, threshold=0.5)

Compute the crossing of a threshold by an array of values.

Parameters:
  • array (numpy.ndarray) – Array of values.

  • wavenumber (numpy.ndarray) – Array of wavenumbers.

  • threshold (float, optional) – Threshold for crossing, defaults to 0.5.

Returns:

  • float – Effective resolution.

  • bool – Flag indicating multiple crossings.

mod_spectral.compute_median_dx(dataset)

Compute the median distance between consecutive points in a dataset.

Parameters:

dataset (xarray.Dataset) – Dataset containing longitude and latitude data.

Returns:

The median distance in meters.

Return type:

float

mod_spectral.compute_median_lon_lat(vlon, vlat, sub_segment_point, npt)

Compute the median longitude and latitude for a sub-segment of points.

Parameters:
  • vlon (numpy.ndarray) – Array of longitudes.

  • vlat (numpy.ndarray) – Array of latitudes.

  • sub_segment_point (int) – Starting index of the sub-segment.

  • npt (int) – Number of points in the sub-segment.

Returns:

  • float – The median longitude.

  • float – The median latitude.

mod_spectral.compute_psd_scores(ds_interp, output_filename, lenght_scale=1500.0)

Compute power spectral density (PSD) scores and save them to a NetCDF file.

Parameters:
  • ds_interp (xarray.Dataset) – Interpolated dataset.

  • output_filename (str) – Output NetCDF file path.

  • length_scale (float, optional) – Length scale, defaults to 1500.0.

Return type:

None

mod_spectral.compute_psd_scores_current(ds_interp, output_filename, lenght_scale=numpy.timedelta64, method_name=' ')

Compute power spectral density (PSD) scores for current data and save them to a NetCDF file.

Parameters:
  • ds_interp (xarray.Dataset) – Interpolated dataset.

  • output_filename (str) – Output NetCDF file path.

  • length_scale (np.timedelta64, optional) – Length scale, defaults to 40 days.

  • method_name (str, optional) – Method name for the NetCDF file, defaults to ‘ ‘.

Return type:

None

mod_spectral.compute_psd_scores_v2(ds_interp, output_filename, lenght_scale=1500.0, method_name=' ')

Compute power spectral density (PSD) scores using a different method and save them to a NetCDF file.

Parameters:
  • ds_interp (xarray.Dataset) – Interpolated dataset.

  • output_filename (str) – Output NetCDF file path.

  • length_scale (float, optional) – Length scale, defaults to 1500.0.

  • method_name (str, optional) – Method name for the NetCDF file, defaults to ‘ ‘.

Return type:

None

mod_spectral.compute_resolution(lon, lat, wavenumber, psd_diff, psd_ref)

Compute the resolution.

Parameters:
  • lon (numpy.ndarray) – Array of longitudes.

  • lat (numpy.ndarray) – Array of latitudes.

  • wavenumber (numpy.ndarray) – Array of wavenumbers.

  • psd_diff (numpy.ndarray) – Power spectral density of the difference between study and reference.

  • psd_ref (numpy.ndarray) – Power spectral density of the reference.

Returns:

Array of resolution values.

Return type:

numpy.ndarray

mod_spectral.compute_segment(ds_interp, npt, ref_var_name='sla_unfiltered', study_var_name='msla_interpolated', max_time_gap=numpy.timedelta64, segment_overlapping=0.25)

Compute segments of data based on specified criteria.

Parameters:
  • ds_interp (xarray.Dataset) – Interpolated dataset containing relevant variables.

  • npt (int) – Number of points in each segment.

  • ref_var_name (str, optional) – Name of the reference variable. Default is ‘sla_unfiltered’.

  • study_var_name (str, optional) – Name of the study variable. Default is ‘msla_interpolated’.

  • max_time_gap (numpy.timedelta64, optional) – Maximum time gap to consider for segmenting. Default is 2 seconds.

  • segment_overlapping (float, optional) – Overlapping factor between segments. Default is 0.25.

Returns:

  • numpy.ndarray – Array of median longitudes for each segment.

  • numpy.ndarray – Array of median latitudes for each segment.

  • numpy.ndarray – Array of segmented reference variable data.

  • numpy.ndarray – Array of segmented study variable data.

mod_spectral.compute_segment_v2(ds_interp, npt, delta_x, ref_var_name='sla_unfiltered', study_var_name='msla_interpolated', max_time_gap=numpy.timedelta64, segment_overlapping=0.25)

Compute various spectral features for segments of data.

Parameters:
  • ds_interp (xarray.Dataset) – Interpolated dataset containing relevant variables.

  • npt (int) – Number of points in each segment.

  • delta_x (float) – Spatial resolution for spectral computation.

  • ref_var_name (str, optional) – Name of the reference variable. Default is ‘sla_unfiltered’.

  • study_var_name (str, optional) – Name of the study variable. Default is ‘msla_interpolated’.

  • max_time_gap (numpy.timedelta64, optional) – Maximum time gap to consider for segmenting. Default is 2 seconds.

  • segment_overlapping (float, optional) – Overlapping factor between segments. Default is 0.25.

Returns:

  • numpy.ndarray – Array of wavenumbers.

  • numpy.ndarray – Array of latitudes.

  • numpy.ndarray – Array of longitudes.

  • numpy.ndarray – Array of segment counts per grid cell.

  • numpy.ndarray – Array of reference variable power spectral densities.

  • numpy.ndarray – Array of study variable power spectral densities.

  • numpy.ndarray – Array of power spectral densities of the difference between study and reference.

  • numpy.ndarray – Array of coherence values.

  • numpy.ndarray – Array of cross-spectral densities.

mod_spectral.compute_wk(lon, lat, ssh)

Compute the wavenumber-frequency spectrum of sea surface height (SSH) data.

Parameters:
  • lon (array-like) – Longitudes.

  • lat (array-like) – Latitudes.

  • ssh (array-like) – Sea surface height data.

Returns:

  • wavenumber (array) – Isotropic wavenumber values.

  • frequency (array) – Frequency values.

  • SSH_wavenum_freq_spectrum (array) – Two-dimensional wavenumber-frequency spectrum of SSH.

mod_spectral.detrendn(da, axes=None)

Detrend by subtracting out the least-square plane or least-square cubic fit depending on the number of axis.

Parameters:

da (dask.array) – The data to be detrended

Returns:

da – The detrended input data

Return type:

numpy.array

mod_spectral.get_dx_dy(data, navlon, navlat)

Obtain dx and dy from navigation data.

Parameters:
  • data (numpy.ndarray) – Data.

  • navlon (numpy.ndarray) – Longitude.

  • navlat (numpy.ndarray) – Latitude.

Returns:

  • dx (float) – Grid spacing in the x-direction (kilometers).

  • dy (float) – Grid spacing in the y-direction (kilometers).

mod_spectral.get_f_k_in_2D(kradial, wavnum, spec2D)

Compute 2D spectra in frequency-wavenumber space.

Parameters:
  • kradial (numpy.ndarray) – Radial wavenumber values.

  • wavnum (numpy.ndarray) – Wavenumber values.

  • spec2D (numpy.ndarray) – 2D spectrum.

Returns:

spec_1D – 2D spectra in frequency-wavenumber space.

Return type:

numpy.ndarray

mod_spectral.get_f_kx_ky(hat)

Get frequency, kx, and ky from a hat object.

Parameters:

hat (hat object) – The hat object.

Returns:

  • f (numpy.ndarray) – Frequency values.

  • kx (numpy.ndarray) – Wavenumber values in the x-direction.

  • ky (numpy.ndarray) – Wavenumber values in the y-direction.

mod_spectral.get_f_kx_ky_flo(hat)

Get frequency, kx, and ky from a FLOWMOS hat object.

Parameters:

hat (FLOWMOS hat object) – The FLOWMOS hat object.

Returns:

  • f (numpy.ndarray) – Frequency values.

  • kx (numpy.ndarray) – Wavenumber values in the x-direction.

  • ky (numpy.ndarray) – Wavenumber values in the y-direction.

mod_spectral.get_f_kx_ky_jet(hat)

Get frequency, kx, and ky from a JET hat object.

Parameters:

hat (JET hat object) – The JET hat object.

Returns:

  • f (numpy.ndarray) – Frequency values.

  • kx (numpy.ndarray) – Wavenumber values in the x-direction.

  • ky (numpy.ndarray) – Wavenumber values in the y-direction.

mod_spectral.get_f_kx_ky_mit(hat)

Get frequency, kx, and ky from a MITgcm hat object.

Parameters:

hat (MITgcm hat object) – The MITgcm hat object.

Returns:

  • f (numpy.ndarray) – Frequency values.

  • kx (numpy.ndarray) – Wavenumber values in the x-direction.

  • ky (numpy.ndarray) – Wavenumber values in the y-direction.

mod_spectral.get_flux(wavnum2D, wavnum1D, spec_2D)

Compute kinetic energy flux.

Parameters:
  • wavnum2D (numpy.ndarray) – 2D wavenumber values.

  • wavnum1D (numpy.ndarray) – 1D wavenumber values.

  • spec_2D (numpy.ndarray) – 2D spectrum.

Returns:

flux – Kinetic energy flux.

Return type:

numpy.ndarray

mod_spectral.get_flux_in_1D(kradial, wavnum, spec2D)

Compute 1D kinetic energy flux.

Parameters:
  • kradial (numpy.ndarray) – Radial wavenumber values.

  • wavnum (numpy.ndarray) – Wavenumber values.

  • spec2D (numpy.ndarray) – 2D spectrum.

Returns:

flux_1D – 1D kinetic energy flux.

Return type:

numpy.ndarray

mod_spectral.get_spec_1D(kradial, wavnum, spec_2D)

Compute the azimuthal average of the 2D spectrum.

Parameters:
  • kradial (numpy.ndarray) – Radial wavenumber values.

  • wavnum (numpy.ndarray) – Wavenumber values.

  • spec_2D (numpy.ndarray) – 2D spectrum.

Returns:

spec_1D – Azimuthal average of the 2D spectrum.

Return type:

numpy.ndarray

mod_spectral.get_wavnum_kradial(kx, ky)

Compute a wavenumber vector and radial wavenumber.

Parameters:
  • kx (numpy.ndarray) – Wavenumber values in the x-direction.

  • ky (numpy.ndarray) – Wavenumber values in the y-direction.

Returns:

  • wavnum (numpy.ndarray) – Wavenumber values.

  • kradial (numpy.ndarray) – Radial wavenumber values.

mod_spectral.spectral_computation(lon_segment, lat_segment, ref_segments, study_segments, delta_x, npt)

Perform spectral computation and analysis for grid cells.

Parameters:
  • lon_segment (numpy.ndarray) – Array of segment longitudes.

  • lat_segment (numpy.ndarray) – Array of segment latitudes.

  • ref_segments (numpy.ndarray) – Array of reference variable segments.

  • study_segments (numpy.ndarray) – Array of study variable segments.

  • delta_x (float) – Spatial resolution for spectral computation.

  • npt (int) – Number of points in each segment.

Returns:

  • numpy.ndarray – Array of wavenumbers.

  • numpy.ndarray – Array of latitudes.

  • numpy.ndarray – Array of longitudes.

  • numpy.ndarray – Array of segment counts per grid cell.

  • numpy.ndarray – Array of reference variable power spectral densities.

  • numpy.ndarray – Array of study variable power spectral densities.

  • numpy.ndarray – Array of power spectral densities of the difference between study and reference.

  • numpy.ndarray – Array of coherence values.

  • numpy.ndarray – Array of cross-spectral densities.

mod_spectral.spectral_computation_drifters_by_latbin(lon_segment, lat_segment, ref_segments, study_segments, delta_x, npt)

Compute spectral properties of drifters by latitude bin.

Parameters:
  • lon_segment (numpy.ndarray) – Array of segmented longitudes.

  • lat_segment (numpy.ndarray) – Array of segmented latitudes.

  • ref_segments (numpy.ndarray) – Array of segmented reference segments.

  • study_segments (numpy.ndarray) – Array of segmented study segments.

  • delta_x (float) – Delta x value.

  • npt (int) – Number of points.

Returns:

  • wavenumber_to_keep (numpy.ndarray) – Array of wavenumbers.

  • vlat (numpy.ndarray) – Array of latitudes.

  • nb_segment (numpy.ndarray) – Array of segment counts.

  • psd_ref (numpy.ndarray) – Power spectral density of the reference.

  • psd_study (numpy.ndarray) – Power spectral density of the study.

  • psd_diff (numpy.ndarray) – Power spectral density of the difference.

  • coherence (numpy.ndarray) – Magnitude squared coherence.

  • cross_spectrum (numpy.ndarray) – Complex cross-spectrum.

mod_spectral.spectral_computation_v2(lon_segment, lat_segment, ref_segments, study_segments, delta_x, npt)

Perform spectral computation and analysis for grid cells.

Parameters:
  • lon_segment (numpy.ndarray) – Array of segment longitudes.

  • lat_segment (numpy.ndarray) – Array of segment latitudes.

  • ref_segments (numpy.ndarray) – Array of reference variable segments.

  • study_segments (numpy.ndarray) – Array of study variable segments.

  • delta_x (float) – Spatial resolution for spectral computation.

  • npt (int) – Number of points in each segment.

Returns:

  • numpy.ndarray – Array of wavenumbers.

  • numpy.ndarray – Array of latitudes.

  • numpy.ndarray – Array of longitudes.

  • numpy.ndarray – Array of segment counts per grid cell.

  • numpy.ndarray – Array of reference variable power spectral densities.

  • numpy.ndarray – Array of study variable power spectral densities.

  • numpy.ndarray – Array of power spectral densities of the difference between study and reference.

  • numpy.ndarray – Array of coherence values.

  • numpy.ndarray – Array of cross-spectral densities.

mod_spectral.velocity_derivatives(u, v, xdim, ydim, dx)

Compute the derivatives of velocity fields.

Parameters:
  • u (array-like) – Velocity component in the x-direction.

  • v (array-like) – Velocity component in the y-direction.

  • xdim (str) – Name of the x-dimension.

  • ydim (str) – Name of the y-dimension.

  • dx (float) – Grid spacing.

Returns:

ds_derivatives – Dataset containing the derivatives: - ‘u_x’: Derivative of u in the x-direction. - ‘u_y’: Derivative of u in the y-direction. - ‘v_x’: Derivative of v in the x-direction. - ‘v_y’: Derivative of v in the y-direction.

Return type:

xarray.Dataset

mod_spectral.write_psd_current_output(output_netcdf_file, wavenumber, vlat, nb_segment, psd_ref, psd_study, psd_diff_ref_study, coherence, cross_spectrum, method_name='')

Write power spectral density (PSD) current data to a NetCDF file.

Parameters:
  • output_netcdf_file (str) – Output NetCDF file path.

  • wavenumber (numpy.ndarray) – Array of wavenumbers.

  • vlat (numpy.ndarray) – Array of latitudes.

  • nb_segment (numpy.ndarray) – Array of segment counts.

  • psd_ref (numpy.ndarray) – Power spectral density of the reference.

  • psd_study (numpy.ndarray) – Power spectral density of the study.

  • psd_diff_ref_study (numpy.ndarray) – Power spectral density of the difference between study and reference.

  • coherence (numpy.ndarray) – Magnitude squared coherence.

  • cross_spectrum (numpy.ndarray) – Complex cross-spectrum.

  • method_name (str, optional) – Method name for the NetCDF file, defaults to an empty string.

Return type:

None

mod_spectral.write_psd_output(output_netcdf_file, wavenumber, vlat, vlon, nb_segment, psd_ref, psd_study, psd_diff_ref_study, coherence, cross_spectrum, one_sided=True, method_name=' ')

Write power spectral density and related variables to a NetCDF file.

Parameters:
  • output_netcdf_file (str) – Output NetCDF file path.

  • wavenumber (numpy.ndarray) – Array of wavenumbers.

  • vlat (numpy.ndarray) – Array of latitudes.

  • vlon (numpy.ndarray) – Array of longitudes.

  • nb_segment (numpy.ndarray) – Array of segment counts per grid cell.

  • psd_ref (numpy.ndarray) – Power spectral density of the reference field.

  • psd_study (numpy.ndarray) – Power spectral density of the study field.

  • psd_diff_ref_study (numpy.ndarray) – Power spectral density of the difference between study and reference fields.

  • coherence (numpy.ndarray) – Magnitude squared coherence between reference and study fields.

  • cross_spectrum (numpy.ndarray) – Complex cross-spectrum between reference and study fields.

  • one_sided (bool, optional) – Whether to use one-sided wavenumbers, defaults to True.

  • method_name (str, optional) – Method name for the NetCDF file, defaults to ‘ ‘.