|
本帖最后由 浅冰Column 于 2024-8-25 17:33 编辑
搭建志愿站的过程中,查询国标得知计算平均风向需要采用单位矢量平均法。
网上关于该问题的帖子稀少,特用 LaTeX 整理公式和几种主要语言的实例代码,供各位使用。
公式
需要 LaTex 公式的朋友可以查看我的博客(https://blog.birdbird.fun/archives/63),因为论坛没有 LaTex,此处公式提供图片。
代码
1. Python
- import math
- # 单位矢量平均法计算平均风向
- def calculate_average_wind_direction(wind_directions):
- # 转为弧度制
- wind_directions_rad = [math.radians(d) for d in wind_directions]
-
- # 计算平均值
- X_avg = sum(math.sin(d) for d in wind_directions_rad) / len(wind_directions)
- Y_avg = sum(math.cos(d) for d in wind_directions_rad) / len(wind_directions)
-
- # 计算风向平均值的弧度
- average_wind_direction_rad = math.atan2(X_avg, Y_avg)
-
- # 转回角度制
- average_wind_direction_deg = math.degrees(average_wind_direction_rad)
-
- # 出界修正
- if average_wind_direction_deg < 0:
- average_wind_direction_deg += 360
-
- return average_wind_direction_deg
复制代码
2. C++
- #include <vector>
- #include <cmath>
- #include <numeric>
-
- double calculate_average_wind_direction(const std::vector<double>& wind_directions) {
- // 转为弧度制
- std::vector<double> wind_directions_rad;
- wind_directions_rad.reserve(wind_directions.size());
- for (double d : wind_directions) {
- wind_directions_rad.push_back(d * M_PI / 180.0);
- }
-
- // 计算平均值
- double X_avg = std::accumulate(wind_directions_rad.begin(), wind_directions_rad.end(), 0.0,
- [](double sum, double d) { return sum + std::sin(d); }) / wind_directions.size();
-
- double Y_avg = std::accumulate(wind_directions_rad.begin(), wind_directions_rad.end(), 0.0,
- [](double sum, double d) { return sum + std::cos(d); }) / wind_directions.size();
-
- // 计算风向平均值的弧度
- double average_wind_direction_rad = std::atan2(X_avg, Y_avg);
-
- // 转回角度制
- double average_wind_direction_deg = average_wind_direction_rad * 180.0 / M_PI;
-
- // 出界修正
- if (average_wind_direction_deg < 0) {
- average_wind_direction_deg += 360.0;
- }
-
- return average_wind_direction_deg;
- }
复制代码
3. C#
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- public class WindDirectionCalculator
- {
- public static double CalculateAverageWindDirection(List<double> windDirections)
- {
- // 转为弧度制
- var windDirectionsRad = windDirections.Select(d => d * Math.PI / 180.0).ToList();
-
- // 计算平均值
- double X_avg = windDirectionsRad.Select(d => Math.Sin(d)).Average();
- double Y_avg = windDirectionsRad.Select(d => Math.Cos(d)).Average();
-
- // 计算风向平均值的弧度
- double averageWindDirectionRad = Math.Atan2(X_avg, Y_avg);
-
- // 转回角度制
- double averageWindDirectionDeg = averageWindDirectionRad * 180.0 / Math.PI;
-
- // 出界修正
- if (averageWindDirectionDeg < 0)
- {
- averageWindDirectionDeg += 360.0;
- }
-
- return averageWindDirectionDeg;
- }
- }
复制代码
4. Java
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- public class WindDirectionCalculator
- {
- public static double CalculateAverageWindDirection(List<double> windDirections)
- {
- // 转为弧度制
- var windDirectionsRad = windDirections.Select(d => d * Math.PI / 180.0).ToList();
-
- // 计算平均值
- double X_avg = windDirectionsRad.Select(d => Math.Sin(d)).Average();
- double Y_avg = windDirectionsRad.Select(d => Math.Cos(d)).Average();
-
- // 计算风向平均值的弧度
- double averageWindDirectionRad = Math.Atan2(X_avg, Y_avg);
-
- // 转回角度制
- double averageWindDirectionDeg = averageWindDirectionRad * 180.0 / Math.PI;
-
- // 出界修正
- if (averageWindDirectionDeg < 0)
- {
- averageWindDirectionDeg += 360.0;
- }
-
- return averageWindDirectionDeg;
- }
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|