Skip to main content

Library for ocean wave spectra

Project description

Python library for ocean wave spectra.

https://zenodo.org/badge/205463939.svg GitHub Workflow Status (with event) https://coveralls.io/repos/github/wavespectra/wavespectra/badge.svg?branch=master https://readthedocs.org/projects/wavespectra/badge/?version=latest https://img.shields.io/pypi/v/wavespectra.svg PyPI - Downloads https://anaconda.org/conda-forge/wavespectra/badges/version.svg https://anaconda.org/conda-forge/wavespectra/badges/platforms.svg https://img.shields.io/badge/code%20style-black-000000.svg PyPI - Python Version

Main contents:

  • SpecArray: extends xarray’s DataArray with methods to manipulate wave spectra and calculate spectral statistics.

  • SpecDataset: wrapper around SpecArray with methods for selecting and saving spectra in different formats.

Documentation:

The documentation is hosted on ReadTheDocs at https://wavespectra.readthedocs.io/en/latest/.

Install:

Where to get it

The source code is currently hosted on GitHub at: https://github.com/wavespectra/wavespectra

Binary installers for the latest released version are available at the Python package index.

Install from pypi

# Default install, miss some dependencies and functionality
pip install wavespectra

# Complete install
pip install wavespectra[extra]

Install from conda

# wavespectra is available in the conda-forge channel
conda install -c conda-forge wavespectra

Install from sources

Install requirements. Navigate to the base root of wavespectra and execute:

# Default install, miss some dependencies and functionality
pip install -r requirements/default.txt

# Also, for complete install
pip install -r requirements/extra.txt

Then install wavespectra:

python setup.py install

# Run pytest integration
python setup.py test

Alternatively, to install in development mode:

pip install -e .

Code structure:

The two main classes SpecArray and SpecDataset are defined as xarray accessors. The accessors are registered on xarray’s DataArray and Dataset respectively as a new namespace called spec.

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 the spec attribute, e.g.

import datetime
import numpy as np
import xarray as xr

from wavespectra.specarray import SpecArray
from wavespectra.specdataset import SpecDataset

coords = {'time': [datetime.datetime(2017,01,n+1) for n in range(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]: efth
Out[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) float64 0.05 0.1
  * dir      (dir) int64 0 120 240
  * time     (time) datetime64[ns] 2017-01-01 2017-01-02

In [2]: efth.spec
Out[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) float64 0.05 0.1
  * dir      (dir) int64 0 120 240
  * time     (time) datetime64[ns] 2017-01-01 2017-01-02

In [3]: efth.spec.hs()
Out[3]:
<xarray.DataArray 'hs' (time: 2)>
array([ 10.128485,   9.510618])
Coordinates:
  * time     (time) datetime64[ns] 2017-01-01 2017-01-02
Attributes:
    standard_name: sea_surface_wave_significant_height
    units: m

SpecDataset 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]: True

Data requirements:

SpecArray methods require DataArray to have the following attributes:

  • wave frequency coordinate in Hz named as freq (required).

  • wave frequency coordinate in Hz named as freq (required).

  • wave direction coordinate in degree (coming from) named as dir (optional for 1D, required for 2D spectra).

  • wave energy density data in m2/Hz/degree (2D) or m2/Hz (1D) named as efth

SpecDataset methods require xarray’s Dataset to have the following attributes:

  • spectra DataArray named as efth, complying with the above specifications

Examples:

Define and plot spectra history from example SWAN spectra file:

from wavespectra import read_swan

dset = read_swan('/source/wavespectra/tests/manus.spec')
spec_hist = dset.isel(lat=0, lon=0).sel(freq=slice(0.05,0.2)).spec.oned().T
spec_hist.plot.contourf(levels=10)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wavespectra-4.1.1.tar.gz (105.3 kB view details)

Uploaded Source

File details

Details for the file wavespectra-4.1.1.tar.gz.

File metadata

  • Download URL: wavespectra-4.1.1.tar.gz
  • Upload date:
  • Size: 105.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.6

File hashes

Hashes for wavespectra-4.1.1.tar.gz
Algorithm Hash digest
SHA256 d894ea165c36b1cdf15700a9d42510bfb5f70162991ad7b8d7172199deadd32a
MD5 78ccc396ee7bc0f269b22f43b7b411b4
BLAKE2b-256 0a27192865d8ea7c1f751daac7fbdc628f9f47a19e560d6f45b1babe18e84f55

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page