Good morning, I am working through one of the MetPy Monday videos and am stuck. I was hoping that you might be able to point me in the right direction. The video I was working through is at https://www.youtube.com/watch?v=r9oMB0ZhWSk.
My code is below. I’ve commented out what doesn’t seem to work and I’m fairly certain I typed things as they were in the video.
from metpy.cbook import get_test_data
import xarray as xr
narr = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))
from metpy.plots.declarative import *
from metpy.units import units
import metpy.calc as mpcalc
smoothed = mpcalc.smooth_n_point(narr['Temperature'].sel(isobaric=850).isel(time=0), 5, 3)
#narr['Temperature_5pt_3pass'] = (['y','x'],smoothed)
#narr['Temperature_5pt_3pass'].attrs = narr['Temperature'].attrs
contour_raw = ContourPlot()
contour_raw.data = narr
contour_raw.field = 'Temperature'
contour_raw.level = 850 * units.hPa
contour_raw.linecolor= 'tab:red'
contour_raw.linewidth = 1
contour_raw.linestyle = '--'
contour_raw.contours = 15
#contour_smooth = ContourPlot()
#contour_smooth.data=narr
#contour_smooth.field = 'Temperature_5pt_3pass'
#contour_smooth.linecolor = 'tab:blue'
#contour_smooth.linewidth = 1
#contour_smooth.linestyle = '--'
#contour_smooth.contours = 15
panel = MapPanel()
panel.area = 'co'
panel.layers = ['coastline','borders', 'states']
panel.title = 'Smoothing of 850hPa Temperatures'
panel.plots = [contour_raw]
pc = PanelContainer()
pc.size = (10,8)
pc.panels = [panel]
pc.show()
The error I get is
PS C:\Users\User\Dropbox\Python> & 'c:\Users\User\AppData\Local\Programs\Python\Python312\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '60022' '--' 'c:\Users\User\Desktop\Test\test.py'
PS C:\Users\User\Dropbox\Python> ^C
PS C:\Users\User\Dropbox\Python>
PS C:\Users\User\Dropbox\Python> c:; cd 'c:\Users\User\Dropbox\Python'; & 'c:\Users\User\AppData\Local\Programs\Python\Python312\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '60062' '--' 'c:\Users\User\Desktop\Test\test.py'
PS C:\Users\User\Dropbox\Python> ^C
PS C:\Users\User\Dropbox\Python>
PS C:\Users\User\Dropbox\Python> c:; cd 'c:\Users\User\Dropbox\Python'; & 'c:\Users\User\AppData\Local\Programs\Python\Python312\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '60086' '--' 'c:\Users\User\Desktop\Test\test.py'
Traceback (most recent call last):
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 1477, in _construct_dataarray
variable = self._variables[name]
~~~~~~~~~~~~~~~^^^^^^
KeyError: 'Temperature_5pt_3pass'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 1576, in __getitem__
return self._construct_dataarray(key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 1479, in _construct_dataarray
_, name, variable = _get_virtual_variable(self._variables, name, self.sizes)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 213, in _get_virtual_variable
raise KeyError(key)
KeyError: 'Temperature_5pt_3pass'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\User\Desktop\Test\test.py", line 43, in <module>
pc.show()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 200, in show
self.draw()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 187, in draw
panel.draw()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 511, in draw
p.draw()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 891, in draw
self._build()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 1062, in _build
x_like, y_like, imdata = self.plotdata
^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 875, in plotdata
plot_x_dim = self.griddata.metpy.find_axis_number('x')
^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 819, in griddata
data = self.data.metpy.parse_cf(self.field)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\xarray.py", line 817, in parse_cf
var = self._dataset[varname]
~~~~~~~~~~~~~^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 1578, in __getitem__
raise KeyError(
KeyError: "No variable named 'Temperature_5pt_3pass'. Variables on the dataset include ['Temperature', 'time', 'isobaric', 'y', 'x', ..., 'lon', 'u_wind', 'v_wind', 'Geopotential_height', 'Specific_humidity']"
PS C:\Users\User\Dropbox\Python>
Any help is appreciated. Thanks!
David Moran, CCM
Weather Risk Analyst
DTN
Phone: +1.405.693.5632
Greetings!
When I uncomment those lines and run with the current version of xarray (2024.7.0), I get the following error:
>
TypeError: Variable 'Temperature_5pt_3pass': Using a DataArray object to construct a variable is ambiguous, please extract the data using the .data property.
That more helpful error leads me to add a `.data` property to the line storing the data in `narr`:
narr['Temperature_5pt_3pass'] = (['y', 'x'], smoothed.data)
After making this change, everything runs fine for me. Does this solve it for you?
Cheers,
Ryan
Good morning, I am working through one of the MetPy Monday videos and am stuck. I was hoping that you might be able to point me in the right direction. The video I was working through is at https://www.youtube.com/watch?v=r9oMB0ZhWSk.
My code is below. I’ve commented out what doesn’t seem to work and I’m fairly certain I typed things as they were in the video.
from metpy.cbook import get_test_data
import xarray as xr
narr = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))
from metpy.plots.declarative import *
from metpy.units import units
import metpy.calc as mpcalc
smoothed = mpcalc.smooth_n_point(narr['Temperature'].sel(isobaric=850).isel(time=0), 5, 3)
#narr['Temperature_5pt_3pass'] = (['y','x'],smoothed)
#narr['Temperature_5pt_3pass'].attrs = narr['Temperature'].attrs
contour_raw = ContourPlot()
contour_raw.data = narr
contour_raw.field = 'Temperature'
contour_raw.level = 850 * units.hPa
contour_raw.linecolor= 'tab:red'
contour_raw.linewidth = 1
contour_raw.linestyle = '--'
contour_raw.contours = 15
#contour_smooth = ContourPlot()
#contour_smooth.data=narr
#contour_smooth.field = 'Temperature_5pt_3pass'
#contour_smooth.linecolor = 'tab:blue'
#contour_smooth.linewidth = 1
#contour_smooth.linestyle = '--'
#contour_smooth.contours = 15
panel = MapPanel()
panel.area = 'co'
panel.layers = ['coastline','borders', 'states']
panel.title = 'Smoothing of 850hPa Temperatures'
panel.plots = [contour_raw]
pc = PanelContainer()
pc.size = (10,8)
pc.panels = [panel]
pc.show()
The error I get is
PS C:\Users\User\Dropbox\Python> & 'c:\Users\User\AppData\Local\Programs\Python\Python312\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '60022' '--' 'c:\Users\User\Desktop\Test\test.py'
PS C:\Users\User\Dropbox\Python> ^C
PS C:\Users\User\Dropbox\Python>
PS C:\Users\User\Dropbox\Python> c:; cd 'c:\Users\User\Dropbox\Python'; & 'c:\Users\User\AppData\Local\Programs\Python\Python312\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '60062' '--' 'c:\Users\User\Desktop\Test\test.py'
PS C:\Users\User\Dropbox\Python> ^C
PS C:\Users\User\Dropbox\Python>
PS C:\Users\User\Dropbox\Python> c:; cd 'c:\Users\User\Dropbox\Python'; & 'c:\Users\User\AppData\Local\Programs\Python\Python312\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.debugpy-2024.10.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '60086' '--' 'c:\Users\User\Desktop\Test\test.py'
Traceback (most recent call last):
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 1477, in _construct_dataarray
variable = self._variables[name]
~~~~~~~~~~~~~~~^^^^^^
KeyError: 'Temperature_5pt_3pass'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 1576, in __getitem__
return self._construct_dataarray(key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 1479, in _construct_dataarray
_, name, variable = _get_virtual_variable(self._variables, name, self.sizes)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 213, in _get_virtual_variable
raise KeyError(key)
KeyError: 'Temperature_5pt_3pass'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\User\Desktop\Test\test.py", line 43, in <module>
pc.show()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 200, in show
self.draw()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 187, in draw
panel.draw()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 511, in draw
p.draw()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 891, in draw
self._build()
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 1062, in _build
x_like, y_like, imdata = self.plotdata
^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 875, in plotdata
plot_x_dim = self.griddata.metpy.find_axis_number('x')
^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\plots\declarative.py", line 819, in griddata
data = self.data.metpy.parse_cf(self.field)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\metpy\xarray.py", line 817, in parse_cf
var = self._dataset[varname]
~~~~~~~~~~~~~^^^^^^^^^
File "c:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\xarray\core\dataset.py", line 1578, in __getitem__
raise KeyError(
KeyError: "No variable named 'Temperature_5pt_3pass'. Variables on the dataset include ['Temperature', 'time', 'isobaric', 'y', 'x', ..., 'lon', 'u_wind', 'v_wind', 'Geopotential_height', 'Specific_humidity']"
PS C:\Users\User\Dropbox\Python>
Thanks, Ryan! That did it!
David
From: NSF Unidata MetPy, Siphon (Python) <support-python@unidata.ucar.edu>
Sent: Wednesday, September 4, 2024 5:03 PM
To: David Moran <David.Moran@dtn.com>
Cc: archive-python@unidata.ucar.edu
Subject: RE: (#CQC-6179022) MetPy Question
You don't often get email from support-python@unidata.ucar.edu. Learn why this is important |
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.