[英]Diff of two wind directions
I want to plot graph showing difference of two wind directions (measured by two different sensors not at the same place) during the time period.我想绘制显示一段时间内两个风向差异的图表(由不在同一地点的两个不同传感器测量)。
I have both values presented as azimuth, ie north = 0, east = 90, south = 180, west = 270. I am thinking about the following requirements:我将两个值都显示为方位角,即北 = 0、东 = 90、南 = 180、西 = 270。我正在考虑以下要求:
I think that this requirements can also be understand so that I want to rotate reference system to make valueA get azimuth = 0 and then return the azimuth of value B on the scale -180.. +180.我认为这个要求也可以理解,所以我想旋转参考系统以使 valueA 获得方位角 = 0,然后返回值 B 在比例尺上的方位角 -180.. +180。
I have something like this:我有这样的事情:
diff = abs(valueB - valueA)
if (diff >= 180) diff = 360 - diff
if (valueA < 180) offsetA = valueA else offsetA = valueA - 360
offsetB = valueB - offsetA
if (offsetB < 0) offsetB = offsetB + 360
if (offsetB >= 360) offsetB = offsetB - 360
if (offsetB > 180) diff = (-1) * diff
Question 1: Is shorter code possible?问题 1:是否可以使用更短的代码?
Question 2: Is there a better presentation of difference between two wind directions?问题2:两个风向之间的差异有更好的表述吗? I do not see how Wind Rose (which is nice for presentation of speed + direction) could be used to present the difference between two directions during the time period.我看不出如何使用 Wind Rose(非常适合表示速度 + 方向)来表示该时间段内两个方向之间的差异。
Question 3: I will use Python or R. Are there already such function implemented in some library?问题3:我会用Python或者R,有没有库已经实现了这样的功能?
If another SE forum is more appropriate, please suggest.如果其他SE论坛更合适,请提出建议。
In a CodeProject article I wrote way back in 2011 "Circular Values Math and Statistics with C++11" I addressed the same problem.在我早在 2011 年写的一篇 CodeProject 文章“C++11 的循环值数学和统计”中,我解决了同样的问题。 I called it the directed distance between two circular values .我称它为两个圆形值之间的定向距离。
If the inputs are in the range [0..360) you can use the following:如果输入在 [0..360) 范围内,您可以使用以下内容:
// the length of the directed walk from c1 to c2, with the lowest absolute-value length
// return value is in [-180, 180)
double Sdist(double c1, double c2)
{
double d = c2 - c1;
if (d < -180.) return d + 180.;
if (d >= 180.) return d - 180.;
return d ;
}
I also defined another distance type for circular values: the increasing distance (clockwise).我还为圆形值定义了另一种距离类型:递增距离(顺时针)。
// the length of the increasing walk from c1 to c2 with the lowest length
// return value is in [0, 360)
double Pdist(double c1, double c2)
{
return c2 >= c1 ? c2 - c1 : 360. - c1 + c2;
}
See sections 8 and 9 in the article.请参阅文章中的第 8 和第 9 节。 The article and source code with some fixes are also available on GitHub . GitHub 上还提供了这篇文章和带有一些修复的源代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.