I have a wind rose with 72 evenly-spaced directions, spanning 360°, each describing a direction-specific average wind speed and associated probability. We must condense this information to 36 evenly-spaced direction bins. "probability" is the normalized frequency of occurrences. The probabilities should sum to one. If 5° has a 0.1 probability. that means that the wind came from that direction 10% of the time.
I average across the midpoints of each bin, which results in undesirably shifting the wind rose so that the center is at 2.5 degrees. Is there a more elegant way to do this in theory or implementation?
import pandas as pd
import numpy as np
import wget
# Load data!
url = 'https://raw.githubusercontent.com/scls19fr/windrose/master/samples/amalia_directionally_averaged_speeds.txt'
wget.download(url, 'amalia_directionally_averaged_speeds.txt')
dat = pd.read_csv('./amalia_directionally_averaged_speeds.txt',
sep=r'\s+', header=None, skiprows=[0])
dat.columns = ['direction', 'average_speed', 'probability']
# two steps
# 1. average frequency across speeds
# 2. compute average speed weighted by frequencies
# (the scheme is to average across both midpoints. this
# shift the centers by 2.5 degrees and bin by 10)
directions, speeds, frequencies = [], [], []
for i in range(dat.shape[0] / 2):
directions.append(dat.direction[i * 2] + 2.5)
frequencies.append(np.mean(dat.probability[[i * 2, i * 2 + 1]].values))
speeds.append((dat.direction[i * 2] * dat.average_speed[i*2] +
dat.direction[i*2 + 1] * dat.average_speed[i * 2 + 1]) /
(dat.direction[i * 2] + dat.direction[i * 2+1]))
# save data
done = pd.DataFrame({'direction': directions,
'speed': speeds, 'probability': frequencies})
done.to_csv('wind_rose.csv')
dat = pd.read_csv('./amalia_directionally_averaged_speeds.txt', sep=r'\s+')
. \$\endgroup\$ – Graipher Mar 31 '17 at 9:48