Python 在氣象上的應用
為什么選擇python
- 免費和開源,沒有商業許可限制
anaconda
pycharm
jupyter - 龐大而穩定的社區
stackoverflow - 功能齊全的編程語言,真正面向對象
- 非常強大和靈活 (https://www.lfd.uci.edu/~gohlke/pythonlibs/)
- 喜歡可讀的代碼
- 出版質量圖繪制
- 輕松讀/寫netcdf和grib數據
- 輕松使用Fortran / C / C ++
- 廣泛的庫支持數字和非數字工作
科學計算
1.Numpy
Numpy是python科學計算的基礎包,它提供以下功能(不限於此):
(1)快速高效的多維數組對象ndarray
(2)用於對數組執行元素級計算以及直接對數組執行數學運算的函數
(3)用於讀寫硬盤上基於數組的數據集的工具
(4)線性代數運算、傅里葉變換,以及隨機數生成
(5)用於將C、C++、Fortran代碼集成到python的工具
2.pandas
pandas提供了使我們能夠快速便捷地處理結構化數據的大量數據結構和函數。pandas兼具Numpy高性能的數組計算功能以及電子表格和關系型數據(如SQL)靈活的數據處理能力。它提供了復雜精細的索引功能,以便更為便捷地完成重塑、切片和切塊、聚合以及選取數據子集等操作。
對於金融行業的用戶,pandas提供了大量適用於金融數據的高性能時間序列功能和工具。
DataFrame是pandas的一個對象,它是一個面向列的二維表結構,且含有行標和列標。
ps.引用一段網上的話說明DataFrame的強大之處:
Excel 2007及其以后的版本的最大行數是1048576,最大列數是16384,超過這個規模的數據Excel就會彈出個框框“此文本包含多行文本,無法放置在一個工作表中”。Pandas處理上千萬的數據是易如反掌的事情,同時隨后我們也將看到它比SQL有更強的表達能力,可以做很多復雜的操作,要寫的code也更少。 說了一大堆它的好處,要實際感觸還得動手碼代碼。
3.Scipy
一組專門解決科學計算中各種標准問題域的包的集合。
scipy/climpy
4.statsmodels
一個Python模塊,它提供對許多不同統計模型估計的類和函數,並且可以進行統計測試和統計數據的探索
5.RPy
An interface to R running embedded in a Python process
- sympy
A Python library for symbolic mathematics
7.atmqty
A Python Package for Calculating Atmospheric Quantities
8.PyWavelets
A Python wavelet transforms module
數據處理
Read/Write NetCDF file
To create a NetCDF file:
from Scientific.IO.NetCDF import NetCDFFile import numpy as np f = NetCDFFile('scientificio.nc', 'w') # dimension f.createDimension('time', 12) # variable time = f.createVariable('time', 'd', ('time',)) # data time[:] = np.random.uniform(size=12) f.close()
To read the file:
from Scientific.IO.NetCDF import NetCDFFile import matplotlib.pyplot as plt f = NetCDFFile('scientificio.nc') fig = plt.figure() ax = fig.add_subplot(111) ax.plot(f.variables['time']) plt.show()
Read/Write NetCDF file with netcdf4-python
To create a NetCDF file:
from netCDF4 import Dataset import numpy as np root_grp = Dataset('test.nc', 'w', format='NETCDF4') root_grp.description = 'Example temperature data' # dimensions root_grp.createDimension('time', None) root_grp.createDimension('lat', 72) root_grp.createDimension('lon', 144) # variables times = root_grp.createVariable('time', 'f8', ('time',)) latitudes = root_grp.createVariable('latitude', 'f4', ('lat',)) longitudes = root_grp.createVariable('longitude', 'f4', ('lon',)) temp = root_grp.createVariable('temp', 'f4', ('time', 'lat', 'lon',)) # data lats = np.arange(-90, 90, 2.5) lons = np.arange(-180, 180, 2.5) latitudes[:] = lats longitudes[:] = lons for i in range(5): temp[i,:,:] = np.random.uniform(size=(len(lats), len(lons))) # group # my_grp = root_grp.createGroup('my_group') root_grp.close()
To read the file:
from netCDF4 import Dataset import pylab as pl root_grp = Dataset('test.nc') temp = root_grp.variables['temp'] for i in range(len(temp)): pl.clf() pl.contourf(temp[i]) pl.show() raw_input('Press enter.')
Read/Write Grib files with pygrib
To read a Grib file:
import pygrib grbs = pygrib.open('sampledata/flux.grb') grbs.seek(2) grbs.tell() grb = grbs.read(1)[0] print grb grb = grbs.select(name='Maximum temperature')[0]
To write a Grib file:
import pygrib grbout = open('test.grb','wb') grbout.write(msg) grbout.close() print pygrib.open('test.grb').readline()
Read/Write Matlab .mat file
To read a .mat file:
import scipy.io as sio mat_contents = sio.loadmat('data.mat') print mat_contents
To write a .mat file:
import numpy as np import scipy.io as sio vect = np.arange(10) print vect.shape sio.savemat('data.mat', {'vect':vect})
for hdf5
f = h5py.File('foo.hdf5','w')
其他:
繪圖


基礎繪圖類



氣象常用類
4)cis
衛星
其他繪圖工具
色標 : https://matplotlib.org/cmocean/
http://colorcet.pyviz.org/
http://holoviews.org/user_guide/Colormaps.html
爬蟲
機器學習
- 10 Useful Python Data Visualization Libraries for Any Discipline
- Python Data Visualization: Comparing 7 tools
- How to make beautiful data visualization in Python with matplotlib
- Dashboard API in Python
- Bokeh Applications
- pyecharts + notebook,真的不需要PPT了耶
- Pycon 2017: Python可視化庫大全
- A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences
- An Introduction to Using Python in the Atmospheric and Oceanic Sciences
- PyAOS
- Ninth Symposium on Advances in Modeling and Analysis Using Python