截面分析问题

Cross Section Analysis Issue

我正尝试按照 Metpy 网站上的示例代码对网格数据集执行横截面分析。

首先,我可以使用 xarray 读取原始的 grib2 数据集,也可以在将其转换为 netCDF 格式后读取。

我成功导入了所有需要的包:

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

import metpy.calc as mpcalc
from metpy.interpolate import cross_section

然后,我确保可以使用以下命令读取文件:

data = xr.open_dataset('/home/rik/hrrr/gtgn/gtgn.t2215z.edr.f000.nc', False)
print(data)
<xarray.Dataset>
Dimensions:            (alt: 52, time: 1, x: 451, y: 337)
Coordinates:
  * time               (time) datetime64[ns] 2020-06-18T22:15:00
  * x                  (x) float64 0.0 1.355e+04 ... 6.082e+06 6.095e+06
  * y                  (y) float64 0.0 1.355e+04 ... 4.538e+06 4.551e+06
  * alt                (alt) float64 0.0 30.0 304.0 ... 1.494e+04 1.524e+04
Data variables:
    Lambert_Conformal  int32 ...
    param30.19.0       (time, alt, y, x) float32 ...
Attributes:
    CDI:          Climate Data Interface version 1.9.8 (https://mpimet.mpg.de...
    Conventions:  CF-1.6
    history:      Wed Oct 07 17:19:41 2020: cdo -f nc copy gtgn.t2215z.edr.f0...
    institution:  National Center for Atmospheric Research
    CDO:          Climate Data Operators version 1.9.8 (https://mpimet.mpg.de...

添加 MetPy 的 CF 解析命令给我以下 ValueError:

data = xr.open_dataset('/home/rik/hrrr/gtgn/gtgn.t2215z.edr.f000.nc')
data = data.metpy.parse_cf().squeeze()
print(data)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-fa63435fbf07> in <module>
      1 data = xr.open_dataset('/home/rik/hrrr/gtgn/gtgn.t2215z.edr.f000.nc')
----> 2 data = data.metpy.parse_cf().squeeze()
      3 print(data)

~/miniconda3/envs/metpy/lib/python3.7/site-packages/metpy/xarray.py in parse_cf(self, varname, coordinates)
    510         """
    511         from .cbook import iterable
--> 512         from .plots.mapping import CFProjection
    513 
    514         if varname is None:

~/miniconda3/envs/metpy/lib/python3.7/site-packages/metpy/plots/__init__.py in <module>
     25                 convert_gempak_color])  # pylint: disable=undefined-variable
     26 try:
---> 27     from .cartopy_utils import USCOUNTIES, USSTATES
     28     __all__.extend([USCOUNTIES, USSTATES])
     29 except ImportError:

~/miniconda3/envs/metpy/lib/python3.7/site-packages/metpy/plots/cartopy_utils.py in <module>
     41 
     42 
---> 43 USCOUNTIES = MetPyMapFeature('us_counties', '20m', facecolor='None', edgecolor='black')
     44 
     45 USSTATES = MetPyMapFeature('us_states', '20m', facecolor='None', edgecolor='black')

~/miniconda3/envs/metpy/lib/python3.7/site-packages/metpy/plots/cartopy_utils.py in __init__(self, name, scale, **kwargs)
     14     def __init__(self, name, scale, **kwargs):
     15         """Create USCountiesFeature instance."""
---> 16         super().__init__('', name, scale, **kwargs)
     17 
     18     def geometries(self):

~/miniconda3/envs/metpy/lib/python3.7/site-packages/cartopy/feature/__init__.py in __init__(self, category, name, scale, **kwargs)
    262         self.scaler = scale
    263         # Make sure this is a valid resolution
--> 264         self._validate_scale()
    265 
    266     @property

~/miniconda3/envs/metpy/lib/python3.7/site-packages/cartopy/feature/__init__.py in _validate_scale(self)
    272             raise ValueError(
    273                 '{} is not a valid Natural Earth scale. '.format(self.scale) +
--> 274                 'Valid scales are "110m", "50m", and "10m".'
    275             )
    276 

ValueError: 20m is not a valid Natural Earth scale. Valid scales are "110m", "50m", and "10m".

我正在使用 Metpy v0.12.1 和 xarray v0.15.1

而且,有趣的是,当我重新 运行 相同的代码时,我收到了另一条错误消息: NameError: name 'ctables' is not defined

请指教解析数据时出了什么问题。

get_test_data() 仅适用于使用 MetPy 的 built-in 测试数据集之一。使用您自己的数据时,您应该省略调用此函数,因此使用 open_dataset 的代码应如下所示:

data = xr.open_dataset('/home/rik/hrrr/gtgn/gtgn.t2215z.edr.f000.nc')