{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\nLightning Nowcast (Hong Kong)\n========================================================\nThis example demonstrates how to perform\nlightning nowcasts of up to three hours, using data\nfrom Hong Kong.\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Definitions\n-----------------------------------------------------------\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nimport time\nfrom copy import copy\n\nimport pyproj\nimport pandas as pd\nimport xarray as xr\nimport matplotlib.pyplot as plt\nfrom matplotlib.colors import BoundaryNorm, ListedColormap, LinearSegmentedColormap\nfrom pyresample import utils\n\nfrom swirlspy.qpf import rover\nfrom swirlspy.qpf import sla\nfrom swirlspy.qpe.utils import timestamps_ending, locate_file\nfrom swirlspy.rad.iris import read_iris_raw\nfrom swirlspy.obs.lightning import Lightning\nfrom swirlspy.ltg.map import gen_ltgv\nfrom swirlspy.utils import standardize_attr, FrameType\n\nplt.switch_backend('agg')\nTHIS_DIR = os.getcwd()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Initialising\n-------------------------------------------------------------------------\n\nStep 1: Define the target grid as a pyresample AreaDefinition.\nIf a zoom in during the plotting phase is required, a separate\nAreaDefinition can be defined.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Defining target grid\narea_id = \"hk1980_250km\"\ndescription = (\"A 250 m resolution rectangular grid \"\n \"centred at HKO and extending to 250 km \"\n \"in each direction in HK1980 easting/northing coordinates\")\nproj_id = 'hk1980'\nprojection = ('+proj=tmerc +lat_0=22.31213333333334 '\n '+lon_0=114.1785555555556 +k=1 +x_0=836694.05 '\n '+y_0=819069.8 +ellps=intl +towgs84=-162.619,-276.959,'\n '-161.764,0.067753,-2.24365,-1.15883,-1.09425 +units=m '\n '+no_defs')\n\nx_size = 500\ny_size = 500\n\narea_extent = (587000, 569000, 1087000, 1069000)\n\narea_def = utils.get_area_def(\n area_id, description, proj_id, projection, x_size, y_size, area_extent\n)\n\narea_extent_plot = (712000, 695000, 962000, 945000)\n\narea_def_plot = utils.get_area_def(\n area_id, description, proj_id, projection, x_size, y_size, area_extent_plot\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Step 2: Defining a basetime, nowcast interval and nowcast duration.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "basetime = pd.Timestamp('20180811073000').floor('min')\nbasetime_str = basetime.strftime('%Y%m%d%H%M')\ninterval = pd.Timedelta(minutes=15)\nnowcast_duration = pd.Timedelta(hours=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Step 3: Extracting Lightning Location Information System (LLIS)\nlightning files to read.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Defining the times of files and the accumulation endtimes of files\nendtimes = [basetime - interval, basetime]\ntimestamps = []\nfor time in endtimes:\n ts = timestamps_ending(\n time,\n duration=interval,\n interval=pd.Timedelta(minutes=1)\n )\n timestamps.append(ts)\n\n# Locating files\nllis_dir = THIS_DIR + '/../tests/samples/llis'\nlocated_files = []\n\nfor tss in timestamps:\n lf = []\n for timestamp in tss:\n lf.append(locate_file(llis_dir, timestamp))\n located_files.append(lf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Step 4: Read data from files into Lightning objects.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Initialising Lightning objects\n# Coordinates are geodetic\nltg_object_list_geodetic = []\nfor idx, lst in enumerate(located_files):\n ltg_object_geodetic = Lightning(\n lst, 'geodetic', endtimes[idx]-interval, endtimes[idx]\n )\n ltg_object_list_geodetic.append(ltg_object_geodetic)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Step 5: Locating IRIS RAW radar files needed to generate motion field.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Generating timestrings for required times\nendtimes_str = []\nfor time in endtimes:\n endtimes_str.append(time.strftime('%y%m%d%H%M'))\n\nrad_dir = THIS_DIR + '/../tests/samples/iris/'\nlocated_rad_files = []\nfor time_str in endtimes_str:\n located_rad_files.append(locate_file(rad_dir, time_str))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Step 6: Read from iris radar files using read_iris_raw(). Projection system\nis specified in the read_iris_raw() function using the AreaDefinition\ndefined above. The resulting xarray.DataArrays are then concatenated\nalong the time axis.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reflec_list = []\nfor file in located_rad_files:\n reflec = read_iris_raw(\n file, area_def=area_def,\n coord_label=['easting', 'northing']\n )\n reflec_list.append(reflec)\n\nreflectivity = xr.concat(reflec_list, dim='time')\nstandardize_attr(reflectivity, frame_type=FrameType.dBZ)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data Reprojection\n---------------------------------------------------------------------------\n\nSince the coordinates of the Lightning objects are geodetic, a conversion\nfrom geodetic coordinates to that of the user-defined projection system is\nrequired.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define a function for neater conversion\n\n\ndef convert_ltg(\n ltg_object_geodetic,\n area_def\n):\n \"\"\"\n Convert coordinates in object from geodetic to HK1980.\n\n Parameters\n -----------\n ltg_object_geodetic: swirlspy.obs.lightning.Lightning\n Lightning object with geodetic coordinates.\n area_def: pyresample.geometry.AreaDefinition\n AreaDefinition of the target grid.\n\n Returns\n -------\n ltg_object: swirlspy.obs.lightning.Lightning\n Rain object in HK1980.\n\n \"\"\"\n\n # Defining Proj objects\n geod = pyproj.Proj(init=\"epsg:4326\")\n outproj = pyproj.Proj(area_def.proj_str)\n\n lx = []\n ly = []\n data = []\n\n for tup in ltg_object_geodetic.data:\n lat = tup[1]\n lon = tup[2]\n easting, northing = pyproj.transform(\n geod, outproj, lon, lat\n )\n lx.append(easting)\n ly.append(northing)\n data.append((tup[0], northing, easting))\n\n ltg_object = copy(ltg_object_geodetic)\n ltg_object.lx = lx\n ltg_object.ly = ly\n ltg_object.data = data\n ltg_object.proj = 'HK1980'\n\n return ltg_object" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Calling the function to convert projection system of coordinates\n\n\nltg_object_list = []\nfor ltg_object_geodetic in ltg_object_list_geodetic:\n ltg_object = convert_ltg(ltg_object_geodetic, area_def)\n ltg_object_list.append(ltg_object)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generating lightning potential field\n------------------------------------------------------------------\n\nGenerate lightning potential fields using gen_ltgv(). Lightning\npotential decrease normally according to the provided sigma from\nthe site. Lightning potentials are then concatenated along the time\naxis.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Obtaining lightning potential field from lightning objects\nltgv_list = []\nfor ltg_object in ltg_object_list:\n ltgvi = gen_ltgv(\n ltg_object,\n area_def,\n ltg_object.start_time,\n ltg_object.end_time,\n sigma=20000,\n coord_label=['easting', 'northing']\n )\n ltgv_list.append(ltgvi)\n\n\n# Concatenating lightning potential along time dimension\nltgv = xr.concat(ltgv_list, dim='time')\nstandardize_attr(ltgv, frame_type=FrameType.dBZ)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generating motion fields and extrapolating\n--------------------------------------------------------------\n\n1. Generate motion field using ROVER.\n2. Extrapolate lightning potential field using the motion field\n by Semi-Lagrangian Advection\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# ROVER\nmotion = rover(reflectivity)\n# SLA\nsteps = int(nowcast_duration.total_seconds()) // int(interval.total_seconds())\nltgvf = sla(ltgv, motion, nowcast_steps=steps)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting results\n----------------------------------------------------------------\n\nPlot motion fields with reflectivities and lightning potential nowcasts.\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting reflectivities with motion fields.\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 = area_def.to_cartopy_crs()\n\nqx = motion.coords['easting'].values[::5]\nqy = motion.coords['northing'].values[::5]\nqu = motion.values[0, ::5, ::5]\nqv = motion.values[1, ::5, ::5]\n\n# Plotting\np = reflectivity.plot(\n col='time',\n subplot_kws={'projection': crs},\n cbar_kwargs={\n 'extend': 'max',\n 'ticks': levels[1:],\n 'format': '%.3g'\n },\n cmap=cmap,\n norm=norm\n)\nfor idx, ax in enumerate(p.axes.flat):\n time = pd.Timestamp(reflectivity.coords['time'].values[idx])\n ax.quiver(qx, qy, qu, qv, pivot='mid', regrid_shape=20)\n ax.coastlines('10m')\n ax.gridlines()\n ax.set_title(\n \"Reflectivity\\n\"\n f\"Based @ {basetime.strftime('%H:%MH')}\",\n loc='left',\n fontsize=9\n )\n ax.set_title(\n ''\n )\n ax.set_title(\n f\"{basetime.strftime('%Y-%m-%d')} \\n\"\n f\"Valid @ {time.strftime('%H:%MH')} \",\n loc='right',\n fontsize=9\n )\nplt.savefig(\n THIS_DIR +\n f\"/../tests/outputs/reflectivity-hk.png\",\n dpi=300\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting lightning potentials. In this example, only hourly\npotentials are plotted.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Defining the crs\ncrs = area_def_plot.to_cartopy_crs()\n\n# Defining zoom\nzoom = (712000, 962000, 695000, 945000) # (x0, x1, y0, y1)\n\n# Generating a timelist for every hour\ntimelist = [\n basetime + pd.Timedelta(minutes=60*i) for i in range(4)\n]\n\n# Obtaining the slice of the xarray to be plotted\nda_plot = ltgvf.sel(\n time=timelist,\n easting=slice(zoom[0], zoom[1]),\n northing=slice(zoom[3], zoom[2])\n)\n\ncmap = plt.get_cmap('Reds')\n\n# Defining motion quivers\nmotion_sel = motion.sel(\n easting=slice(zoom[0], zoom[1]),\n northing=slice(zoom[3], zoom[2])\n)\nqx = motion_sel.coords['easting'].values[::10]\nqy = motion_sel.coords['northing'].values[::10]\nqu = motion_sel.values[0, ::10, ::10]\nqv = motion_sel.values[1, ::10, ::10]\n\n# Plotting\np = da_plot.plot(\n col='time', col_wrap=2,\n subplot_kws={'projection': crs},\n cbar_kwargs={\n 'extend': 'max',\n 'ticks': [i/10 for i in range(11)],\n 'format': '%.3g'\n },\n cmap=cmap\n)\nfor idx, ax in enumerate(p.axes.flat):\n ax.quiver(qx, qy, qu, qv, pivot='mid')\n ax.coastlines('10m')\n ax.set_xlim(zoom[0], zoom[1])\n ax.set_ylim(zoom[2], zoom[3])\n ax.gridlines()\n ax.set_title(\n \"Lightning Potential\\n\"\n f\"Based @ {basetime.strftime('%H:%MH')}\",\n loc='left',\n fontsize=8\n )\n ax.set_title(\n ''\n )\n ax.set_title(\n f\"{basetime.strftime('%Y-%m-%d')} \\n\"\n f\"Valid @ {timelist[idx].strftime('%H:%MH')} \",\n loc='right',\n fontsize=8\n )\nplt.savefig(\n THIS_DIR +\n f\"/../tests/outputs/ltgvf.png\",\n dpi=300\n)" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 0 }