You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

温度x、y方向偏导数(水平平流)计算方法正确性咨询

温度水平平流计算方法准确性对比

我想明确计算温度在x、y方向的偏导数(进而得到温度水平平流)的最准确方法,以下是两段Python实现代码,第二段采用全层的温度、纬向风(u)、经向风(v)数据矩阵进行计算,请判断哪种方法更准确:

第一段代码

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import metpy
import metpy.calc as mpcalc
from metpy.units import units

# 读取GRIB数据文件
ds = xr.open_dataset('cdas1_2022011700_.t00z.pgrbh00.grib2', engine='cfgrib', backend_kwargs={
                     'filter_by_keys': {'typeOfLevel': 'isobaricInhPa'}})
ds = ds.metpy.parse_cf()

# 筛选775hPa层数据
u_775hPa = ds.u.sel(isobaricInhPa=775)
v_775hPa = ds.v.sel(isobaricInhPa=775)
t_775hPa = ds.t.sel(isobaricInhPa=775)

# 计算温度在x、y方向的偏导数(温度水平平流)
# 初始化dTdX为0矩阵
dT_dx = np.zeros_like(t_775hPa.values)
dT_dy = np.zeros_like(t_775hPa.values)
# 使用numpy.gradient计算空间导数的有限差分
dT_dx, dT_dy = np.gradient(t_775hPa)

# 计算风与温度水平梯度的乘积(Tadv=-(u750*dTdX+v750*dTdY))
gradiente_horizontal = -u_775hPa * dT_dx - v_775hPa * dT_dy

# 创建绘图窗口和子图
fig = plt.figure(figsize=(16, 14))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())

# 添加地理特征
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.STATES)

# 添加经纬网格线
ax.gridlines(draw_labels=True, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')

# 设置地图属性
central_lon, central_lat = -33, -55
extent = [-75, -45, -40, -16]
ax.set_extent(extent, crs=ccrs.PlateCarree())
ax.set_title(
    "horizontal advection of temperature", fontsize=16, fontweight='bold')

# 绘制水平平流场填色图
contourf = ax.contourf(ds['longitude'], ds['latitude'], gradiente_horizontal,
                           cmap= 'rainbow' , transform=ccrs.PlateCarree())

# 添加颜色条
cbar_temp = plt.colorbar(
    contourf, ax=ax, orientation='horizontal',aspect=40, pad=0.02)
cbar_temp.set_label('horizontal advection of temperature ', size=14)

# 显示图像
plt.show()

第二段代码

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import metpy
import metpy.calc as mpcalc
from metpy.units import units

# 读取GRIB数据文件
ds = xr.open_dataset('cdas1_2022011700_.t00z.pgrbh00.grib2', engine='cfgrib', backend_kwargs={
                     'filter_by_keys': {'typeOfLevel': 'isobaricInhPa'}})
ds = ds.metpy.parse_cf()
# 使用温度、纬向风和经向风的数据矩阵计算温度平流
# 提取温度(T)、纬向风分量(u)和经向风分量(v)数据
T = ds.t
u = ds.u
v = ds.v

# 计算温度在x、y方向的偏导数
dT_dx, dT_dy = np.gradient(T, axis=(1,2))

# 计算温度平流
advection_of_temperature = -u * dT_dx - v * dT_dy

# 获取775hPa层的温度平流数据
advection_of_temperature = advection_of_temperature.sel(isobaricInhPa=775)
# 创建绘图窗口和子图
fig = plt.figure(figsize=(16, 14))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())

# 添加地理特征
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.STATES)

# 添加经纬网格线
ax.gridlines(draw_labels=True, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')

# 设置地图属性
central_lon, central_lat = -33, -55
extent = [-75, -45, -40, -16]
ax.set_extent(extent, crs=ccrs.PlateCarree())
ax.set_title(
    "horizontal advection of temperature", fontsize=16, fontweight='bold')

# 绘制水平平流场填色图
contourf = ax.contourf(ds['longitude'], ds['latitude'], advection_of_temperature,
                           cmap= 'rainbow' , transform=ccrs.PlateCarree())

# 添加颜色条
cbar_temp = plt.colorbar(
    contourf, ax=ax, orientation='horizontal',aspect=40, pad=0.02)
cbar_temp.set_label('horizontal advection of temperature ', size=14)

# 显示图像
plt.show()

准确性分析与结论

  • 第一段代码逻辑正确:它先筛选出目标层次(775hPa)的温度、u、v数据,再对单一层的水平维度(经度、纬度)使用np.gradient计算偏导数,完全符合温度水平平流的物理定义——水平平流是某一层次上,风场输送温度的效应,仅针对水平空间维度计算梯度。
  • 第二段代码存在核心错误:np.gradient(T, axis=(1,2))的参数设置错误。GRIB数据中温度场ds.t的维度通常为[time, isobaricInhPa, latitude, longitude],这里axis=(1,2)实际是对垂直层次(isobaricInhPa)和纬度求梯度,而非水平方向的经度(x)和纬度(y)。这会导致计算出的dT_dx是垂直方向的温度梯度,完全偏离了水平平流所需的水平梯度,结果毫无物理意义。

综上,第一段代码的计算方法更准确。

内容的提问来源于stack exchange,提问作者leojim19

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 18:05:57