Basemap 特别擅长绘制数值天气模式输出数据,比如 WRF。WRF [注1] 模式是广泛使用的数值预报模式,只要变量名合适,大部分情况下都可以使用其它模式的输出来运行。
在 UCAR 网站可以下载 WRF 输出数据 [注2]。
输出文件描述 [注3] 包含了模式规模,域,投影等信息。例如,我们需要投影相关的信息来正确投影输出。
:CEN_LAT = 34.83158f ;
:CEN_LON = -81.02756f ;
:TRUELAT1 = 30.f ;
:TRUELAT2 = 60.f ;
注意:
确保 gdal 库安装了,从而便于读取 NetCDF 文件。
译注: 读取 NetCDF文件也可以使用 netcdf4 库
绘制域
from osgeo import gdal
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
wrf_out_file = "wrfout_v2_Lambert.nc"
ds_lon = gdal.Open('NETCDF:"'+wrf_out_file+'":XLONG')
ds_lat = gdal.Open('NETCDF:"'+wrf_out_file+'":XLAT')
ds_t2 = gdal.Open('NETCDF:"'+wrf_out_file+'":T2')
map = Basemap(llcrnrlon=-95.,llcrnrlat=24.,urcrnrlon=-66.,urcrnrlat=45.)
map.contourf(ds_lon.ReadAsArray()[1], ds_lat.ReadAsArray()[1], ds_t2.ReadAsArray()[1])
map.drawcoastlines()
plt.show()
投影地图
模式使用的是 Lambert conformal 投影,因此 Basemap 应该也设置为 Lambert conformal 。
from osgeo import gdal
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
wrf_out_file = "wrfout_v2_Lambert.nc"
ds_lon = gdal.Open('NETCDF:"'+wrf_out_file+'":XLONG')
ds_lat = gdal.Open('NETCDF:"'+wrf_out_file+'":XLAT')
ds_t2 = gdal.Open('NETCDF:"'+wrf_out_file+'":T2')
map = Basemap(llcrnrlon=-95.,llcrnrlat=27.,urcrnrlon=-65.,urcrnrlat=40.,
projection='lcc', lat_1=30.,lat_2=60.,lat_0=34.83158,lon_0=-98.)
x, y = map(ds_lon.ReadAsArray()[1], ds_lat.ReadAsArray()[1])
map.contourf(x, y, ds_t2.ReadAsArray()[1])
map.drawcoastlines()
plt.show()
Basemap 有函数可以很容易绘制 wind barbs(不像其它 gis 库)
from osgeo import gdal
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
wrf_out_file = "wrfout_v2_Lambert.nc"
ds_lon = gdal.Open('NETCDF:"'+wrf_out_file+'":XLONG')
ds_lat = gdal.Open('NETCDF:"'+wrf_out_file+'":XLAT')
ds_u = gdal.Open('NETCDF:"'+wrf_out_file+'":U10')
ds_v = gdal.Open('NETCDF:"'+wrf_out_file+'":V10')
map = Basemap(llcrnrlon=-93.7, llcrnrlat=28., urcrnrlon=-66.1, urcrnrlat=39.5,
resolution = 'l',
projection='lcc', lat_1=30., lat_2=60., lat_0=34.83158, lon_0=-98.)
x, y = map(ds_lon.ReadAsArray()[1], ds_lat.ReadAsArray()[1])
u = ds_u.ReadAsArray()[1]
v = ds_v.ReadAsArray()[1]
yy = np.arange(0, y.shape[0], 4)
xx = np.arange(0, x.shape[1], 4)
points = np.meshgrid(yy, xx)
map.contourf(x, y, np.sqrt(u*u + v*v), alpha = 0.4)
map.barbs(x[points], y[points], u[points], v[points])
map.drawcoastlines(color = '0.15')
plt.show()
注1:
https://en.wikipedia.org/wiki/Weather_Research_and_Forecasting_Model
注2:
http://www.unidata.ucar.edu/software/netcdf/examples/wrfout_v2_Lambert.nc
注3:
https://www.unidata.ucar.edu/software/netcdf/examples/wrfout_v2_Lambert.cdl