.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_gallery/plot_numpy_as_input.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_gallery_plot_numpy_as_input.py: Using numpy arrays as input =========================== You don't have a `xarray.Dataset` ready to use as input? No worries, you can use the `.from_numpy()` class method to initialise the `Triplets` and `Arrays` classes from `numpy.ndarray` objects .. GENERATED FROM PYTHON SOURCE LINES 12-24 .. code-block:: Python import numpy as np import xarray as xr from scipy.io import loadmat from matplotlib import pyplot as plt import ewdm from ewdm.sources import SpotterBuoysDataSource from ewdm.plots import plot_directional_spectrum .. GENERATED FROM PYTHON SOURCE LINES 25-29 Testing with ewdm.Triplets -------------------------- Let's see how this feature work with the ewdm.Triplets class .. GENERATED FROM PYTHON SOURCE LINES 29-43 .. code-block:: Python spotter = SpotterBuoysDataSource("../../data/displacement.csv") dataset = spotter.read_dataset() subset = dataset.sel(time=slice("2020-08-20 10:00", "2020-08-20 10:30")) # extract numpy arrays from dataset time = subset["time"].data surface_elevation = subset["surface_elevation"].data eastward_displacement = subset["eastward_displacement"].data northward_displacement = subset["northward_displacement"].data sampling_rate = subset.attrs["sampling_rate"] .. GENERATED FROM PYTHON SOURCE LINES 44-45 Now, we can initialise the `ewdm.Triplets` class with the numpy arrays .. GENERATED FROM PYTHON SOURCE LINES 45-56 .. code-block:: Python spec = ewdm.Triplets.from_numpy( time = time, surface_elevation = surface_elevation, eastward_displacement = eastward_displacement, northward_displacement = northward_displacement, fs=sampling_rate, max_time_gap="30s" ) print("Sampling rate is: ", spec.fs) print("Maximum allowed time gap is: ", spec.max_time_gap) .. rst-class:: sphx-glr-script-out .. code-block:: none Sampling rate is: 2.5 Maximum allowed time gap is: 30s .. GENERATED FROM PYTHON SOURCE LINES 57-58 Let's compute the spectra and print the output .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python output_triplets = spec.compute() print(output_triplets) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 95kB Dimensions: (frequency: 81, direction: 72) Coordinates: * frequency (frequency) float64 648B 0.03125 0.03263 ... 1.0 * direction (direction) float64 576B -180.0 -175.0 ... 175.0 Data variables: directional_spectrum (frequency, direction) float64 47kB 0.0001156 .... directional_distribution (frequency, direction) float64 47kB 0.001684 ..... frequency_spectrum (frequency) float64 648B 0.06864 ... 0.0003756 .. GENERATED FROM PYTHON SOURCE LINES 63-67 Testing with ewdm.Arrays ------------------------ It works for `ewdm.Arrays` class too. .. GENERATED FROM PYTHON SOURCE LINES 67-82 .. code-block:: Python # loading matlab file mat_fname = "../../data/donelan_run62.mat" mat_data = loadmat(mat_fname, simplify_cells=True) sampling_rate = mat_data["fs"] time = np.arange(len(mat_data["eta"])) / sampling_rate elements = np.arange(len(mat_data["x"])) print("Surface elevation shape: ", mat_data["eta"].shape) print("Time array shape: ", time.shape) print("Position x shape : ", mat_data["x"].shape) print("Position y shape : ", mat_data["y"].shape) .. rst-class:: sphx-glr-script-out .. code-block:: none Surface elevation shape: (4096, 6) Time array shape: (4096,) Position x shape : (6,) Position y shape : (6,) .. GENERATED FROM PYTHON SOURCE LINES 83-84 Now let's create spec object .. GENERATED FROM PYTHON SOURCE LINES 84-97 .. code-block:: Python spec = ewdm.Arrays.from_numpy( time = time, surface_elevation = mat_data["eta"], position_x = mat_data["x"], position_y = mat_data["y"], fs = sampling_rate, max_nan_ratio = 0.05, ) print("Sampling rate is: ", spec.fs) print("Maximum allowed nan ratio: ", spec.max_nan_ratio) .. rst-class:: sphx-glr-script-out .. code-block:: none Sampling rate is: 4 Maximum allowed nan ratio: 0.05 .. GENERATED FROM PYTHON SOURCE LINES 98-99 Let's compute the spectra and print the output .. GENERATED FROM PYTHON SOURCE LINES 99-103 .. code-block:: Python output_array = spec.compute() print(output_array) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 114kB Dimensions: (frequency: 97, direction: 72) Coordinates: * frequency (frequency) float64 776B 0.03125 0.03263 ... 2.0 * direction (direction) float64 576B -180.0 -175.0 ... 175.0 Data variables: directional_spectrum (frequency, direction) float64 56kB 1.444e-05 .... directional_distribution (frequency, direction) float64 56kB 0.003793 ..... frequency_spectrum (frequency) float64 776B 0.003806 ... 3.278e-05 .. GENERATED FROM PYTHON SOURCE LINES 104-108 Let's plot the results ---------------------- It's always nice to see some specral plots, isn't it? .. GENERATED FROM PYTHON SOURCE LINES 108-122 .. code-block:: Python fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8,4), layout="constrained") plot_directional_spectrum( output_array.directional_spectrum, ax=ax1, levels=None, colorbar=True, axes_kw={"rmin": 0.1, "rmax": 1.2, "rstep": 0.2, "angle": 135}, cbar_kw={"label": ""} ) plot_directional_spectrum( output_triplets.directional_spectrum, ax=ax2, levels=None, colorbar=True, axes_kw={"rmin": 0.1, "rmax": 1.2, "rstep": 0.2, "angle": 135}, cbar_kw={"label": "$E(f,\\theta)$"} ) .. image-sg:: /auto_gallery/images/sphx_glr_plot_numpy_as_input_001.png :alt: plot numpy as input :srcset: /auto_gallery/images/sphx_glr_plot_numpy_as_input_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.364 seconds) .. _sphx_glr_download_auto_gallery_plot_numpy_as_input.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_numpy_as_input.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_numpy_as_input.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_numpy_as_input.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_