Last active
April 16, 2024 13:55
-
-
Save jmquintana79/3761c1a44e68e5557943667c4b49662d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
## angle format: 0/360 to -180/180 | |
def angles_format(angle_0_360:np.array)->np.array: | |
return np.array([v-360 if v>=180 else v for v in angle_0_360]) | |
## aggregation for angular data | |
def angles_agg(angle_0_360:np.array, func_agg) -> float: | |
""" | |
Calculate wind direction average. | |
angle_0_360 -- array of angles to be averaged. | |
func_agg -- aggregation fucntion. | |
return -- aggregated value. | |
""" | |
# validation | |
if len(angle_0_360) == 0: | |
return np.nan | |
# format | |
D0 = angles_format(angle_0_360) | |
# degree to rad | |
ph = D0/180*np.pi | |
# angular to cartesian components | |
Ds = np.sin(ph) | |
Dc = np.cos(ph) | |
# aggregation, cartesian to angular and rad to angular | |
wd0 = 180/np.pi*np.arctan2(func_agg(Ds), func_agg(Dc)) | |
# format -180/180 | |
if wd0 > 180: | |
wd0 -= 360 | |
agg_wd = wd0 | |
# return | |
return agg_wd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## aggregation for angular data: sum | |
def angles_sum(angles:np.array)->float: | |
return angles_agg(angles, func_agg=np.nanmean) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## aggregation for angular data: sum | |
def angles_sum(angles:np.array)->float: | |
return angles_agg(angles, func_agg=np.nansum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## aggregation for angular data: variance | |
def angles_var(angles:np.array)->float: | |
return np.sum((angles - angles_agg(angles, func_agg=np.nanmean))**2) / (len(angles)-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment