|
38 | 38 | "import numpy as np\n",
|
39 | 39 | "import xarray as xr\n",
|
40 | 40 | "\n",
|
| 41 | + "from metpy.io import add_station_lat_lon\n", |
41 | 42 | "from metpy.plots import StationPlot\n",
|
42 | 43 | "from metpy.units import units\n",
|
43 | 44 | "from siphon.simplewebservice.iastate import IAStateUpperAir"
|
|
123 | 124 | " marker='x', color='black', transform=transform)"
|
124 | 125 | ]
|
125 | 126 | },
|
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 |
| - }, |
239 | 127 | {
|
240 | 128 | "cell_type": "markdown",
|
241 | 129 | "metadata": {
|
|
312 | 200 | "df = data[data_subset]\n",
|
313 | 201 | "\n",
|
314 | 202 | "# 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)" |
318 | 204 | ]
|
319 | 205 | },
|
320 | 206 | {
|
|
0 commit comments