基于Python的Grib數(shù)據(jù)可視化
作者:kallan
一、庫(kù)的安裝
(一)matplotlib安裝
-
matplotlib依賴
-
- nose
- numpy
- pyparsing
- python-dateutil
- cycler
- pkg-config
- freetype
- libpng
-
安裝過(guò)程
這里我都是通過(guò)源碼包安裝的,大家也可以再終端里通過(guò)pip install 命令來(lái)安裝
1、安裝nose
解壓縮后,進(jìn)入命令提示符 運(yùn)行
1 python3 setup.py install
2、安裝numpy
解壓縮后,進(jìn)入命令提示符 運(yùn)行
1 python3 setup.py install
3、安裝pyparsing
解壓縮后,進(jìn)入命令提示符 運(yùn)行
1 python3 setup.py install
4、安裝python-dateutil
解壓縮后,進(jìn)入命令提示符 運(yùn)行
1 python3 setup.py install
5、安裝cycler
解壓縮后,進(jìn)入命令提示符 運(yùn)行
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
解壓縮后,進(jìn)入命令提示符 運(yùn)行
1 python3 setup.py install
(二)basemap安裝
-
basemap依賴
-
- geos
- pyproj
-
安裝過(guò)程
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安裝
-
pygrib依賴
-
- Jasper
- GRIB API
- numpy
- pyproj
-
安裝過(guò)程
由于之前已經(jīng)安裝了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之前首先要根據(jù)自己的實(shí)際情況修改文件目錄下的setup.cfg文件,最主要的就是修改grib_api_dir和jasper_dir,這兩個(gè)是剛剛安裝的Jasper和GRIB API的路徑,如果這兩個(gè)地址不正確安裝會(huì)報(bào)錯(cuò)
?修改好就可以正常安裝了
1 python3 setup.py install
二、grib數(shù)據(jù)讀取
雖然我做的東西和氣象沾邊,但是我本身并不是氣象專業(yè)出身,所有這些東西都是我慢慢研究琢磨出來(lái)的,所以有些方面可能講的比較外行,有不對(duì)的地方歡迎大家留言指正。
????(一)導(dǎo)入pygrib模塊
1 >>> import pygrib
????(二)打開Grib文件
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
? ? ? ?信息解讀
1?:數(shù)據(jù)列表的行號(hào),有的文件可能包括多個(gè)數(shù)據(jù)
Geopotential?Height:數(shù)據(jù)的名稱
gpm?(instant):數(shù)據(jù)的單位
regular_ll:常規(guī)數(shù)據(jù),其實(shí)這個(gè)字段我也不清楚
isobaricInhPa:這個(gè)字段表示的是數(shù)據(jù)屬性,此處表示是以hPa為單位的等壓面
level?500:這個(gè)字段表示的是高度層
fcst time?24?:預(yù)報(bào)時(shí)效
from?201507081200 :起報(bào)時(shí)間
????綜合上面的信息可以得出,這個(gè)文件是從2015年7月8日12時(shí)開始的24小時(shí)后500hPa等壓面高度場(chǎng)數(shù)據(jù)
(四)導(dǎo)出文件數(shù)據(jù)
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. ]]
三、grib數(shù)據(jù)可視化
(一)導(dǎo)入需要的模塊
1 >>> import matplotlib.pyplot as plt 2 >>> from mpl_toolkits.basemap import Basemap 3 >>> import numpy as np
(二)創(chuàng)建一個(gè)figure
1 >>> plt.figure() 2 <matplotlib.figure.Figure object at 0x107e65198>
(三)創(chuàng)建一個(gè)basemap實(shí)例
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))
(四)將lat,lon的數(shù)據(jù)格式轉(zhuǎn)換成投影需要的格式存入x,y
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()
(七)圖像展示
End.
轉(zhuǎn)載請(qǐng)注明來(lái)自36大數(shù)據(jù)(36dsj.com): 36大數(shù)據(jù) ? 基于Python的Grib數(shù)據(jù)可視化