python绘制智能网格天气预报产品
1.产品说明
智能网格天气预报业务化下发产品包括:
PRE/GUST/FOG/HZ/SNOW/TMAX/TMIN/VIS/R24/RH/CLOUD/TA/TMP/WIN/PRS/TCC/SAND/SOIL/SUNLIGHT/HOURS:
固定代码,表示产品内容是降水/阵风/雾/霾/积雪/最高气温/最低气温/能见度/24小时降水量/相对湿度/云量/气温距平/温度/风/气压/总云量/沙尘/土壤/日照/时数
数据格式
国家级智能网格预报指导产品均采用GRIB2文件格式。
例:Z_NWGD_C_BABJ_YYYYMMDDhhmmss_P_RFFC_SCMOC-SSM-PRO_YYYYMMDDhhmm_02401.GRB2
产品分文件存储
产品下发路径
与前期已下发网格预报指导产品路径一致,新增的国家级智能网格预报指导产品通过国内通信系统实时资料下载平台提供订阅,网格预报产品接入至本省气象大数据云平台“天擎”提供数据服务。
绘制产品图
根据2米温度产品为例编写代码绘制,其他产品类似,数据格式一致,读取获取后配置不同color 和camp即可
def draw_main(filepath, outpath):
fileName = os.path.basename(filepath)
type = fileName.split('-')[1].split('_')[0]
ele = get_element_code_from_filename(type)
ds = xr.open_dataset(filepath, engine='cfgrib')
# fileName = os.path.basename(filepath)
time = ds['time'].values
time_str = np.datetime64(time).astype('M8[ms]').item().strftime("%Y%m%d%H%M%S")
lon = ds['longitude'].values
lat = ds['latitude'].values
lonMin, lonMax, latMin, latMax = lon.min(), lon.max(), lat.min(), lat.max()
step = ds['step'].values
element = 't2m'
t2m = ds[element].values
print("经纬度范围:", lonMin, lonMax, latMin, latMax)
# 创建时间文件夹【起报时间】
outpath = outpath + '/' + type + '/' + time_str
if not os.path.exists(outpath):
os.makedirs(outpath)
for i, step_value in enumerate(step):
current_t2m = t2m[i, :, :] - 273.15
ytime = time + step_value
fileTime = np.datetime64(ytime).astype('M8[ms]').item().strftime("%Y%m%d%H%M%S")
plt.figure(figsize=(10, 8))
hex_colors = color_smart['TMP']['colors']
temperature_bounds = color_smart['TMP']['levels']
colors = [matplotlib.colors.to_rgb(color) for color in hex_colors]
cmap = matplotlib.colors.ListedColormap(colors, name="custom_temp")
norm = matplotlib.colors.BoundaryNorm(temperature_bounds, len(colors))
plt.contourf(lon, lat, current_t2m, levels=temperature_bounds, cmap=cmap, norm=norm, extend='both')
plt.axis('off')
plt.gca().set_frame_on(False)
output_filename = f"{fileTime}.png"
output_path = os.path.join(outpath, output_filename)
plt.savefig(output_path, bbox_inches='tight', pad_inches=0)
plt.close()
部分产品展示
2米温度
相对湿度