Skip to content

Commit b0067ee

Browse files
authored
Merge pull request #14 from dopplershift/fix-difax
Update Upper Air Obs (DIFAX) example
2 parents 02d6a93 + 68140a5 commit b0067ee

File tree

1 file changed

+2
-116
lines changed

1 file changed

+2
-116
lines changed

notebooks/synoptic/Upperair_Obs.ipynb

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"import numpy as np\n",
3939
"import xarray as xr\n",
4040
"\n",
41+
"from metpy.io import add_station_lat_lon\n",
4142
"from metpy.plots import StationPlot\n",
4243
"from metpy.units import units\n",
4344
"from siphon.simplewebservice.iastate import IAStateUpperAir"
@@ -123,119 +124,6 @@
123124
" marker='x', color='black', transform=transform)"
124125
]
125126
},
126-
{
127-
"cell_type": "markdown",
128-
"metadata": {
129-
"cell_marker": "######################################################################"
130-
},
131-
"source": [
132-
"Station Information\n",
133-
"-------------------\n",
134-
"\n",
135-
"A helper function for obtaining radiosonde station information (e.g.,\n",
136-
"latitude/longitude) requried to plot data obtained from each station.\n",
137-
"Original code by github user sgdecker."
138-
]
139-
},
140-
{
141-
"cell_type": "code",
142-
"execution_count": null,
143-
"metadata": {
144-
"lines_to_next_cell": 1
145-
},
146-
"outputs": [],
147-
"source": [
148-
"def station_info(stid):\n",
149-
" r\"\"\"Provide information about weather stations.\n",
150-
"\n",
151-
" Parameters\n",
152-
" ----------\n",
153-
" stid: str or iterable object containing strs\n",
154-
" The ICAO or IATA code(s) for which station information is requested.\n",
155-
" with_units: bool\n",
156-
" Whether to include units for values that have them. Default True.\n",
157-
"\n",
158-
" Returns\n",
159-
" -------\n",
160-
" info: dict\n",
161-
" Information about the station(s) within a dictionary with these keys:\n",
162-
" 'state': Two-character ID of the state/province where the station is located,\n",
163-
" if applicable\n",
164-
" 'name': The name of the station\n",
165-
" 'lat': The latitude of the station [deg]\n",
166-
" 'lon': The longitude of the station [deg]\n",
167-
" 'elevation': The elevation of the station [m]\n",
168-
" 'country': Two-character ID of the country where the station is located\n",
169-
"\n",
170-
" Modified code from Steven Decker, Rutgers University\n",
171-
"\n",
172-
" \"\"\"\n",
173-
" # Provide a helper function for later usage\n",
174-
" def str2latlon(s):\n",
175-
" deg = float(s[:3])\n",
176-
" mn = float(s[-3:-1])\n",
177-
" if s[-1] == 'S' or s[-1] == 'W':\n",
178-
" deg = -deg\n",
179-
" mn = -mn\n",
180-
" return deg + mn / 60.\n",
181-
"\n",
182-
" # Various constants describing the underlying data\n",
183-
" url = 'https://www.aviationweather.gov/docs/metar/stations.txt'\n",
184-
" # file = 'stations.txt'\n",
185-
" state_bnds = slice(0, 2)\n",
186-
" name_bnds = slice(3, 19)\n",
187-
" icao_bnds = slice(20, 24)\n",
188-
" iata_bnds = slice(26, 29)\n",
189-
" lat_bnds = slice(39, 45)\n",
190-
" lon_bnds = slice(47, 54)\n",
191-
" z_bnds = slice(55, 59)\n",
192-
" cntry_bnds = slice(81, 83)\n",
193-
"\n",
194-
" # Generalize to any number of IDs\n",
195-
" if isinstance(stid, str):\n",
196-
" stid = [stid]\n",
197-
"\n",
198-
" # Get the station dataset\n",
199-
" infile = urllib.request.urlopen(url)\n",
200-
" data = infile.readlines()\n",
201-
"\n",
202-
" state = []\n",
203-
" name = []\n",
204-
" lat = []\n",
205-
" lon = []\n",
206-
" z = []\n",
207-
" cntry = []\n",
208-
"\n",
209-
" for s in stid:\n",
210-
" s = s.upper()\n",
211-
" for line_bytes in data:\n",
212-
" line = line_bytes.decode('UTF-8')\n",
213-
" icao = line[icao_bnds]\n",
214-
" iata = line[iata_bnds]\n",
215-
" if len(s) == 3 and s in iata or len(s) == 4 and s in icao:\n",
216-
" state.append(line[state_bnds].strip())\n",
217-
" name.append(line[name_bnds].strip())\n",
218-
" lat.append(str2latlon(line[lat_bnds]))\n",
219-
" lon.append(str2latlon(line[lon_bnds]))\n",
220-
" z.append(float(line[z_bnds]))\n",
221-
" cntry.append(line[cntry_bnds])\n",
222-
"\n",
223-
" break\n",
224-
" else:\n",
225-
" state.append('NA')\n",
226-
" name.append('NA')\n",
227-
" lat.append(np.nan)\n",
228-
" lon.append(np.nan)\n",
229-
" z.append(np.nan)\n",
230-
" cntry.append('NA')\n",
231-
"\n",
232-
" infile.close()\n",
233-
"\n",
234-
" return {'state': np.array(state), 'name': np.array(name), 'lat': np.array(lat),\n",
235-
" 'lon': np.array(lon), 'elevation': np.array(z), 'country': np.array(cntry),\n",
236-
" 'units': {'lat': 'deg', 'lon': 'deg', 'z': 'm'}}"
237-
]
238-
},
239127
{
240128
"cell_type": "markdown",
241129
"metadata": {
@@ -312,9 +200,7 @@
312200
"df = data[data_subset]\n",
313201
"\n",
314202
"# Get station lat/lon from look-up file; add to Dataframe\n",
315-
"stn_info = station_info(list(df.station.values))\n",
316-
"df.insert(10, 'latitude', stn_info['lat'])\n",
317-
"df.insert(11, 'longitude', stn_info['lon'])"
203+
"df = add_station_lat_lon(df)"
318204
]
319205
},
320206
{

0 commit comments

Comments
 (0)