python学习记录—— 利用再分析数据绘制天气图
程序员文章站
2022-03-31 11:43:36
1 方法数据1.1数据数据采用1°*1°的NCEP/NCAR的再分析数据,数据格式为grib2。数据下载地址:FNL1*1说明:需要用邮箱注册账号,之后按需求下载具体日期的数据,每日4个时次,间隔6hr。数据变量介绍:可参考1.2 方法平台:mac ospython及库安装管理:Anaconda使用库:pygrib,cartopy,matplotlib,numpy编辑器:JupyterLabpygrib库使用方法介绍参考公众号MeteoAIcartopy介绍公众号云台书使2 绘...
1 方法数据
1.1数据
数据采用1°*1°的NCEP/NCAR的再分析数据,数据格式为grib2。
数据下载地址:FNL1*1
说明:需要用邮箱注册账号,之后按需求下载具体日期的数据,每日4个时次,间隔6hr。
数据变量介绍:可参考
1.2 方法
平台:mac os
python及库安装管理:Anaconda
使用库:pygrib,cartopy,matplotlib,numpy
编辑器:JupyterLab
pygrib库使用方法介绍
参考公众号MeteoAI
cartopy介绍
公众号云台书使
2 绘图方法
一页4图,绘制500风场及高度场、700的风场和相对湿度、850hpa的风场及高度场、海平面气压场。
代码如下:
%matplotlib inline
#库引用
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import pygrib
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
#打开文件
path='fnl_20200327_00_00.grib2'
ds=pygrib.open(path)
#数据处理
## 500hpa 数据
hgt_500 = ds.select(name='Geopotential Height', typeOfLevel='isobaricInhPa', level=500)[0]
temp_500 = ds.select(name='Temperature', typeOfLevel='isobaricInhPa', level=500)[0]
u_500 = ds.select(name='U component of wind', typeOfLevel='isobaricInhPa', level=500)[0]
v_500= ds.select(name='V component of wind', typeOfLevel='isobaricInhPa', level=500)[0]
lons=hgt_500.data()[2][0,:]
lats=hgt_500.data()[1][:,0]
hgt_500=hgt_500.data()[0]*0.1 ## 单位换算为dagpm
temp_500 = temp_500.data()[0]-273.15
u_500 = u_500.data()[0]
v_500 = v_500.data()[0]
#网格化 ,生成网格点坐标矩阵
lons, lats = np.meshgrid(lons, lats)
## 700
hgt_700 = ds.select(name='Geopotential Height', typeOfLevel='isobaricInhPa', level=700)[0]
temp_700 = ds.select(name='Temperature', typeOfLevel='isobaricInhPa', level=700)[0]
u_700 = ds.select(name='U component of wind', typeOfLevel='isobaricInhPa', level=700)[0]
v_700= ds.select(name='V component of wind', typeOfLevel='isobaricInhPa', level=700)[0]
rh_700=ds.select(name='Relative humidity', typeOfLevel='isobaricInhPa', level=700)[0]
hgt_700=hgt_700.data()[0]*0.1 ## 单位换算为dagpm
temp_700 = temp_700.data()[0]-273.15
u_700 = u_700.data()[0]
v_700 = v_700.data()[0]
rh_700 = rh_700.data()[0]
## 850
hgt_850 = ds.select(name='Geopotential Height', typeOfLevel='isobaricInhPa', level=850)[0]
temp_850 = ds.select(name='Temperature', typeOfLevel='isobaricInhPa', level=850)[0]
u_850 = ds.select(name='U component of wind', typeOfLevel='isobaricInhPa', level=850)[0]
v_850= ds.select(name='V component of wind', typeOfLevel='isobaricInhPa', level=850)[0]
hgt_850=hgt_850.data()[0]*0.1 ## 单位换算为dagpm
temp_850 = temp_850.data()[0]-273.15
u_850 = u_850.data()[0]
v_850 = v_850.data()[0]
## 地面
slp = ds.select(name='Pressure reduced to MSL', typeOfLevel='meanSea', level=0)[0]
slp = slp.data()[0]*0.01## 换算成hpa
#绘图
#一页4图
#设置投影方式,地图边界
proj = ccrs.PlateCarree() #定义投影转换,后面都会用到,不必重复输入‘ccrs.PlateCarree()’
#proj=ccrs.LambertConformal(central_longitude=120.0,central_latitude=45,standard_parallels=[40])
leftlon, rightlon, lowerlat, upperlat = (70,140,15,55)
img_extent = [leftlon, rightlon, lowerlat, upperlat]
#建立画布
fig=plt.figure(figsize=(12,8))
#添加第一子图
ax1 = fig.add_axes([0.1, 0.55, 0.4, 0.4],projection = proj)
#contour_map(ax1,img_extent,10)
#气象要素绘制
##等高线
isoheight=ax1.contour(lons,lats,hgt_500,transform=proj,levels=np.arange(500,600,4),colors='k',linewidths=1.1)
#等高线标注
#ax1.clabel(cs,cs.levels[::2],colors='k',fontsize=10,fmt='%.0f')
ax1.clabel(isoheight,fontsize=7,colors='k',inline_spacing=-4,fmt='%d')
#ax1.clabel(isoheight,inline=1,colors='k',fontsize=7,fmt='%d')
#等温线
isotherm=ax1.contour(lons,lats,temp_500,transform=proj,levels=range(-40,40,4),
colors='r',linestyles='--',linewidths=1,alpha=0.8)
ax1.clabel(isotherm,colors='r',fontsize=7,inline_spacing=-4,fmt='%.0f')
#风场
ax1.barbs(lons[::4,::4],lats[::4,::4],u_500[::4,::4],v_500[::4,::4],
linewidth=0.4,flagcolor='k',linestyle='-',length=5,
pivot='tip',barb_increments=dict(half=2,full=4,flag=20),
sizes=dict(spacing=0.15,height=0.5,width=0.12),transform=proj)
##定义地理坐标标签格式
xstep, ystep = 10, 10
###set_extent需要配置相应的crs,否则出来的地图范围不准确
ax1.set_extent(img_extent,crs=proj)
#### 标注坐标轴
ax1.set_xticks(np.arange(leftlon, rightlon+xstep,xstep), crs=proj)
ax1.set_yticks(np.arange(lowerlat, upperlat+ystep,ystep), crs=proj)
# zero_direction_label用来设置经度的0度加不加E和W
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax1.xaxis.set_major_formatter(lon_formatter)
ax1.yaxis.set_major_formatter(lat_formatter)
ax1.set_title('(a)',loc='left')
#地图设置
## Add ocean, land, rivers and lakes
#ax1.add_feature(cfeature.OCEAN.with_scale('50m'))
#ax1.add_feature(cfeature.LAND.with_scale('50m'))
#ax1.add_feature(cfeature.RIVERS.with_scale('50m'))
ax1.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.add_feature(cfeature.BORDERS, linestyle='-',lw=0.25)
##海岸线,50m精度
ax1.add_feature(cfeature.COASTLINE.with_scale('50m'))
## 本地shp文件
china = shpreader.Reader('bou2_4l.dbf').geometries()
###绘制中国国界省界九段线等等
ax1.add_geometries(china, proj,facecolor='none', edgecolor='black',zorder = 1)
###添加南海,实际上就是新建一个子图覆盖在之前子图的右下角
sub_ax1 = fig.add_axes([0.437, 0.58, 0.07, 0.1],projection = proj)
sub_ax1.set_extent([105, 125, 0, 25], crs=ccrs.PlateCarree())
sub_ax1.add_feature(cfeature.COASTLINE.with_scale('50m'))
china = shpreader.Reader('bou2_4l.dbf').geometries()
sub_ax1.add_geometries(china, ccrs.PlateCarree(),facecolor='none', edgecolor='black',zorder = 1)
#第二子图
ax2 = fig.add_axes([0.55, 0.55, 0.4, 0.4],projection = proj)
#湿度场
rh=ax2.contourf(lons,lats,rh_700,transform=proj,levels=np.arange(50,100,10),extend='both',cmap=plt.cm.Greens)
##等高线
isoheight=ax2.contour(lons,lats,hgt_700,transform=proj,levels=np.arange(200,400,4),colors='k',linewidths=1.1)
#等高线标注
ax2.clabel(isoheight,fontsize=7,colors='k',inline_spacing=-4,fmt='%d')
#风场
ax2.barbs(lons[::4,::4],lats[::4,::4],u_700[::4,::4],v_700[::4,::4],
linewidth=0.4,flagcolor='k',linestyle='-',length=5,
pivot='tip',barb_increments=dict(half=2,full=4,flag=20),
sizes=dict(spacing=0.15,height=0.5,width=0.12),transform=proj)
##定义地理坐标标签格式
xstep, ystep = 10, 10
###set_extent需要配置相应的crs,否则出来的地图范围不准确
ax2.set_extent(img_extent,crs=proj)
#### 标注坐标轴
ax2.set_xticks(np.arange(leftlon, rightlon+xstep,xstep), crs=proj)
ax2.set_yticks(np.arange(lowerlat, upperlat+ystep,ystep), crs=proj)
# zero_direction_label用来设置经度的0度加不加E和W
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax2.xaxis.set_major_formatter(lon_formatter)
ax2.yaxis.set_major_formatter(lat_formatter)
#ax1.set_title('Station',loc='center',fontsize =15)
ax2.set_title('(b)',loc='left')
#地图设置
## Add ocean, land, rivers and lakes
#ax1.add_feature(cfeature.OCEAN.with_scale('50m'))
#ax1.add_feature(cfeature.LAND.with_scale('50m'))
#ax1.add_feature(cfeature.RIVERS.with_scale('50m'))
ax2.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.add_feature(cfeature.BORDERS, linestyle='-',lw=0.25)
##海岸线,50m精度
ax2.add_feature(cfeature.COASTLINE.with_scale('50m'))
## 本地shp文件
china = shpreader.Reader('bou2_4l.dbf').geometries()
###绘制中国国界省界九段线等等
ax2.add_geometries(china, proj,facecolor='none', edgecolor='gray',zorder = 1)
###添加南海,实际上就是新建一个子图覆盖在之前子图的右下角
sub_ax2 = fig.add_axes([0.887, 0.58, 0.07, 0.1],projection = proj)
sub_ax2.set_extent([105, 125, 0, 25], crs=ccrs.PlateCarree())
sub_ax2.add_feature(cfeature.COASTLINE.with_scale('50m'))
china = shpreader.Reader('/Users/wangzelin/2020/china_basic_map/bou2_4l.dbf').geometries()
sub_ax2.add_geometries(china, ccrs.PlateCarree(),facecolor='none', edgecolor='black',zorder = 1)
#第三子图
ax3 = fig.add_axes([0.1, 0.1, 0.4, 0.4],projection = proj)
##等高线
isoheight=ax3.contour(lons,lats,hgt_850,transform=proj,levels=np.arange(100,200,4),colors='k',linewidths=1.1)
#等高线标注
ax3.clabel(isoheight,fontsize=7,colors='k',inline_spacing=-4,fmt='%d')
#等温线
isotherm=ax1.contour(lons,lats,temp_500,transform=proj,levels=range(-40,40,4),
colors='r',linestyles='--',linewidths=1,alpha=0.8)
ax1.clabel(isotherm,colors='r',fontsize=7,inline_spacing=-4,fmt='%.0f')
#风场
ax3.barbs(lons[::4,::4],lats[::4,::4],u_850[::4,::4],v_850[::4,::4],
linewidth=0.4,flagcolor='k',linestyle='-',length=5,
pivot='tip',barb_increments=dict(half=2,full=4,flag=20),
sizes=dict(spacing=0.15,height=0.5,width=0.12),transform=proj)
##定义地理坐标标签格式
xstep, ystep = 10, 10
###set_extent需要配置相应的crs,否则出来的地图范围不准确
ax3.set_extent(img_extent,crs=proj)
#### 标注坐标轴
ax3.set_xticks(np.arange(leftlon, rightlon+xstep,xstep), crs=proj)
ax3.set_yticks(np.arange(lowerlat, upperlat+ystep,ystep), crs=proj)
# zero_direction_label用来设置经度的0度加不加E和W
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax3.xaxis.set_major_formatter(lon_formatter)
ax3.yaxis.set_major_formatter(lat_formatter)
ax3.set_title('(c)',loc='left')
#地图设置
## Add ocean, land, rivers and lakes
#ax1.add_feature(cfeature.OCEAN.with_scale('50m'))
#ax1.add_feature(cfeature.LAND.with_scale('50m'))
#ax1.add_feature(cfeature.RIVERS.with_scale('50m'))
ax3.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.add_feature(cfeature.BORDERS, linestyle='-',lw=0.25)
##海岸线,50m精度
ax3.add_feature(cfeature.COASTLINE.with_scale('50m'))
## 本地shp文件
china = shpreader.Reader('bou2_4l.dbf').geometries()
###绘制中国国界省界九段线等等
ax3.add_geometries(china, proj,facecolor='none', edgecolor='black',zorder = 1)
###添加南海,实际上就是新建一个子图覆盖在之前子图的右下角
sub_ax3 = fig.add_axes([0.437, 0.13, 0.07, 0.1],projection = proj)
sub_ax3.set_extent([105, 125, 0, 25], crs=ccrs.PlateCarree())
sub_ax3.add_feature(cfeature.COASTLINE.with_scale('50m'))
china = shpreader.Reader('bou2_4l.dbf').geometries()
sub_ax3.add_geometries(china, ccrs.PlateCarree(),facecolor='none', edgecolor='black',zorder = 1)
#第四子图
ax4 = fig.add_axes([0.55, 0.1, 0.4, 0.4],projection = proj)
##等高线
c_slp=ax4.contour(lons,lats,slp,transform=proj,levels=np.arange(900,1040,2.5),colors='k',linewidths=1.1)
#等高线标注
ax4.clabel(c_slp,fontsize=5,colors='k',inline_spacing=-10,fmt='%.1f')
##定义地理坐标标签格式
xstep, ystep = 10, 10
###set_extent需要配置相应的crs,否则出来的地图范围不准确
ax4.set_extent(img_extent,crs=proj)
#### 标注坐标轴
ax4.set_xticks(np.arange(leftlon, rightlon+xstep,xstep), crs=proj)
ax4.set_yticks(np.arange(lowerlat, upperlat+ystep,ystep), crs=proj)
# zero_direction_label用来设置经度的0度加不加E和W
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax4.xaxis.set_major_formatter(lon_formatter)
ax4.yaxis.set_major_formatter(lat_formatter)
ax4.set_title('(d)',loc='left')
#地图设置
## Add ocean, land, rivers and lakes
#ax1.add_feature(cfeature.OCEAN.with_scale('50m'))
#ax1.add_feature(cfeature.LAND.with_scale('50m'))
#ax1.add_feature(cfeature.RIVERS.with_scale('50m'))
ax4.add_feature(cfeature.LAKES, alpha=0.5)
#ax1.add_feature(cfeature.BORDERS, linestyle='-',lw=0.25)
##海岸线,50m精度
ax4.add_feature(cfeature.COASTLINE.with_scale('50m'))
## 本地shp文件
china = shpreader.Reader('bou2_4l.dbf').geometries()
###绘制中国国界省界九段线等等
ax4.add_geometries(china, proj,facecolor='none', edgecolor='black',zorder = 1)
###添加南海,实际上就是新建一个子图覆盖在之前子图的右下角
sub_ax4 = fig.add_axes([0.887, 0.13, 0.07, 0.1],projection = proj)
sub_ax4.set_extent([105, 125, 0, 25], crs=ccrs.PlateCarree())
sub_ax4.add_feature(cfeature.COASTLINE.with_scale('50m'))
china = shpreader.Reader('bou2_4l.dbf').geometries()
sub_ax4.add_geometries(china,ccrs.PlateCarree(),facecolor='none', edgecolor='black',zorder = 1)
#存储
plt.savefig('weathermap.png',format='png',dpi=500)
效果如下:
参考学习的网站
本文地址:https://blog.csdn.net/wdd_1992/article/details/107190387
上一篇: emacs 利用 auto-complete 自动补齐
下一篇: Java中的static关键字解析