首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xESMF将高分辨率GRIB网格重定向到更粗的分辨率?

如何使用xESMF将高分辨率GRIB网格重定向到更粗的分辨率?
EN

Stack Overflow用户
提问于 2021-02-12 19:27:29
回答 2查看 544关注 0票数 2

我试图使用GRIB2包将一组0.25度分辨率的xESMF数组重采样到更粗的0.5度分辨率(xarray的粗化方法在这里不起作用,因为纬度上有一个奇数的坐标)。

我已经通过pygrib包将GRIB数据转换为xarray格式,然后将所需的特定网格细分出来:

代码语言:javascript
复制
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度域,如下所示:

代码语言:javascript
复制
regridder = xe.Regridder(ds, gribs['dataset0.5'], 'bilinear')
print(regridder)
ds2 = regridder(ds)

我的问题是在尝试使用regridder时生成两条警告消息:

代码语言:javascript
复制
/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确实有正确的坐标,但是网格内的值是很远的(在更精细的分辨率网格的极大值/极小值之外),并且显示出这些奇怪的带状模式,它们没有任何物理意义。

我想知道的是,这是使用xEMSF升级数组的正确过程吗?如果不是,我将如何解决这个问题?

任何帮助都将不胜感激,谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-12 19:38:50

我建议先尝试保守而不是双线性(在他们的文档中是推荐的),也许检查你是否正确地使用了参数,因为它似乎是错误的,我的第一个猜测是,你正在做的事情因为某种原因而移动纬度,我把docs链接留在这里,希望有人知道更多。

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

票数 2
EN

Stack Overflow用户

发布于 2021-02-12 22:39:12

由于MASACR 99提供的文档链接和建议,我对xESMF包做了更多的深入研究,并找到了来自包作者(https://github.com/geoschem/GEOSChem-python-tutorial/blob/main/Chapter03_regridding.ipynb)的重采样方法的工作示例,通过两个更改解决了我的问题:

  1. I将方法从双线性改为保守(这还需要在输入数组中添加两个字段(纬度和直接将变量重放到重放的边界),相反,我必须定义两个固定的网格来创建重放器,然后传递单个变量。

为了解决第一个变化,我创建了一个新函数来给出边界变量:

代码语言:javascript
复制
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定义和应用程序,以使用静态定义的网格,然后传递所需的变量来重采样:

代码语言:javascript
复制
regridder = xe.Regridder(get_bounds(gribs['dataset0.25'], 0.25), get_bounds(gribs['dataset0.5'], 0.5), 'conservative')
print(regridder)
ds2 = regridder(ds)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66177931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档