{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Superpose an observation point on a H2D plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:31:12.472864Z", "iopub.status.busy": "2025-01-13T11:31:12.472369Z", "iopub.status.idle": "2025-01-13T11:31:12.996081Z", "shell.execute_reply": "2025-01-13T11:31:12.995476Z" } }, "outputs": [], "source": [ "%matplotlib inline\n", "# for figures in notebook\n", "\n", "# import & initialize epygram\n", "import epygram\n", "epygram.init_env()\n", "\n", "import os\n", "INPUTS_DIR = os.path.join('..', 'inputs')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:31:12.998241Z", "iopub.status.busy": "2025-01-13T11:31:12.998033Z", "iopub.status.idle": "2025-01-13T11:31:13.113224Z", "shell.execute_reply": "2025-01-13T11:31:13.112728Z" } }, "outputs": [], "source": [ "r = epygram.open(os.path.join(INPUTS_DIR, 'grid.arome-forecast.guyane0025+0024:00.grib'), 'r')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:31:13.115273Z", "iopub.status.busy": "2025-01-13T11:31:13.115106Z", "iopub.status.idle": "2025-01-13T11:31:13.411925Z", "shell.execute_reply": "2025-01-13T11:31:13.411424Z" } }, "outputs": [], "source": [ "f = r.readfield('shortName:2r')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:31:13.414077Z", "iopub.status.busy": "2025-01-13T11:31:13.413914Z", "iopub.status.idle": "2025-01-13T11:31:13.416868Z", "shell.execute_reply": "2025-01-13T11:31:13.416485Z" } }, "outputs": [], "source": [ "import cartopy.crs as ccrs\n", "lonlat_crs = ccrs.PlateCarree()\n", "station = (-52.6, 5.1)\n", "value = 60." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:31:13.418871Z", "iopub.status.busy": "2025-01-13T11:31:13.418580Z", "iopub.status.idle": "2025-01-13T11:31:14.166923Z", "shell.execute_reply": "2025-01-13T11:31:14.166484Z" } }, "outputs": [], "source": [ "fig, ax = f.cartoplot(colormap='viridis_r')\n", "x, y = ax.projection.transform_point(*station, lonlat_crs)\n", "ax.scatter(x, y, s=100, c='k')\n", "ax.text(x, y, ' Kourou', size=20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now if we want the color of the obs point to match the colormap of the field:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:31:14.170174Z", "iopub.status.busy": "2025-01-13T11:31:14.169957Z", "iopub.status.idle": "2025-01-13T11:31:14.172699Z", "shell.execute_reply": "2025-01-13T11:31:14.172329Z" } }, "outputs": [], "source": [ "# for that we need arrays\n", "import numpy as np\n", "lons = np.array([station[0]])\n", "lats = np.array([station[1]])\n", "values = np.array([value])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:31:14.174558Z", "iopub.status.busy": "2025-01-13T11:31:14.174310Z", "iopub.status.idle": "2025-01-13T11:31:14.645015Z", "shell.execute_reply": "2025-01-13T11:31:14.644439Z" } }, "outputs": [], "source": [ "fig, ax = f.cartoplot(colormap='viridis_r')\n", "xyz = ax.projection.transform_points(lonlat_crs, lons, lats)\n", "x = xyz[..., 0]\n", "y = xyz[..., 1]\n", "ax.scatter(x, y, s=100, c=values, cmap='viridis_r',\n", " vmin=f.data.min(), vmax=f.data.max())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## And now if the colormap is complex and needs a ColormapHelper to handle:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:31:14.647913Z", "iopub.status.busy": "2025-01-13T11:31:14.647713Z", "iopub.status.idle": "2025-01-13T11:31:15.253556Z", "shell.execute_reply": "2025-01-13T11:31:15.253060Z" } }, "outputs": [], "source": [ "f = r.readfield('parameterCategory:1,parameterNumber:19,typeOfStatisticalProcessing:0')\n", "values = np.array([12]) # the red color in colormap\n", "fig, ax = f.cartoplot(colormap='ptype')\n", "ch = epygram.colormapping.get_ColormapHelper('ptype')\n", "ax.scatter(x, y, s=100, c=values, **ch.kwargs_for_plot('scatter'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }