Skip to content

Commit efcda0a

Browse files
authored
Add optional parameter frequency_dimension across wave.resource (#336)
* add frequency_dimension parameter across all necessary wave.resource functions * specify frequency_dimension in surface_elevation summation
1 parent e0d59c7 commit efcda0a

File tree

1 file changed

+134
-50
lines changed

1 file changed

+134
-50
lines changed

mhkit/wave/resource.py

Lines changed: 134 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,8 @@ def surface_elevation(
398398
)
399399

400400
# wave elevation
401-
# eta = xr.DataArray(columns=S.columns, index=time_index)
402-
# for mcol in eta.columns:
403401
C = np.cos(B + phase)
404-
# C = xr.DataArray(data=C, index=time_index, columns=omega.index)
405-
eta[var] = (C * A).sum(axis=1)
402+
eta[var] = (C * A).sum(dim=frequency_dimension)
406403

407404
if to_pandas:
408405
eta = eta.to_dataframe()
@@ -475,14 +472,19 @@ def frequency_moment(S, N, frequency_bins=None, frequency_dimension="", to_panda
475472
return m
476473

477474

478-
def significant_wave_height(S, frequency_bins=None, to_pandas=True):
475+
def significant_wave_height(
476+
S, frequency_dimension="", frequency_bins=None, to_pandas=True
477+
):
479478
"""
480479
Calculates wave height from spectra
481480
482481
Parameters
483482
------------
484483
S: pandas DataFrame, pandas Series, xarray DataArray, or xarray Dataset
485484
Spectral density [m^2/Hz] indexed by frequency [Hz]
485+
frequency_dimension: string (optional)
486+
Name of the xarray dimension corresponding to frequency. If not supplied,
487+
defaults to the first dimension. Does not affect pandas input.
486488
frequency_bins: numpy array or pandas Series (optional)
487489
Bin widths for frequency of S. Required for unevenly sized bins
488490
to_pandas: bool (optional)
@@ -498,9 +500,13 @@ def significant_wave_height(S, frequency_bins=None, to_pandas=True):
498500
raise TypeError(f"to_pandas must be of type bool. Got: {type(to_pandas)}")
499501

500502
# Eq 12 in IEC 62600-101
501-
m0 = frequency_moment(S, 0, frequency_bins=frequency_bins, to_pandas=False).rename(
502-
{"m0": "Hm0"}
503-
)
503+
m0 = frequency_moment(
504+
S,
505+
0,
506+
frequency_bins=frequency_bins,
507+
frequency_dimension=frequency_dimension,
508+
to_pandas=False,
509+
).rename({"m0": "Hm0"})
504510
Hm0 = 4 * np.sqrt(m0)
505511

506512
if to_pandas:
@@ -509,14 +515,19 @@ def significant_wave_height(S, frequency_bins=None, to_pandas=True):
509515
return Hm0
510516

511517

512-
def average_zero_crossing_period(S, frequency_bins=None, to_pandas=True):
518+
def average_zero_crossing_period(
519+
S, frequency_dimension="", frequency_bins=None, to_pandas=True
520+
):
513521
"""
514522
Calculates wave average zero crossing period from spectra
515523
516524
Parameters
517525
------------
518526
S: pandas DataFrame, pandas Series, xarray DataArray, or xarray Dataset
519527
Spectral density [m^2/Hz] indexed by frequency [Hz]
528+
frequency_dimension: string (optional)
529+
Name of the xarray dimension corresponding to frequency. If not supplied,
530+
defaults to the first dimension. Does not affect pandas input.
520531
frequency_bins: numpy array or pandas Series (optional)
521532
Bin widths for frequency of S. Required for unevenly sized bins
522533
to_pandas: bool (optional)
@@ -532,12 +543,20 @@ def average_zero_crossing_period(S, frequency_bins=None, to_pandas=True):
532543
raise TypeError(f"to_pandas must be of type bool. Got: {type(to_pandas)}")
533544

534545
# Eq 15 in IEC 62600-101
535-
m0 = frequency_moment(S, 0, frequency_bins=frequency_bins, to_pandas=False).rename(
536-
{"m0": "Tz"}
537-
)
538-
m2 = frequency_moment(S, 2, frequency_bins=frequency_bins, to_pandas=False).rename(
539-
{"m2": "Tz"}
540-
)
546+
m0 = frequency_moment(
547+
S,
548+
0,
549+
frequency_bins=frequency_bins,
550+
frequency_dimension=frequency_dimension,
551+
to_pandas=False,
552+
).rename({"m0": "Tz"})
553+
m2 = frequency_moment(
554+
S,
555+
2,
556+
frequency_bins=frequency_bins,
557+
frequency_dimension=frequency_dimension,
558+
to_pandas=False,
559+
).rename({"m2": "Tz"})
541560

542561
Tz = np.sqrt(m0 / m2)
543562

@@ -547,14 +566,19 @@ def average_zero_crossing_period(S, frequency_bins=None, to_pandas=True):
547566
return Tz
548567

549568

550-
def average_crest_period(S, frequency_bins=None, to_pandas=True):
569+
def average_crest_period(
570+
S, frequency_dimension="", frequency_bins=None, to_pandas=True
571+
):
551572
"""
552573
Calculates wave average crest period from spectra
553574
554575
Parameters
555576
------------
556577
S: pandas DataFrame, pandas Series, xarray DataArray, or xarray Dataset
557578
Spectral density [m^2/Hz] indexed by frequency [Hz]
579+
frequency_dimension: string (optional)
580+
Name of the xarray dimension corresponding to frequency. If not supplied,
581+
defaults to the first dimension. Does not affect pandas input.
558582
frequency_bins: numpy array or pandas Series (optional)
559583
Bin widths for frequency of S. Required for unevenly sized bins
560584
to_pandas: bool (optional)
@@ -570,12 +594,20 @@ def average_crest_period(S, frequency_bins=None, to_pandas=True):
570594
if not isinstance(to_pandas, bool):
571595
raise TypeError(f"to_pandas must be of type bool. Got: {type(to_pandas)}")
572596

573-
m2 = frequency_moment(S, 2, frequency_bins=frequency_bins, to_pandas=False).rename(
574-
{"m2": "Tavg"}
575-
)
576-
m4 = frequency_moment(S, 4, frequency_bins=frequency_bins, to_pandas=False).rename(
577-
{"m4": "Tavg"}
578-
)
597+
m2 = frequency_moment(
598+
S,
599+
2,
600+
frequency_bins=frequency_bins,
601+
frequency_dimension=frequency_dimension,
602+
to_pandas=False,
603+
).rename({"m2": "Tavg"})
604+
m4 = frequency_moment(
605+
S,
606+
4,
607+
frequency_bins=frequency_bins,
608+
frequency_dimension=frequency_dimension,
609+
to_pandas=False,
610+
).rename({"m4": "Tavg"})
579611

580612
Tavg = np.sqrt(m2 / m4)
581613

@@ -585,14 +617,17 @@ def average_crest_period(S, frequency_bins=None, to_pandas=True):
585617
return Tavg
586618

587619

588-
def average_wave_period(S, frequency_bins=None, to_pandas=True):
620+
def average_wave_period(S, frequency_dimension="", frequency_bins=None, to_pandas=True):
589621
"""
590622
Calculates mean wave period from spectra
591623
592624
Parameters
593625
------------
594626
S: pandas DataFrame, pandas Series, xarray DataArray, or xarray Dataset
595627
Spectral density [m^2/Hz] indexed by frequency [Hz]
628+
frequency_dimension: string (optional)
629+
Name of the xarray dimension corresponding to frequency. If not supplied,
630+
defaults to the first dimension. Does not affect pandas input.
596631
frequency_bins: numpy array or pandas Series (optional)
597632
Bin widths for frequency of S. Required for unevenly sized bins
598633
to_pandas: bool (optional)
@@ -607,12 +642,20 @@ def average_wave_period(S, frequency_bins=None, to_pandas=True):
607642
if not isinstance(to_pandas, bool):
608643
raise TypeError(f"to_pandas must be of type bool. Got: {type(to_pandas)}")
609644

610-
m0 = frequency_moment(S, 0, frequency_bins=frequency_bins, to_pandas=False).rename(
611-
{"m0": "Tm"}
612-
)
613-
m1 = frequency_moment(S, 1, frequency_bins=frequency_bins, to_pandas=False).rename(
614-
{"m1": "Tm"}
615-
)
645+
m0 = frequency_moment(
646+
S,
647+
0,
648+
frequency_bins=frequency_bins,
649+
frequency_dimension=frequency_dimension,
650+
to_pandas=False,
651+
).rename({"m0": "Tm"})
652+
m1 = frequency_moment(
653+
S,
654+
1,
655+
frequency_bins=frequency_bins,
656+
frequency_dimension=frequency_dimension,
657+
to_pandas=False,
658+
).rename({"m1": "Tm"})
616659

617660
Tm = np.sqrt(m0 / m1)
618661

@@ -664,14 +707,17 @@ def peak_period(S, frequency_dimension="", to_pandas=True):
664707
return Tp
665708

666709

667-
def energy_period(S, frequency_bins=None, to_pandas=True):
710+
def energy_period(S, frequency_dimension="", frequency_bins=None, to_pandas=True):
668711
"""
669712
Calculates wave energy period from spectra
670713
671714
Parameters
672715
------------
673716
S: pandas DataFrame, pandas Series, xarray DataArray, or xarray Dataset
674717
Spectral density [m^2/Hz] indexed by frequency [Hz]
718+
frequency_dimension: string (optional)
719+
Name of the xarray dimension corresponding to frequency. If not supplied,
720+
defaults to the first dimension. Does not affect pandas input.
675721
frequency_bins: numpy array or pandas Series (optional)
676722
Bin widths for frequency of S. Required for unevenly sized bins
677723
to_pandas: bool (optional)
@@ -687,11 +733,19 @@ def energy_period(S, frequency_bins=None, to_pandas=True):
687733
raise TypeError(f"to_pandas must be of type bool. Got: {type(to_pandas)}")
688734

689735
mn1 = frequency_moment(
690-
S, -1, frequency_bins=frequency_bins, to_pandas=False
736+
S,
737+
-1,
738+
frequency_bins=frequency_bins,
739+
frequency_dimension=frequency_dimension,
740+
to_pandas=False,
691741
).rename({"m-1": "Te"})
692-
m0 = frequency_moment(S, 0, frequency_bins=frequency_bins, to_pandas=False).rename(
693-
{"m0": "Te"}
694-
)
742+
m0 = frequency_moment(
743+
S,
744+
0,
745+
frequency_bins=frequency_bins,
746+
frequency_dimension=frequency_dimension,
747+
to_pandas=False,
748+
).rename({"m0": "Te"})
695749

696750
# Eq 13 in IEC 62600-101
697751
Te = mn1 / m0
@@ -702,14 +756,17 @@ def energy_period(S, frequency_bins=None, to_pandas=True):
702756
return Te
703757

704758

705-
def spectral_bandwidth(S, frequency_bins=None, to_pandas=True):
759+
def spectral_bandwidth(S, frequency_dimension="", frequency_bins=None, to_pandas=True):
706760
"""
707761
Calculates bandwidth from spectra
708762
709763
Parameters
710764
------------
711765
S: pandas DataFrame, pandas Series, xarray DataArray, or xarray Dataset
712766
Spectral density [m^2/Hz] indexed by frequency [Hz]
767+
frequency_dimension: string (optional)
768+
Name of the xarray dimension corresponding to frequency. If not supplied,
769+
defaults to the first dimension. Does not affect pandas input.
713770
frequency_bins: numpy array or pandas Series (optional)
714771
Bin widths for frequency of S. Required for unevenly sized bins
715772
to_pandas: bool (optional)
@@ -724,15 +781,27 @@ def spectral_bandwidth(S, frequency_bins=None, to_pandas=True):
724781
if not isinstance(to_pandas, bool):
725782
raise TypeError(f"to_pandas must be of type bool. Got: {type(to_pandas)}")
726783

727-
m2 = frequency_moment(S, 2, frequency_bins=frequency_bins, to_pandas=False).rename(
728-
{"m2": "e"}
729-
)
730-
m0 = frequency_moment(S, 0, frequency_bins=frequency_bins, to_pandas=False).rename(
731-
{"m0": "e"}
732-
)
733-
m4 = frequency_moment(S, 4, frequency_bins=frequency_bins, to_pandas=False).rename(
734-
{"m4": "e"}
735-
)
784+
m2 = frequency_moment(
785+
S,
786+
2,
787+
frequency_bins=frequency_bins,
788+
frequency_dimension=frequency_dimension,
789+
to_pandas=False,
790+
).rename({"m2": "e"})
791+
m0 = frequency_moment(
792+
S,
793+
0,
794+
frequency_bins=frequency_bins,
795+
frequency_dimension=frequency_dimension,
796+
to_pandas=False,
797+
).rename({"m0": "e"})
798+
m4 = frequency_moment(
799+
S,
800+
4,
801+
frequency_bins=frequency_bins,
802+
frequency_dimension=frequency_dimension,
803+
to_pandas=False,
804+
).rename({"m4": "e"})
736805

737806
e = np.sqrt(1 - (m2**2) / (m0 / m4))
738807

@@ -742,14 +811,17 @@ def spectral_bandwidth(S, frequency_bins=None, to_pandas=True):
742811
return e
743812

744813

745-
def spectral_width(S, frequency_bins=None, to_pandas=True):
814+
def spectral_width(S, frequency_dimension="", frequency_bins=None, to_pandas=True):
746815
"""
747816
Calculates wave spectral width from spectra
748817
749818
Parameters
750819
------------
751820
S: pandas DataFrame, pandas Series, xarray DataArray, or xarray Dataset
752821
Spectral density [m^2/Hz] indexed by frequency [Hz]
822+
frequency_dimension: string (optional)
823+
Name of the xarray dimension corresponding to frequency. If not supplied,
824+
defaults to the first dimension. Does not affect pandas input.
753825
frequency_bins: numpy array or pandas Series (optional)
754826
Bin widths for frequency of S. Required for unevenly sized bins
755827
to_pandas: bool (optional)
@@ -765,13 +837,25 @@ def spectral_width(S, frequency_bins=None, to_pandas=True):
765837
raise TypeError(f"to_pandas must be of type bool. Got: {type(to_pandas)}")
766838

767839
mn2 = frequency_moment(
768-
S, -2, frequency_bins=frequency_bins, to_pandas=False
840+
S,
841+
-2,
842+
frequency_bins=frequency_bins,
843+
frequency_dimension=frequency_dimension,
844+
to_pandas=False,
769845
).rename({"m-2": "v"})
770-
m0 = frequency_moment(S, 0, frequency_bins=frequency_bins, to_pandas=False).rename(
771-
{"m0": "v"}
772-
)
846+
m0 = frequency_moment(
847+
S,
848+
0,
849+
frequency_bins=frequency_bins,
850+
frequency_dimension=frequency_dimension,
851+
to_pandas=False,
852+
).rename({"m0": "v"})
773853
mn1 = frequency_moment(
774-
S, -1, frequency_bins=frequency_bins, to_pandas=False
854+
S,
855+
-1,
856+
frequency_bins=frequency_bins,
857+
frequency_dimension=frequency_dimension,
858+
to_pandas=False,
775859
).rename({"m-1": "v"})
776860

777861
# Eq 16 in IEC 62600-101

0 commit comments

Comments
 (0)