wavespectra 4.6.0__cp313-cp313-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wavespectra/__init__.py +55 -0
- wavespectra/cli.py +195 -0
- wavespectra/construct/__init__.py +191 -0
- wavespectra/construct/direction.py +104 -0
- wavespectra/construct/frequency.py +209 -0
- wavespectra/core/__init__.py +0 -0
- wavespectra/core/attributes.py +54 -0
- wavespectra/core/attributes.yml +121 -0
- wavespectra/core/fitting.py +140 -0
- wavespectra/core/npstats.py +255 -0
- wavespectra/core/select.py +400 -0
- wavespectra/core/swan.py +261 -0
- wavespectra/core/utils.py +475 -0
- wavespectra/core/xrstats.py +233 -0
- wavespectra/input/__init__.py +123 -0
- wavespectra/input/awac.py +513 -0
- wavespectra/input/dataset.py +57 -0
- wavespectra/input/datawell.py +180 -0
- wavespectra/input/era5.py +114 -0
- wavespectra/input/funwave.py +88 -0
- wavespectra/input/funwave_new.py +104 -0
- wavespectra/input/json.py +58 -0
- wavespectra/input/ncswan.py +110 -0
- wavespectra/input/ndbc.py +155 -0
- wavespectra/input/ndbc_ascii.py +194 -0
- wavespectra/input/netcdf.py +89 -0
- wavespectra/input/obscape.py +285 -0
- wavespectra/input/octopus.py +120 -0
- wavespectra/input/spotter.py +294 -0
- wavespectra/input/swan.py +577 -0
- wavespectra/input/triaxys.py +270 -0
- wavespectra/input/wavespectra.py +57 -0
- wavespectra/input/ww3.py +101 -0
- wavespectra/input/ww3_station.py +186 -0
- wavespectra/input/wwm.py +101 -0
- wavespectra/input/xwaves.py +68 -0
- wavespectra/output/__init__.py +19 -0
- wavespectra/output/funwave.py +235 -0
- wavespectra/output/json.py +23 -0
- wavespectra/output/netcdf.py +46 -0
- wavespectra/output/octopus.py +209 -0
- wavespectra/output/orcaflex.py +111 -0
- wavespectra/output/swan.py +101 -0
- wavespectra/output/ww3.py +119 -0
- wavespectra/output/ww3.yml +158 -0
- wavespectra/partition/__init__.py +0 -0
- wavespectra/partition/hanson_and_phillips_2001.py +369 -0
- wavespectra/partition/partition.py +1297 -0
- wavespectra/partition/specpart/specpart.c +305 -0
- wavespectra/partition/specpart/specpart.h +25 -0
- wavespectra/partition/specpart/specpart_wrap.c +98 -0
- wavespectra/partition/specpart.cp313-win_amd64.pyd +0 -0
- wavespectra/partition/tracking.py +484 -0
- wavespectra/plot.py +355 -0
- wavespectra/specarray.py +1074 -0
- wavespectra/specdataset.py +175 -0
- wavespectra-4.6.0.dist-info/METADATA +403 -0
- wavespectra-4.6.0.dist-info/RECORD +63 -0
- wavespectra-4.6.0.dist-info/WHEEL +5 -0
- wavespectra-4.6.0.dist-info/entry_points.txt +24 -0
- wavespectra-4.6.0.dist-info/licenses/AUTHORS.rst +45 -0
- wavespectra-4.6.0.dist-info/licenses/LICENSE.txt +19 -0
- wavespectra-4.6.0.dist-info/top_level.txt +1 -0
wavespectra/__init__.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Define module attributes.
|
|
2
|
+
|
|
3
|
+
- Defining packaging attributes accessed by setup.py
|
|
4
|
+
- Making reading functions available at module level
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import warnings
|
|
9
|
+
|
|
10
|
+
try:
|
|
11
|
+
from wavespectra.specdataset import SpecDataset
|
|
12
|
+
from wavespectra.specarray import SpecArray
|
|
13
|
+
except ImportError as exc:
|
|
14
|
+
warnings.warn(f"Cannot import accessors at the main module level:\n{exc}")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
__version__ = "4.6.0"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _import_functions(pkgname="input", prefix="read"):
|
|
21
|
+
"""Import functions from pkgname with defined prefix at module level.
|
|
22
|
+
|
|
23
|
+
Functions are imported here if:
|
|
24
|
+
- they are defined in a module wavespectra.{pkgname}.{name}
|
|
25
|
+
- they are named as {prefix}_{name}
|
|
26
|
+
|
|
27
|
+
Example:
|
|
28
|
+
- wavespectra.input.swan.read_swan
|
|
29
|
+
|
|
30
|
+
Import failures (e.g. a missing optional dependency) are reported as
|
|
31
|
+
warnings rather than raised, so the package remains importable; the
|
|
32
|
+
corresponding function is simply not made available.
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
from importlib import import_module
|
|
36
|
+
from pathlib import Path
|
|
37
|
+
|
|
38
|
+
here = Path(__file__).parent
|
|
39
|
+
for path in sorted((here / pkgname).glob("*.py")):
|
|
40
|
+
module = path.stem
|
|
41
|
+
if module == "__init__":
|
|
42
|
+
continue
|
|
43
|
+
func_name = f"{prefix}_{module}"
|
|
44
|
+
try:
|
|
45
|
+
globals()[func_name] = getattr(
|
|
46
|
+
import_module(f"wavespectra.{pkgname}.{module}"), func_name
|
|
47
|
+
)
|
|
48
|
+
except Exception as exc:
|
|
49
|
+
warnings.warn(
|
|
50
|
+
f"Cannot import function {func_name} from module "
|
|
51
|
+
f"wavespectra.{pkgname}.{module}:\n{exc}"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
_import_functions(pkgname="input", prefix="read")
|
wavespectra/cli.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""Console script for wavespectra."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
import click
|
|
5
|
+
import yaml
|
|
6
|
+
import xarray as xr
|
|
7
|
+
from dask.diagnostics.progress import ProgressBar
|
|
8
|
+
|
|
9
|
+
from wavespectra.construct import partition_and_reconstruct
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def file_reader(function):
|
|
13
|
+
"""Arguments and options for reading files to share among commands."""
|
|
14
|
+
function = click.argument("engine")(function)
|
|
15
|
+
function = click.argument("infile")(function)
|
|
16
|
+
function = click.option(
|
|
17
|
+
"-k",
|
|
18
|
+
"--engine_kwargs",
|
|
19
|
+
type=(str, str),
|
|
20
|
+
nargs=2,
|
|
21
|
+
multiple=True,
|
|
22
|
+
help="kwargs to pass to file reader engine, repeat for multiple key-value pairs",
|
|
23
|
+
show_default=False,
|
|
24
|
+
)(function)
|
|
25
|
+
return function
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def parse_kwargs(kwargs):
|
|
29
|
+
"""Parse kwargs from command line into a dictionary."""
|
|
30
|
+
return {k: yaml.safe_load(v) for k, v in dict(kwargs).items()}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@click.group()
|
|
34
|
+
def main(args=None):
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# =====================================================================================
|
|
39
|
+
# Convert commands
|
|
40
|
+
# =====================================================================================
|
|
41
|
+
@main.group()
|
|
42
|
+
def convert():
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@convert.command()
|
|
47
|
+
@file_reader
|
|
48
|
+
@click.argument("outfile")
|
|
49
|
+
@click.argument("fmt")
|
|
50
|
+
@click.option("-o", "--overwrite", is_flag=True, help="Overwrite OUTFILE if it exists")
|
|
51
|
+
def format(infile, engine, outfile, fmt, engine_kwargs, overwrite):
|
|
52
|
+
"""Converts INFILE with format defined by ENGINE to OUTFILE with new format FMT."""
|
|
53
|
+
|
|
54
|
+
if Path(infile).absolute() == Path(outfile).absolute() and not overwrite:
|
|
55
|
+
raise ValueError(
|
|
56
|
+
"INFILE and OUTFILE must be different to avoid overwriting if -o is not set"
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# Engine specific kwargs, force as_file for swan ascii if not provided
|
|
60
|
+
kw = parse_kwargs(engine_kwargs)
|
|
61
|
+
if not kw and engine == "swan":
|
|
62
|
+
kw["as_site"] = True
|
|
63
|
+
|
|
64
|
+
# File spectra to convert
|
|
65
|
+
dset = xr.open_dataset(infile, engine=engine, **kw)
|
|
66
|
+
|
|
67
|
+
# Save to file
|
|
68
|
+
if not hasattr(dset.spec, f"to_{fmt}"):
|
|
69
|
+
raise ValueError(f"Output format '{fmt}' not supported.")
|
|
70
|
+
getattr(dset.spec, f"to_{fmt}")(outfile)
|
|
71
|
+
|
|
72
|
+
click.echo(f"Converted file created: {outfile}")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@convert.command()
|
|
76
|
+
@file_reader
|
|
77
|
+
@click.argument("outfile")
|
|
78
|
+
@click.option(
|
|
79
|
+
"-p",
|
|
80
|
+
"--parameters",
|
|
81
|
+
multiple=True,
|
|
82
|
+
default=["hs", "tp", "dpm"],
|
|
83
|
+
help="Integrated parameters to include, repeat for multiple parameters",
|
|
84
|
+
show_default=True,
|
|
85
|
+
)
|
|
86
|
+
def stats(infile, engine, outfile, parameters, engine_kwargs):
|
|
87
|
+
"""Write a new file OUTFILE with integrated parameters from INFILE."""
|
|
88
|
+
|
|
89
|
+
if Path(infile).absolute() == Path(outfile).absolute():
|
|
90
|
+
raise ValueError(
|
|
91
|
+
"INFILE and OUTFILE must be different to avoid overwriting if -o is not set"
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
# File spectra to convert
|
|
95
|
+
dset = xr.open_dataset(infile, engine=engine, **parse_kwargs(engine_kwargs))
|
|
96
|
+
|
|
97
|
+
# Integrated stats
|
|
98
|
+
dsout = dset.spec.stats(parameters)
|
|
99
|
+
|
|
100
|
+
# Save to file
|
|
101
|
+
dsout.to_netcdf(outfile)
|
|
102
|
+
|
|
103
|
+
click.echo(f"Integrated parameters file created: {outfile}")
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# =====================================================================================
|
|
107
|
+
# Reconstruct commands
|
|
108
|
+
# =====================================================================================
|
|
109
|
+
@main.group()
|
|
110
|
+
def reconstruct():
|
|
111
|
+
pass
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@reconstruct.command()
|
|
115
|
+
@file_reader
|
|
116
|
+
@click.argument("outfile")
|
|
117
|
+
@click.option(
|
|
118
|
+
"-f", "--freq_name", default="jonswap", help="Frequency function", show_default=True
|
|
119
|
+
)
|
|
120
|
+
@click.option(
|
|
121
|
+
"-d", "--dir_name", default="cartwright", help="Spread function", show_default=True
|
|
122
|
+
)
|
|
123
|
+
@click.option(
|
|
124
|
+
"-pm",
|
|
125
|
+
"--partition_method",
|
|
126
|
+
default="ptm3",
|
|
127
|
+
help="Partitioning method, either `ptm1`, `ptm2` or `ptm3`",
|
|
128
|
+
show_default=True,
|
|
129
|
+
)
|
|
130
|
+
@click.option(
|
|
131
|
+
"-m",
|
|
132
|
+
"--method_combine",
|
|
133
|
+
default="max",
|
|
134
|
+
help="Method to combine partitions",
|
|
135
|
+
show_default=True,
|
|
136
|
+
)
|
|
137
|
+
@click.option(
|
|
138
|
+
"-p",
|
|
139
|
+
"--parts",
|
|
140
|
+
type=int,
|
|
141
|
+
default=4,
|
|
142
|
+
help="Number of partitions to keep",
|
|
143
|
+
show_default=True,
|
|
144
|
+
)
|
|
145
|
+
@click.option(
|
|
146
|
+
"-c",
|
|
147
|
+
"--chunks",
|
|
148
|
+
type=(str, str),
|
|
149
|
+
nargs=2,
|
|
150
|
+
multiple=True,
|
|
151
|
+
help="Chunks dict to chunk dataset before partitioning, repeat for multiple dims",
|
|
152
|
+
show_default=False,
|
|
153
|
+
)
|
|
154
|
+
def spectra(
|
|
155
|
+
infile,
|
|
156
|
+
engine,
|
|
157
|
+
outfile,
|
|
158
|
+
engine_kwargs,
|
|
159
|
+
freq_name,
|
|
160
|
+
dir_name,
|
|
161
|
+
partition_method,
|
|
162
|
+
method_combine,
|
|
163
|
+
parts,
|
|
164
|
+
chunks,
|
|
165
|
+
):
|
|
166
|
+
"""Partition and reconstruct spectra from file."""
|
|
167
|
+
|
|
168
|
+
if Path(infile).absolute() == Path(outfile).absolute():
|
|
169
|
+
raise ValueError("INFILE and OUTFILE must be different to avoid overwriting.")
|
|
170
|
+
|
|
171
|
+
# File spectra to convert
|
|
172
|
+
dset = xr.open_dataset(infile, engine=engine, **parse_kwargs(engine_kwargs))
|
|
173
|
+
dset = dset.chunk(**parse_kwargs(chunks))
|
|
174
|
+
|
|
175
|
+
# Sorting out input arguments
|
|
176
|
+
if "," in freq_name:
|
|
177
|
+
freq_name = freq_name.split(",")
|
|
178
|
+
if "," in dir_name:
|
|
179
|
+
dir_name = dir_name.split(",")
|
|
180
|
+
|
|
181
|
+
# Run
|
|
182
|
+
reconstructed = partition_and_reconstruct(
|
|
183
|
+
dset,
|
|
184
|
+
parts=parts,
|
|
185
|
+
freq_name=freq_name,
|
|
186
|
+
dir_name=dir_name,
|
|
187
|
+
partition_method=partition_method,
|
|
188
|
+
method_combine=method_combine,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
# Save to file
|
|
192
|
+
with ProgressBar():
|
|
193
|
+
reconstructed.spec.to_netcdf(outfile)
|
|
194
|
+
|
|
195
|
+
click.echo(f"Reconstructed file created: {outfile}")
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""Wave spectra reconstruction.
|
|
2
|
+
|
|
3
|
+
References:
|
|
4
|
+
|
|
5
|
+
- Bouws, E., Gunther, H., Rosenthal, W., Vincent, C. L. (1985), Similarity of the
|
|
6
|
+
wind wave spectrum in finite depth water 1. Spectral form. Journal of
|
|
7
|
+
Geophysical Research, 90 (C1): 975-986.
|
|
8
|
+
|
|
9
|
+
- Bunney, C., Saulter, A., Palmer, T. (2014), Reconstruction of complex 2D
|
|
10
|
+
wave spectra for rapid deployment of nearshore wave models. From Sea to
|
|
11
|
+
Shore—Meeting the Challenges of the Sea (Coasts, Marine Structures and
|
|
12
|
+
Breakwaters 2013), W. Allsop and K. Burgess, Eds., 1050-1059.
|
|
13
|
+
|
|
14
|
+
- Cartwright, D. E. (1963), The use of directional spectra in studying the output of
|
|
15
|
+
a wave recorder on a moving ship. Ocean Wave Spectra, 203-18, Prentice-Hall, NJ.
|
|
16
|
+
|
|
17
|
+
- Hasselmann, K. et al. (1973), Measurements of wind-wave growth and swell decay
|
|
18
|
+
during the joint North Sea Wave Project (JONSWAP). Deutsche hydrographische
|
|
19
|
+
Zeitschrift, Erganzungshefte Reihe A 12: 8-95.
|
|
20
|
+
|
|
21
|
+
- Pierson, W. J., Moskowitz, L. (1964), A proposed spectral form for fully developed
|
|
22
|
+
wind seas based on the similarity theory of Kitaigorodski. Journal of
|
|
23
|
+
geophysical research 69: 5181-5190.
|
|
24
|
+
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import xarray as xr
|
|
28
|
+
import wavespectra
|
|
29
|
+
from wavespectra.core.attributes import attrs, set_spec_attributes
|
|
30
|
+
from wavespectra.core.utils import load_function
|
|
31
|
+
from wavespectra.construct import frequency, direction
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# Wave stats to use with reconstruction
|
|
35
|
+
STATS = [
|
|
36
|
+
"hs",
|
|
37
|
+
"tp",
|
|
38
|
+
"fp",
|
|
39
|
+
"dm",
|
|
40
|
+
"dpm",
|
|
41
|
+
"dspr",
|
|
42
|
+
"dpspr",
|
|
43
|
+
"tm01",
|
|
44
|
+
"tm02",
|
|
45
|
+
"gamma",
|
|
46
|
+
"alpha",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def construct_partition(
|
|
51
|
+
freq_name="jonswap", dir_name="cartwright", freq_kwargs={}, dir_kwargs={}
|
|
52
|
+
):
|
|
53
|
+
"""Fit frequency-direction E(f, d) parametric spectrum for a partition.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
- freq_name (str): Name of a valid spectral fit function, e.g. `jonswap`.
|
|
57
|
+
- dir_name (str): Name of a valid directional spreak function, e.g. `cartwright`.
|
|
58
|
+
- freq_kwargs (dict): Kwargs to run the `freq_name` spectral fit function.
|
|
59
|
+
- dir_kwargs (dict): Kwargs to run the `dir_name` directional spread function.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
- efth (SpecArray): Two-dimensional, frequency-direction spectrum E(f, d) (m2/Hz/deg).
|
|
63
|
+
|
|
64
|
+
Note:
|
|
65
|
+
- Function `freq_name` must be available in main wavespectra package.
|
|
66
|
+
- Function `dir_name` must be available in wavespectra.directional subpackage.
|
|
67
|
+
- Missing values in output spectrum are filled with zeros.
|
|
68
|
+
|
|
69
|
+
"""
|
|
70
|
+
# Import spectral fit and spreading functions
|
|
71
|
+
freq_func = load_function("wavespectra.construct.frequency", freq_name)
|
|
72
|
+
dir_func = load_function("wavespectra.construct.direction", dir_name)
|
|
73
|
+
|
|
74
|
+
# frequency spectrum
|
|
75
|
+
efth1d = freq_func(**freq_kwargs)
|
|
76
|
+
|
|
77
|
+
# Directional spreading
|
|
78
|
+
spread = dir_func(**dir_kwargs)
|
|
79
|
+
|
|
80
|
+
# Frequency-direction spectrum
|
|
81
|
+
dset = efth1d * spread
|
|
82
|
+
set_spec_attributes(dset)
|
|
83
|
+
|
|
84
|
+
return dset.fillna(0.0)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def partition_and_reconstruct(
|
|
88
|
+
dset,
|
|
89
|
+
parts=4,
|
|
90
|
+
freq_name="jonswap",
|
|
91
|
+
dir_name="cartwright",
|
|
92
|
+
partition_method="ptm3",
|
|
93
|
+
method_combine="max",
|
|
94
|
+
use_defaults=["alpha"],
|
|
95
|
+
):
|
|
96
|
+
"""Partition and reconstruct existing spectra to evaluate.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
- dset (SpecDataset): Spectra object to partition and reconstruct.
|
|
100
|
+
- parts (int): Number of partitions to use in reconstruction.
|
|
101
|
+
- freq_name (str, list): Name of a valid fit function, e.g. `jonswap`, or a
|
|
102
|
+
list of names with len=`parts` to define one fit function for each partition.
|
|
103
|
+
- dir_name (str, list): Name of a valid directional spread function, e.g.
|
|
104
|
+
`cartwright`, or a list of names with len=`parts` to define one directional
|
|
105
|
+
spread function for each partition.
|
|
106
|
+
- partition_method (str): Partitioning method, either `ptm1`, `ptm2` or `ptm3`.
|
|
107
|
+
- method_combine (str): Method to combine partitions when reconstructing.
|
|
108
|
+
- use_defaults (list): List of default parameters to use in the construct
|
|
109
|
+
shape functions, i.e., those that are not calculated from the input spectra.
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
- dsout (SpecArray): Reconstructed spectra with same coordinates as dset.
|
|
113
|
+
|
|
114
|
+
Note:
|
|
115
|
+
- The `ptm3` partitioning method is used by default in which wind sea and swell
|
|
116
|
+
systems are not distiguished or merged together.
|
|
117
|
+
- If `freq_name` or `dir_name` are str, the functions specified by these
|
|
118
|
+
arguments are applied to all sea and swell partitions.
|
|
119
|
+
|
|
120
|
+
"""
|
|
121
|
+
# Parameter checking
|
|
122
|
+
if isinstance(freq_name, str):
|
|
123
|
+
freq_name = (parts) * [freq_name]
|
|
124
|
+
if isinstance(dir_name, str):
|
|
125
|
+
dir_name = (parts) * [dir_name]
|
|
126
|
+
for name in [freq_name, dir_name]:
|
|
127
|
+
if len(name) != parts:
|
|
128
|
+
raise ValueError(
|
|
129
|
+
f"Len of '{name}' must correspond to the "
|
|
130
|
+
f"number of wave systems '{parts}'"
|
|
131
|
+
)
|
|
132
|
+
if partition_method not in ["ptm1", "ptm2", "ptm3"]:
|
|
133
|
+
raise ValueError(
|
|
134
|
+
f"Invalid partition method '{partition_method}'. "
|
|
135
|
+
f"Choose between 'ptm1', 'ptm2' and 'ptm3'."
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
coords = {attrs.FREQNAME: dset[attrs.FREQNAME], attrs.DIRNAME: dset[attrs.DIRNAME]}
|
|
139
|
+
|
|
140
|
+
# Partitioning
|
|
141
|
+
if partition_method == "ptm3":
|
|
142
|
+
kw = {"parts": parts}
|
|
143
|
+
else:
|
|
144
|
+
for v in ["wspd", "wdir", "dpt"]:
|
|
145
|
+
if v not in dset.data_vars:
|
|
146
|
+
raise ValueError(f"Missing variable '{v}' in dset for partitioning.")
|
|
147
|
+
kw = dict(wspd=dset.wspd, wdir=dset.wdir, dpt=dset.dpt, swells=parts - 1)
|
|
148
|
+
dspart = getattr(dset.spec.partition, partition_method)(**kw)
|
|
149
|
+
|
|
150
|
+
# Calculating parameters
|
|
151
|
+
stats = list(set(STATS) - set(use_defaults))
|
|
152
|
+
dparam = dspart.spec.stats(stats)
|
|
153
|
+
dparam["fm"] = 1 / dparam.tm01
|
|
154
|
+
|
|
155
|
+
# Reconstruct partitions
|
|
156
|
+
reconstructed = []
|
|
157
|
+
for ipart, part in enumerate(dspart.part):
|
|
158
|
+
# Turn partitioned parameters for current partition into functions kwargs
|
|
159
|
+
kw = {**coords, **{k: v for k, v in dparam.sel(part=[part]).data_vars.items()}}
|
|
160
|
+
|
|
161
|
+
# Reconstruct current partition
|
|
162
|
+
reconstructed.append(
|
|
163
|
+
construct_partition(
|
|
164
|
+
freq_name=freq_name[ipart],
|
|
165
|
+
dir_name=dir_name[ipart],
|
|
166
|
+
freq_kwargs=kw,
|
|
167
|
+
dir_kwargs=kw,
|
|
168
|
+
)
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
# Combine partitions
|
|
172
|
+
reconstructed = getattr(xr.concat(reconstructed, attrs.PARTNAME), method_combine)(
|
|
173
|
+
attrs.PARTNAME
|
|
174
|
+
)
|
|
175
|
+
reconstructed = reconstructed.to_dataset(name=attrs.SPECNAME)
|
|
176
|
+
set_spec_attributes(reconstructed)
|
|
177
|
+
|
|
178
|
+
# Add back winds and depth
|
|
179
|
+
reconstructed["wspd"] = dset.wspd
|
|
180
|
+
reconstructed["wdir"] = dset.wdir
|
|
181
|
+
reconstructed["dpt"] = dset.dpt
|
|
182
|
+
|
|
183
|
+
# Set some attributes
|
|
184
|
+
reconstructed.attrs = {
|
|
185
|
+
"title": "Spectra Reconstruction",
|
|
186
|
+
"source": "wavespectra <https://github.com/wavespectra/wavespectra>",
|
|
187
|
+
"partitions": parts,
|
|
188
|
+
"spectral_shapes": freq_name,
|
|
189
|
+
"directional_spread": dir_name,
|
|
190
|
+
}
|
|
191
|
+
return reconstructed
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import xarray as xr
|
|
3
|
+
from scipy.constants import pi
|
|
4
|
+
|
|
5
|
+
from wavespectra import SpecArray
|
|
6
|
+
from wavespectra.core.utils import R2D, check_same_coordinates, to_coords
|
|
7
|
+
from wavespectra.core.attributes import attrs
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def cartwright(dir, dm, dspr, under_90=False, **kwargs):
|
|
11
|
+
"""Cosine-squared directional spreading of Cartwright (1963).
|
|
12
|
+
|
|
13
|
+
:math:`G(\\theta,f)=F(s)cos^{2}\\frac{1}{2}(\\theta-\\theta_{m})`
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
- dir (DataArray, 1darray, list): Wave direction coords (degree).
|
|
17
|
+
- dm: (DataArray, float): Mean wave direction (degree).
|
|
18
|
+
- dspr (DataArray, float): Directional spreading (degree).
|
|
19
|
+
- under_90 (bool): Zero the spreading curve above 90 degrees range from dm.
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
- gth (DataArray): Normalised spreading function :math:`g(\\theta)`.
|
|
23
|
+
|
|
24
|
+
Note:
|
|
25
|
+
- If `dm` and `dspr` are DataArrays they must share the same coordinates.
|
|
26
|
+
|
|
27
|
+
"""
|
|
28
|
+
check_same_coordinates(dm, dspr)
|
|
29
|
+
if not isinstance(dir, xr.DataArray):
|
|
30
|
+
dir = to_coords(dir, "dir")
|
|
31
|
+
|
|
32
|
+
# Angular difference from mean direction:
|
|
33
|
+
dth = np.abs(dir - dm)
|
|
34
|
+
dth = dth.where(dth <= 180, 360.0 - dth) # for directional wrapping
|
|
35
|
+
|
|
36
|
+
# spread function:
|
|
37
|
+
s = 2.0 / (np.deg2rad(dspr) ** 2) - 1
|
|
38
|
+
gth = np.cos(0.5 * np.deg2rad(dth)) ** (2 * s)
|
|
39
|
+
|
|
40
|
+
# mask directions +-90 deg from mean
|
|
41
|
+
if under_90:
|
|
42
|
+
gth = gth.where(np.abs(dth) <= 90.0, 0.0)
|
|
43
|
+
|
|
44
|
+
# normalise
|
|
45
|
+
gsum = 1.0 / (gth.sum(attrs.DIRNAME) * (2 * pi / dir.size))
|
|
46
|
+
gth = gth * gsum
|
|
47
|
+
|
|
48
|
+
return gth / R2D
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def asymmetric(dir, freq, dm, dpm, dspr, dpspr, fm, fp, **kwargs):
|
|
52
|
+
"""Asymmetric directional spreading of Bunney et al. (2014).
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
- dir (DataArray, 1darray, list): Wave direction coords (degree).
|
|
56
|
+
- freq (DataArray, 1darray, list): Wave frequency coords (Hz).
|
|
57
|
+
- dm: (DataArray, float): Mean wave direction (degree).
|
|
58
|
+
- dpm: (DataArray, float): Peak wave direction (degree).
|
|
59
|
+
- dspr (DataArray, float) Mean directional spreading (degree).
|
|
60
|
+
- dpspr (DataArray, float) Peak directional spreading (degree).
|
|
61
|
+
- fm (DataArray, float) Mean wave frequency (Hz).
|
|
62
|
+
- fp (DataArray, float) Peak wave frequency (Hz).
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
- gfth (DataArray): Modified normalised spreading function :math:`g(f,\\theta)`.
|
|
66
|
+
|
|
67
|
+
Note:
|
|
68
|
+
- If arguments other than `dir` and `freq` are DataArrays
|
|
69
|
+
they must share the same coordinates.
|
|
70
|
+
|
|
71
|
+
"""
|
|
72
|
+
check_same_coordinates(dm, dpm, dspr, dpspr, fm, fp)
|
|
73
|
+
if not isinstance(freq, xr.DataArray):
|
|
74
|
+
freq = to_coords(freq, "freq")
|
|
75
|
+
if not isinstance(dir, xr.DataArray):
|
|
76
|
+
dir = to_coords(dir, "dir")
|
|
77
|
+
|
|
78
|
+
# Gradients
|
|
79
|
+
# ==========
|
|
80
|
+
# Limiters to avoid negative and large numbers
|
|
81
|
+
dd = dm - dpm
|
|
82
|
+
ds = np.maximum(dspr - dpspr, 0)
|
|
83
|
+
df = np.maximum(fm - fp, 0.001)
|
|
84
|
+
dddf = dd / df
|
|
85
|
+
dsdf = ds / df
|
|
86
|
+
|
|
87
|
+
# Modified peak direction
|
|
88
|
+
# ========================
|
|
89
|
+
# Limiter for frequency band peak directon (30% peak spread)
|
|
90
|
+
theta = dpm + dddf * (freq - fp)
|
|
91
|
+
theta = np.minimum(1.5 * dpm, np.maximum(0.5 * dpm, theta))
|
|
92
|
+
|
|
93
|
+
# Modified spread parameter
|
|
94
|
+
# ==========================
|
|
95
|
+
# Limiter for frequency band spreading, 0.14 limits s to ~100 for narrow beamwidths
|
|
96
|
+
sigma = dpspr + dsdf * (freq - fp)
|
|
97
|
+
sigma = np.maximum(0.5 * dspr, np.minimum(1.5 * np.maximum(dspr, dpspr), sigma))
|
|
98
|
+
sigma = sigma.where(sigma >= 0.14, 0.14)
|
|
99
|
+
|
|
100
|
+
# Apply cosine-square to modified parameters
|
|
101
|
+
# ===========================================
|
|
102
|
+
gfth = cartwright(dir, theta, sigma, under_90=False)
|
|
103
|
+
|
|
104
|
+
return gfth
|