日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

grib1文件解析 python_基于Python的Grib数据可视化

發布時間:2024/3/24 python 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 grib1文件解析 python_基于Python的Grib数据可视化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

利用Python語言實現Grib數據可視化主要依靠三個庫——pygrib、numpy和matplotlib。pygrib是歐洲中期天氣預報中心(ECMWF)的GRIG API C庫的Python接口,通過這個庫可以將Grib數據讀取出來;numpy是Python的一種開源的數值計算擴展,這種工具可用來存儲和處理大型矩陣;matplotlib是python著名的繪圖庫,它提供了一整套和matlab相似的命令API,十分適合交互式地進行制圖;在數據可視化過程中,我們常需要將數據在地圖上畫出來,所以還需要matplotlib的一個子包basemap,負責地圖繪制。

一、庫的安裝

matplotlib依賴

安裝過程

這里我都是通過源碼包安裝的,大家也可以再終端里通過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-glib2 make && date

3 sudo make install && date

7、安裝freetype

1 ./configure2 make && date

3 sudo make install && date

8、安裝libpng

1 ./configure2 make && date

3 sudo make install && date

9、安裝matplotlib-1.5.0

解壓縮后,進入命令提示符 運行

1 python3 setup.py install

basemap依賴

安裝過程

1、安裝GEOS

1 ./configure2 make && date

3 sudo make install && date

2、安裝pyproj

1 python3 setup.py install

3、安裝basemap

1 python3 setup.py install

(三)pygrib安裝

pygrib依賴

安裝過程

由于之前已經安裝了numpy和pyproj,這里只需安裝Jasper和GRIB API即可安裝pygrib

1、安裝Jasper

1 ./configure2 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/local12 #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/local17 #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 = /usr27 #if grib_api was built with png support,

28 #uncomment and set to zlib install location.

29 zlib_dir = /usr30 #install man pages for command line utilities here

31 #man_dir = /usr/local/man

View Code

修改好就可以正常安裝了

1 python3 setup.py install

二、grib數據讀取

雖然我做的東西和氣象沾邊,但是我本身并不是氣象專業出身,所有這些東西都是我慢慢研究琢磨出來的,所以有些方面可能講的比較外行,有不對的地方歡迎大家留言指正。

(一)導入pygrib模塊

1 >>> import pygrib

(二)打開Grib文件

1 >>> grbs = pygrib.open('/Users/Kallan/Documents/data/echhae50.082')

(三)提取文件信息

1 >>>grbs.seek(0)2 >>> for grb ingrbs:3 grb4 1:Geopotential Height:gpm (instant):regular_ll:isobaricInhPa:level 500:fcst time 24 :from 201507081200

信息解讀

1 :數據列表的行號,有的文件可能包括多個數據

GeopotentialHeight:數據的名稱

gpm (instant):數據的單位

regular_ll:常規數據,其實這個字段我也不清楚

isobaricInhPa:這個字段表示的是數據屬性,此處表示是以hPa為單位的等壓面

level 500:這個字段表示的是高度層

fcst time 24:預報時效

from201507081200 :起報時間

綜合上面的信息可以得出,這個文件是從2015年7月8日12時開始的24小時后500hPa等壓面高度場數據

(四)導出文件數據

1 >>> grb = grbs.select(name='Geopotential Height')[0]2 >>> data =grb.values3 >>> 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.50. ]15 [-90. -87.5 -85. ..., -5. -2.50. ]16 [-90. -87.5 -85. ..., -5. -2.50. ]17 ...,18 [-90. -87.5 -85. ..., -5. -2.50. ]19 [-90. -87.5 -85. ..., -5. -2.50. ]20 [-90. -87.5 -85. ..., -5. -2.5 0. ]]

三、grib數據可視化

(一)導入需要的模塊

1 >>> importmatplotlib.pyplot as plt2 >>> from mpl_toolkits.basemap importBasemap3 >>> import numpy as np

(二)創建一個figure

1 >>>plt.figure()2

(三)創建一個basemap實例

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

6 >>> m.drawcountries(linewidth=0.25)7

8 >>> m.fillcontinents(color='coral',lake_color='aqua')9 >>> m.drawmapboundary(fill_color='aqua')10

11 >>> m.drawmeridians(np.arange(0,360,30))12 >>> m.drawparallels(np.arange(-90,90,30))

(四)將lat,lon的數據格式轉換成投影需要的格式存入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

3 >>> plt.show()

(七)圖像展示

本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。

想觀看Matplotlib教學視頻,了解更多Matplotlib實用技巧可關注

微信公眾賬號:?MatplotlibClass

今日頭條號:Matplotlib小講堂

總結

以上是生活随笔為你收集整理的grib1文件解析 python_基于Python的Grib数据可视化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。