6.1. Python 2 and 3¶
The Python package spectral_wave_data contains a module
spectral_wave_data.py.
It defines the generic class SpectralWaveData
to be applied in applications based on Python-2 or Python-3.
6.1.2. Exception handling¶
Related to the class SpectralWaveData
is a set of class specific exceptions
defined in the same module. These exceptions as described in the table below, can be
imported and applied in application programs and scripts.
>>> from spectral_wave_data import SpectralWaveData, SwdError, SwdInputValueError, ...
>>> ...
>>> try:
>>> swd = SpectralWaveData('my_waves.swd', x0=0.0, y0=0.0, t0=0.0, beta=180.0)
>>> except SwdError as err:
>>> print(err) # Print actual error message
>>> # You may do some application specific error recovery before the show must go on...
>>> ...
If no try/except block is applied, the application program will abort and print the exception and backtrace as a normal Python crash.
The only other associated class methods that may throw exceptions are update_time()
,
convergence()
, strip()
and get()
.
Python Exceptions |
Usage |
---|---|
|
Base class for all SWD specific exceptions |
|
Typical if SWD file is not an existing file. |
|
SWD file does not apply float/little-endian |
|
Error during reading and checking SWD data |
|
Input arguments for class methods are not sound |
|
Not able to allocate internal SWD storage |
6.1.3. Metadata¶
To extract metadata from a SWD file you may apply the method swd.get(key)
,
or using the more Pythonesque syntax swd[key]
, where key is a string to identify
the requested metadata. A key is either:
A relevant parameter from the SWD-file format description.
A constructor parameter.
A key from the table below.
Only scalar meta data is supported in this version.
Key |
Returned value |
---|---|
version |
The repository version number of spectral_wave_data applied in this
Python distribution.
|
class |
Name of the specialized SWD-class handling this object. (Fortran class) |
tmax |
Maximum allowed application time consistent with the header of
the SWD file and the constructor parameter t0=0.0. [s]
|
lmin |
Shortest wave length component. [m] |
lmax |
Longest wave length component. [m] |
sizex |
Periodic length of wave domain in x-direction (swd-system). [m] |
sizey |
Periodic length of wave domain in y-direction (swd-system). [m] |
A SwdInputValueError
exception is raised if the actual key is not relevant
for the actual SWD class.
6.1.3.1. The script swd_meta¶
For convenience this Python wheel package includes the script swd_meta
listing the relevant metadata for a given SWD-file. It runs on Windows and Linux.
In a terminal window with access to your installed spectral_wave_data package you can
invoke it like in this example where my.swd
is the name of the actual SWD-file:
>>> swd_meta my.swd
version: 1.0.0-beta.9
prog: raschii-1.0.3.dev0
date: 2020:01:22 19:57:55
fmt: 100
shp: 2
amp: 1
tmax: 6.3000000938773155
dt: 0.10000000149011612
nsteps: 64
nstrip: 0
order: -1
depth: 32.0
n: 50
sizex: 220.00000561733003
lmax: 220.00000561733003
lmin: 4.400000112346601
dk: 0.028559932485222816
cid: {'model': 'Fenton', 'T': 12.792885811907514, 'height': 18.5, 'depth': 32.0, 'N': 50, 'air': 'NoneType', 'g': 9.81, 'c': 17.19705805512825, 'relax': 0.5}
6.1.4. Implementation¶
In the current version the implementation of the class SpectralWaveData
is obtained by wrapping the C-implementation using the standard Python module
ctypes
in the package module swd_c_interface.py.