Package: wavespectra
Python library for ocean wave spectra.Main contents:SpecArray: extends xarray’sDataArraywith methods to manipulate wave spectra and calculate spectral statistics.SpecDataset: wrapper aroundSpecArraywith methods for selecting and saving spectra in different formats.Documentation:The documentation is hosted on ReadTheDocs athttps://wavespectra.readthedocs.io/en/latest/.Install:Where to get itThe source code is currently hosted on GitHub at:https://github.com/wavespectra/wavespectraBinary installers for the latest released version are available at thePython package index.Install from pypi# Default install, miss some dependencies and functionalitypipinstallwavespectra# Complete installpipinstallwavespectra[extra]Install from conda# wavespectra is available in the conda-forge channelcondainstall-cconda-forgewavespectraInstall from sourcesInstall requirements. Navigate to the base root ofwavespectraand execute:# Default install, miss some dependencies and functionalitypipinstall-rrequirements/default.txt# Also, for complete installpipinstall-rrequirements/extra.txtThen install wavespectra:pythonsetup.pyinstall# Run pytest integrationpythonsetup.pytestAlternatively, to install indevelopment mode:pipinstall-e.Code structure:The two main classesSpecArrayandSpecDatasetare defined asxarray accessors. The accessors are registered on xarray’sDataArrayandDatasetrespectively as a new namespace calledspec.To use methods in the accessor classes simply import the classes into your code and they will be available to your xarray.Dataset or xarray.DataArray instances through thespecattribute, e.g.importdatetimeimportnumpyasnpimportxarrayasxrfromwavespectra.specarrayimportSpecArrayfromwavespectra.specdatasetimportSpecDatasetcoords={'time':[datetime.datetime(2017,01,n+1)forninrange(2)],'freq':[0.05,0.1],'dir':np.arange(0,360,120)}efth=xr.DataArray(data=np.random.rand(2,2,3),coords=coords,dims=('time','freq','dir'),name='efth')In[1]:efthOut[1]:<xarray.DataArray(time:2,freq:2,dir:3)>array([[[0.100607,0.328229,0.332708],[0.532,0.665938,0.177731]],[[0.469371,0.002963,0.627179],[0.004523,0.682717,0.09766]]])Coordinates:*freq(freq)float640.050.1*dir(dir)int640120240*time(time)datetime64[ns]2017-01-012017-01-02In[2]:efth.specOut[2]:<SpecArray(time:2,freq:2,dir:3)>array([[[0.100607,0.328229,0.332708],[0.532,0.665938,0.177731]],[[0.469371,0.002963,0.627179],[0.004523,0.682717,0.09766]]])Coordinates:*freq(freq)float640.050.1*dir(dir)int640120240*time(time)datetime64[ns]2017-01-012017-01-02In[3]:efth.spec.hs()Out[3]:<xarray.DataArray'hs'(time:2)>array([10.128485,9.510618])Coordinates:*time(time)datetime64[ns]2017-01-012017-01-02Attributes:standard_name:sea_surface_wave_significant_heightunits:mSpecDataset provides a wrapper around the methods in SpecArray. For instance, these produce same result:In[4]:dset=efth.to_dataset(name='efth')In[5]:tm01=dset.spec.tm01()In[6]:tm01.identical(dset.efth.spec.tm01())Out[6]:TrueData requirements:SpecArraymethods requireDataArrayto have the following attributes:wave frequency coordinate inHznamed asfreq(required).wave frequency coordinate inHznamed asfreq(required).wave direction coordinate indegree(coming from) named asdir(optional for 1D, required for 2D spectra).wave energy density data inm2/Hz/degree(2D) orm2/Hz(1D) named asefthSpecDatasetmethods require xarray’sDatasetto have the following attributes:spectra DataArray named asefth, complying with the above specificationsExamples:Define and plot spectra history from exampleSWANspectra file:fromwavespectraimportread_swandset=read_swan('/source/wavespectra/tests/manus.spec')spec_hist=dset.isel(lat=0,lon=0).sel(freq=slice(0.05,0.2)).spec.oned().Tspec_hist.plot.contourf(levels=10)