Extract some time-series from the database and analysis#

import resourcecode
import resourcecode.spectrum
import matplotlib.pyplot as plot

plot.rcParams["figure.dpi"] = 400

Node selection and data extraction#

Below, we will look for data next to the location of interest, located in the vicinity of Brest Bay. We will look at the time-series data and also some spectral data

selected_node = resourcecode.data.get_closest_point(latitude=48.3026514, longitude=-4.6861533)
selected_node
(np.int32(134940), np.float64(296.89))
selected_specPoint = resourcecode.data.get_closest_station(latitude=48.3026514, longitude=-4.6861533)
selected_specPoint
('W004679N48304', np.float64(808.82))

Extraction of data from the Hindcast database#

Sea-state parameters extraction and helpers from the toolbox#

Once the node is selected, it is possible to retrieve the data from the Cassandra database using the commands below. It is also possible to select which variables to retrieve. The list of available variables can be obtained using the get_variables method. We study here the following variables:

  • \(H_s\), the significant wave height;

  • \(T_p\) the peak period;

  • \(D_p\) the direction at peak frequency;

  • The energy flux \(CgE\);

  • zonal and meridional velocity components of wind;

For this example, we have selectedonly year 2010.

client = resourcecode.Client()
data = client.get_dataframe(
    pointId=selected_node[0],
    startDateTime="2010-01-01T01:00:00",
    endDateTime="2011-01-01T00:00:00",
    parameters=("hs", "uwnd", "vwnd", "t02", "tp", "dp", "cge"),
)
data.head()
hs uwnd vwnd t02 tp dp cge
2010-01-01 01:00:00 1.360 -6.0 -6.7 4.78 11.363636 221.0 9.3
2010-01-01 02:00:00 1.350 -6.0 -7.0 4.68 11.363636 221.0 8.9
2010-01-01 03:00:00 1.324 -5.6 -7.3 4.62 11.494253 222.0 8.5
2010-01-01 04:00:00 1.282 -5.4 -7.8 4.56 11.494253 223.0 7.9
2010-01-01 05:00:00 1.220 -5.3 -8.1 4.60 11.494253 224.0 7.2


With the toolbox, is is possible to convert zonal and meridional velocity components of wind the the more convenient Intensity-direction variables.

data["wspd"], data["wdir"] = resourcecode.utils.zmcomp2metconv(data.uwnd, data.vwnd)

The figure below is an example of the histograme of the variables that can be extracted from the database. \(H_s\),:math:T_p, \(W_s\) and \(CgE\) are shown here with the wind and wave directions, but the code can be changed to plot any of the available variables in the Hindcast database.

data[["hs", "tp", "cge", "wspd", "dp", "wdir"]].hist(bins=15, figsize=[16, 8])
plot.tight_layout()
hs, tp, cge, wspd, dp, wdir

Spectral data extraction and computation of sea-state parameters#

The toolbox also offers the possibility to download the spectral data from the coarser ‘SPEC’ grid, corresponding to the orange dots of the web portal. This is possible thanks to the get_2D_spectrum and get_1D_spectrum from the spectrum module. An example is shown below:

spec = resourcecode.spectrum.get_2D_spectrum(selected_specPoint[0], years=["2010"], months=["01"])
/home/runner/work/py-resourcecode/py-resourcecode/.venv/lib/python3.12/site-packages/xarray/computation/apply_ufunc.py:820: RuntimeWarning: overflow encountered in power
  result_data = func(*input_data)

And we offer function to represent the spectral data, both for 2D and 1D spectrum.

resourcecode.spectrum.plot_2D_spectrum(spec, 1)
plot.show()
Wave directional spectrum at point W004679N48304 (-4.68°W,48.30°N) on 1970-01-01 00:00:00.000007305
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:151: RuntimeWarning: invalid value encountered in scalar divide
  T01 = M0 / M01
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:154: RuntimeWarning: invalid value encountered in scalar divide
  T02 = np.sqrt(M0 / M02)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:157: RuntimeWarning: invalid value encountered in scalar divide
  Te = Me / M0
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:169: RuntimeWarning: invalid value encountered in scalar divide
  nu = np.sqrt((M0 * M02) / (M01**2) - 1)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:170: RuntimeWarning: invalid value encountered in scalar divide
  mu = np.sqrt(1 - M01**2 / (M0 * M02))
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:174: RuntimeWarning: invalid value encountered in scalar divide
  km = np.trapezoid(k * Ef, x=freq) / M0
/home/runner/work/py-resourcecode/py-resourcecode/.venv/lib/python3.12/site-packages/numpy/lib/_function_base_impl.py:5036: RuntimeWarning: invalid value encountered in add
  ret = (d * (y[tuple(slice1)] + y[tuple(slice2)]) / 2.0).sum(axis)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:283: RuntimeWarning: overflow encountered in square
  S2 = E**2
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:287: RuntimeWarning: invalid value encountered in scalar divide
  Qp = 2 * MQ / (M0**2)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/plots.py:109: RuntimeWarning: invalid value encountered in divide
  Ef = Ef / Ef.max()

There is also function to compute the 1D spectrum from the 2D.

spec1D = resourcecode.spectrum.convert_spectrum_2Dto1D(spec)
resourcecode.spectrum.plot_1D_spectrum(spec1D, 1, sea_state=False)
<Figure size 700x500 with 1 Axes>

Among the functionalities of the toolbox, it is possible to compute the sea-state parameters from spectral data. Small discrepancies can be found between the Hindcast sea-state parameters and the one computed with the toolbox.

parameters_df = resourcecode.spectrum.compute_parameters_from_2D_spectrum(spec, use_depth=True)
parameters_df.head()
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:270: RuntimeWarning: invalid value encountered in scalar divide
  Spr = np.sqrt(2 * (1 - np.sqrt((am**2 + bm**2) / M0**2)))
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:169: RuntimeWarning: overflow encountered in scalar multiply
  nu = np.sqrt((M0 * M02) / (M01**2) - 1)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:169: RuntimeWarning: overflow encountered in scalar power
  nu = np.sqrt((M0 * M02) / (M01**2) - 1)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:170: RuntimeWarning: overflow encountered in scalar power
  mu = np.sqrt(1 - M01**2 / (M0 * M02))
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:170: RuntimeWarning: overflow encountered in scalar multiply
  mu = np.sqrt(1 - M01**2 / (M0 * M02))
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:270: RuntimeWarning: overflow encountered in scalar power
  Spr = np.sqrt(2 * (1 - np.sqrt((am**2 + bm**2) / M0**2)))
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:287: RuntimeWarning: overflow encountered in scalar power
  Qp = 2 * MQ / (M0**2)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:270: RuntimeWarning: invalid value encountered in sqrt
  Spr = np.sqrt(2 * (1 - np.sqrt((am**2 + bm**2) / M0**2)))
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:169: RuntimeWarning: invalid value encountered in sqrt
  nu = np.sqrt((M0 * M02) / (M01**2) - 1)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:170: RuntimeWarning: invalid value encountered in sqrt
  mu = np.sqrt(1 - M01**2 / (M0 * M02))
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:147: RuntimeWarning: invalid value encountered in sqrt
  Hm0 = 4 * np.sqrt(M0)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:255: RuntimeWarning: overflow encountered in multiply
  cgef = np.trapezoid(cg[:, np.newaxis] * E.T, x=vdir, axis=1)
/home/runner/work/py-resourcecode/py-resourcecode/.venv/lib/python3.12/site-packages/numpy/_core/_methods.py:49: RuntimeWarning: invalid value encountered in reduce
  return umr_sum(a, axis, dtype, out, keepdims, initial, where)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:156: RuntimeWarning: overflow encountered in divide
  Me = np.trapezoid(Ef / freq, x=freq)
/home/runner/work/py-resourcecode/py-resourcecode/resourcecode/spectrum/compute_parameters.py:183: RuntimeWarning: overflow encountered in multiply
  cgef = np.trapezoid(cg * Ef, x=freq)
time Hm0 Tp T01 T02 Te mu nu CgE km lm depth Thetam Thetapm Spr Qp wnd wnddir cur curdir
0 7305.000000 inf 29.498525 NaN NaN NaN NaN NaN inf NaN NaN 57 225.0 225.0 NaN NaN 90 444 3 2189
0 7305.041667 inf 29.498525 NaN NaN NaN NaN NaN inf NaN NaN 60 225.0 225.0 NaN NaN 90 420 4 2272
0 7305.083333 inf 29.498525 NaN NaN NaN NaN NaN inf NaN NaN 63 225.0 225.0 NaN NaN 92 402 3 2346
0 7305.125000 inf 29.498525 NaN NaN NaN NaN NaN inf NaN NaN 65 225.0 225.0 NaN NaN 92 378 2 2460
0 7305.166667 inf 29.498525 NaN NaN NaN NaN NaN inf NaN NaN 65 225.0 225.0 NaN NaN 95 348 1 2859


Total running time of the script: (0 minutes 19.674 seconds)

Gallery generated by Sphinx-Gallery