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

如何计算并可视化不同时段格点降水数据的差值?

CRU降水时段差异计算与可视化解决方案

一、先解决数据维度对齐问题

直接对绘图对象做减法是错误的,必须先处理原始格点数据集,确保两个数据的空间(经纬度)、时间维度完全匹配:

  • 使用xarray的xr.align()方法强制对齐两个数据集的经纬度网格和时间轴,避免因维度不匹配导致的运算失败
  • 确认CRU数据的时间分辨率(通常是月尺度),保证两个数据集的时间格式统一为datetime64类型

二、正确的差异计算步骤

1. 截取匹配的时间范围

从1901-2021的长时段数据中提取1979-2021的子数据集,确保两个数据集的时间长度一致:

import xarray as xr

# 加载原始数据
ds_full = xr.open_dataset('cru_pre_1901-2021.nc')
ds_recent = xr.open_dataset('cru_pre_1979-2021.nc')

# 截取长时段数据的匹配时间范围
ds_full_subset = ds_full.sel(time=slice('1979-01-01', '2021-12-31'))

# 对齐两个数据集(精确匹配经纬度和时间)
ds_full_aligned, ds_recent_aligned = xr.align(ds_full_subset, ds_recent, join='exact')

2. 计算降水差异

根据需求选择逐时间步差异或时段平均差异:

  • 逐时间步差异(展示每个月的降水波动差异):
    pre_diff = ds_recent_aligned['pre'] - ds_full_aligned['pre']
    
  • 时段平均差异(展示整体降水变化趋势,更常用):
    # 计算两个时段的月平均降水
    pre_mean_full = ds_full_aligned['pre'].mean(dim='time')
    pre_mean_recent = ds_recent_aligned['pre'].mean(dim='time')
    # 计算平均差异
    pre_diff_mean = pre_mean_recent - pre_mean_full
    

三、差异场可视化

用cartopy+matplotlib绘制差异图,清晰展示正负差异区域:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

fig, ax = plt.subplots(figsize=(12, 8), subplot_kw={'projection': ccrs.PlateCarree()})
ax.coastlines(linewidth=0.8)

# 绘制平均降水差异
im = pre_diff_mean.plot(ax=ax, transform=ccrs.PlateCarree(), 
                        cmap='RdBu_r', vmin=-20, vmax=20,
                        cbar_kwargs={'label': 'Precipitation Difference (mm/month)'})
plt.title('1979-2021 vs 1901-2021 Mean Precipitation Anomaly')
plt.show()

四、可参考的资料

  • CRU官方数据说明文档:明确数据的变量定义、单位、缺失值编码,避免因数据理解错误导致的运算偏差
  • xarray官方用户指南:重点学习维度对齐、数据子集化、算术运算的章节,掌握格点数据的核心操作逻辑
  • 气候学教材:了解降水差异分析的统计方法,明确绝对差异与相对差异的适用场景

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 01:05:23