I have a Postgres Table of u and v wind velocity components as follows:
ugrid vgrid
-2.7 -6
-2 -6.3
-1.7 -6.4
I need to calculate the speed and direction as per the JScript Psuedocode below and store the result in two new new columns in the table:
uu = U Value
vv = V Value
if vv = 0 and uu = 0 then everything is zero
if vv = 0 and uu > 0 dd = 270, ff = uu
if uu < 0 dd = 90, ff = -uu
if vv < 0 dd=Math.atan(uu/vv)*180/Math.PI
else
dd=Math.atan(uu/vv)*180/Math.PI+180;
if dd <0 dd=dd+360
ff = Math.sqrt(uu*uu+vv*vv)
returns dd as wind direction in decimal degrees
returns ff as wind speed in m/s
I have never worked with mathematical functions in Postgres.
What is the best approach, can a Query handle this type of logic, or should I process the data in some other system first?
If a query is possible, how would I construct it?