Skip to content

Commit

Permalink
Merge pull request #14 from dopplershift/fix-difax
Browse files Browse the repository at this point in the history
Update Upper Air Obs (DIFAX) example
  • Loading branch information
dcamron authored Jun 17, 2024
2 parents 02d6a93 + 68140a5 commit b0067ee
Showing 1 changed file with 2 additions and 116 deletions.
118 changes: 2 additions & 116 deletions notebooks/synoptic/Upperair_Obs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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)"
]
},
{
Expand Down

0 comments on commit b0067ee

Please sign in to comment.