Python绘制风场

这次利用NCEP/NCAR的再分析数据(https://psl.noaa.gov/data/gridded/data.ncep.reanalysis.html)绘制风场。数据是来自1991年至2020年的长期月平均值(Long term monthly means, derived from years 1991 to 2020),纬向风数据和经向风数据为两个单独的文件,其中纬向风数据下载地址为:

https://downloads.psl.noaa.gov/Datasets/ncep.reanalysis/Monthlies/surface_gauss/uwnd.10m.mon.ltm.1991-2020.nc

经向风数据下载地址为:

https://downloads.psl.noaa.gov/Datasets/ncep.reanalysis/Monthlies/surface_gauss/vwnd.10m.mon.ltm.1991-2020.nc

首先导入数据处理与绘图的库

import numpy as npimport matplotlib.pyplot as pltimport xarray as xrimport cartopy.crs as ccrsimport geopandas as gpd

读取并查看数据

diri = "C:/Rmet/data/"filiu = "uwnd.10m.mon.ltm.1991-2020.nc" # wind of U component filiv = "vwnd.10m.mon.ltm.1991-2020.nc" # wind of V componentfu = xr.open_dataset(diri + filiu)fv = xr.open_dataset(diri + filiv)

数据读取后,会给出一个警告信息,如下:

图片

以上的一些警告信息,如 SerializationWarning: Unable to decode time axis into full numpy.datetime64 objects, continuing using cftime.datetime objects instead, reason: dates out of range。

这不算报错,只是一些警告。这个数据是从1991-2020年的数据中计算来的长期月平均值(Long term monthly means, derived from years 1991 to 2020),只有12个时刻的数值,分别是1, 2, 3, …, 12月。但是在往数据中写入时间维度时,需要写上年、月、日等完整的时间信息,所以1月写成“0001/01/01T00:00:00”,12月写成“0001/12/01T00:00:00”,如此,月份前面的0001年会被软件库认为是公元元年。公元1582年,罗马教皇格里高利十三世批准颁行新历法《格里高利历》,新历法从日历中删除了几天(1582年10月5-14日),从1582-10-15年当天或之后的日期假定为现行公历日期(即格里高利历),而在此之前的日期被假定为儒略日期。在现行公历开始实施之前(即儒略历),人们对日期的转换没有达成一致意见,不同的软件库可能使用不同的日历来解释时间值。这里的提示是不能将nc数据存储的时间值转换为numpy库的datetime64对象,所以继续使用cftime库的datetime对象。

接下来,查看数据,输入:

fu

返回如下结果

图片

可以看到,该数据有三个维度,纬度有94个,经度有192个,时间有12个。

选定研究区范围的数据

xmin, xmax, ymin, ymax = 70, 180, 0, 60x = fu.coords["lon"]y = fu.coords["lat"]fu = fu.loc[dict(lon = x[(x >= xmin) & (x <= xmax)],                 lat = y[(y >= ymin) & (y <= ymax)])]fv = fv.loc[dict(lon = x[(x >= xmin) & (x <= xmax)],                 lat = y[(y >= ymin) & (y <= ymax)])]
# 生成数据的坐标矩阵Lon, Lat = np.meshgrid(fu.coords["lon"], fu.coords["lat"])

查看时间维度

f.coords['time']

图片

数据在时间上一共有12个月。在python里面,数据的索引是从0开始的,因此,假如我们要选出4月的数据,代码如下:

indTime = 3   # time index. AprilU = fu['uwnd'].isel(time = indTime) V = fv['vwnd'].isel(time = indTime)

从高德读取中国地图矢量文件

#diriMap = "C:/Rmet/data/ChinaMap/CHN_amap.shp"diriMap = "https://geo.datav.aliyun.com/areas_v3/bound/100000.json"cmap = gpd.read_file(diriMap)cmap

绘图

# plotdata_crs = ccrs.PlateCarree()  # 设定坐标系fig = plt.figure(figsize=(8, 5)) # 设置图片大小# The projection argument is used when creating plots and determines the projection of the resulting plot ax = fig.add_subplot(1, 1, 1, projection = data_crs)ax.set_extent([70, 180, 0, 60], crs = data_crs)  # 限定绘图范围ax.coastlines(linewidth = 0.6)#ax.stock_img() # Add a standard image to the mapax.add_geometries(cmap['geometry'], crs = data_crs, fc = "none",                  edgecolor = "red", linewidth = 0.6)
# add gridlinesax.gridlines(    crs = data_crs,    draw_labels = {"bottom": "x", "left": "y", "right": "y"},    rotate_labels = False,    xlocs = np.arange(70, 181, 10),     ylocs = np.arange(0, 61, 10),    x_inline = False, y_inline = False,    linewidth=0.5, linestyle='--', color='black')ax.set_title("April mean wind of 10m from the NCEP Reanalysis",            loc = "left")# add wind vectornxy = 2 # skip 2 gridsquiver = ax.quiver(Lon[::nxy, ::nxy], Lat[::nxy, ::nxy],                    U[::nxy, ::nxy], V[::nxy, ::nxy],                   transform = data_crs)# 设置风速图例ax.quiverkey(quiver, 0.93, 1.03, 5, "5m/s",labelpos='E', coordinates='axes')plt.savefig('C:/Rmet/figures/Wind.png', dpi = 600)plt.show()

绘图效果如下

图片

原创文章,作者:guozi,如若转载,请注明出处:https://www.sudun.com/ask/88208.html

(0)
guozi的头像guozi
上一篇 2024年6月3日 下午4:10
下一篇 2024年6月3日 下午4:13

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注