{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "c219a358", "metadata": {}, "source": [ "# 📍 Nearest Points\n", "\n", "This demonstration to get the value at the nearest latitude/longitude point builds on info from these sources:\n", "\n", "- Stack Overflow: https://stackoverflow.com/questions/58758480/xarray-select-nearest-lat-lon-with-multi-dimension-coordinates\n", "- MetPy Details: https://unidata.github.io/MetPy/latest/tutorials/xarray_tutorial.html?highlight=assign_y_x" ] }, { "cell_type": "code", "execution_count": 1, "id": "7cf9c7a5", "metadata": {}, "outputs": [], "source": [ "from herbie import Herbie\n", "from metpy.units import units\n", "import matplotlib.pyplot as plt\n", "from toolbox import EasyMap, pc, ccrs\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "from shapely.geometry import MultiPoint\n", "from toolbox.gridded_data import pluck_points" ] }, { "cell_type": "code", "execution_count": 2, "id": "227fb9d7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "✅ Found ┊ model=hrrr ┊ \u001b[3mproduct=sfc\u001b[0m ┊ \u001b[38;2;41;130;13m2021-Sep-23 00:00 UTC\u001b[92m F00\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mGRIB2 @ aws\u001b[0m ┊ \u001b[38;2;255;153;0m\u001b[3mIDX @ aws\u001b[0m\n", "👨🏻🏭 Created directory: [/home/blaylock/data/hrrr/20210923]\n" ] } ], "source": [ "H = Herbie(\"2021-9-23\")\n", "ds = H.xarray(\"TMP:2 m\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "1c998310", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n",
"Dimensions: (y: 1059, x: 1799)\n",
"Coordinates:\n",
" time datetime64[ns] 2021-09-23\n",
" step timedelta64[ns] 00:00:00\n",
" heightAboveGround float64 2.0\n",
" latitude (y, x) float64 21.14 21.15 21.15 ... 47.86 47.85 47.84\n",
" longitude (y, x) float64 237.3 237.3 237.3 ... 299.0 299.0 299.1\n",
" valid_time datetime64[ns] 2021-09-23\n",
"Dimensions without coordinates: y, x\n",
"Data variables:\n",
" t2m (y, x) float32 297.7 297.7 297.8 ... 288.6 288.6 288.6\n",
" gribfile_projection object None\n",
"Attributes:\n",
" GRIB_edition: 2\n",
" GRIB_centre: kwbc\n",
" GRIB_centreDescription: US National Weather Service - NCEP\n",
" GRIB_subCentre: 0\n",
" Conventions: CF-1.7\n",
" institution: US National Weather Service - NCEP\n",
" model: hrrr\n",
" product: sfc\n",
" description: High-Resolution Rapid Refresh - CONUS\n",
" remote_grib: https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr....\n",
" local_grib: /home/blaylock/data/hrrr/20210923/subset_1beffd5...\n",
" searchString: TMP:2 m<xarray.Dataset>\n",
"Dimensions: (point: 2)\n",
"Coordinates:\n",
" time datetime64[ns] 2021-09-23\n",
" step timedelta64[ns] 00:00:00\n",
" heightAboveGround float64 2.0\n",
" latitude (point) float64 40.01 33.99\n",
" longitude (point) float64 260.0 245.0\n",
" valid_time datetime64[ns] 2021-09-23\n",
" metpy_crs object Projection: lambert_conformal_conic\n",
" y (point) float64 1.707e+05 -3.483e+05\n",
" x (point) float64 -2.135e+05 -1.609e+06\n",
" * point (point) <U7 'point_a' 'point_b'\n",
" point_latitude (point) int64 40 34\n",
" point_longitude (point) int64 -100 -115\n",
"Data variables:\n",
" t2m (point) float32 298.5 310.2\n",
" gribfile_projection (point) object None None\n",
"Attributes:\n",
" GRIB_edition: 2\n",
" GRIB_centre: kwbc\n",
" GRIB_centreDescription: US National Weather Service - NCEP\n",
" GRIB_subCentre: 0\n",
" Conventions: CF-1.7\n",
" institution: US National Weather Service - NCEP\n",
" model: hrrr\n",
" product: sfc\n",
" description: High-Resolution Rapid Refresh - CONUS\n",
" remote_grib: https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr....\n",
" local_grib: /home/blaylock/data/hrrr/20210923/subset_1beffd5...\n",
" searchString: TMP:2 m<xarray.Dataset>\n",
"Dimensions: (point: 13)\n",
"Coordinates:\n",
" time datetime64[ns] 2021-09-23\n",
" step timedelta64[ns] 00:00:00\n",
" heightAboveGround float64 2.0\n",
" latitude (point) float64 25.01 27.01 29.0 ... 44.99 46.99 49.0\n",
" longitude (point) float64 240.0 242.0 244.0 ... 260.0 262.0 264.0\n",
" valid_time datetime64[ns] 2021-09-23\n",
" metpy_crs object Projection: lambert_conformal_conic\n",
" y (point) float64 -1.23e+06 -1.056e+06 ... 1.176e+06\n",
" x (point) float64 -2.305e+06 -2.053e+06 ... 1.105e+05\n",
" * point (point) int64 0 1 2 3 4 5 6 7 8 9 10 11 12\n",
" point_latitude (point) int64 25 27 29 31 33 35 37 39 41 43 45 47 49\n",
" point_longitude (point) int64 -120 -118 -116 -114 ... -102 -100 -98 -96\n",
"Data variables:\n",
" t2m (point) float32 22.38 22.5 20.69 ... 20.56 19.63 19.69\n",
" gribfile_projection (point) object None None None None ... None None None\n",
"Attributes:\n",
" GRIB_edition: 2\n",
" GRIB_centre: kwbc\n",
" GRIB_centreDescription: US National Weather Service - NCEP\n",
" GRIB_subCentre: 0\n",
" Conventions: CF-1.7\n",
" institution: US National Weather Service - NCEP\n",
" model: hrrr\n",
" product: sfc\n",
" description: High-Resolution Rapid Refresh - CONUS\n",
" remote_grib: https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr....\n",
" local_grib: /home/blaylock/data/hrrr/20210923/subset_1beffd5...\n",
" searchString: TMP:2 m<xarray.Dataset>\n",
"Dimensions: (time: 2, step: 18, point: 1)\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 2022-01-01 2022-01-01T06:00:00\n",
" * step (step) timedelta64[ns] 00:00:00 01:00:00 ... 17:00:00\n",
" heightAboveGround float64 2.0\n",
" latitude float64 40.01\n",
" longitude float64 260.0\n",
" valid_time (step, time) datetime64[ns] 2022-01-01 ... 2022-01-0...\n",
" metpy_crs object Projection: lambert_conformal_conic\n",
" y float64 1.707e+05\n",
" x float64 -2.135e+05\n",
"Dimensions without coordinates: point\n",
"Data variables:\n",
" t2m (point, step, time) float32 267.2 260.9 ... 256.0 258.3\n",
" gribfile_projection (point) object None\n",
"Attributes:\n",
" GRIB_edition: 2\n",
" GRIB_centre: kwbc\n",
" GRIB_centreDescription: US National Weather Service - NCEP\n",
" GRIB_subCentre: 0\n",
" Conventions: CF-1.7\n",
" institution: US National Weather Service - NCEP\n",
" model: hrrr\n",
" product: sfc\n",
" description: High-Resolution Rapid Refresh - CONUS\n",
" searchString: TMP:2 m