Skip to content

Instantly share code, notes, and snippets.

@mpiannucci
Last active August 14, 2017 17:28
Show Gist options
  • Save mpiannucci/4ed3edd1c27dfe6b0d2091d8b2161b28 to your computer and use it in GitHub Desktop.
Save mpiannucci/4ed3edd1c27dfe6b0d2091d8b2161b28 to your computer and use it in GitHub Desktop.
Parse Raw NDBC Directional Wave Spectra
import urllib.request as request
import json
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cm
import numpy as np
class BuoySpectraPlotter:
def __init__(self, buoy_station):
self.data = None
self.buoy_station = buoy_station
def fetch_data(self):
url = 'https://buoyfinder.appspot.com/api/latest/wave/' + self.buoy_station
req = request.Request(url)
with request.urlopen(req) as response:
self.data = json.loads(response.read().decode('utf8'))
return self.data is not None
def plot_radar(self):
if self.data is None:
return
frequencies = np.array(self.data['BuoyData']['WaveSpectra']['Frequencies'])
energies = np.array(self.data['BuoyData']['WaveSpectra']['Energies'])
angles = np.array(self.data['BuoyData']['WaveSpectra']['Angles']) * (np.pi/180.0)
ax = plt.subplot(111, projection='polar')
ax.set_title('Station ' + self.buoy_station + ': Directional Wave Spectra\n')
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
bars = ax.bar(angles, energies, align='center')
# Map the color of each bar in the plot to its period value
norm = colors.Normalize(vmin=frequencies[0], vmax=frequencies[-1])
cmap = cm.jet_r
colormap = cm.ScalarMappable(norm=norm, cmap=cmap)
for freq, energy, bar in zip(frequencies, energies, bars):
bar.set_facecolor(colormap.to_rgba(freq))
bar.set_alpha(0.5)
plt.show()
if __name__ == '__main__':
buoy_plotter = BuoySpectraPlotter('44097')
buoy_plotter.fetch_data()
buoy_plotter.plot_radar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment