"""
nlayer 80 Number of layers at the fixed heights of 0.00-0.25 km, 0.25-0.50 km, ..., 19.50-19.75 km, and 19.75-20.00 km.
"""
from pathlib import Path
from collections import namedtuple
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.cm as cm
import matplotlib.colors as mcolors
import cmaps
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import xarray as xr
# 上一篇有介绍不做赘述
Point = namedtuple('Point', ['x', 'y'])
Pair = namedtuple('Pair', ['start', 'end'])
def region_mask(lon, lat, extents):
'''返回用于索引经纬度方框内数据的Boolean数组.'''
lonmin, lonmax, latmin, latmax = extents
return (
(lon >= lonmin) & (lon <= lonmax) &
(lat >= latmin) & (lat <= latmax)
)
def make_LH_cmap(levels):
'''制作潜热的colormap.'''
levels = np.asarray(levels)
cmap = cmaps.BlueWhiteOrangeRed # 可以调用NCL的颜色,想要什么颜色就 .+颜色名进行调用
nbin = len(levels) - 1 # 两个数字之间分配一个颜色
n_negative = np.count_nonzero(levels < 0) # 不同数据大小进行分配
n_positive = np.count_nonzero(levels > 0)
colors = np.vstack([
cmap(np.linspace(0, 0.5, n_negative)[:-1]),
cmap(np.linspace(0.5, 1, n_positive))
])
cmap = mcolors.ListedColormap(colors) # 将colors全部选择,可选择截断一部分
norm = mcolors.BoundaryNorm(levels, nbin) # levels需要单调递增,bin表示映射颜色的数量
return cmap, norm
def make_Rr_cmap(levels):
'''制作降水的colormap.'''
nbin = len(levels) - 1
cmap = cm.get_cmap('jet', nbin)
norm = mcolors.BoundaryNorm(levels, nbin)
return cmap, norm
def get_line_label_pos(pair, space=0.5):
'''根据两点连线计算起点旁边标签的位置.'''
x0, y0 = pair.start
x1, y1 = pair.end
length = np.hypot(x1 - x0, y1 - y0)
r = space / length
x = (1 + r) * x0 - r * x1
y = (1 + r) * y0 - r * y1
return Point(x, y)
class CrossSectionGPM:
'''
对GPM二级产品求剖面的类.
Python绘制GPM二级数据降水剖面图
于 2023-04-02 20:36:32 首次发布