Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating wind direction from U and V components of the wind using lapply or ifelse

Tags:

r

Averaging scalar wind direction data yields inaccurate values due to the compass headings ranging from 0-360 degrees, so I have converted my list to u and v components from the magnitude and wind direction angles already.

In order to back out the proper wind direction, for averaging purposes, I need to develop some sort of apply, ifelse, function for the 3 following 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 data set I am looking to analyze, Ucomp is greater than 0 and Vcomp is less than zero, but there will undoubtedly be scenarios where all 3 will pan out, so I need a function to parse through and calculate iteratively and applying the correct formula for each time step. I have not used lapply or functions before, so me playing around with them has not worked.

I provide a sample of 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
like image 254
RWJ Avatar asked Dec 29 '11 20:12

RWJ


People also ask

How do we calculate wind direction?

Meteorological wind direction is defined as the direction from which it originates. For example, a northerly wind blows from the north to the south. Wind direction is measured in degrees clockwise from due north. Hence, a wind coming from the south has a wind direction of 180 degrees; one from the east is 90 degrees.

What are the U and V components of wind?

The U wind component is parallel to the x-axis (i.e. longitude). A positive U wind comes from the west, and a negative U wind comes from the east. The V wind component is parallel to the y- axis (i.e. latitude). A positive V wind comes from the south, and a negative V wind comes from the north.

How do you calculate wind speed and direction?

For those of you in the future that may be wondering. To find the wind speed = apply this formula WS=SQRT((x^2)+(y^2)) If it is in Meters per Second, be sure to convert to Knots if needed. To find the wind direction = apply this formula WD=ATAN2(y,x) where Y == to North/South vector and the X == the East/West vector.


2 Answers

You should look at using the atan2 function, it will probably remove the need for all the if statements and extra computing.

If you are doing a lot with directions then you should also look into the circular and CircStats packages which take care of a lot of these details for you (some is similar to what you did, just more automatic).

like image 183
Greg Snow Avatar answered Sep 28 '22 07:09

Greg Snow


First define the function to do the calculation:

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'm using ddply, which is a nice "apply" 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
like image 24
John Colby Avatar answered Sep 28 '22 06:09

John Colby