Skip to content

Instantly share code, notes, and snippets.

@sgdecker
Created August 12, 2021 14:57
Show Gist options
  • Save sgdecker/8c90f2300da03de296e1d934b47e1cd1 to your computer and use it in GitHub Desktop.
Save sgdecker/8c90f2300da03de296e1d934b47e1cd1 to your computer and use it in GitHub Desktop.
Problem with interactive use of declarative syntax
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "12bd2b79-5072-440a-ab46-21d84404b5a0",
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime, timedelta\n",
"from metpy.io import metar\n",
"from metpy.plots.declarative import PlotObs, MapPanel, PanelContainer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97173674-17d8-44cb-8bba-edc1b14fd260",
"metadata": {},
"outputs": [],
"source": [
"def top_of_hr():\n",
" now = datetime.utcnow()\n",
" ymd = now.strftime('%y%m%d')\n",
" hr = now.strftime('%H')\n",
" dt_top = now.replace(microsecond=0, second=0, minute=0)\n",
" return ymd, hr, dt_top\n",
"\n",
"ymd, hr, plot_time = top_of_hr()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2394a54d-a475-4ae9-90bf-ce23de9fddb9",
"metadata": {},
"outputs": [],
"source": [
"# Replace with appropriate path on your system\n",
"sfc_file = '/nfs/wxdata1/ldmdata/weather/sfc/' + ymd + hr + '_sao.txt'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "103ef608-e4f9-4984-9d11-23754e175e67",
"metadata": {},
"outputs": [],
"source": [
"sfc_obs = metar.parse_metar_file(sfc_file)\n",
"\n",
"obs = PlotObs()\n",
"obs.data = sfc_obs\n",
"obs.time = plot_time\n",
"obs.time_window = timedelta(minutes=15)\n",
"obs.level = None\n",
"obs.fields = ['cloud_coverage', 'air_temperature', 'dew_point_temperature',\n",
" 'air_pressure_at_sea_level', 'current_wx1_symbol']\n",
"obs.plot_units = [None, 'degF', 'degF', None, None]\n",
"obs.locations = ['C', 'NW', 'SW', 'NE', 'W']\n",
"obs.formats = ['sky_cover', None, None, lambda p: format(p * 10, '.0f')[-3:],\n",
" 'current_weather']\n",
"obs.reduce_points = 2\n",
"obs.vector_field = ['eastward_wind', 'northward_wind']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "037585c0-bfbf-4c58-bb6e-61d656c1df96",
"metadata": {},
"outputs": [],
"source": [
"panel = MapPanel()\n",
"panel.layout = (1, 1, 1)\n",
"panel.projection = 'lcc'\n",
"panel.area = 'us'\n",
"panel.layers = ['countries','states']\n",
"#panel.layers = ['borders','states']\n",
"panel.title = f'Surface plot for {plot_time}'\n",
"panel.plots = [obs]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f89d215-18c3-44c5-bae4-216df4dc00fb",
"metadata": {},
"outputs": [],
"source": [
"pc = PanelContainer()\n",
"pc.size = (10, 10)\n",
"pc.panels = [panel]\n",
"pc.show()"
]
},
{
"cell_type": "markdown",
"id": "214da4a4-75ce-458e-8fcc-ddfafd45ba77",
"metadata": {},
"source": [
"Running this code as is generates:\n",
"```\n",
"AttributeError: module 'metpy.plots.cartopy_utils' has no attribute 'COUNTRIES'\n",
"```\n",
"\n",
"If I then fix the code by commenting out the bad definition of `panel.layers` in cell 5 and uncommenting out the good definition, and rerun cell 5, I get this error:\n",
"```\n",
"AttributeError: 'NoneType' object has no attribute 'figure'\n",
"```\n",
"\n",
"However, if I \"Restart Kernel and Run All Cells\", the fixed code works as it should."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment