利用Python语言实现Grib数据可视化主要依靠三个库——pygrib、numpy和matplotlib。pygrib是欧洲中期天气预报中心(ECMWF)的GRIG API C库的Python接口,通过这个库可以将Grib数据读取出来;numpy是Python的一种开源的数值计算扩展,这种工具可用来存储和处理大型矩阵;matplotlib是python著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图;在数据可视化过程中,我们常需要将数据在地图上画出来,所以还需要matplotlib的一个子包basemap,负责地图绘制。
matplotlib依赖
nose
numpy
pyparsing
python-dateutil
cycler
pkg-config
freetype
libpng
安装过程
这里我都是通过源码包安装的,大家也可以再终端里通过pip install 命令来安装
1、安装nose
解压缩后,进入命令提示符 运行
1 python3 setup.py install
2、安装numpy
解压缩后,进入命令提示符 运行
1 python3 setup.py install
3、安装pyparsing
解压缩后,进入命令提示符 运行
1 python3 setup.py install
4、安装python-dateutil
解压缩后,进入命令提示符 运行
1 python3 setup.py install
5、安装cycler
解压缩后,进入命令提示符 运行
1 python3 setup.py install
6、安装pkg-config
1 ./configure --with-intermal-glib 2 make && date 3 sudo make install && date
7、安装freetype
1 ./configure 2 make && date 3 sudo make install && date
8、安装libpng
1 ./configure 2 make && date 3 sudo make install && date
9、安装matplotlib-1.5.0
解压缩后,进入命令提示符 运行
1 python3 setup.py install
(二) basemap 安装
basemap依赖
geos
pyproj
安装过程
1、安装GEOS
1 ./configure 2 make && date 3 sudo make install && date
2、安装pyproj
1 python3 setup.py install
3、安装basemap
1 python3 setup.py install
pygrib依赖
Jasper
GRIB API
numpy
pyproj
安装过程
由于之前已经安装了numpy和pyproj,这里只需安装Jasper和GRIB API即可安装pygrib
1、安装Jasper
1 ./configure 2 make && date 3 sudo make install && date
2、安装GRIB API
1 ./configure --with-jasper='/usr/local/' 2 make && date 3 sudo make install && date
3、安装pygrib
安装pygrib之前首先要根据自己的实际情况修改文件目录下的setup.cfg文件,最主要的就是修改grib_api_dir和jasper_dir,这两个是刚刚安装的Jasper和GRIB API的路径,如果这两个地址不正确安装会报错
1 # Rename this file to setup.cfg to set pygrib's 2 # build options. 3 # Follow instructions below for editing. 4 [directories] 5 # uncomment and set to grib_api install location. 6 # Include files should be located in grib_api_dir/include and 7 # the library should be located in grib_api_dir/lib. 8 # If the libraries and include files are installed in separate locations, 9 # use grib_api_libdir and grib_api_incdir to specify the locations 10 # separately. 11 grib_api_dir = /usr/local 12 # if grib_api was built with jasper support for JPEG200, 13 # uncomment and set to jasper lib install location. 14 # If the libraries and include files are installed in separate locations, 15 # use jasper_libdir and jasper_incdir. 16 jasper_dir = /usr/local 17 # if grib_api was built with openjpeg support for JPEG200, 18 # uncomment and set to openjpeg lib install location. 19 # If the libraries and include files are installed in separate locations, 20 # use openjpeg_libdir and openjpeg_incdir. 21 #openjpeg_dir = /opt/local 22 # if grib_api was built with png support, 23 # uncomment and set to png lib install location. 24 # If the libraries and include files are installed in separate locations, 25 # use png_libdir and png_incdir. 26 png_dir = /usr 27 # if grib_api was built with png support, 28 # uncomment and set to zlib install location. 29 zlib_dir = /usr 30 # install man pages for command line utilities here 31 #man_dir = /usr/local/man View Code
修改好就可以正常安装了
1 python3 setup.py install
虽然我做的东西和气象沾边,但是我本身并不是气象专业出身,所有这些东西都是我慢慢研究琢磨出来的,所以有些方面可能讲的比较外行,有不对的地方欢迎大家留言指正。
1 >>> import pygrib
1 >>> grbs = pygrib.open('/Users/Kallan/Documents/data/echhae50.082')
1 >>> grbs.seek(0) 2 >>> for grb in grbs: 3 grb 4 1:Geopotential Height:gpm (instant):regular_ll:isobaricInhPa:level 500:fcst time 24 :from 201507081200
<span> 1 <span>:数据列表的行号,有的文件可能包括多个数据</span></span>
<span> Geopotential<span> <span>Height<span>:数据的名称</span></span></span></span>
<span> gpm <span>(<span>instant<span>):数据的单位</span></span></span></span>
<span> regular_ll<span>:常规数据,其实这个字段我也不清楚</span></span>
<span> isobaricInhPa<span>:这个字段表示的是数据属性,此处表示是以hPa为单位的等压面</span></span>
<span> level <span>500<span>:这个字段表示的是高度层</span></span></span>
<span> fcst time <span>24<span> <span>:预报时效</span></span></span></span>
<span> from<span> <span>201507081200 :起报时间</span></span></span>
综合上面的信息可以得出,这个文件是从2015年7月8日12时开始的24小时后500hPa等压面高度场数据
1 >>> grb = grbs.select(name='Geopotential Height')[0] 2 >>> data = grb.values 3 >>> print(data.shape,data.min(),data.max()) 4 (37, 37) 5368.6796875 5941.0390625 5 >>> lat,lon=grb.latlons() 6 >>> print(lat,'/n',lon) 7 [[ 0. 0. 0. ..., 0. 0. 0. ] 8 [ 2.5 2.5 2.5 ..., 2.5 2.5 2.5] 9 [ 5. 5. 5. ..., 5. 5. 5. ] 10 ..., 11 [ 85. 85. 85. ..., 85. 85. 85. ] 12 [ 87.5 87.5 87.5 ..., 87.5 87.5 87.5] 13 [ 90. 90. 90. ..., 90. 90. 90. ]] 14 [[-90. -87.5 -85. ..., -5. -2.5 0. ] 15 [-90. -87.5 -85. ..., -5. -2.5 0. ] 16 [-90. -87.5 -85. ..., -5. -2.5 0. ] 17 ..., 18 [-90. -87.5 -85. ..., -5. -2.5 0. ] 19 [-90. -87.5 -85. ..., -5. -2.5 0. ] 20 [-90. -87.5 -85. ..., -5. -2.5 0. ]]
1 >>> import matplotlib.pyplot as plt 2 >>> from mpl_toolkits.basemap import Basemap 3 >>> import numpy as np
1 >>> plt.figure() 2 <matplotlib.figure.Figure object at 0x107e65198>
1 >>> m=Basemap(projection='mill',lat_ts=10,llcrnrlon=lon.min(), / 2 urcrnrlon=lon.max(),llcrnrlat=lat.min(),urcrnrlat=lat.max(), / 3 resolution='c') 4 >>> m.drawcoastlines(linewidth=0.25) 5 <matplotlib.collections.LineCollection object at 0x1091c1f28> 6 >>> m.drawcountries(linewidth=0.25) 7 <matplotlib.collections.LineCollection object at 0x10621d0f0> 8 >>> m.fillcontinents(color='coral',lake_color='aqua') 9 >>> m.drawmapboundary(fill_color='aqua') 10 <matplotlib.patches.Rectangle object at 0x10918b3c8> 11 >>> m.drawmeridians(np.arange(0,360,30)) 12 >>> m.drawparallels(np.arange(-90,90,30))
1 >>> x, y = m(lon,lat)
1 >>> cs = m.contour(x,y,data,15,linewidths=1.5)
1 >>> plt.title('Geopotential Height Contour from Grib') 2 <matplotlib.text.Text object at 0x10918bda0> 3 >>> plt.show()