读取 FNL 再分析资料中 2021 年 1 月 1 日 0 时的数据。计算热带地区温度大于 10 摄氏度的区域的平均地表以上 2 米的露点温度(摄氏度,取整)。
import numpy as np
import xarray as xr
data=xr.open_dataset("/home/mw/input/fnl6384/fnl/fnl_20210101_00_00.grib2",engine="pynio") #grib2数据读取,通过xarray库
#rh_1 = data["RH_P0_L103_GLL0"].loc[23+26/60:-23-26/60,:]
#t_1 = data["TMP_P0_L103_GLL0"][0,...].loc[23+26/60:-23-26/60,:]
#t_1 #这个是参考答案的索引方式
t_1=data['TMP_P0_L103_GLL0'][0,][67:-67,] #南北纬24°之间(提示说是23.5°)
rh_1=data['RH_P0_L103_GLL0'][67:-67,]
t_1
#这是我的索引方法,索引的范围一样,维度索引也可以使用[66:-66,]结果不影响。之前由于索引的湿度变量错误导致计算结果不一样。经修正,计算结果一致。
t_2=np.array(t_1)
rh_2= np.array(rh_1)
t_3=t_2.reshape(16920,1) #调整为16920*1维度
rh_3=rh_2.reshape(16920,1)
import pandas as pd
t= pd.DataFrame(np.array(t_3)) #转化为dataframe格式,主要是为了使用后面的函数。本人还不太掌握自定义的 ufunc: numpy 的函数向量化。
rh= pd.DataFrame(np.array(rh_3))
#合并
df1=pd.concat([t,rh],axis=1)
df1
df1.to_csv('t_rh.csv') ## 将dataframe导出为csv文件
df = pd.read_csv("t_rh.csv") #再读取
df.columns = ['index','t','rh'] #重命名
import math
def ludian(t,rh): #参考答案的公式计算露点温度函数。
if t > 273.15+10:
GC=461.5
GCX=GC/ (1000*4.186)
LHV = (597.3-0.57* (t-273.15))/GCX
TDK = t*LHV/ (LHV-t*math.log(rh*0.01))
return TDK
else:
return np.nan
df['ludian'] = df.apply(lambda x : ludian(x['t'],x['rh']),axis=1)
df.describe()
a1=int(df.loc[:,"ludian"].mean()-273.15)
a1 #在.loc[23+26/60:-23-26/60,:]索引下,计算结果为19,在[66:-66,]索引下,计算结果为5摄氏度。
"""
def ludian(t,rh): #定义露点温度函数
if np.isnan(t) or np.isnan(rh):
return np.nan
else:
if t>=283.15:
return 237.7*(17.27*(t-273.15)/(237.7+(t-273.15)) + np.log( rh/100 ))/(17.27-(17.27*(t-273.15)/(237.7+(t-273.15)) + np.log( rh/100 )))
df['ludian'] = df.apply(lambda x : ludian(x['t'],x['rh']),axis=1)
df.describe()
a1=int(df.loc[:,"ludian"].mean())
a1
"""