Calculation of wind direction from U and V wind components using a lappel or ifelse

Averaging the scalar wind direction data gives inaccurate values ​​due to compass headers in the range from 0 to 360 degrees, so I already converted my list to u and v components from direction and tilt angles.

In order to discard the correct wind direction, for averaging I need to develop some ifelse function applicable for the following three scenarios:

V > 0...((180 / pi) * atan((Ucomp/Vcomp)) + 180) U and V < 0...((180 / pi) * atan((Ucomp/Vcomp)) + 0) U > 0 and V < 0...((180 / pi) * atan((Ucomp/Vcomp)) + 360) 

In the dataset I'm looking for analysis, Ucomp is greater than 0 and Vcomp is less than zero, but there will undoubtedly be scenarios in which all three will be deployed, so I need a function for parsing and calculating iteratively and applying the correct formula for each time step. I have not used functions or functions before, so I played with them, did not work.

I provide sample data below ...

  DateTime Wind.Spd Wind.Direction Air.Density Temp.C GEP.GE16XLE GCF.GE16XLE Ucomp Vcomp 1 1981 7.662370 248.3395 0.9148207 11.28967 597.7513 37.35946 5.253453 -0.7404972 2 1982 8.199412 251.6763 0.9172176 10.12751 678.8595 42.42872 5.867979 -0.6191475 3 1983 8.188782 251.7889 0.9162767 10.30619 667.9461 41.74663 5.777208 -1.0473982 4 1984 7.942632 246.7908 0.9174074 10.05093 642.6374 40.16484 5.415773 -0.6796723 5 1985 8.016558 252.7305 0.9171721 10.38414 654.2588 40.89117 5.649406 -0.9999082 6 1986 7.739984 249.6431 0.9158740 10.99859 607.0542 37.94089 5.305971 -0.9118965 
+4
r
source share
4 answers

First define a function to calculate:

 windDir <- function(u, v) { if(v > 0) ((180 / pi) * atan(u/v) + 180) if(u < 0 & v < 0) ((180 / pi) * atan(u/v) + 0) if(u > 0 & v < 0) ((180 / pi) * atan(u/v) + 360) } 

Then apply it to each row. Here I use ddply , which is a good “applicable” variety for data frames:

 > library(plyr) > ddply(data, 'DateTime', summarize, windDir=windDir(Ucomp, Vcomp)) DateTime windDir 1 1981 278.0232 2 1982 276.0232 3 1983 280.2760 4 1984 277.1531 5 1985 280.0370 6 1986 279.7517 
+5
source share

You should look at using the atan2 function, it will probably eliminate the need for all if statements and additional calculations.

If you do a lot with directions, you should also look into the circular and CircStats , which take care of all these details for you (some of them look like what you did, just more automatic).

+8
source share

Departure:

https://www.eol.ucar.edu/content/wind-direction-quick-reference

In short, you want to use atan2 to take care of different quadrants, but don't forget that angles are defined differently in meteorology! We measure the wind direction in degrees clockwise from the north, while atan2-type functions usually work in radians from the X-direction (i.e. east). So you want to use something like:

WDIR = 270-atan2 (V, U) * 180 / pi

Add% 360 to provide an output between 0 and 360:

WDIR = (270-atan2 (V, U) * 180 / pi)% 360

+3
source share
 windDir <- function(u, v) { (180 / pi) * atan(u/v) + ifelse(v>0,180,ifelse(u>0,360,0)) } 
+2
source share

All Articles