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

使用Cartopy绘制HDF5数据集时的经纬度与数据维度匹配问题求助

解决HDF5数据经纬度与观测变量形状不匹配的Cartopy绘图问题

首先,你的推测基本是对的:(4224, 3)的经纬度形状,大概率对应4224条卫星扫描轨道/观测记录,每条记录包含扫描路径的起始、中间、终点三个位置的经纬度;而(4224, 399)的消光系数,应该是每条扫描路径上的399个采样点(可能是沿轨道的水平采样,或者垂直高度层的观测值)。

要让数据适配Cartopy绘图,核心是让每个消光系数值都对应一对经纬度,下面提供两种实用的解决方案:


方法1:取中间经纬度并扩展维度(快速简便)

如果每条扫描的399个数据点属于同一轨道段,不需要太精细的位置对应,可以直接用每条扫描的中间经纬度作为该段所有数据点的位置,再扩展维度匹配消光系数:

import numpy as np

# 提取每条扫描的中间经纬度点(索引1对应中间位置)
lon_mid = f['Longitude'][:, 1]  # 形状变为 (4224,)
lat_mid = f['Latitude'][:, 1]    # 形状变为 (4224,)

# 将经纬度扩展为 (4224, 399),和消光系数形状一致
lon_expanded = np.repeat(lon_mid[:, np.newaxis], 399, axis=1)
lat_expanded = np.repeat(lat_mid[:, np.newaxis], 399, axis=1)

# 展平为一维数组,适合Cartopy散点/密度绘图
lon_flat = lon_expanded.flatten()
lat_flat = lat_expanded.flatten()
ext_flat = f['Extinction_Coefficient_532'].flatten()

方法2:线性插值生成精确对应位置(更准确)

如果399个消光系数点是沿扫描路径(从起点到终点)的连续采样,建议对每条扫描的经纬度做线性插值,生成399个匹配的位置点:

import numpy as np

# 定义原始3个点的位置权重(0=起点,0.5=中点,1=终点)
x_original = np.array([0, 0.5, 1])
# 生成399个插值点的权重(从0到1均匀分布)
x_new = np.linspace(0, 1, 399)

# 对每条扫描的经纬度分别插值
lon_interp = np.array([np.interp(x_new, x_original, lon_scan) for lon_scan in f['Longitude']])
lat_interp = np.array([np.interp(x_new, x_original, lat_scan) for lat_scan in f['Latitude']])

# 此时 lon_interp 和 lat_interp 的形状都是 (4224, 399),直接和消光系数匹配
ext_data = f['Extinction_Coefficient_532']

Cartopy绘图示例

用插值后的经纬度和消光系数绘制散点图:

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

# 创建PlateCarree投影地图
fig, ax = plt.subplots(figsize=(10, 8), subplot_kw={'projection': ccrs.PlateCarree()})
ax.coastlines(resolution='50m', linewidth=0.8)
ax.gridlines(draw_labels=True, linestyle='--')

# 绘制散点,用消光系数值映射颜色
scatter = ax.scatter(
    lon_interp.flatten(), 
    lat_interp.flatten(), 
    c=ext_data.flatten(), 
    cmap='viridis', 
    s=5,  # 点大小可调整
    transform=ccrs.PlateCarree()
)

# 添加颜色条和标题
plt.colorbar(scatter, ax=ax, label='Extinction Coefficient (532 nm)')
plt.title('Satellite Observed Extinction Coefficient')
plt.show()

额外注意事项

  1. 验证经纬度含义:可以通过查看变量属性确认:

    print(f['Longitude'].attrs)
    print(f['Latitude'].attrs)
    

    通常会有units(确认是度)和long_name字段,帮你确认3个维度的具体含义。

  2. 数据类型处理:你的消光系数是>f4(大端序单精度浮点数),numpy会自动识别处理,如果遇到读取问题,可手动转换:

    ext_data = f['Extinction_Coefficient_532'].astype(np.float32)
    
  3. 垂直层数据的情况:如果399是垂直高度层,你可以选择特定高度层绘图,比如取第100层:

    ext_layer = f['Extinction_Coefficient_532'][:, 100]
    # 此时用 (lon_mid, lat_mid) 直接匹配绘图即可
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 12:44:06