重新投影Xarray数据集

时间:2019-01-14 20:59:32

标签: python-3.x python-xarray cartopy pyproj metpy

我正在尝试将Lambert Conformal数据集重新投影到Plate Carree。我知道,可以使用Cartopy在视觉上轻松完成此操作。但是,我试图创建一个新的数据集,而不仅仅是显示重新投影的图像。下面是我已经列出的方法,但是我无法正确地对数据集进行子集化(Python 3.5,MacOSx)。

from siphon.catalog import TDSCatalog
import xarray as xr
from xarray.backends import NetCDF4DataStore
import numpy as np
import cartopy.crs as ccrs
from scipy.interpolate import griddata
import numpy.ma as ma
from pyproj import Proj, transform
import metpy

# Declare bounding box
min_lon = -78
min_lat = 36
max_lat = 40
max_lon = -72
boundinglat = [min_lat, max_lat]
boundinglon = [min_lon, max_lon]

# Load the dataset
cat = TDSCatalog('https://thredds.ucar.edu/thredds/catalog/grib/NCEP/HRRR/CONUS_2p5km/latest.xml')
dataset_name = sorted(cat.datasets.keys())[-1]
dataset = cat.datasets[dataset_name]
ds = dataset.remote_access(service='OPENDAP')
ds = NetCDF4DataStore(ds)
ds = xr.open_dataset(ds)

# parse the temperature at 850 and @ 0z reftime
tempiso = ds.metpy.parse_cf('Temperature_isobaric')
t850 = tempiso[0][2]

# transform bounding lat/lons to src_proj
src_proj = tempiso.metpy.cartopy_crs #aka lambert conformal conical
extents = src_proj.transform_points(ccrs.PlateCarree(), np.array(boundinglon), np.array(boundinglat))

# subset the data using the indexes of the closest values to the src_proj extents
t850_subset = t850[(np.abs(tempiso.y.values - extents[1][0])).argmin():(np.abs(tempiso.y.values - extents[1][1])).argmin()][(np.abs(tempiso.x.values - extents[0][1])).argmin():(np.abs(tempiso.x.values - extents[0][0])).argmin()]

# t850_subset should be a small, reshaped dataset, but it's shape is 0x2145
# now use nplinspace, npmeshgrid & scipy interpolate to reproject

我的变换点>查找最近的值子设置不起作用。它声称最接近的点在数据集的范围之外。如前所述,我计划使用nplinspace,npmeshgrid和scipy插值从t850_subset创建一个新的方形经纬度数据集。

是否有更简便的方法来调整xarray数据集的大小并对其进行重新投影?

2 个答案:

答案 0 :(得分:2)

您最简单的前进途径是利用xarray进行类似熊猫的数据选择的能力;这是IMO最好的xarray部分。将最后两行替换为:

# By transposing the result of transform_points, we can unpack the
# x and y coordinates into individual arrays.
x_lim, y_lim, _ = src_proj.transform_points(ccrs.PlateCarree(),
    np.array(boundinglon), np.array(boundinglat)).T
t850_subset = t850.sel(x=slice(*x_lim), y=slice(*y_lim))

您可以在xarray的selection and indexing functionality的文档中找到更多信息。您可能也会对xarray的built-in support for interpolation感兴趣。而且,如果对除SciPy之外的插值方法感兴趣,MetPy还提供了一套other interpolation methods

答案 1 :(得分:0)

Iris中,我们有各种“重新网格化”方法,如果这对您来说并不过分上下文切换的话。
Xarray解释了它与虹膜here的关系,并提供了a to_iris() method