如何使用 xESMF 将高分辨率 GRIB 网格重新采样为较粗糙的分辨率?
How do I resample a high-resolution GRIB grid to a coarser resolution using xESMF?
我正在尝试使用 xESMF 包将一组分辨率为 0.25 度的 GRIB2 阵列重新采样为更粗糙的 0.5 度分辨率(xarray 的粗化方法在这里不起作用,因为纬度中的坐标数量为奇数) .
我已经通过 pygrib 包将 GRIB 数据转换为 xarray 格式,然后子集化出我需要的特定网格:
fhr = 96
gridDefs = {
"0.25":
{'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/landsfc.pgrb2.0p25"},
"0.5":
{'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/landsfc.pgrb2.0p50"},
}
fileDefs = {
"0.25":
{'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/GEFSv12/reforecast/2019/2019051900/c00/Days%3A1-10/tmp_pres_2019051900_c00.grib2",
'localfile': "tmp_pres.grib2"},
"0.5":
{'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/GEFSv12/reforecast/2019/2019051900/c00/Days%3A1-10/tmp_pres_abv700mb_2019051900_c00.grib2",
'localfile': "tmp_pres_abv_700.grib2"},
}
def grib_to_xs(grib, vName):
arr = xr.DataArray(grib.values)
arr = arr.rename({'dim_0':'lat', 'dim_1':'lon'})
xs = arr.to_dataset(name=vName)
return xs
gribs = {}
for key, item in gridDefs.items():
if not os.path.exists(item['url'][item['url'].rfind('/')+1:]):
os.system("wget " + item['url'])
lsGrib = pygrib.open(item['url'][item['url'].rfind('/')+1:])
landsea = lsGrib[1].values
gLats = lsGrib[1]["distinctLatitudes"]
gLons = lsGrib[1]["distinctLongitudes"]
gribs["dataset" + key] = xr.Dataset({'lat': gLats, 'lon': gLons})
lsGrib.close()
for key, item in fileDefs.items():
if not os.path.exists(item['localfile']):
os.system("wget " + item['url'])
os.system("mv " + item['url'][item['url'].rfind('/')+1:] + " " + item['localfile'])
for key, item in fileDefs.items():
hold = pygrib.open(item['localfile'])
subsel = hold.select(forecastTime=fhr)
#Grab the first item
gribs[key] = grib_to_xs(subsel[1], "TT" + key)
hold.close()
以上代码在两个网格域(0.25 和 0.5)下载两个常量文件 (landsfc),然后在每个分辨率下下载两个 GRIB 文件。我正在尝试将 0.25 度 GRIB 文件 (tmp_pres.grib2) 重新采样到 0.5 度域,如下所示:
regridder = xe.Regridder(ds, gribs['dataset0.5'], 'bilinear')
print(regridder)
ds2 = regridder(ds)
我的问题是在尝试使用 regridder 时生成两条警告消息:
/media/robert/HDD/Anaconda3/envs/wrf-work/lib/python3.8/site-packages/xarray/core/dataarray.py:682: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
return key in self.data
/media/robert/HDD/Anaconda3/envs/wrf-work/lib/python3.8/site-packages/xesmf/backend.py:53: UserWarning: Latitude is outside of [-90, 90]
warnings.warn('Latitude is outside of [-90, 90]')
输出的 xarray 确实具有正确的坐标,但是网格内的值有很大偏差(在更高分辨率网格的 maxima/minima 之外),并显示出这些没有物理意义的奇怪条带模式。
我想知道的是,这是使用 xEMSF 升级阵列的正确过程吗?如果不是,我将如何解决该问题?
如有任何帮助,我们将不胜感激!
我会建议首先尝试保守而不是双线性(在他们的文档中推荐)并且可能检查你是否正确使用了参数因为它似乎有问题,我的第一个猜测是你正在做的事情出于某种原因移动纬度,我将文档 link 留在这里,希望有人知道更多。
Regridder 文档:
https://xesmf.readthedocs.io/en/latest/user_api.html?highlight=regridder#xesmf.frontend.Regridder.__init__
升级建议(搜索升级,也有提高分辨率的指南):
https://xesmf.readthedocs.io/en/latest/notebooks/Compare_algorithms.html?highlight=upscaling
感谢 MASACR 99 提供的文档链接和建议,我能够对 xESMF 包进行更多挖掘,并从包作者 (https://github.com/geoschem/GEOSChem-python-tutorial/blob/main/Chapter03_regridding.ipynb) 那里找到了重采样方法的工作示例,我的问题已通过两项更改解决:
- 我将方法从双线性更改为保守(这还需要在输入数组中添加两个字段(纬度和经度的边界)。
- 我没有直接将重采样的变量传递给重采样器,而是必须定义两个固定网格来创建重采样器,然后传递各个变量。
为了解决第一个变化,我创建了一个新函数来给我边界变量:
def get_bounds(arr, gridSize):
lonMin = np.nanmin(arr["lon"].values)
latMin = np.nanmin(arr["lat"].values)
lonMax = np.nanmax(arr["lon"].values)
latMax = np.nanmax(arr["lat"].values)
sizeLon = len(arr["lon"])
sizeLat = len(arr["lat"])
bounds = {}
bounds["lon"] = arr["lon"].values
bounds["lat"] = arr["lat"].values
bounds["lon_b"] = np.linspace(lonMin-(gridSize/2), lonMax+(gridSize/2), sizeLon+1)
bounds["lat_b"] = np.linspace(latMin-(gridSize/2), latMax+(gridSize/2), sizeLat+1).clip(-90, 90)
return bounds
对于第二个更改,我修改了 regridder 定义和应用程序以使用静态定义的网格,然后将所需的变量传递给重新采样:
regridder = xe.Regridder(get_bounds(gribs['dataset0.25'], 0.25), get_bounds(gribs['dataset0.5'], 0.5), 'conservative')
print(regridder)
ds2 = regridder(ds)
我正在尝试使用 xESMF 包将一组分辨率为 0.25 度的 GRIB2 阵列重新采样为更粗糙的 0.5 度分辨率(xarray 的粗化方法在这里不起作用,因为纬度中的坐标数量为奇数) .
我已经通过 pygrib 包将 GRIB 数据转换为 xarray 格式,然后子集化出我需要的特定网格:
fhr = 96
gridDefs = {
"0.25":
{'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/landsfc.pgrb2.0p25"},
"0.5":
{'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/landsfc.pgrb2.0p50"},
}
fileDefs = {
"0.25":
{'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/GEFSv12/reforecast/2019/2019051900/c00/Days%3A1-10/tmp_pres_2019051900_c00.grib2",
'localfile': "tmp_pres.grib2"},
"0.5":
{'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/GEFSv12/reforecast/2019/2019051900/c00/Days%3A1-10/tmp_pres_abv700mb_2019051900_c00.grib2",
'localfile': "tmp_pres_abv_700.grib2"},
}
def grib_to_xs(grib, vName):
arr = xr.DataArray(grib.values)
arr = arr.rename({'dim_0':'lat', 'dim_1':'lon'})
xs = arr.to_dataset(name=vName)
return xs
gribs = {}
for key, item in gridDefs.items():
if not os.path.exists(item['url'][item['url'].rfind('/')+1:]):
os.system("wget " + item['url'])
lsGrib = pygrib.open(item['url'][item['url'].rfind('/')+1:])
landsea = lsGrib[1].values
gLats = lsGrib[1]["distinctLatitudes"]
gLons = lsGrib[1]["distinctLongitudes"]
gribs["dataset" + key] = xr.Dataset({'lat': gLats, 'lon': gLons})
lsGrib.close()
for key, item in fileDefs.items():
if not os.path.exists(item['localfile']):
os.system("wget " + item['url'])
os.system("mv " + item['url'][item['url'].rfind('/')+1:] + " " + item['localfile'])
for key, item in fileDefs.items():
hold = pygrib.open(item['localfile'])
subsel = hold.select(forecastTime=fhr)
#Grab the first item
gribs[key] = grib_to_xs(subsel[1], "TT" + key)
hold.close()
以上代码在两个网格域(0.25 和 0.5)下载两个常量文件 (landsfc),然后在每个分辨率下下载两个 GRIB 文件。我正在尝试将 0.25 度 GRIB 文件 (tmp_pres.grib2) 重新采样到 0.5 度域,如下所示:
regridder = xe.Regridder(ds, gribs['dataset0.5'], 'bilinear')
print(regridder)
ds2 = regridder(ds)
我的问题是在尝试使用 regridder 时生成两条警告消息:
/media/robert/HDD/Anaconda3/envs/wrf-work/lib/python3.8/site-packages/xarray/core/dataarray.py:682: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
return key in self.data
/media/robert/HDD/Anaconda3/envs/wrf-work/lib/python3.8/site-packages/xesmf/backend.py:53: UserWarning: Latitude is outside of [-90, 90]
warnings.warn('Latitude is outside of [-90, 90]')
输出的 xarray 确实具有正确的坐标,但是网格内的值有很大偏差(在更高分辨率网格的 maxima/minima 之外),并显示出这些没有物理意义的奇怪条带模式。
我想知道的是,这是使用 xEMSF 升级阵列的正确过程吗?如果不是,我将如何解决该问题?
如有任何帮助,我们将不胜感激!
我会建议首先尝试保守而不是双线性(在他们的文档中推荐)并且可能检查你是否正确使用了参数因为它似乎有问题,我的第一个猜测是你正在做的事情出于某种原因移动纬度,我将文档 link 留在这里,希望有人知道更多。
Regridder 文档: https://xesmf.readthedocs.io/en/latest/user_api.html?highlight=regridder#xesmf.frontend.Regridder.__init__
升级建议(搜索升级,也有提高分辨率的指南): https://xesmf.readthedocs.io/en/latest/notebooks/Compare_algorithms.html?highlight=upscaling
感谢 MASACR 99 提供的文档链接和建议,我能够对 xESMF 包进行更多挖掘,并从包作者 (https://github.com/geoschem/GEOSChem-python-tutorial/blob/main/Chapter03_regridding.ipynb) 那里找到了重采样方法的工作示例,我的问题已通过两项更改解决:
- 我将方法从双线性更改为保守(这还需要在输入数组中添加两个字段(纬度和经度的边界)。
- 我没有直接将重采样的变量传递给重采样器,而是必须定义两个固定网格来创建重采样器,然后传递各个变量。
为了解决第一个变化,我创建了一个新函数来给我边界变量:
def get_bounds(arr, gridSize):
lonMin = np.nanmin(arr["lon"].values)
latMin = np.nanmin(arr["lat"].values)
lonMax = np.nanmax(arr["lon"].values)
latMax = np.nanmax(arr["lat"].values)
sizeLon = len(arr["lon"])
sizeLat = len(arr["lat"])
bounds = {}
bounds["lon"] = arr["lon"].values
bounds["lat"] = arr["lat"].values
bounds["lon_b"] = np.linspace(lonMin-(gridSize/2), lonMax+(gridSize/2), sizeLon+1)
bounds["lat_b"] = np.linspace(latMin-(gridSize/2), latMax+(gridSize/2), sizeLat+1).clip(-90, 90)
return bounds
对于第二个更改,我修改了 regridder 定义和应用程序以使用静态定义的网格,然后将所需的变量传递给重新采样:
regridder = xe.Regridder(get_bounds(gribs['dataset0.25'], 0.25), get_bounds(gribs['dataset0.5'], 0.5), 'conservative')
print(regridder)
ds2 = regridder(ds)