【问题标题】:Python3.8: MetPy issue: declarative plotting of grib2 file plotting blank mapPython3.8:MetPy 问题:grib2 文件的声明性绘图绘制空白地图
【发布时间】:2021-06-15 12:14:23
【问题描述】:

自学 MetPy 中的声明式绘图功能,并不断遇到困难。这来自于在通过 xarray/cfgrib 打开后尝试将声明性绘图包应用于 grib2 文件。数据看起来健康,但实际温度数据似乎没有被传递和绘制。当我绘制它时,我得到一张空白地图。

我有下面的压缩代码,以及一些数据检查的打印输出,以表明数据似乎正常。

我错过了什么吗? (我确定我是,但我想知道什么?)

谢谢!

import matplotlib.pyplot as plt
import xarray as xr
import numpy as np
import pygrib
import cartopy.crs as ccrs
import cartopy.feature as cfeat
import cartopy
from datetime import datetime, timedelta
import io
from metpy.units import units
from metpy.plots import ImagePlot, MapPanel, PanelContainer

##Open GFS grib2 file, initialized 2021060700, and pull data from hPa level designation.
ds = xr.open_dataset('/fewxops/Tom/learn_python/data/grib2/gfs.t00z.pgrb2.0p25.f024', engine='cfgrib', filter_by_keys={'typeOfLevel': 'isobaricInhPa'})

##Select individual level (from designated 'type of level')
ds_z500=ds.sel(isobaricInhPa=500)

##Create base image via MetPy
img = ImagePlot()
img.data = ds_z500
img.field = 't'
img.colormap = 'plasma'

#Create map panel (e.g. subplot in matplotlib)
panel = MapPanel()
panel.area = 'us'
#panel.layers = ['states']
panel.title = 'GFS 500mb Temp Forecast Example'
panel.plots = [img]

##Create panel container (e.g. figure in matplotlib)
pc = PanelContainer()
pc.size = (10,8)
pc.panels = [panel]
pc.show()

*******************************
home/fewx/anaconda3/lib/python3.8/site-packages/metpy/xarray.py:349: UserWarning: More than one time coordinate present for variable "t".
  warnings.warn('More than one ' + axis + ' coordinate present for variable'
Found valid latitude/longitude coordinates, assuming latitude_longitude for projection grid_mapping variable
print(ds_z500)
<xarray.Dataset>
Dimensions:        (latitude: 721, longitude: 1440)
Coordinates:
    time           datetime64[ns] ...
    step           timedelta64[ns] ...
    isobaricInhPa  float64 500.0
  * latitude       (latitude) float64 90.0 89.75 89.5 ... -89.5 -89.75 -90.0
  * longitude      (longitude) float64 0.0 0.25 0.5 0.75 ... 359.2 359.5 359.8
    valid_time     datetime64[ns] ...
Data variables:
    gh             (latitude, longitude) float32 ...
    t              (latitude, longitude) float32 ...
    r              (latitude, longitude) float32 ...
    q              (latitude, longitude) float32 ...
    w              (latitude, longitude) float32 ...
    wz             (latitude, longitude) float32 ...
    u              (latitude, longitude) float32 ...
    v              (latitude, longitude) float32 ...
    absv           (latitude, longitude) float32 ...
    o3mr           (latitude, longitude) float32 ...
Attributes:
    GRIB_edition:            2
    GRIB_centre:             kwbc
    GRIB_centreDescription:  US National Weather Service - NCEP
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             US National Weather Service - NCEP
    history:                 2021-06-15T08:05 GRIB to CDM+CF via cfgrib-0.9.9...


print(ds_z500['t'].values)
[[245.93057 245.93057 245.93057 ... 245.93057 245.93057 245.93057]
 [245.85057 245.84056 245.84056 ... 245.85057 245.85057 245.85057]
 [245.72057 245.72057 245.71057 ... 245.73056 245.73056 245.72057]
 ...
 [226.94057 226.94057 226.94057 ... 226.94057 226.94057 226.94057]
 [226.96057 226.96057 226.96057 ... 226.96057 226.96057 226.96057]
 [226.88057 226.88057 226.88057 ... 226.88057 226.88057 226.88057]]

GFS Example

【问题讨论】:

    标签: python metpy


    【解决方案1】:

    您没有遗漏任何内容,您只是在 ImagePlot 中的某些代码中遇到了限制,该代码试图帮助 CartoPy ——特别是关于使用 imshow 和纬度/经度数据进行绘图。您的示例代码应该可以正常工作,并且适用于ContourPlot。我已打开 an issue 以进一步调查如何在下一个版本中解决此问题。

    同时,一种解决方法是在绘图之前对数据进行子集化,以消除 MetPy 有问题的包装“帮助”(大约 -180/+180 和 0/360 经度):

    # The order in these slices needs to match that of the data
    ds_z500 = ds_z500.metpy.sel(longitude=slice(250, 315), latitude=slice(60, 10))
    

    【讨论】:

    • 太棒了,谢谢!我可以确认 lat/lon 切片确实解决了这个问题(并且是一个有助于简洁的好技巧)。另一方面,我注意到 MetPy 的声明式绘图比简单的 matplotlib/cartopy contourf 更快地绘制数据,这可能会很麻烦,并且在复杂的绘图上花费大量时间(不是很好)。我是在想象那个(?),还是 MetPy not 没有为它的图像/填充绘图使用轮廓(我只是盲目地假设它有)?只是好奇!
    • 我没想到 MetPy 的 ContourPlot 会更快,因为它只是直接使用 CartoPy。我的第一个猜测是,其他一些步骤/转换的使用以某种方式走上了幸福的道路。这很有趣。
    • 另外,如果它解决了您的问题,请不要忘记接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2021-09-18
    • 2022-01-25
    相关资源
    最近更新 更多