user27100490
user27100490

Reputation: 1

Fixing number of directional bins in windrose plot

I'm trying to fix my windrose plot so the data are partitioned into the correct bins for direction. My data is wind data from a NOAA weather station, and the original measurements were degrees, which I converted to cardinal directions. However, I ended up with two 'Norths'.

Since the degrees are from a circle, I divided them in bins base on the number of directions I was using. However, since 'North' encompasses degrees from 0-11 AND 348-360, this means I have 2 bins for 'North'. I already tried to set the unique=True, but that causes an error and won't produce the plot. This is my code for the preprocessing, but I can add the snippets for the plot if necessary.

# Load the data
df = pd.read_csv('NOAA_AA_2015(in).csv')
df.head()
    STATION          DATE             HourlyWindDirection   HourlyWindGustSpeed HourlyWindSpeed
0   72537494889     2015-07-15T00:53:00  20.0                       NaN       10.0
1   72537494889     2015-07-15T01:53:00  20.0                       NaN       13.0
2   72537494889     2015-07-15T02:53:00  20.0                       NaN       11.0
3   72537494889     2015-07-15T03:53:00  30.0                       NaN        9.0
4   72537494889     2015-07-15T04:53:00  30.0                       NaN        9.0

# Check the range of wind speed to adjust bins accordingly
max_speed = df['strength'].max()
bins_mag = np.linspace(0, max_speed, num=7)  # Create 6 bins between 0 and max_speed
bins_mag_labels = [f'{bins_mag[i]:.1f}-{bins_mag[i+1]:.1f}' for i in range(len(bins_mag)-1)]

# Create bins for directions
bins_dir = [0, 11.25, 33.75, 56.25, 78.75, 101.25, 123.75, 146.25, 168.75, 191.25, 213.75, 236.25, 258.75, 281.25, 303.75, 326.25, 348.75, 360.00]
bins_dir_labels = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N ']

df['strength_binned'] = pd.cut(df['strength'], bins_mag, labels=bins_mag_labels, right=False)
df['direction_binned'] = pd.cut(df['direction'], bins_dir, labels=bins_dir_labels, right=False)

# Handle the redistribution of 'N 360' data
df.loc[df['direction'] == 360, 'direction'] = 0
df['direction_binned'] = pd.cut(df['direction'], bins_dir, labels=bins_dir_labels, right=False)

# Create a new DataFrame with necessary columns and calculate frequency
dfe = df[['strength_binned', 'direction_binned', 'DATE']].copy()
dfe.rename(columns={'DATE': 'frequency'}, inplace=True)

# Group by binned magnitudes and directions, and calculate frequencies
g = dfe.groupby(['strength_binned', 'direction_binned']).count().reset_index()
g['frequency'] = g['frequency'] / g['frequency'].sum() * 100  # Convert to percentage

# Apply logarithmic transformation to frequency
g['log_frequency'] = np.log1p(g['frequency'])  # Use log1p to handle zero values

# Rename the strength_binned column to 'Wind Speed m/s'
g.rename(columns={'strength_binned': 'Wind Speed m/s'}, inplace=True)

Upvotes: 0

Views: 37

Answers (0)

Related Questions