读取 FNL 再分析资料中 2021 年 1 月每个时次的地面 2m 温度的二维数组。对所有温度数据取整(int),然后计算这 4 个时次中,最常出现的温度(单位:K)的值。
串行写会很慢的哦!建议还是使用 DASK 并行来计算!
import dask.array as da
import xarray as xr
import numpy as np
import glob # 使用glob.glob,字符串中为包含了正则的字符串。返回所有文件夹或文件的字符串列表
@np.vectorize # 函数向量化,实现int函数对多维数组取整。
def int_array(x):
return int(x)
# 以列表形式返回结果,每个元素是字符串形式
a = glob.glob("/home/mw/input/fnl6384/fnl/fnl_202101*.grib2")
lena = len(a)
for i in range(lena):
# grib2数据读取,通过xarray库;a[i]是字符串形式
data_i = xr.open_dataset(a[i], engine="pynio")
tem_i = int_array(data_i['TMP_P0_L103_GLL0'][0, ])
b = np.stack((tem_i)).flatten()
b.shape # 一维,所以dask的chunks也要是一维分割
# DASK版本
dask_array_list = [da.from_array(b, chunks=(2000))]
# 查看分块情况
#dask_array_list[0]
value, count = np.unique(b,
return_index=False, # 如果为true,返回新列表元素在旧列表中第一次出现的index
return_inverse=False, # 如果为true,返回旧列表元素在新列表中的index
return_counts=True, # 如果为true,返回去重数组中的元素在原数组中的出现次数)
)
value, count = list(value), list(count)
a1 = value[count.index(max(count))] # 提取最大值,学习了上一节参考答案的代码。
a1