marinebon / mbon-dashboard-server

server software for MBON early alert dashboard using Docker
1 stars 2 forks source link

Display issue with time series of directional data #75

Open dotis opened 3 months ago

dotis commented 3 months ago

There is an issue with the display of data that goes from 0 to 360 degrees in grafana. An arithmetic mean does not work, as the mean of 1 degree and 360 degrees will be around 180 degrees. According to NDBC, the only thing that changed at this station is "We did a service visit in mid-June 2023 that converted the station from once per hour transmissions to every 10 minutes (twice per hour waves)."

Here is the wave direction from NDBC for the buoy at Gray's reef: image

Here is the same data from grafana for roughly the same time period. image

7yl4r commented 2 months ago

This one will be an interesting challenge. We might look into alternative visualization panels for display, but I don't see any built-in to deal with polar data.

To deal with the averaging issue at the database level we may be able to use functions in InfluxQL.

Step-by-step Process

  1. Convert degrees to radians
  2. Convert each angle to its Cartesian coordinates (x, y) using COS() and SIN().
  3. Average the x and y coordinates separately using the MEAN() function.
  4. Compute the angle from the averaged x and y values using the ATAN2() function.

PI/180 ~= 0.0174532925199 so step 1 will look something like:

SELECT 0.0174532925199 * "value" FROM "selector"

conceptual example of steps 2-4:

SELECT ATAN2(MEAN(SIN(radians)), MEAN(COS(radians))) AS mean_angle
FROM your_measurement
WHERE time >= 'start_time' AND time <= 'end_time'
GROUP BY time_group

This is going to take some fiddling around in the grafana interface to get right.