<jupyter_start><jupyter_text>From significant wave height to directional spectrum, and back<jupyter_code># measurement times
time     = [datetime(2013,12,1),
            datetime(2014,12,2)]

# measurement locations
location = [(0,0),(0,1),(1,1),(.5,.5)]

# initialize OceanWaves object
ow = oceanwaves.OceanWaves(time=time,
                           time_var='datetime',
                           location=location,
                           crs='epsg:28992',
                           energy_units='m',
                           energy_var='waveheight')   # note we are defining wave heights here!

ow['_energy'].values = np.random.rand(*ow.shape) * 10.    # random wave heights

# plot data and print shape and units
ow.plot()
print(ow.shape, ow.units)
# convert wave heights to wave spectrum with JONSWAP shape
frequency = np.arange(0,1.,.01)
ow_spc = ow.as_spectral(frequency, shape='jonswap')

# plot data and print shape and units
ow_spc.plot(col='location')
print(ow_spc.shape, ow_spc.units)
# convert omnidirectional wave spectrum to directional wave spectrum
theta = np.arange(0., 360., 5.)
ow_dir = ow_spc.as_directional(theta, s=5)

# plot data and print shape and units
ow_dir.plot(col='location', row='datetime')
print(ow_dir.shape, ow_dir.units)
# convert directional wave spectrum back to omnidirectional wave spectrum
ow_omn = ow_dir.as_omnidirectional()

# plot data and print shape and units
ow_omn.plot(col='location')
print(ow_omn.shape, ow_omn.units)
# convert wave spectrum to significant wave heights
ow_Hs = ow_omn.Hm0()

# plot data and print shape and units
ow_Hs.plot()
print(ow_Hs.shape, ow_Hs.units)<jupyter_output>(2, 4) m<jupyter_text>Other conversions<jupyter_code># convert wave spectrum to wave period
ow_Tm02 = ow_omn.Tm02()

# plot data and print shape and units
ow_Tm02.plot()
print(ow_Tm02.shape, ow_Tm02.units)
# convert significant wave height to directional waves
theta = np.arange(0., 360., 5.)
ow_dir2 = ow.as_directional(theta, s=5)

# plot data and print shape and units
ow_dir2.plot(col='datetime') # location is on the radials...
                             # ... and energy is still in m
print(ow_dir2.shape, ow_dir2.units)<jupyter_output>(2, 4, 72) m deg^-1<jupyter_text>Plot spectra on map<jupyter_code># plot spectra on a map
ow_dir[dict(datetime=0)].plot.spatial_map(scale=.3, subplot_kw=dict(projection='polar'))<jupyter_output><empty_output><jupyter_text>Read SWaN spectra and tables<jupyter_code>import oceanwaves

fig = plt.figure()
ow = oceanwaves.OceanWaves.from_swan('../data/swan/P1.SP1')
ow.plot()

fig = plt.figure()
theta = np.arange(0., 360., 5.)
ow = oceanwaves.OceanWaves.from_swan('../data/swan/P1b.SP1').as_directional(theta)
ow.plot(col='location')

fig = plt.figure()
ow = oceanwaves.OceanWaves.from_swan('../data/swan/P1.SP2')
ow.plot(col='location')

fig = plt.figure()
ow = oceanwaves.OceanWaves.from_swantable('../data/swan/P1.TAB')
ow.plot()

fig = plt.figure()
ow = oceanwaves.OceanWaves.from_swantable('../data/swan/TBL1.tbl',
                                          columns=['Xp','Yp','Botlev','Hsig','RTpeak','TPsmoo','Tm01','Tm02'])
ow.plot()

fig = plt.figure()
ow = oceanwaves.OceanWaves.from_swantable('../data/swan/TBL2.tbl')
ow.plot()

fig = plt.figure()
ow = oceanwaves.OceanWaves.from_swantable('../data/swan/TBL2NS.tbl')
ow.plot()<jupyter_output><empty_output><jupyter_text>Write SWaN spectra<jupyter_code>def print_contents(fname, context=5):
    
    with open(fname, 'r') as fp:
        for line in fp:
            if re.match('[A-Z]+', line):
                n = context + 1
            if n > 0:
                print(line.strip())
            if n == 0:
                print('...')
            n -= 1
ow = oceanwaves.OceanWaves.from_swantable('../data/swan/P1.TAB')
ow = ow[dict(Location=range(5))]

frequency = np.arange(0,1.,.05)
ow_spc = ow.as_spectral(frequency, shape='jonswap')
ow_spc.to_swan('P1.SP1')
print_contents('P1.SP1')
theta = np.arange(0., 360., 30)
ow_dir = ow_spc.as_directional(theta, s=5)
ow_dir.to_swan('P1.SP2')
print_contents('P1.SP2')<jupyter_output>SWAN    1
LOCATIONS
5
30000.00  404500.00
35800.00  404000.00
35800.00  404500.00
36844.00  402839.00
...
AFREQ
19
0.0500
0.1000
0.1500
0.2000
...
NDIR
12
0.00
30.00
60.00
90.00
...
QUANT
1
VaDens
m^2 s
-99.0
FACTOR
3.131590e-08
0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0
5776     2814      180        0        0        0        0        0        0        0      180     2814
...
FACTOR
3.857757e-08
0        0        0        0        0        0        0        0        0        0        0        0
0        0        0        0        0        0        0        0        0        0        0        0
54031    26320     1688        0        0        0        0        0        0        0     1688    26320
999999   487138    31249        0[...]<jupyter_text>Write netCDF<jupyter_code>ow_dir.to_netcdf('P1.nc')<jupyter_output><empty_output><jupyter_text>Read Datawell files<jupyter_code>try:
    ow = oceanwaves.OceanWaves.from_datawell('../data/datawell/example.dat')
except NotImplementedError as e:
    print(e)<jupyter_output>Reading of Datawell files is not yet implemented<jupyter_text>Reading WaveDroid files<jupyter_code>try:
    ow = oceanwaves.OceanWaves.from_wavedroid('../data/wavedroid/example.dat')
except NotImplementedError as e:
    print(e)<jupyter_output>Reading of WaveDroid files is not yet implemented<jupyter_text>Unit conversion<jupyter_code>print(oceanwaves.units.simplify('m^2 / m'))
print(oceanwaves.units.simplify('m ^ 2 / m'))                               # test spaces
print(oceanwaves.units.simplify('m2 / m'))                                  # test missing exponents
print(oceanwaves.units.simplify('m^3 / (m * m)'))                           # test groups
print(oceanwaves.units.simplify('m^4 / (m * (m / m^-1))'))                  # test nested groups
print(oceanwaves.units.simplify('m^5 / (m * m)^2'))                         # test group exponents
print(oceanwaves.units.simplify('m^5 / (m * m)^2 + NAP'))                   # test terms
print(oceanwaves.units.simplify('(kg * m^2 * s^2)^2 / (kg^2 * m^3 * s^4)')) # multiple units
print(oceanwaves.units.simplify('(m^2 / Hz) * m^-2'))                       # hertz to seconds conversion<jupyter_output>m
m
m
m
m
m
m + NAP
m
s
{'fraction_non_alphanumeric': 0.12761410471683712, 'fraction_numerical': 0.059532895741108226, 'mean_word_length': 2.38255033557047, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '610789', 'license': 'MIT', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2326, 'n_words': 519, 'path': 'notebooks/oceanwaves.ipynb', 'repo_name': 'bartvanwesten/oceanwaves-python', 'size': '6551'}