November 28, 2022

Calculate wind using U and V GRIB components, NodeRed, JS

Updated 2022-11-28 !

In a former post I showed how to download and extract data from forecast GRIB files but avoided to handle if the wind direction and speed was declared as U and V components, vectors. 

In this post I will extract wind data from a GRIB forecast file and use the U V components to calculate, and display, wind speed and direction at a chosen point. The data is visualized as graphs in a NodeRed dashboard.

Normally, using charts in NodeRed, the timestamp is taken from the time when the input message is created but here I will use the timestamp from the forecast, loading the whole chart in one go.

Intro
As in the former post I'm using the GRIB files from OpenSkiron site and their forecast file for the  Copenhagen area. I'm fetching, with wget, the GRIB file in the morning, CET, so the file is named 

Copenhagen_ICON-D2_EWAM_YYYYMMDD-00.grb2

To extract the wind components the "grib_ls" command is used. The command is included in the ecCodes package and installation is described in a former post.

NodeRed and "node-red-dashboard" is used to show the wind data in charts.

Math
Extracting data from the GRIB file could look like this

$ grib_ls cph.grib -l 56.12,12.60,1 -w shortName=10u/10v -p shortName,count,dataDate,validityDate,validityTime
cph.grib
shortName    count dataDate validityDate validityTime value
10u            539 20221101 20221101        0          -3.31436
10u            540 20221101 20221101        100        -4.47415
10u            541 20221101 20221101        200        -3.67762
10u            542 20221101 20221101        300        -4.08878
10u            543 20221101 20221101        400        -3.80177
....
....
10v            588 20221101 20221101        0          1.0684
10v            589 20221101 20221101        100        3.23086
10v            590 20221101 20221101        200        2.61733
10v            591 20221101 20221101        300        2.27063
10v            592 20221101 20221101        400        3.99174   
....
....

To calculate the windspeed, m/s, use 
=> WindSpeed, ws = sqrt(u2+v2)
which "translated" to JavaScript is

let windSpeed = Math.sqrt(Math.pow(u, 2) + Math.pow(v, 2))

for wind direction in degrees, using a spreadsheet
=> WindDir, ϕ=mod(180+180/π * atan2(u,v),360)
which gives JavaScript

let windDir = (180 + (180 / Math.PI) * (Math.atan2(u, v))) % 360;

The flow

The upper part of the flow is creating two CSV files with the U respective V values. JS code 

let outPut = shortName + ";" + validityDate + ";" + validityTime + ";" + value

The lower part, after a delay of 3 minutes, creates two flow context arrays from the CSV files. These arrays are then, after another 5 seconds delay, used to calculate the wind, speed and direction, together with the forecast timestamp. These values are sent to the line chart nodes.

Get the flow here.

Remarks
NodRed
Here you can find extra information on how to insert extra data points, in charts, by specifying the "timestamp" property.

Error handling
The error handling could be improved 😉, but this was more a POC then actual "production".

No comments:

Post a Comment

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !