安裝與基本指令
Table of contents
背景
有關wrf-python
- wrf-python顧名思義就是處理wrf相關檔案的python程式庫,包括讀寫、分析、以及繪圖等等。
- wrf-python是NCAR下的開放源專案,有14位作者聯合的貢獻。目前版本為1.3.3.。
- wrf-python也是NCL停止繼續發展後轉接到python的幾個專案之一,除了wrf-python,NCAR同步也持續發展PyNGL、VAPOR等等。
- 這也意味wrf-python並不是發展完全的系統,有待持續關注、也表示如果有必要,還是必須自己使用matplotlib寫繪圖套件。
- 程式碼:wrf-python
- 說明、範例:官網
程式庫安裝
conda安裝
- 見官網-安裝
- 如未安裝PyNGL and/or PyNIO,建議先予安裝,將環境切換到pyn_env後再裝wrf-python。
conda install -c conda-forge wrf-python
openMP重新編譯
- 如果資料分析時能夠啟動電腦的所有核心,那是再好不過了
git clone https://github.com/NCAR/wrf-python
cd ./fortran/build_help
gfortran -o sizes -fopenmp omp_sizes.f90
python sub_sizes.py
cd ..
gfortran -E ompgen.F90 -fopenmp -cpp -o omp.f90
f2py *.f90 -m _wrffortran -h wrffortran.pyf --overwrite-signature
cd ..
python setup.py clean --all
python setup.py config_fc --f90flags="-mtune=generic -fopenmp" build_ext --libraries="gomp" build
pip install .
其他相依軟件
- PyNGL and/or PyNIO
conda create --name pyn_env --channel conda-forge pynio pyngl
- matplotlib
pip install matplotlib
- cartopy:
brew install proj geos
(macOS)pip3 install --upgrade pyshp
pip3 install shapely --no-binary shapely
brew install pkg-config
export PKG_CONFIG_PATH=/usr/local/bin/pkgconfig
conda install -c conda-forge cartopy
- Proj
conda install -c conda-forge proj
- or install from source code(centos)
kuang@centos8 ~/MyPrograms/PROJ/build
$ cat cmake.kng
cmake -DTIFF_INCLUDE_DIR=/opt/anaconda3/include \
-DTIFF_LIBRARY_RELEASE=/opt/anaconda3/lib/libtiff.so \
-DSQLITE3_INCLUDE_DIR=/opt/anaconda3/envs/py37/include \
-DSQLITE3_LIBRARY=/opt/anaconda3/envs/py37/lib/sqlite3.30.1.2/libsqlite3.30.1.2.so ..
cmake --build .
變數定義
- wrf-python最強項的功能除了繪圖之外,就屬getvar函數及其內插程式。
- 引數:
(wrfin, varname, timeidx=0, method='cat', squeeze=True, cache=None, meta=True, **kwargs)
wrfin
: wrf檔案名稱varnam
: wrf-python使用的變數名稱,不是wrfout檔案的變數名稱。為區別二者,前者用小寫,後者在ncf檔案中是大寫。timeidx
: 時間序,0~nt-1method
: 時間軸是否壓縮。'cat'
:保持時間軸(內設);'join'
:聯合(壓縮)時間軸squeeze
: 如維度僅有一層在輸出矩陣的shape中是否予以去除?內設為True
cache
: 將程式中反覆使用的變數暫存成矩陣名稱(varnam:ndarray
),以避免反覆讀取增加執行效,內設為None
。meta
:Additional Keyword Arguments
: 每個變數不太相同,詳下列表格。- 如果只有一種單位,就不能使用
units
選項。
- 如果只有一種單位,就不能使用
- 輸出
getvar
會按照新變數定義的意義輸出成帶座標系統(meta
)的矩陣。
- 原表格按照字母順序排列,以下按照變數類別分類。
Dimensions and Terran
Variable Name | Description | Available Units | Additional Keyword Arguments |
---|---|---|---|
lat | Latitude | decimal degrees | |
lon | Longitude | decimal degrees | |
ter | Model Terrain Height | m km dm ft mi | units (str) : Set to desired units. Default is ‘m’. |
times | Times in the File or Sequence | ||
xtimes | XTIME Coordinate (if applicable) | minutes since start of model run |
Height
Variable Name | Description | Available Units | Additional Keyword Arguments |
---|---|---|---|
z/height | Model Height for Mass Grid | m km dm ft mi | msl (boolean): Set to False to return AGL values. True is for MSL. Default is True. units (str) : Set to desired units. Default is ‘m’. |
height_agl | Model Height for Mass Grid (AGL) | m km dm ft mi | units (str) : Set to desired units. Default is ‘m’. |
zstag | Model Height for Vertically Staggered Grid | m km dm ft mi | msl (boolean): Set to False to return AGL values. True is for MSL. Default is True. units (str) : Set to desired units. Default is ‘m’. |
Temperatures
Variable Name | Description | Available Units | Additional Keyword Arguments |
---|---|---|---|
eth/theta_e | Equivalent Potential Temperature | K degC degF | units (str) : Set to desired units. Default is ‘K’. |
ctt | Cloud Top Temperature | degC K degF | fill_nocloud (boolean): Set to True to use fill values for cloud free regions rather than surface temperature. Default is False. missing (float): The fill value to use when fill_nocloud is True. opt_thresh (float): The optical depth required to trigger the cloud top temperature calculation. Default is 1.0. units (str) : Set to desired units. Default is ‘degC’. |
T2 | 2m Temperature | K | |
td2 | 2m Dew Point Temperature | degC K degF | units (str) : Set to desired units. Default is ‘degC’. |
td | Dew Point Temperature | degC K degF | units (str) : Set to desired units. Default is ‘degC’. |
tc | Temperature in Celsius | degC | |
th/theta | Potential Temperature | K degC degF | units (str) : Set to desired units. Default is ‘K’. |
temp | Temperature (in specified units) | K degC degF | units (str) : Set to desired units. Default is ‘K’. |
tk | Temperature in Kelvin | K | |
tv | Virtual Temperature | K degC degF | units (str) : Set to desired units. Default is ‘K’. |
twb | Wet Bulb Temperature | K degC degF | units (str) : Set to desired units. Default is ‘K’. |
Pressures
Variable Name | Description | Available Units | Additional Keyword Arguments |
---|---|---|---|
p/pres | Full Model Pressure (in specified units) | Pa hPa mb torr mmhg atm | units (str) : Set to desired units. Default is ‘Pa’. |
pressure | Full Model Pressure (hPa) | hPa | |
slp | Sea Level Pressure | hPa hPa mb torr mmhg atm | units (str) : Set to desired units. Default is ‘hPa’. |
Velocities
Variable Name | Description | Available Units | Additional Keyword Arguments |
---|---|---|---|
omg/omega | Omega(wiki, earthscience) | Pa s-1 | |
ua | U-component of Wind on Mass Points | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
va | V-component of Wind on Mass Points | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
wa | W-component of Wind on Mass Points | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
uvmet10 | 10 m U and V Components of Wind Rotated to Earth Coordinates | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
uvmet | U and V Components of Wind Rotated to Earth Coordinates | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
wspd_wdir | Wind Speed and Direction (wind_from_direction) in Grid Coordinates | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
wspd_wdir10 | 10m Wind Speed and Direction (wind_from_direction) in Grid Coordinates | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
uvmet_wspd_wdir | Wind Speed and Direction (wind_from_direction) Rotated to Earth Coordinates | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
uvmet10_wspd_wdir | 10m Wind Speed and Direction (wind_from_direction) Rotated to Earth Coordinates | m s-1 km h-1 mi h-1 kt ft s-1 | units (str) : Set to desired units. Default is ‘m s-1’. |
Energy
Variable Name | Description | Available Units | Additional Keyword Arguments |
---|---|---|---|
cape_2d | 2D CAPE, Convective Available Potential Energy, Convective Inhibition, Lifted condensation level, level of free convection (MCAPE/MCIN/LCL/LFC) | J kg-1 ; J kg-1 ; m ; m | missing (float): Fill value for output only |
cape_3d | 3D CAPE and CIN (Convective INhibition, wiki) | J kg-1 | missing (float): Fill value for output only |
geopt/geopotential | Geopotential for the Mass Grid | m2 s-2 | |
geopt_stag | Geopotential for the Vertically Staggered Grid | m2 s-2 |
High Order Dynamics
Variable Name | Description | Available Units | Additional Keyword Arguments |
---|---|---|---|
avo | Absolute Vorticity | 10-5 s-1 | |
pvo | Potential Vorticity | PVU | |
helicity | Storm Relative Helicity(see ref.) | m2 s-2 | top (float): The top level for the calculation in meters. Default is 3000.0. |
updraft_helicity | Updraft Helicity | m2 s-2 | bottom (float): The bottom level for the calculation in meters. Default is 2000.0. top (float): The top level for the calculation in meters. Default is 5000.0. |
Vapors, Cloudness and Precipitation
Variable Name | Description | Available Units | Additional Keyword Arguments |
---|---|---|---|
pw | Precipitable Water | kg m-2 | |
rh | Relative Humidity | % | |
rh2 | 2m Relative Humidity | % | |
cloudfrac | Cloud Fraction | % | vert_type (str): The vertical coordinate type for the cloud thresholds. Must be ‘height_agl’, ‘height_msl’, or ‘pres’. Default is ‘height_agl’. low_thresh (float): The low cloud threshold (meters for ‘height_agl’ and ‘height_msl’, pascals for ‘pres’). Default is 300 m (97000 Pa) mid_thresh (float): The mid cloud threshold (meters for ‘height_agl’ and ‘height_msl’, pascals for ‘pres’). Default is 2000 m (80000 Pa) high_thresh (float): The high cloud threshold (meters for ‘height_agl’ and ‘height_msl’, pascals for ‘pres’). Default is 6000 m (45000 Pa) |
dbz | Reflectivity | dBZ | do_variant (boolean): Set to True to enable variant calculation. Default is False. do_liqskin (boolean): Set to True to enable liquid skin calculation. Default is False. |
mdbz | Maximum Reflectivity | dBZ | do_variant (boolean): Set to True to enable variant calculation. Default is False. do_liqskin (boolean): Set to True to enable liquid skin calculation. Default is False. |
色標
- wrf-python適用所有matplotlib選項
所有選項
- cmap選項常用有:”jet”、”rainbow”等
- 分為7大類
- plot_color_gradients(‘Perceptually Uniform Sequential’, [‘viridis’, ‘plasma’, ‘inferno’, ‘magma’, ‘cividis’])
- plot_color_gradients(‘Sequential’, [‘Greys’, ‘Purples’, ‘Blues’, ‘Greens’, ‘Oranges’, ‘Reds’, ‘YlOrBr’, ‘YlOrRd’, ‘OrRd’, ‘PuRd’, ‘RdPu’, ‘BuPu’, ‘GnBu’, ‘PuBu’, ‘YlGnBu’, ‘PuBuGn’, ‘BuGn’, ‘YlGn’])
- plot_color_gradients(‘Sequential (2)’, [‘binary’, ‘gist_yarg’, ‘gist_gray’, ‘gray’, ‘bone’, ‘pink’, ‘spring’, ‘summer’, ‘autumn’, ‘winter’, ‘cool’, ‘Wistia’, ‘hot’, ‘afmhot’, ‘gist_heat’, ‘copper’])
- plot_color_gradients(‘Diverging’, [‘PiYG’, ‘PRGn’, ‘BrBG’, ‘PuOr’, ‘RdGy’, ‘RdBu’, ‘RdYlBu’, ‘RdYlGn’, ‘Spectral’, ‘coolwarm’, ‘bwr’, ‘seismic’])
- plot_color_gradients(‘Cyclic’, [‘twilight’, ‘twilight_shifted’, ‘hsv’])
- plot_color_gradients(‘Qualitative’, [‘Pastel1’, ‘Pastel2’, ‘Paired’, ‘Accent’, ‘Dark2’, ‘Set1’, ‘Set2’, ‘Set3’, ‘tab10’, ‘tab20’, ‘tab20b’, ‘tab20c’])
- plot_color_gradients(‘Miscellaneous’, [‘flag’, ‘prism’, ‘ocean’, ‘gist_earth’, ‘terrain’, ‘gist_stern’, ‘gnuplot’, ‘gnuplot2’, ‘CMRmap’, ‘cubehelix’, ‘brg’, ‘gist_rainbow’, ‘rainbow’, ‘jet’, ‘turbo’, ‘nipy_spectral’, ‘gist_ncar’])
- 反轉色標:XXX_r。
- 如rainbow -> rainbow_r
選擇考量
- 性質迥異之整數純量,如土地使用、行政區別等,可以考慮使用Qualitative。
- 即使為連續場,如數量差異很大,如dB、或有標準上下考量,亦可以嘗試Qualitative。
- Sequential雙色漸層色彩較為不純,可以考慮用 Sequential (2) 的四季
- Cyclic容易誤解,慎重使用
- 如果圖面太偏低值或高值,不必重選顏色,而用不等間距的interval來取代等間距
- color list的使用,詳見公版模式後製工具等值圖的問題
Reference
- Gene Z. Ragen, discussion on Plotting wind vectors on vertical cross-section with matplotlib 2019, Oct. 8.