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

Python计算气候数据温度距平时遇维度不兼容错误求助

问题

作为Python初学者,我尝试基于AIRS 2010-2015年月度温度时序观测数据,参考ECMWF 1950-1980年月度气候态数据计算温度距平。二者网格分辨率不同,我已完成重格网处理,但执行距平计算时出现错误:

incompatible dimensions for a grouped binary operation: the group variable “month” is not a dimension on the other argument

可复现代码如下:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
import xarray as xr
import scipy

# import the target data
airs_temp_2010_15 = xr.open_dataset('temp_2010_15.nc')

# import the climatological data
climatology_ecmwf = xr.open_dataset('monthly_climatology_1950_1980.nc')

# essential to change the lon progression patten in compatible with airs target data
lon_name = 'longitude'  # whatever name is in the data

# Adjust lon values to make sure they are within (-180, 180)
climatology_ecmwf['_longitude_adjusted'] = xr.where(
    climatology_ecmwf[lon_name] > 180,
    climatology_ecmwf[lon_name] - 360,
    climatology_ecmwf[lon_name])

# reassign the new coords to as the main lon coords
# and sort DataArray using new coordinate values
climatology_ecmwf = (
    climatology_ecmwf
    .swap_dims({lon_name: '_longitude_adjusted'})
    .sel(**{'_longitude_adjusted': sorted(climatology_ecmwf._longitude_adjusted)})
    .drop(lon_name))
# finally rename the field again to longitude
climatology_ecmwf = climatology_ecmwf.rename({'_longitude_adjusted': lon_name})
print(climatology_ecmwf)

# let's change the grid resolution of the ecmwf climatology data
regrid_lon_axis_clim = np.arange(-180,180, 0.5)
regrid_lat_axis_clim = np.arange(-90,90, 0.5)

# apply the regridding over the climatology from ecmwf
regridded_clim_ecmwf = climatology_ecmwf.interp(longitude=regrid_lon_axis_clim, latitude=regrid_lat_axis_clim)
print(regridded_clim_ecmwf['t2m'].shape)

# regrid the target data as well to make sure the shapes are same for both the data
regrid_lon_axis = np.arange(-180, 180, 0.5)
regrid_lat_axis = np.arange(-90,90, 0.5)

# apply it over the airs temperature data
regridded_temp_2010_15 = airs_temp_2010_15.interp(Longitude=regrid_lon_axis,Latitude=regrid_lat_axis)
print(regridded_temp_2010_15['Temperature'].shape)

# Isolate one layer from target data which will be used as mask layer to mask all values of climatological data

regridded_temp_2010_15_1st_layer = regridded_temp_2010_15.isel(time=0)


# Mask the regridded climatology data at per regridded airs data
masked_regrid_clim_t2m = regridded_clim_ecmwf.where(regridded_temp_2010_15_1st_layer['Temperature'].values >0)
print(masked_regrid_clim_t2m)

# Now compute the anomaly
anom_2010_15 = regridded_temp_2010_15.groupby('time.month') - masked_regrid_clim_t2m
解决方案

错误核心原因

当用groupby('time.month')对AIRS数据分组后,xarray要求气候态数据必须包含month维度,才能按月份匹配做减法。但你的气候态数据(masked_regrid_clim_t2m)是原始的长时序数据,没有单独的month维度,维度结构和分组后的AIRS数据完全不匹配。

修复步骤

  1. 提取气候态月平均:先对ECMWF 1950-1980年的月度数据计算1-12月的多年平均,得到仅包含month维度的气候态场。
  2. 统一变量名:将气候态数据的t2m重命名为Temperature,和AIRS数据的变量名保持一致,避免维度匹配时的隐性错误。
  3. 对齐维度后计算距平:用分组后的AIRS数据减去对应月份的气候态均值。

修改后的关键代码如下:

# 1. 计算ECMWF气候态的月平均(得到1-12月的多年平均场)
clim_monthly_mean = regridded_clim_ecmwf.groupby('time.month').mean(dim='time')
# 2. 统一变量名,和AIRS数据的Temperature匹配
clim_monthly_mean = clim_monthly_mean.rename({'t2m': 'Temperature'})
# 3. 对气候态月平均场应用mask(和原逻辑保持一致)
masked_clim_monthly_mean = clim_monthly_mean.where(regridded_temp_2010_15_1st_layer['Temperature'] > 0)
# 4. 计算距平:分组后的AIRS数据自动匹配对应月份的气候态均值
anom_2010_15 = regridded_temp_2010_15.groupby('time.month') - masked_clim_monthly_mean

补充说明

  • 原代码直接使用原始气候态时序数据,没有做月平均,导致维度和分组后的AIRS数据无法对齐。通过groupby('time.month').mean(dim='time')处理后,气候态数据维度变为(month, latitude, longitude),正好和AIRS分组后的结构匹配。
  • 变量名统一后,xarray会自动识别并对齐所有维度,无需手动指定匹配规则。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 17:14:55