问题
I am getting this error:
AttributeError: 'Dataset' object has no attribute 'metpy'
when I am running my code. The particular lines in particular are:
import Scientific.IO.NetCDF as S
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import xarray as xr
import metpy
# Any import of metpy will activate the accessors
import metpy.calc as mpcalc
#from metpy.testing import get_test_data
from metpy.units import units
# Open the netCDF file as a xarray Datase
#
datadir='C:/Users/stratusshow/AppData/Local/lxss/home/stratus/PROJECT/NEWPROJECT/FEB012017/nam_218_20170131_1200_000.nc'
data = xr.open_dataset(datadir,decode_cf=True)
# To parse the full dataset, we can call parse_cf without an argument, and assign the returned
# Dataset.
data = data.metpy.parse_cf()
# If we instead want just a single variable, we can pass that variable name to parse_cf and
# it will return just that data variable as a DataArray.
data_var = data.metpy.parse_cf('Temperature_isobaric')
The line in bold is where the attribute error is popping up on. I am trying to learn how to use netcdf in metpy for some independent research as well as to learn metpy in general for future projects.
回答1:
The issue here is an out-of-date MetPy version. The Xarray accessor was added in v0.8. To update to the current version of MetPy with the latest functionality, you can upgrade with conda update metpy
or pip install --upgrade metpy
.
来源:https://stackoverflow.com/questions/58045079/how-to-fix-attribute-error-for-metpy-data-from-a-netcdf-file-involving-xarray