{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Convert to RGB format (vector data)\nThis example demonstrates convertion from wind data to uv rgb format (with HKO earth format).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Definitions\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import all required modules and methods:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Python package to allow system command line functions\nimport os\n# Python package to manage warning message\nimport warnings\n# Python package for time calculations\nimport pandas as pd\n# Python package for numerical calculations\nimport numpy as np\n# Python package for image generation\nfrom PIL import Image\n# Python package for image preview\nimport matplotlib.pyplot as plt\n\n# swirlspy iris parser function\nfrom swirlspy.rad.iris import read_iris_grid\n# swirlspy regrid function\nfrom swirlspy.preprocess import grid_align\n# swirlspy rgb convertion function\nfrom swirlspy.utils.conversion import to_rgb_data, to_hko_earth_format\n# directory constants\nfrom swirlspy.tests.samples import DATA_DIR\nfrom swirlspy.tests.outputs import OUTPUT_DIR\n\nwarnings.filterwarnings(\"ignore\")\n\n# Logging\nstart_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loading radar data\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Specify the basetime\nbasetime = pd.Timestamp('201902190800')\n\n# Reading the wind data\nreflec = read_iris_grid(\n os.path.join(\n DATA_DIR,\n basetime.strftime(\"iris/ppi/TMS%y%m%d%H%M02.PPIMK3B\")\n )\n)\n\ninitialising_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reproject to WGS 84, required for HKO earth format\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# calculate x, y step size\ny_step = -0.025\nx_step = 0.025\ny = np.arange(24.5, 20.5, y_step)\nx = np.arange(112, 116, x_step)\n\nreflec_wgs84 = grid_align(\n reflec, reflec.attrs['area_def'].proj_str,\n x, y, '+proj=longlat +datum=WGS84 +no_defs'\n)\n\npreparation_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert image data into rgb format\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# only shape with (y, x) is allowed\ndata = reflec_wgs84.sel(time=basetime)\n\n# only allow positive values\ndata = data.where(data >= 0)\n\n# this step is not necessary, depends on any meta data and preprocess is required by your platform\nearth_data = to_hko_earth_format(data, coords_dp=3, y_step=y_step, x_step=x_step)\nrgb = to_rgb_data(earth_data)\n\nconvertion_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualisation\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "path = os.path.join(OUTPUT_DIR, \"rgb_wind.png\")\nwith Image.fromarray(rgb, 'RGBA') as img:\n img.save(path, 'png')\n\n# preview\nwith Image.open(path) as image:\n plt.axis('off')\n plt.imshow(image)\n plt.plot()\n plt.show()\n\nvisualise_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Checking run time of each component\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(f\"Start time: {start_time}\")\nprint(f\"Initialising time: {initialising_time}\")\nprint(f\"Preparation time: {preparation_time}\")\nprint(f\"Convertion time: {convertion_time}\")\nprint(f\"Visualise time: {visualise_time}\")\n\nprint(f\"Time to initialise: {initialising_time - start_time}\")\nprint(f\"Time to prepare information: {preparation_time - initialising_time}\")\nprint(f\"Time to convertion: {convertion_time - preparation_time}\")\nprint(f\"Time to visualise: {visualise_time - convertion_time}\")\n\nprint(f\"Total: {visualise_time - start_time}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.15" } }, "nbformat": 4, "nbformat_minor": 0 }