{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Himawari-8 data\nThis example demonstrates how to read Himawari-8 data files\nas reflectivity data.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Definitions\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nimport numpy as np\nimport pandas as pd\nimport xarray as xr\nimport cartopy as ct\nimport cartopy.feature as cfeature\nimport cartopy.crs as ccrs\nimport cartopy.io.shapereader as shpreader\nimport matplotlib.pyplot as plt\nfrom matplotlib.colors import BoundaryNorm, ListedColormap\n\nfrom swirlspy.sat.h8 import read_h8_data\n\nplt.switch_backend('agg')\n\nroot_dir = os.getcwd()\n\nstart_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialising\n\nThis section demonstrates parsing\nHimawari-8 data.\n\nStep 1: Define necessary parameter.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define base time\nbase_time = pd.Timestamp(\"2019-07-31T07:00\")\n\n# Define data boundary in WGS84 (latitude)\nlatitude_from = 30.\nlatitude_to = 16.\nlongitude_from = 105.\nlongitude_to = 122.\n\narea = (\n latitude_from, latitude_to,\n longitude_from, longitude_to\n)\n\n# Define grid size, use negative value for descending range\ngrid_size = (-.025, .025)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Step 2: Define data directory\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Supply data directory.\n# Please make sure H8 data filename is follow the naming pattern -\n# HS_H08_{date}_{time}_B{channel:02}_FLDK_R{rsol:02}_S{seg:02}10.DAT\n# example:\n# base time = 2019-07-31 07:00 UTC\n# channel = 4\n# resolution = 10\n# segment = 2\n# ========================================\n# filename: HS_H08_20190731_0700_B04_FLDK_R10_S0410.DAT\ndata_dir = os.path.join(root_dir, \"../tests/samples/h8\")\n\ninitialising_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Step 3: Parse data into reflectivity as xarray.DataArray\nusing read_h8_data().\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reflec = read_h8_data(\n data_dir,\n base_time,\n area,\n grid_size\n)\n\nsat_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Step 4: Remove invalid data if needed.\n**those data may be useful during post process, so this step is optional.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reflec.values[reflec.values < 13.] = reflec.attrs['zero_value']\n\nsat_post_time = pd.Timestamp.now()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating radar reflectivity maps\n\nDefine the color scale and format of the plots\nand plot using xarray.plot().\n\nIn this example, only hourly images will be plotted.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Defining colour scale and format\nlevels = [\n -32768,\n 10, 15, 20, 24, 28, 32,\n 34, 38, 41, 44, 47, 50,\n 53, 56, 58, 60, 62\n]\ncmap = ListedColormap([\n '#FFFFFF', '#08C5F5', '#0091F3', '#3898FF', '#008243', '#00A433',\n '#00D100', '#01F508', '#77FF00', '#E0D100', '#FFDC01', '#EEB200',\n '#F08100', '#F00101', '#E20200', '#B40466', '#ED02F0'\n])\n\nnorm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)\n\n# Defining the crs\ncrs = ct.crs.PlateCarree()\n\n# Defining coastlines\nmap_shape_file = os.path.join(root_dir, \"./../tests/samples/shape/rsmc\")\nocean_color = np.array([[[178, 208, 254]]], dtype=np.uint8)\nland_color = cfeature.COLORS['land']\ncoastline = cfeature.ShapelyFeature(\n list(shpreader.Reader(map_shape_file).geometries()),\n ccrs.PlateCarree()\n)\n\n# Plotting\nf = plt.figure()\nax = plt.axes(projection=crs)\nax.set_extent((\n longitude_from, longitude_to,\n latitude_from, latitude_to\n), crs=crs)\n\n# ocean\nax.imshow(np.tile(ocean_color, [2, 2, 1]),\n origin='upper',\n transform=ccrs.PlateCarree(),\n extent=[-180, 180, -180, 180],\n zorder=-1)\n# coastline, color\nax.add_feature(coastline,\n facecolor=land_color, edgecolor='none', zorder=0)\n# overlay coastline without color\nax.add_feature(coastline, facecolor='none',\n edgecolor='gray', linewidth=0.5, zorder=3)\nax.gridlines()\n\nreflec.where(reflec != reflec.attrs['zero_value']).plot(\n ax=ax,\n cbar_kwargs={\n 'extend': 'max',\n 'ticks': levels[1:],\n 'format': '%.3g'\n },\n cmap=cmap,\n norm=norm\n)\nax.set_title(\n \"Reflectivity\\n\"\n f\"Based @ {base_time.strftime('%H:%MH')}\",\n loc='left',\n fontsize=9\n)\nax.set_title(\n ''\n)\nax.set_title(\n f\"{base_time.strftime('%Y-%m-%d')} \\n\"\n f\"Valid @ {(base_time + pd.Timedelta(minutes=10)).strftime('%H:%MH')} \",\n loc='right',\n fontsize=9\n)\n\nplt.savefig(\n root_dir +\n f\"/../tests/outputs/h8.png\",\n dpi=300\n)\n\nsat_image_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\"H8 data parsing time: {sat_time}\")\nprint(f\"Post H8 data processing time: {sat_post_time}\")\nprint(f\"Plotting sat image time: {sat_image_time}\")\n\nprint(f\"Time to initialise: {initialising_time - start_time}\")\nprint(f\"Time to run data parsing: {sat_time - initialising_time}\")\nprint(f\"Time to perform post process: {sat_post_time - sat_time}\")\nprint(f\"Time to plot reflectivity image: {sat_image_time - sat_post_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 }