python 如何利用Metpy计算水汽通量散度
python通过metpy库,比湿数据计算水汽通量散度
·
MetPy是一个Python工具集合库,用于阅读、可视化和执行天气数据的计算。
本文介绍如何在python中通过metpy计算水汽通量散度。
数据:
- 1、比湿数据
- 2、风速分量U
- 3、风速分量V
主要函数:
- 1、metpy.calc.divergence(u, v, *, dx=None, dy=None, x_dim=- 1, y_dim=- 2)
计算矢量的水平散度。 - 2、metpy.calc.lat_lon_grid_deltas(longitude, latitude, x_dim=- 1, y_dim=- 2, geod=None)
计算纬度/经度格式的网格点之间的实际增量。
注意点
- 1、使用merpy计算过程中需要带上单位
- 2、最好先选取部分区域进行测试,然后再对全球进行循环
- 3、注意及时保存数据,避免浪费时间
- 4、数据的分辨率保持一致
代码:
# -*- coding: utf-8 -*-
"""
introduction : keep learning althongh walk slowly
"""
# # # import package
import xarray as xr
import numpy as np
from metpy import units
import metpy.constants as constants
import metpy.calc as mpcalc
# # # read data
p1=r'D:/sph.nc'
p2=r'D:/u.nc'
p3=r'D:/v.nc'
f_u = xr.open_dataset(p2)
f_v = xr.open_dataset(p3)
f_q = xr.open_dataset(p1)
# # # read variables
u = f_u.u
v = f_v.v
q = f_q.q
lat = q.lat
lon = q.lon
lev = q.level
time=q.time
# # #
qv_u = u*q/constants.g
qv_v = v*q/constants.g
# # #
dx, dy = mpcalc.lat_lon_grid_deltas(lon, lat)
div_qv = np.zeros((q.time.shape[0],lev.shape[0],lat.shape[0],lon.shape[0]))
for j in range(q.time.shape[0]):
print(j)
for i in range(lev.shape[0]):
div_qv[j,i] = mpcalc.divergence(u = qv_u[j,i],v = qv_v[j,i],dx = dx ,dy = dy)
# # # 计算整层水汽通量散度
total_div_qv = np.trapz(div_qv[::-1],lev[::-1],axis=1)
# # # save data
div_qv_nc = xr.Dataset(
{
"mfd":(("time",'level',"lat","lon"), div_qv)
},
coords={
'level':lev,
"time":time,
"lat":lat,
"lon":lon,
}
)
div_qv_nc.attrs["long_name"] = "div-qv"
div_qv_nc.to_netcdf("qiv-qv.nc")
total_qv_nc = xr.Dataset(
{
"total_mfd":(("time","lat","lon"), total_div_qv)
},
coords={
"time":time,
"lat":lat,
"lon":lon,
}
)
total_qv_nc.attrs["long_name"] = "total-div-qv"
total_qv_nc.to_netcdf("total-qiv-qv.nc")
一个努力学习python的海洋
水平有限,欢迎指正!!!
欢迎评论、收藏、点赞、转发、关注。
关注我不后悔,记录学习进步的过程~~
更多推荐
已为社区贡献16条内容
所有评论(0)