Emily Emily - 2 months ago 32
R Question

How to calculate wind direction from U and V wind components in R

I have U and V wind component data and I would like to calculate wind direction from these values in R.

I would like to end up with wind direction data on a scale of 0-360 degrees, with 0° or 360° indicating a wind blowing to the north, 90° indicating a wind blowing to the east, 180° indicating a wind blowing to the south and 270° indicating a wind blowing to the west.

Below is some example data:

> dput(wind)
structure(list(u_ms = c(-3.711, -2.2417, -1.8188, -1.6164, -1.3941,
-1.0682, -0.57611, -1.5698, -1.4976, -1.3537, -1.0901, -0.60403,
-0.70812, -0.49045, -0.39849, 0.17875, 0.48356, 1.5082, 1.4219,
2.5881), v_ms = c(-1.471, -1.6118, -1.6613, -1.7037, -1.7388,
-1.8748, -1.8359, -1.6766, -1.6994, -1.7505, -1.4947, -0.96283,
-1.1194, -0.6849, -0.7847, -0.80349, -0.19352, -0.97815, -1.0835,
-0.81666), u_rad = c(-0.064769155, -0.039125038, -0.031744042,
-0.028211496, -0.02433163, -0.018643603, -0.010055014, -0.027398173,
-0.026138045, -0.023626517, -0.01902583, -0.01054231, -0.012359023,
-0.008559966, -0.006954961, 0.003119775, 0.008439712, 0.02632305,
0.024816831, 0.045170857), v_rad = c(-0.025673788, -0.028131211,
-0.028995149, -0.029735168, -0.030347779, -0.032721426, -0.032042493,
-0.029262184, -0.029660119, -0.030551982, -0.026087431, -0.01680455,
-0.019537212, -0.011953758, -0.013695596, -0.014023543, -0.00337756,
-0.017071935, -0.018910639, -0.014253403)), .Names = c("u_ms",
"v_ms", "u_rad", "v_rad"), class = "data.frame", row.names = c(NA,
-20L))


I have used the following code to try and obtain wind direction (column
td
), but I am not convinced that the returned angles are those that I want (i.e. 0°/360° indicating a wind blowing to the north, 90° indicating a wind blowing to the east etc…).

u = wind$u_rad # u component in radians
v = wind$v_rad # v component in radians

d = (180/pi)*(atan2(u,v))
td = as.matrix(d + 180)
df = cbind(wind, d, td)

> df
u_ms v_ms u_rad v_rad d td
1 -3.71100 -1.47100 -0.064769155 -0.02567379 -111.6228 68.37716
2 -2.24170 -1.61180 -0.039125038 -0.02813121 -125.7164 54.28357
3 -1.81880 -1.66130 -0.031744042 -0.02899515 -132.4087 47.59129
4 -1.61640 -1.70370 -0.028211496 -0.02973517 -136.5062 43.49379
5 -1.39410 -1.73880 -0.024331630 -0.03034778 -141.2788 38.72124
6 -1.06820 -1.87480 -0.018643603 -0.03272143 -150.3269 29.67308
7 -0.57611 -1.83590 -0.010055014 -0.03204249 -162.5780 17.42199
8 -1.56980 -1.67660 -0.027398173 -0.02926218 -136.8842 43.11576
9 -1.49760 -1.69940 -0.026138045 -0.02966012 -138.6118 41.38819
10 -1.35370 -1.75050 -0.023626517 -0.03055198 -142.2844 37.71557
11 -1.09010 -1.49470 -0.019025830 -0.02608743 -143.8963 36.10365
12 -0.60403 -0.96283 -0.010542310 -0.01680455 -147.8980 32.10204
13 -0.70812 -1.11940 -0.012359023 -0.01953721 -147.6830 32.31699
14 -0.49045 -0.68490 -0.008559966 -0.01195376 -144.3939 35.60607
15 -0.39849 -0.78470 -0.006954961 -0.01369560 -153.0774 26.92258
16 0.17875 -0.80349 0.003119775 -0.01402354 167.4578 347.45783
17 0.48356 -0.19352 0.008439712 -0.00337756 111.8112 291.81121
18 1.50820 -0.97815 0.026323050 -0.01707193 122.9656 302.96561
19 1.42190 -1.08350 0.024816831 -0.01891064 127.3077 307.30771
20 2.58810 -0.81666 0.045170857 -0.01425340 107.5128 287.51279


I would appreciate any advice on whether my method is correct, and if not how I could correctly obtain the desired wind direction values. While Calculating wind direction from U and V components of the wind using lapply or ifelse was helpful, the code did work with my data, and I am sure that there is an easier away to obtain wind direction. Many thanks!

Answer

There are three problems with this:

  1. You cannot convert m/s to radians. In order to input wind components into atan2, you must normalize them, but you don't do this by multiplying m/s by pi/180 (which you did to get u_rad and v_rad). You should make a column of absolute windspeed (sqrt(u_ms^2 + v_ms^2)) and take atan2(u_ms/wind_abs, v_ms/wind_abs). (also note that atan2 takes y component first - make sure that's what you want)
  2. atan2 will give you an answer in the unit circle coordinates, which increase counterclockwise and have a zero on the x-axis. You want an answer in cardinal coordinates which increase clockwise and have a zero on the y-axis. To convert unit circle to cardinal coordinates, you must subtract the unit circle angle from 90.
  3. You must know whether the wind info refers to the direction the wind is coming from (standard for cardinal coordinates) or the direction the wind is blowing (standard for trig/vector operations)

If you are given u_ms = = -3.711 and v_ms = -1.471 (on the unit circle it is blowing down and slightly to the left, so it is coming from the northeast), then:

wind_abs = sqrt(u_ms^2 + v_ms^2)
wind_dir_trig_to = atan2(u_ms/wind_abs, v_ms/wind_abs) 
wind_dir_trig_to_degrees = wind_dir_trig_to * 180/pi ## -111.6 degrees

Then you must convert this wind vector to the meteorological convention of the direction the wind is coming from:

wind_dir_trig_from_degrees = wind_dir_trig_to_degrees + 180 ## 68.38 degrees

Then you must convert that angle from "trig" coordinates to cardinal coordinates:

wind_dir_cardinal = 90 - wind_dir_trig_from_degrees
[1] 21.62284 #From the northeast.