From 68140a596bc15660ec107b7ebd0e996e8a0ff421 Mon Sep 17 00:00:00 2001 From: Ryan May Date: Mon, 17 Jun 2024 14:05:46 -0600 Subject: [PATCH] Update Upper Air Obs (DIFAX) example This was failing due to a redirect in its custom station data information retrieval. Update to use the station data information available directly from MetPy. --- notebooks/synoptic/Upperair_Obs.ipynb | 118 +------------------------- 1 file changed, 2 insertions(+), 116 deletions(-) diff --git a/notebooks/synoptic/Upperair_Obs.ipynb b/notebooks/synoptic/Upperair_Obs.ipynb index b7147a6..77421ac 100644 --- a/notebooks/synoptic/Upperair_Obs.ipynb +++ b/notebooks/synoptic/Upperair_Obs.ipynb @@ -38,6 +38,7 @@ "import numpy as np\n", "import xarray as xr\n", "\n", + "from metpy.io import add_station_lat_lon\n", "from metpy.plots import StationPlot\n", "from metpy.units import units\n", "from siphon.simplewebservice.iastate import IAStateUpperAir" @@ -123,119 +124,6 @@ " marker='x', color='black', transform=transform)" ] }, - { - "cell_type": "markdown", - "metadata": { - "cell_marker": "######################################################################" - }, - "source": [ - "Station Information\n", - "-------------------\n", - "\n", - "A helper function for obtaining radiosonde station information (e.g.,\n", - "latitude/longitude) requried to plot data obtained from each station.\n", - "Original code by github user sgdecker." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "lines_to_next_cell": 1 - }, - "outputs": [], - "source": [ - "def station_info(stid):\n", - " r\"\"\"Provide information about weather stations.\n", - "\n", - " Parameters\n", - " ----------\n", - " stid: str or iterable object containing strs\n", - " The ICAO or IATA code(s) for which station information is requested.\n", - " with_units: bool\n", - " Whether to include units for values that have them. Default True.\n", - "\n", - " Returns\n", - " -------\n", - " info: dict\n", - " Information about the station(s) within a dictionary with these keys:\n", - " 'state': Two-character ID of the state/province where the station is located,\n", - " if applicable\n", - " 'name': The name of the station\n", - " 'lat': The latitude of the station [deg]\n", - " 'lon': The longitude of the station [deg]\n", - " 'elevation': The elevation of the station [m]\n", - " 'country': Two-character ID of the country where the station is located\n", - "\n", - " Modified code from Steven Decker, Rutgers University\n", - "\n", - " \"\"\"\n", - " # Provide a helper function for later usage\n", - " def str2latlon(s):\n", - " deg = float(s[:3])\n", - " mn = float(s[-3:-1])\n", - " if s[-1] == 'S' or s[-1] == 'W':\n", - " deg = -deg\n", - " mn = -mn\n", - " return deg + mn / 60.\n", - "\n", - " # Various constants describing the underlying data\n", - " url = 'https://www.aviationweather.gov/docs/metar/stations.txt'\n", - " # file = 'stations.txt'\n", - " state_bnds = slice(0, 2)\n", - " name_bnds = slice(3, 19)\n", - " icao_bnds = slice(20, 24)\n", - " iata_bnds = slice(26, 29)\n", - " lat_bnds = slice(39, 45)\n", - " lon_bnds = slice(47, 54)\n", - " z_bnds = slice(55, 59)\n", - " cntry_bnds = slice(81, 83)\n", - "\n", - " # Generalize to any number of IDs\n", - " if isinstance(stid, str):\n", - " stid = [stid]\n", - "\n", - " # Get the station dataset\n", - " infile = urllib.request.urlopen(url)\n", - " data = infile.readlines()\n", - "\n", - " state = []\n", - " name = []\n", - " lat = []\n", - " lon = []\n", - " z = []\n", - " cntry = []\n", - "\n", - " for s in stid:\n", - " s = s.upper()\n", - " for line_bytes in data:\n", - " line = line_bytes.decode('UTF-8')\n", - " icao = line[icao_bnds]\n", - " iata = line[iata_bnds]\n", - " if len(s) == 3 and s in iata or len(s) == 4 and s in icao:\n", - " state.append(line[state_bnds].strip())\n", - " name.append(line[name_bnds].strip())\n", - " lat.append(str2latlon(line[lat_bnds]))\n", - " lon.append(str2latlon(line[lon_bnds]))\n", - " z.append(float(line[z_bnds]))\n", - " cntry.append(line[cntry_bnds])\n", - "\n", - " break\n", - " else:\n", - " state.append('NA')\n", - " name.append('NA')\n", - " lat.append(np.nan)\n", - " lon.append(np.nan)\n", - " z.append(np.nan)\n", - " cntry.append('NA')\n", - "\n", - " infile.close()\n", - "\n", - " return {'state': np.array(state), 'name': np.array(name), 'lat': np.array(lat),\n", - " 'lon': np.array(lon), 'elevation': np.array(z), 'country': np.array(cntry),\n", - " 'units': {'lat': 'deg', 'lon': 'deg', 'z': 'm'}}" - ] - }, { "cell_type": "markdown", "metadata": { @@ -312,9 +200,7 @@ "df = data[data_subset]\n", "\n", "# Get station lat/lon from look-up file; add to Dataframe\n", - "stn_info = station_info(list(df.station.values))\n", - "df.insert(10, 'latitude', stn_info['lat'])\n", - "df.insert(11, 'longitude', stn_info['lon'])" + "df = add_station_lat_lon(df)" ] }, {