来自Grib2文件的卡通图像不符合海岸线和边界在同一CR

2024-09-27 09:25:40 发布

您现在位置:Python中文网/ 问答频道 /正文

这看起来很简单,可以修复我的代码,但是我觉得我现在已经看了太多代码了,需要重新审视一下。我只是想引入一个Grib2文件,我从NCEP下载了HRRR模型。根据他们的信息,网格类型为Lambert共形,角的纬度范围为(21.13812,21.14055,47.84219,47.83862),角的经度为(-122.7195,-72.28972,-60.91719,-134.0955)。在

在尝试放大我感兴趣的区域之前,我只想简单地在适当的CRS中显示一个图像,但是当我尝试对模型的域执行此操作时,我得到了边界和海岸线在该范围内,但是从Grib2文件生成的实际图像只是放大了。我尝试过使用extent=[我的域extent],但它似乎总是使我正在测试它的笔记本崩溃。这是我的代码和从中得到的相关图像。在

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
from mpl_toolkits.basemap import Basemap
from osgeo import gdal
gdal.SetConfigOption('GRIB_NORMALIZE_UNITS', 'NO')

plt.figure()
filename='C:\\Users\\Public\\Documents\\GRIB\\hrrr.t18z.wrfsfcf00.grib2'
grib = gdal.Open(filename, gdal.GA_ReadOnly)

z00 = grib.GetRasterBand(47)
meta00 = z00.GetMetadata()
band_description = z00.GetDescription()

bz00 = z00.ReadAsArray()

latitude_south = 21.13812 #38.5
latitude_north = 47.84219 #50
longitude_west = -134.0955 #-91
longitude_east = -60.91719 #-69

fig = plt.figure(figsize=(20, 20))

title= meta00['GRIB_COMMENT']+' at '+meta00['GRIB_SHORT_NAME']
fig.set_facecolor('white')

ax = plt.axes(projection=ccrs.LambertConformal())

ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.coastlines(resolution='110m')

ax.imshow(bz00,origin='upper',transform=ccrs.LambertConformal())

plt.title(title)
plt.show()

Returns Just Grib File

如果我改变:

^{pr2}$

ax = plt.axes(projection=ccrs.LambertConformal(central_longitude=-95.5, 
central_latitude=38.5,cutoff=21.13)

我得到了我的边界,但我的实际数据不一致,它创造了我所称的蝙蝠侠阴谋。在

Batman Plot

类似的问题也会发生,即使我放大了这个域,仍然有我的边界存在。Grib文件中的底层数据没有改变,与我要获取的数据相对应。在

所以,正如我已经说过的,这可能是一个简单的解决办法,我只是错过了,如果不是的话,我很高兴知道我在搞砸什么步骤或者什么过程,我可以从中学习,这样我以后就不会这样做了!在

更新1: 我添加和更改了一些代码,现在只显示图像而不显示边界和海岸线。在

test_extent = [longitude_west,longitude_east,latitude_south,latitude_north]
ax.imshow(bz00,origin='upper',extent=test_extent)

这给了我下面的图像。 Looks exactly like image 1.

我注意到的另一件事可能是所有这一切的根本原因是,当我打印出plt.gca().get_ylim()和{}的值时,根据显示的内容,我得到的值大不相同。在


Tags: 文件代码图像importpltaxextent边界
1条回答
网友
1楼 · 发布于 2024-09-27 09:25:40

我的问题似乎来自于这样一个事实:Grib文件无论是否可以在其他程序中正确显示,都不能很好地与Matplotlib和Cartopy一起使用。或者至少和我用的Grib文件不一样。为了这一点,也许将来会帮助其他人的是NCEP HRRR模型,您可以得到here或{a2}。在

如果你把文件从Grib2格式转换成NetCDF格式,并且我能在地图上得到我想要的边界、海岸线等,一切看起来都很好。我附上了下面的代码和输出,以展示它是如何工作的。另外,我手工选择了一个单独的数据集,我想显示它来测试我之前的代码,所以如果你想查看文件中其他可用的数据集,你就需要利用ncdump或类似的工具来查看数据集上的信息。在

import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
import cartopy.feature as cfeature
from osgeo import gdal
gdal.SetConfigOption('GRIB_NORMALIZE_UNITS', 'NO')

nc_f = 'C:\\Users\\Public\\Documents\\GRIB\\test.nc'  # Your filename
nc_fid = Dataset(nc_f, 'r')  # Dataset is the class behavior to open the 
                             # file and create an instance of the ncCDF4 
                             # class

# Extract data from NetCDF file
lats = nc_fid.variables['gridlat_0'][:]  
lons = nc_fid.variables['gridlon_0'][:]
temp = nc_fid.variables['TMP_P0_L1_GLC0'][:]

fig = plt.figure(figsize=(20, 20))
states_provinces = cfeature.NaturalEarthFeature(category='cultural', \
      name='admin_1_states_provinces_lines',scale='50m', facecolor='none')
proj = ccrs.LambertConformal()
ax = plt.axes(projection=proj)

plt.pcolormesh(lons, lats, temp, transform=ccrs.PlateCarree(), 
              cmap='RdYlBu_r', zorder=1)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':', zorder=2)
ax.add_feature(states_provinces, edgecolor='black')
ax.coastlines()

plt.show()

地图最终预览

enter image description here

相关问题 更多 >

    热门问题