Skip to content

Commit

Permalink
Merge branch 'optimize_circstd_implementation' of https://github.com/…
Browse files Browse the repository at this point in the history
…sede-open/pyELQ into optimize_circstd_implementation
  • Loading branch information
TannazH committed Aug 30, 2024
2 parents 5819fa5 + 98a620b commit 94321df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/pyelq/meteorology.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,28 @@ def calculate_uv_from_wind_speed_direction(self) -> None:
def calculate_wind_turbulence_horizontal(self, window: str) -> None:
"""Calculate the horizontal wind turbulence values from the wind direction attribute.
Wind turbulence values are calculated as the circular standard deviation defined as
sqrt(-2 log |1 / n sum_{i=1}^{n}(e^(ix_k) | )
Wind turbulence values are calculated as the circular standard deviation defined as
sqrt(-2 log |1 / n sum_{i=1}^{n}(e^(ix_k) | )
(https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.circstd.html)
where i is the imaginary unit, x_k
is the wind direction, n is the number of observations in the window and |.| denotes
where i is the imaginary unit, x_k
is the wind direction, n is the number of observations in the window and |.| denotes
the length of the complex value.
Using Euler formula e^(ix_k) can be exressed as cos(x_k) + i sin(x_k) and the The term
in the || expresses the mean of the complex values over the rolling window. Therefore,
rolling mean can be applied to the sin and cos values of the wind direction. The length
of the complex value is then calculated as the square root of the sum of the squares of
the sin and cos values (hypotenuse). and the wind turbulence is calculated as
sqrt(-2 log(hypotenuse)).
the sin and cos values (hypotenuse). and the wind turbulence is calculated as
sqrt(-2 log(hypotenuse)).
This calculation is equivalent to using the circstd function from scipy.stats as an apply
function on a rolling window. However, using the rolling mean on sin and cos speeds up
the calculation by a factor of 100.
the calculation by a factor of 100.
Outputted values are calculated at the center of the window and at least 3 observations are required in a
window for the calculation. If the window contains less values the result will be np.nan.
The result of the calculation will be stored as the wind_turbulence_horizontal attribute.
Args:
window (str): The size of the window in which values are aggregated specified as an offset alias:
https://pandas.pydata.org/docs/user_guide/timeseries.html#timeseries-offset-aliases
Expand All @@ -135,7 +135,7 @@ def calculate_wind_turbulence_horizontal(self, window: str) -> None:
cos_data_series = np.cos(data_series)
sin_rolling = sin_data_series.rolling(window=window, center=True, min_periods=3).mean()
cos_rolling = cos_data_series.rolling(window=window, center=True, min_periods=3).mean()
hypotenuse = (sin_rolling ** 2 + cos_rolling ** 2) ** 0.5
hypotenuse = (sin_rolling**2 + cos_rolling**2) ** 0.5
aggregated_data = np.sqrt(-2 * np.log(hypotenuse)) * 180 / np.pi
self.wind_turbulence_horizontal = aggregated_data.values

Expand Down
4 changes: 2 additions & 2 deletions tests/test_meteorology.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

import numpy as np
import pandas as pd
from scipy.stats import circstd
import pytest
from pytictoc import TicToc
from scipy.stats import circstd

from pyelq.coordinate_system import LLA
from pyelq.meteorology import Meteorology, MeteorologyGroup
Expand Down Expand Up @@ -226,7 +227,6 @@ def test_calculate_wind_turbulence_horizontal():
wind_turbulance_circstd = data_series.rolling(window="300s", center=True, min_periods=3).apply(
circstd, kwargs={"low": 0, "high": 360})


tolerance = 3 * np.std(met.wind_turbulence_horizontal)
mean_turbulence = np.mean(met.wind_turbulence_horizontal)
mean_turbilance_circstd = np.mean(wind_turbulance_circstd)
Expand Down

0 comments on commit 94321df

Please sign in to comment.