netsse.tools.misc_func#
Miscellaneous functions for NetSSE.
Copyright (C) 2023-2026 Technical University of Denmark, R.E.G. Mounet
This code is part of the NetSSE software.
NetSSE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
NetSSE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
To credit the author, users are encouraged to use below reference:
Mounet, R. E. G., & Nielsen, U. D. NetSSE: An open-source Python package
for network-based sea state estimation from ships, buoys, and other
observation platforms (version 2.2). Technical University of Denmark,
GitLab. February 2026. https://doi.org/10.11583/DTU.26379811.
Last updated on 20-02-2026 by R.E.G. Mounet
Functions#
|
Calculates the signed difference between two angles. |
|
Interpolates angular data. |
|
Calculates the mean angle from an array of angular data. |
|
Calculates the circular standard deviation of an array of |
|
Checks whether two matrices are identical, element-wise. |
|
Checks whether two vectors are identical, element-wise. |
|
Finds the nearest neighbours to a series of waypoints within a grid |
|
Randomly generates a cryptographically secure string of a given size. |
|
Converts a speed from knots to meters per second. |
|
Converts a speed from meters per second to knots. |
|
Adds zeros to an array along one of its dimensions. |
|
Converts angles to a new range (of extent 360 degrees). |
|
Computes the q-th quantiles of weighted data. |
|
Computes the weighted standard deviation of some data. |
|
Wraps an array around one of its dimensions. |
Module Contents#
- netsse.tools.misc_func.ang_diff(alpha, beta, unit='deg')[source]#
Calculates the signed difference between two angles.
- Parameters:
- Returns:
delta – The output difference between alpha and beta in the range [-180,180] degrees.
Note
This function subtracts
alphafrombeta, i.e., computes the signed differencebeta-alpha, taking into account the circularity at 360 degrees.- Return type:
float or array_like
See also
Example
>>> delta = ang_diff(alpha, beta, unit='deg')
- netsse.tools.misc_func.ang_interp(x, alpha, xq, axis=0, unit='deg', datetime=False)[source]#
Interpolates angular data.
This function properly handles the circularity of angles at 360 degrees.
- Parameters:
x (array_like) – Array of independent variable where the angles
alphaare defined.alpha (array_like) – Array of angles to be interpolated.
xq (array_like) – Array of independent variable where the angles
alphamust be interpolated.axis ({0,1}, optional) – If
alphais a 2d-array, the axis ofalphaalong which to interpolate. The default is0.unit ({'deg','rad'}, optional) – Specifies the unit of the angles as
'rad'or'deg'. The default is'deg'.datetime (bool, default False) – If
True, thenxandxqare assumed to be arrays of datetime objects.
- Returns:
alpha_q – Interpolated angles at
xq.- Return type:
array_like
See also
Example
>>> alpha_q = ang_interp(x,alpha,xq,axis=0,unit='deg')
- netsse.tools.misc_func.ang_mean(angles, unit='deg')[source]#
Calculates the mean angle from an array of angular data.
This function properly handles the circularity of angles at 360 degrees.
- Parameters:
angles (array_like) – Array of angles.
unit ({'deg','rad'}, optional) – Specifies the unit of the angles as
'rad'or'deg'. The default is'deg'.
- Returns:
mean_a – Mean angle in the range [-180,180) degrees.
- Return type:
See also
Example
>>> mean_angle = ang_mean(angles, unit='deg')
- netsse.tools.misc_func.ang_std(angles, unit='deg')[source]#
Calculates the circular standard deviation of an array of angular data.
This function properly handles the circularity of angles at 360 degrees. The standard “two-pass” computation is implemented.
- Parameters:
angles (array_like) – Array of angles.
unit ({'deg','rad'}, optional) – Specifies the unit of the angles as
'rad'or'deg'. The default is'deg'.
- Returns:
std_a – Circular standard deviation.
- Return type:
See also
Example
>>> std_angle = ang_std(angles, unit='deg')
- netsse.tools.misc_func.areSame_mat(A, B)[source]#
Checks whether two matrices are identical, element-wise.
- Parameters:
A (2d-array) – Input matrix.
B (2d-array) – Input matrix.
- Returns:
FalseforA!=B,TrueforA=B.- Return type:
Note
If
AandBdo not have the same shape, thenFalseis returned.See also
areSame_vecChecks whether two vectors are identical, element-wise.
Example
>>> areSame_mat(A,B)
- netsse.tools.misc_func.areSame_vec(a, b)[source]#
Checks whether two vectors are identical, element-wise.
- Parameters:
a (1d-array) – Input vector.
b (1d-array) – Input vector.
- Returns:
Falsefora!=b,Truefora=b.- Return type:
Note
If
aandbdo not have the same shape, thenFalseis returned.See also
areSame_matChecks whether two matrices are identical, element-wise.
Example
>>> areSame_vec(a,b)
- netsse.tools.misc_func.find_nearest_gridpoint(lat_wps, lon_wps, lat_grid, lon_grid, interv=0.01)[source]#
Finds the nearest neighbours to a series of waypoints within a grid of points.
Note
The grid points do not need to be uniformly spaced in any direction. The grid can also have been rotated of any angle about the vertical direction (e.g., to be aligned with some shoreline).
For a given waypoint at
(lat_wp,lon_wp), the nearest-neighbour candidates are first selected within the gridpoints that fall in the geographical area delimited bylat_wp*[1-interv,1+interv]in latitude andlon_wp*[1-interv,1+interv]in longitude. Then, the distance between the waypoint and the nearest-neighbour candidates is computed. The output neighbour is found as the candidate that minimises this distance.- Parameters:
lat_wps (float, or 1d-array of shape (Nwp,)) – Vector of waypoint latitudes [deg].
lon_wps (float, or 1d-array of shape (Nwp,)) – Vector of waypoint longitudes [deg].
lat_grid (2d-array) – Matrix defining the latitudes of the grid points [deg].
lon_grid (2d-array) – Matrix defining the longitudes of the grid points [deg].
interv (float, default 0.01) – Half-width [-] of the interval band fraction in latitude and longitude, used for screening the potential nearest-neighbour candidates.
- Returns:
index_nearest (numpy.array of shape (Nwp,2)) – The coordinates in (latitude,longitude) of the nearest gridpoints to the waypoints [deg].
dist_nearest (numpy.array of shape (Nwp,)) – The distance between the nearest gridpoints and the waypoints [km].
Example
>>> index_nearest, dist_nearest = ... find_nearest_gridpoint(lat_wps, lon_wps, lat_grid, lon_grid, interv=0.01)
- netsse.tools.misc_func.id_generator(size=6, chars=string.ascii_uppercase + string.digits)[source]#
Randomly generates a cryptographically secure string of a given size.
- Parameters:
- Returns:
Output random string
- Return type:
Example
>>> id_generator(size=6, chars=string.ascii_uppercase+string.digits)
- netsse.tools.misc_func.knots2ms(U_kn)[source]#
Converts a speed from knots to meters per second.
- Parameters:
U_kn (float or array_like) – Speed in knots [kn].
- Returns:
U_ms – Speed in meters per second [m/s].
- Return type:
float or array_like
See also
ms2knotsConverts a speed from meters per second to knots.
Example
>>> U_ms = knots2ms(U_kn)
- netsse.tools.misc_func.ms2knots(U_ms)[source]#
Converts a speed from meters per second to knots.
- Parameters:
U_ms (float or array_like) – Speed in meters per second [m/s].
- Returns:
U_kn – Speed in knots [kn].
- Return type:
float or array_like
See also
knots2msConverts a speed from knots to meters per second.
Example
>>> U_kn = ms2knots(U_ms)
- netsse.tools.misc_func.pad(U, axis=1, add=0)[source]#
Adds zeros to an array along one of its dimensions.
- Parameters:
U (array_like of shape (n,m) or (n,)) – Input array to be padded with zeros at the beginning.
axis ({1,0}, optional) – If
Uis a 2d-array, the axis ofUalong which to pad. The default is1.add (float or array_like, default 0) – Term that is added to the concatenated zeros.
- Returns:
U_pad – Extended array, where
Uhas been padded with a first column (respectively, row) of zeros (plus an optional added term).Note
If
Uis a 2d-array, then the output arrayU_padhas a new shape of(n+1*(axis==0),m+1*(axis==1)).If
Uis a 1d-array, then the output arrayU_padhas a new shape of(n+1,). A zero element has been added at the beginning.
- Return type:
array_like
See also
wrapWraps an array around one of its dimensions.
Example
>>> U_wrap = pad(U,axis=1,add=0)
- netsse.tools.misc_func.re_range(theta, start=0, unit='deg')[source]#
Converts angles to a new range (of extent 360 degrees).
- Parameters:
theta (array_like) – Array of angles to be rearranged.
start (float, default 0) – Lower boundary of the range that should contain the new values of
theta. The upper boundary will be set automatically tostart + 360degrees.unit ({'deg','rad'}, optional) – Specifies the unit of
thetaandstartas'rad'or'deg'. The default is'deg'.
- Returns:
theta_rerange – Array of angles where the elements of
thetahave been converted to the new range[start, start+360)degrees.- Return type:
array_like
See also
ang_diffCalculates the signed difference between two angles.
Example
>>> theta_rerange = re_range(theta,start=0,unit='deg')
- netsse.tools.misc_func.weighted_quantile(x, q, w, a=0.5, b=0.5, axis=-1)[source]#
Computes the q-th quantiles of weighted data.
The quantiles are computed along one of the axes of the input array
x.- Parameters:
x (array_like) – Input data array for which the quantiles must be computed.
q (1d-array of shape (Nq,)) – Sequence of quantiles to compute, which must be between 0 and 1.
w (array_like) – Array of weights. The arrays
xandwmust be broadcastable.a (floats, default 0.5) – User-defined constant used in the computation of the quantiles. The default is 0.5 to minimize biases (Rogers, 2003).
b (floats, default 0.5) – User-defined constant used in the computation of the quantiles. The default is 0.5 to minimize biases (Rogers, 2003).
axis (integer, default -1) – Axis of
xalong which theq-quantiles are computed.
- Returns:
v – Values of the
q-quantiles computed along the specifiedaxisof the input arrayxwith weightsw.- Return type:
array_like
See also
weighted_stdComputes the weighted standard deviation of some data.
References
Rogers, J.W., 2003. Estimating the variance of percentiles using replicate weights, in: Proc. of the Joint Statistical Meetings, Survey Research Methods Section, American Statistical Association. pp. 3525–3532. URL: http://www.asasrms.org/Proceedings/y2003/Files/JSM2003-000742.pdf
Example
>>> v = weighted_quantile(x, q, w, a=0.5, b=0.5, axis=-1)
- netsse.tools.misc_func.weighted_std(a, w, axis=None, ddof=1)[source]#
Computes the weighted standard deviation of some data.
The weighted standard deviation is a measure of the spread of a distribution of the array elements from the mean, where some of the elements are more significant than others. The weighted standard deviation is calculated based on the weighted mean and it attaches more importance to data that have more weight than to data with less weight (National Institute of Standards and Technology, 1996).
- Parameters:
a (array_like) – Variable for which the weighted std. must be computed.
w (array_like) – Array of weights. All elements must be positive. The array
wmust broadcastable with the arraya.axis (None or int, optional) – Axis or axes along which the standard deviation is computed. The default is to compute the weighted standard deviation of the flattened array.
ddof (int, default 0) – Delta Degrees of Freedom. The divisor used in the calculations is
(N_nz - ddof)/N_nz, whereN_nzrepresents the number of non-zero weights.
- Returns:
std – Weighted standard deviation of the data.
- Return type:
array_like
See also
weighted_quantileComputes the q-th quantiles of weighted data.
References
National Institute of Standards and Technology, 1996. Formula for the weighted standard deviation. Available at: https://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weightsd.pdf (Consulted on 06-08-2023).
Example
>>> std = weighted_std(a,w,axis=None,ddof=1)
- netsse.tools.misc_func.wrap(U, axis=1, add=0)[source]#
Wraps an array around one of its dimensions.
- Parameters:
U (array_like of shape (n,m) or (n,)) – Input array to be wrapped around.
axis ({1,0}, optional) – If
Uis a 2d-array, the axis ofUalong which to wrap. The default is1.add (float or array_like, default 0) – Term that is added to the repeated part of
Ubefore concatenation.
- Returns:
U_wrap – Extended array, where the first column (respectively, row) of
Uhas been repeated by concatenation at the end ofU.Note
If
Uis a 2d-array, then the output array has a shape of(n+1*(axis==0),m+1*(axis==1)).If
Uis a 1d-array, then the output array has a shape of(n+1,). The first element ofUhas been repeated at the end.
- Return type:
array_like
See also
padAdds zeros to an array along one of its dimensions.
Example
>>> U_wrap = wrap(U,axis=1,add=0)