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

CSV格式排放清单转NC4时Out_tonnes_per_year_by_cell无法识别为2D变量问题

网格化CSV排放清单转NetCDF二维变量问题及解决方案

目标

将.csv格式的网格化排放清单转换为netCDF(.nc4)格式,现有代码可完成转换,但无法将Out_tonnes_per_year_by_cell指定为关联经纬度(lat、lon)的可绘制2D变量。

.csv文件元数据

s1
Out[2]: 
        Long   Lat  Out_tonnes_per_year_by_cell
0     -179.5 -89.5                          0.0
1     -178.5 -89.5                          0.0
2     -177.5 -89.5                          0.0
3     -176.5 -89.5                          0.0
4     -175.5 -89.5                          0.0
     ...   ...                          ...
64795  175.5  89.5                          0.0
64796  176.5  89.5                          0.0
64797  177.5  89.5                          0.0
64798  178.5  89.5                          0.0
64799  179.5  89.5                          0.0

[64800 rows x 3 columns]

问题原因

原有代码直接将pandas DataFrame转为xarray Dataset时,所有变量默认以行序号为唯一维度,为一维结构,没有将经纬度设为独立坐标维度,也没有将排放数据重塑为对应经纬度网格的二维结构,因此输出的变量无法识别为关联经纬度的2D变量。

修正后完整代码

# 读取目标CSV排放清单并输出为符合CF规范的NetCDF文件,可直接导入GEOS-Chem GCHP的HEMCO模块使用

# 依赖库导入
import numpy as np
import pandas as pd
import xarray as xr

# 路径配置
GEO_C2H6_Dir = ('E:/PSU/CCAR REU/Emission(s) Inventories (GEOS-CHEM)/Simulation Data/Geo-CH4_emission_grid_files (Geologic)/Gridded Geologic C2H6 - Emissions Inventory/') 
GEO = GEO_C2H6_Dir + 'Total_geoC2H6_output_2018.csv'

# 读取CSV文件
s1 = pd.read_csv(GEO, sep = ",", skipinitialspace = True)

# 提取唯一经纬度序列,适配1°分辨率全球网格:共360个经度格点、180个纬度格点
lons = np.sort(s1['Long'].unique())
lats = np.sort(s1['Lat'].unique())

# 将一维排放数据重塑为(纬度, 经度)的二维网格结构
emission_data = s1['Out_tonnes_per_year_by_cell'].values.reshape(len(lats), len(lons))

# 构建符合规范的xarray Dataset,明确指定维度与坐标映射
xds1 = xr.Dataset(
    {
        'Out_tonnes_per_year_by_cell': (['Lat', 'Long'], emission_data)
    },
    coords={
        'Long': lons,
        'Lat': lats
    }
)

# 变量属性配置,符合CF公约规范
xds1['Long'].attrs = {'units':'degrees_east', 'Long_Name':'Longitudinal coordinate in decimal degrees'}
xds1['Lat'].attrs = {'units':'degrees_north', 'Long_Name':'Latitudinal coordinate in decimal degrees'}
xds1['Out_tonnes_per_year_by_cell'].attrs = {'units':'t yr-1', 'Long_Name':'Total Output of Cumulative Methane in units of Tonnes per Year in each Cell'}

# 全局属性配置
xds1.attrs = {'Conventions':'CF-1.0', 'Title':'Total_GEOCH4_Output_2018', 'summary':'Total output of Cumulative Geologic Emitted Methane from 2018'}

# 输出NetCDF文件
xds1.to_netcdf(GEO_C2H6_Dir + 'Total_GEOCH4_Output_2018.nc')

修正说明

  • 明确提取经纬度作为独立坐标维度,将排放数据重塑为对应二维网格,实现变量与经纬度的关联
  • 修正了经纬度、排放变量的单位属性,符合CF公约要求,可直接被GEOS-Chem HEMCO模块识别读取

内容的提问来源于stack exchange,提问作者Jordan-Yousef Jbour

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 01:57:02