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

gdl*_*wen 4 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.'''

Run Code Online (Sandbox Code Playgroud)

Dop*_*ift 6

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

验证您的纬度和经度坐标是否与您的 CRS 坐标相对应。

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

我不确定您使用的参数来自哪里,但它们对于NAM 218 gridassign_crs来说不正确。您想要的参数是:

  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
Run Code Online (Sandbox Code Playgroud)

获取这些信息的最可靠方法是使用 读取消息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()
Run Code Online (Sandbox Code Playgroud)

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

如果能够本地提供这些参数那就太好了cfgrib,但目前情况并非如此。有一个未解决的问题需要添加此内容。