MHKiT-Python provides the marine renewable energy (MRE) community tools for data processing, visualization, quality control, resource assessment, and device performance.
@ssolson knows that I'm a big xarray evangelist... I need to use some NDBC data and I think we should update the ndbc.py module to output xarrayDataArray objects.
I know @ssolson has already implemented parts of the snippet below (e.g., replacing 999.00 with NaN) in ndbc.py, but I wasn't quite sure where to insert things so I just made a MWE. Should be fairly straightforward to incorporate from here.
import xarray as xr
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# save some data from NDBC as 'data.txt', in this case I used NDBC46050
# https://www.ndbc.noaa.gov/download_data.php?filename=46050w1996.txt.gz&dir=data/historical/swden/
# import data
df = pd.read_csv('data.txt',
sep='\s+',
parse_dates=[['YY', 'MM', 'DD', 'hh']],
index_col=0)
da = df.to_xarray().to_array(dim='Frequency',
name='Spectral energy density')
da.attrs["units"] = "m$^2$/Hz"
da = da.rename({'YY_MM_DD_hh':'Date'})
da['Frequency'] = da['Frequency'].astype(np.float)
da['Frequency'].attrs['units'] = 'Hz'
da = da.where(da != 999.00)
# make a plot for a single day
fig, ax = plt.subplots(nrows=1, sharex=True, figsize=(7,4))
mdate = '1996-07-17'
dtp = da.sel(Date=slice(mdate,mdate))
dtp.plot.contourf()
dtp.plot.contour(colors='w', linewidths=0.5)
ax.set_title('Spectrogram for {}'.format(mdate))
fig.tight_layout()
# mean spectrum for that day
fig, ax = plt.subplots(nrows=1, sharex=True, figsize=(7,4))
pm = dtp.mean('Date').plot(ax=ax, color='b', linewidth=2, label='Mean')
ph = [x[1].plot(ax=ax, color='b', linewidth=0.25, alpha=0.5, label='Hourly') for x in dtp.groupby('Date')]
ax.set_ylim(bottom=0)
ax.set_title('Mean wave spectrum for {}'.format(mdate))
plt.autoscale(enable=True, axis='x', tight=True)
ax.legend(handles=[pm[0], ph[0][0]])
@ssolson knows that I'm a big
xarray
evangelist... I need to use some NDBC data and I think we should update thendbc.py
module to outputxarray
DataArray
objects.I know @ssolson has already implemented parts of the snippet below (e.g., replacing
999.00
withNaN
) inndbc.py
, but I wasn't quite sure where to insert things so I just made a MWE. Should be fairly straightforward to incorporate from here.