本文目录导读:

- Python方案(最常用)
- NCL(NCAR Command Language)脚本
- CDO(Climate Data Operators)命令行
- WGRIB/WGRIB2 工具
- GrADS 脚本
- 实际案例:完整转换脚本
- 常用格式转换对照表
- 注意事项
Python方案(最常用)
使用 xarray 和 pynio
import xarray as xr
import glob
import os
# 批量转换 NetCDF 到 CSV
input_dir = "path/to/input/"
output_dir = "path/to/output/"
for file in glob.glob(input_dir + "*.nc"):
ds = xr.open_dataset(file)
# 转换为 CSV
df = ds.to_dataframe()
output_file = os.path.join(output_dir,
os.path.basename(file).replace('.nc', '.csv'))
df.to_csv(output_file)
ds.close()
使用 cfgrib 处理 GRIB 文件
import xarray as xr
import cfgrib
# GRIB 转 NetCDF
def grib_to_netcdf(grib_file, nc_file):
ds = xr.open_dataset(grib_file, engine='cfgrib')
ds.to_netcdf(nc_file)
NCL(NCAR Command Language)脚本
; 批量转换 NetCDF 到 GRIB
files = systemfunc("ls *.nc")
do i = 0, dimsizes(files)-1
f = addfile(files(i), "r")
; 处理数据并输出
system("cdo -f grb copy " + files(i) + " " + files(i) + ".grb")
end do
CDO(Climate Data Operators)命令行
# 批量转换 NetCDF 到 GRIB
for file in *.nc; do
cdo -f grb copy "$file" "${file%.nc}.grb"
done
# 批量转换 GRIB 到 NetCDF
for file in *.grb; do
cdo -f nc copy "$file" "${file%.grb}.nc"
done
# 提取特定变量
for file in *.nc; do
cdo selvar,temperature "$file" "temp_${file}"
done
WGRIB/WGRIB2 工具
# GRIB2 转 GRIB1
for file in *.grib2; do
wgrib2 "$file" -set_grib_type grib1 -grib_out "${file%.grib2}.grib"
done
# GRIB 转 CSV
for file in *.grb; do
wgrib2 "$file" -csv "${file%.grb}.csv"
done
GrADS 脚本
# 批量转换 CTL 到 NetCDF
for file in *.ctl; do
echo "
open $file
set fwrite ${file%.ctl}.nc
set sdfwrite -nc4
sdfwrite
" > temp.gs
grads -lbc temp.gs
done
实际案例:完整转换脚本
#!/usr/bin/env python3
"""
批量气象数据格式转换脚本
支持:NC ↔ GRIB ↔ CSV ↔ HDF
"""
import xarray as xr
import pandas as pd
import glob
import os
from concurrent.futures import ThreadPoolExecutor
import logging
logging.basicConfig(level=logging.INFO)
class WeatherDataConverter:
def __init__(self, input_dir, output_dir, output_format='netcdf'):
self.input_dir = input_dir
self.output_dir = output_dir
self.output_format = output_format
def convert_file(self, input_file):
"""转换单个文件"""
try:
# 读取数据
if input_file.endswith('.nc'):
ds = xr.open_dataset(input_file)
elif input_file.endswith('.grb') or input_file.endswith('.grib'):
ds = xr.open_dataset(input_file, engine='cfgrib')
else:
raise ValueError(f"不支持的文件格式: {input_file}")
# 生成输出文件名
base_name = os.path.basename(input_file).rsplit('.', 1)[0]
# 根据目标格式转换
if self.output_format == 'netcdf':
output_file = f"{self.output_dir}/{base_name}.nc"
ds.to_netcdf(output_file)
elif self.output_format == 'csv':
output_file = f"{self.output_dir}/{base_name}.csv"
df = ds.to_dataframe()
df.to_csv(output_file)
elif self.output_format == 'grib':
output_file = f"{self.output_dir}/{base_name}.grb"
# 使用 CDO 转换
os.system(f"cdo -f grb copy {input_file} {output_file}")
logging.info(f"成功转换: {input_file} -> {output_file}")
ds.close()
except Exception as e:
logging.error(f"转换失败 {input_file}: {str(e)}")
def batch_convert(self, pattern='*.nc'):
"""批量转换"""
files = glob.glob(f"{self.input_dir}/{pattern}")
# 使用多线程加速
with ThreadPoolExecutor(max_workers=4) as executor:
executor.map(self.convert_file, files)
# 使用示例
if __name__ == "__main__":
converter = WeatherDataConverter(
input_dir="/data/weather/input/",
output_dir="/data/weather/output/",
output_format="csv"
)
converter.batch_convert("*.nc")
常用格式转换对照表
| 源格式 | 目标格式 | 推荐工具 | 示例命令 |
|---|---|---|---|
| NetCDF | GRIB | CDO | cdo -f grb copy input.nc output.grb |
| GRIB | NetCDF | CDO | cdo -f nc copy input.grb output.nc |
| NetCDF | CSV | Python | xarray.to_dataframe().to_csv() |
| GRIB | CSV | WGRIB2 | wgrib2 input.grb -csv output.csv |
| NetCDF | HDF5 | Python | xarray.to_netcdf() with engine='h5netcdf' |
注意事项
- 数据质量检查:转换前后检查数据完整性
- 坐标系统:确保坐标系统正确转换
- 缺失值处理:注意不同格式的缺失值表示
- 压缩选项:大文件建议使用压缩(如
-nc4选项)
选择哪种方法取决于:
- 数据格式和大小
- 系统环境(Linux/Windows)
- 性能要求
- 是否需要特殊处理
对于大多数气象数据转换需求,Python + xarray + CDO 组合是最灵活且功能最完善的解决方案。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播