.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/rgb_wind.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_rgb_wind.py: Convert to RGB format (vector data) =========================== This example demonstrates convertion from wind data to uv rgb format (with HKO earth format). .. GENERATED FROM PYTHON SOURCE LINES 8-11 Definitions -------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 13-14 Import all required modules and methods: .. GENERATED FROM PYTHON SOURCE LINES 14-38 .. code-block:: default # Python package to allow system command line functions import os # Python package to manage warning message import warnings # Python package for time calculations import pandas as pd # Python package for numerical calculations import numpy as np # Python package for xarrays to read and handle netcdf data import xarray as xr # Python package for image generation from PIL import Image # Python package for image preview import matplotlib.pyplot as plt # swirlspy data convertion function from swirlspy.utils.conversion import to_rgb_data, to_hko_earth_format # directory constants from swirlspy.tests.samples import DATA_DIR from swirlspy.tests.outputs import OUTPUT_DIR warnings.filterwarnings("ignore") .. GENERATED FROM PYTHON SOURCE LINES 39-41 Define the working directories: .. GENERATED FROM PYTHON SOURCE LINES 41-47 .. code-block:: default data_dir = os.path.join(DATA_DIR, 'gts') # Logging start_time = pd.Timestamp.now() .. GENERATED FROM PYTHON SOURCE LINES 48-51 Loading wind data ----------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-75 .. code-block:: default # Specify the basetime basetime = pd.Timestamp('202107180000') # Reading the wind data ds = xr.open_dataset( os.path.join( data_dir, f"romas_plv_{basetime.strftime('%Y%m%d%H%M')}.nc" ) ) # pick pressure level, e.g. 200 in this case ds_200 = ds.sel(isobaricInhPa=200) u = ds_200['u'].data v = ds_200['v'].data y = ds['latitude'].data x = ds['longitude'].data # Close dataset ds.close() initialising_time = pd.Timestamp.now() .. GENERATED FROM PYTHON SOURCE LINES 76-79 Extract all necessary values for rgb image ----------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 79-108 .. code-block:: default # prepare placeholder for necessary values d = np.empty((len(y), len(x), 2)) d[:] = 0 d[:, :, 0] = u d[:, :, 1] = v # calculate x, y step size y_step = (y[-1] - y[0]) / len(y) x_step = (x[-1] - x[0]) / len(x) # build image data data = xr.DataArray( d, dims=['y', 'x', 'wind'], coords={ 'wind': ['u', 'v'], 'y': y, 'x': x }, attrs={ 'y_step': y_step, 'x_step': x_step } ) preparation_time = pd.Timestamp.now() .. GENERATED FROM PYTHON SOURCE LINES 109-112 Convert image data into rgb format ----------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 112-119 .. code-block:: default # this step is not necessary, depends on any meta data and preprocess is required by your platform earth_data = to_hko_earth_format(data, coords_dp=3, values_dp=0, uv_mode=True, y_step=y_step, x_step=x_step) rgb = to_rgb_data(earth_data, uv_mode=True) convertion_time = pd.Timestamp.now() .. GENERATED FROM PYTHON SOURCE LINES 120-123 Visualisation ----------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 123-137 .. code-block:: default path = os.path.join(OUTPUT_DIR, "rgb_wind.png") with Image.fromarray(rgb, 'RGBA') as img: img.save(path, 'png') # preview with Image.open(path) as image: plt.axis('off') plt.imshow(image) plt.plot() plt.show() visualise_time = pd.Timestamp.now() .. image-sg:: /auto_examples/images/sphx_glr_rgb_wind_001.png :alt: rgb wind :srcset: /auto_examples/images/sphx_glr_rgb_wind_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 138-141 Checking run time of each component ----------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 141-154 .. code-block:: default print(f"Start time: {start_time}") print(f"Initialising time: {initialising_time}") print(f"Preparation time: {preparation_time}") print(f"Convertion time: {convertion_time}") print(f"Visualise time: {visualise_time}") print(f"Time to initialise: {initialising_time - start_time}") print(f"Time to prepare information: {preparation_time - initialising_time}") print(f"Time to convertion: {convertion_time - preparation_time}") print(f"Time to visualise: {visualise_time - convertion_time}") print(f"Total: {visualise_time - start_time}") .. rst-class:: sphx-glr-script-out .. code-block:: none Start time: 2024-04-18 02:42:05.006435 Initialising time: 2024-04-18 02:42:05.430545 Preparation time: 2024-04-18 02:42:05.434078 Convertion time: 2024-04-18 02:42:05.469792 Visualise time: 2024-04-18 02:42:05.712961 Time to initialise: 0 days 00:00:00.424110 Time to prepare information: 0 days 00:00:00.003533 Time to convertion: 0 days 00:00:00.035714 Time to visualise: 0 days 00:00:00.243169 Total: 0 days 00:00:00.706526 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.861 seconds) .. _sphx_glr_download_auto_examples_rgb_wind.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: rgb_wind.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: rgb_wind.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_