【发布时间】:2021-10-25 23:10:33
【问题描述】:
我使用 MetPy Mondays #154 中的“Gridding METAR Observations”示例代码已经有一段时间了,没有任何问题。直到最近,我都没有限制地通过了整个数据集(除了排除南极附近的站点,因为它们破坏了 Lambert Conformal 变换。)
最近,我试图将我处理的 METAR 数据的域限制在北美。此时,MetPy 的 interpolate_to_grid 函数似乎正在返回 nan,而之前没有。由于我感兴趣的区域远离数据集的边界,我预计对从插值数据得出的轮廓没有影响;相反,它会产生深远的影响(请参阅下面的示例。)我尝试使用 SciPy 的 interp2d 函数对缺失数据的区域 (nan) 进行插值,但数量太多nan 用那个“创可贴步骤”来克服。
问题:这是 interpolate_to_grid 的预期行为,还是我使用不正确?我总是可以继续使用整个数据集,但这确实会减慢速度。感谢您对理解这一点的任何帮助。
在以下示例中,我使用来自 https://tgftp.nws.noaa.gov/data/observations/metar/cycles/ 的 00Z.TXT 文件,但我看到了这个使用其他来源的 METAR 数据。
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import numpy as np
from metpy.io import parse_metar_file
from metpy.interpolate import interpolate_to_grid, remove_nan_observations
%matplotlib inline
# If we are getting data from the filesystem:
month=10
fp = '00Z.TXT'
df0 = parse_metar_file(fp, month=month)
# To avoid the Lambert Conformal transformation from blowing up
# at the South Pole if reports from Antarctica are present:
q = df0.loc[df0['latitude'].values>=-30]
df = q
# Set up the map projection
mapcrs = ccrs.LambertConformal(central_longitude=-100, central_latitude=35,standard_parallels=(30,60))
datacrs= ccrs.PlateCarree()
# 1) Remove NaN
df1=df.dropna(subset=['latitude','longitude','air_temperature'])
lon1=df1['longitude'].values
lat1=df1['latitude'].values
xp1 , yp1 , _ = mapcrs.transform_points(datacrs, lon1, lat1).T
# Interpolate observation data onto grid.
xm1, ym1, tmp = remove_nan_observations(xp1, yp1, df1['air_temperature'].values)
Tgridx, Tgridy, Temp = interpolate_to_grid(xm1, ym1, tmp, hres = 20000, interp_type='cressman')
fig = plt.figure(figsize=(20,15))
ax = fig.add_subplot(1,1,1,projection=mapcrs)
ax.set_extent([-105, -95, 32, 40],datacrs)
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax.add_feature(cfeature.STATES.with_scale('50m'))
c = ax.contour(Tgridx, Tgridy, Temp ,levels=50)
当我采用相同的数据集并限制其域时,我们会得到不连续的轮廓:
# Limit to ~ North America
q = df0.loc[(df0['latitude'].values>=20) & (df0['latitude'].values<=70) &
(df0['longitude'].values>=-150) & (df0['longitude'].values<=-60)]
df = q
# 1) Remove NaN
df2=df.dropna(subset=['latitude','longitude','air_temperature'])
lon2=df2['longitude'].values
lat2=df2['latitude'].values
xp2 , yp2 , _ = mapcrs.transform_points(datacrs, lon2, lat2).T
# Interpolate observation data onto grid.
xm2, ym2, tmp2 = remove_nan_observations(xp2, yp2, df2['air_temperature'].values)
Tgridx2, Tgridy2, Temp2 = interpolate_to_grid(xm2, ym2, tmp2, hres = 20000, interp_type='cressman')
fig2 = plt.figure(figsize=(20,15))
ax2 = fig2.add_subplot(1,1,1,projection=mapcrs)
ax2.set_extent([-105, -95, 32, 40],datacrs)
ax2.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax2.add_feature(cfeature.STATES.with_scale('50m'))
c2 = ax2.contour(Tgridx2, Tgridy2, Temp2 ,levels=50)
我确实确认了站点位置实际上在北美(未显示)。然后我在插值数据中检查了 nan 的位置,发现轮廓在填充有 的区域中断南。作为最终图,我绘制了nan(蓝色)的位置、车站位置(绿色)以及折断的轮廓。
xn , yn = np.where(np.isnan(Temp2))
fig4 = plt.figure(figsize=(20,15))
ax4 = fig4.add_subplot(1,1,1,projection=mapcrs)
ax4.set_extent([-105, -95, 32, 40],datacrs)
ax4.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax4.add_feature(cfeature.STATES.with_scale('50m'))
c4 = ax4.contour(Tgridx2, Tgridy2, Temp2 ,levels=50)
plt.scatter(df2['longitude'],df2['latitude'],transform=datacrs,color='lightgreen')
plt.scatter(Tgridx2[xn,yn], Tgridy2[xn,yn])
plt.show()
【问题讨论】:
标签: grid interpolation metpy