python-xarray - 无法将 y 和 x 坐标分配给 xarray.DataArray

标签 python-xarray metpy

完成 MetPy 横截面示例后,我尝试将该示例推广到 NCEP NAM-12km GRIB2 文件,但未成功。通过将我的文件的 DataArray 与示例文件(netCDF 文件)进行比较,我发现 xarray.open_dataset() 没有生成 x 和 y 坐标。它也没有在输入 GRIB 文件中找到足够的信息来生成 metpy_crs 坐标。我尝试将 GRIB 文件转换为 netCDF,但这并没有解决问题。

即使将输入文件视为非 CF 投诉,我发现我无法分配 x 和 y 坐标。通过检查横截面示例中使用的 netCDF 文件,可以清楚地看到 x 和 y 数组表示具有 GeoX 和 GeoY 坐标轴类型的空间网格。然而,我不清楚的是如何为我的文件创建一个类似的数组。

纬度和经度数组不适合手动折叠它们的尝试(如沿行选择纬度的模式,这是我创建适当坐标向量的尝试之一。)

如果您能协助解决此问题,我们将不胜感激。谢谢!

以下是我正在运行的代码。

import sys
#  pygrib is installed to the "herbie" environment, so we add the path
#  to this so we can load those packages.  Pygrib is used to get the 
#  complete set of keys for a message so we can try to see what is
#  available.

sys.path.append('/miniconda3/envs/herbie/lib/python3.8/site-packages')
import pygrib

%matplotlib inline

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.cbook import get_test_data
from metpy.interpolate import cross_section

# NAM 218 AWIPS Grid - CONUS 
# (12-km Resolution; full complement of pressure level fields and some surface-based fields)
# https://nomads.ncep.noaa.gov/pub/data/nccf/com/nam/prod/nam.20210925/

filename = '/NAM/nam.t00z.awphys00.tm00.grib2'

#   NCEP GRIB files have non-unique keys, so pick from recommended list to filter
#    --Temperature and Pressure levels
#   Non-CF Compliant so don't use .parse_cf()

data = xr.open_dataset(filename, engine="cfgrib",filter_by_keys={'typeOfLevel': 'isobaricInhPa','name':'Temperature'})

#  We are missing a mapping projection, so add it:
data1=data.metpy.assign_crs(
    grid_mapping_name='lambert_conformal_conic',
    latitude_of_projection_origin=35,
    longitude_of_central_meridian=-100,
    standard_parallel=(30,60),
    earth_radius=6371229.0)

print(data1)

#--------------------------  Print Output  ---------------------------------
#<xarray.Dataset>
#Dimensions:        (isobaricInhPa: 39, y: 428, x: 614)
#Coordinates:
#    time           datetime64[ns] 2021-09-25
#    step           timedelta64[ns] 00:00:00
#  * isobaricInhPa  (isobaricInhPa) float64 1e+03 975.0 950.0 ... 75.0 50.0
#    latitude       (y, x) float64 ...
#    longitude      (y, x) float64 ...
#    valid_time     datetime64[ns] 2021-09-25
#    metpy_crs      object Projection: lambert_conformal_conic
#Dimensions without coordinates: y, x
#Data variables:
#    t              (isobaricInhPa, y, x) 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-09-28T22:45 GRIB to CDM+CF via cfgrib-0.9.9...
#  Missing x- and y-coordinates
#----------------------------------------------------------------------

#  Try to add coordinates for y and x
data2=data1.metpy.assign_y_x()

#  Output Error:
# ValueError: Projected y and x coordinates cannot be collapsed to 1D within tolerance. Verify that your latitude and longitude coordinates correspond to your CRS coordinate.'''

最佳答案

因此您包含的错误消息指出了这里的问题:

Verify that your latitude and longitude coordinates correspond to your CRS coordinate.

使用提供的 CRS 参数,计算出的 x/y 坐标似乎不是一维值,这使得它们的使用存在问题,并表明投影参数通常不太正确。

我不确定您在 assign_crs 中使用的参数来自何处,但它们对于 NAM 218 grid 是不正确的.你想要的参数是:

  grid_mapping_name = "lambert_conformal_conic",
  latitude_of_projection_origin = 25.0,
  longitude_of_central_meridian = 265.0,
  standard_parallel = 25.0,
  earth_radius = 6371229.0

获取这些信息的最可靠方法是使用 pygrib 读取消息。假设文件中的所有 GRIB 消息都来自相同的坐标系(这些 NWS 文件很可能就是这种情况),您可以这样做:

import pygrib
from pyproj import CRS

grib = pygrib.open(filename)
msg = grib.message(1)

data1 = data.metpy.assign_crs(CRS(msg.projparams).to_cf())
data2 = data1.metpy.assign_y_x()

来自 pygrib 的消息提供了适当的 pyproj 坐标,可以很容易地将其映射到必要的 Cf 参数。

如果 cfgrib 可以在本地提供这些参数,那就太好了,但目前情况并非如此。有一个 open issue添加这个。

关于python-xarray - 无法将 y 和 x 坐标分配给 xarray.DataArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69370659/

相关文章:

python - 使用 xarray 添加全局属性

python - 将 xarray 坐标替换为另一个坐标

python - 使用 python matplotlib 和 metpy 添加辅助 y 轴

python - 如何使用 cartopy 和 matplotlib 屏蔽海洋中出现的数据

matplotlib - metpy:有飓风标记吗?

python - 从 netCDF 中提取位置和跨时间范围的值

python - 如何使用 groupby 为 xarray 数据集添加新变量并应用?

python - metpy.units 函数有问题吗?

python - 插值后向 matplotlib 图添加特征叠加时出现问题

python - 使用库 xarray (python) 进行分组后,时间维度错误