- 积分
- 25808
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2017-9-4
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 灭火器 于 2019-7-13 10:48 编辑
使用h5py包来读取GPM CSH的潜热数据,然后用matplotlib来绘制填色图。
麻烦的地方在于,如何在潜热数据正负大小不对称的前提下,让潜热为零与红蓝colormap中的白色相对应。这里参考了matplotlib官网给出的解决方法,定义一个MidpointNormalize类,把原本在[vmin, vmax]内线性的normalize调整为在[vmin, midpoint]和[midpoint, vmax]两个区间内分别线性归一化。参考资料如下:
[1] https://www.eorc.jaxa.jp/GPM/doc/program/guide/GPM_read_program_guide_forPython_V5.1.pdf
[2] https://matplotlib.org/users/colormapnorms.html#custom-normalization-two-linear-ranges
仍然需要改进的地方:实现任意剖面、叠加地形……
- import numpy as np
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import h5py
- # 读取经纬度和潜热.
- filename = "2B.GPM.DPRGMI.2HCSHv3-1.20170503-S070958-E084232.018057.V05A.HDF5"
- group = "Swath"
- with h5py.File(filename, "r") as infile:
- lat2d = np.array(infile[group+"/Latitude"])
- lon2d = np.array(infile[group+"/Longitude"])
- LH3d = np.array(infile[group+"/latentHeating"])
-
- nscan = LH3d.shape[0]
- nray = LH3d.shape[1]
- nlayer = LH3d.shape[2]
- # 选取所需的经纬度范围,还有想要画的ray.
- ray = int(nray/2) + 20
- lonmin = 105.0
- lonmax = 120.0
- latmin = 25.0
- latmax = 40.0
- # 根据官方文档设置对应的垂直高度.
- layerSize = 0.25 # Units is km.
- height = (np.arange(1, nlayer+1)-0.5)*layerSize
- # 利用条件判断索引所需的潜热截面数据.
- logi = np.logical_and.reduce((lat2d[:,ray] >= latmin, lat2d[:,ray] <= latmax, \
- lon2d[:,ray] >= lonmin, lon2d[:,ray] <= lonmax))
- lat = lat2d[logi,ray]
- LH = LH3d[logi,ray,:]
- print(LH.min(), LH.max())
- # 定义一个能保证midpoint在colorbar正中间的normalize类.
- class MidpointNormalize(mpl.colors.Normalize):
- def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
- self.midpoint = midpoint
- mpl.colors.Normalize.__init__(self, vmin, vmax, clip)
- def __call__(self, value, clip=None):
- x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
- return np.ma.masked_array(np.interp(value, x, y))
- # 做出潜热截面的填色图.
- fig = plt.figure(dpi=300, figsize=(10,4))
- ax = plt.subplot(111)
- bounds = [-5.0, -2.0, -1.0, -0.1, 0.1, 1.0, 2.0, 5.0, 10.0, 20.0, 30.0]
- norm = mpl.colors.BoundaryNorm(boundaries=bounds, ncolors=256)
- im = ax.contourf(lat, height, LH.T, levels=bounds, cmap="seismic", extend="both", \
- norm=MidpointNormalize(midpoint=0.0, vmin=min(bounds), vmax=max(bounds)))
- ax.axhline(y=4.0, color="k", linestyle="--")
- ax.set_xlabel("Latitude", fontsize=12)
- ax.set_ylabel("Altitude (km)", fontsize=12)
- ax.set_yticks(np.arange(25, step=5))
- ax.set_title("CSH Latent Heat (K/hr)", fontsize=15)
- cbar = fig.colorbar(im, ax=ax, ticks=bounds, orientation="horizontal", \
- shrink=0.7, pad=0.2)
- # plt.show()
- fig.savefig("CSH_plot_py")
复制代码
|
-
|