参考这篇文章:https://blog.csdn.net/showpingzhang/article/details/83111475
这是下载地址:https://sourceforge.net/projects/netcdf4excel/
使用excel插件打开遇到两个问题,见下图:
第一个问题解决办法是在代码中将所有的Declare
替换成Declare PtrSafe
;第二个问题目前还没有解决遂放弃。貌似使用Office较低点的版本这个插件可以用,但是没有尝试。
根据这篇文章:Python提取netCDF数据并转换为csv文件的讲解,对.nc数据的处理思路大致是:
#!/usr/bin/env python3
from netCDF4 import Dataset
import numpy as np
import os
path = "D:\\PycharmProjects\\DataProcess\\MeteData\\2018010203_WS_TEM_SP_BLH2.nc"
dst = Dataset(path, mode='r', format="netCDF4")
# 查看nc文件中包含了什么
print(dst)
print('---------------------------------------------------------')
# 查看nc文件有哪些变量
print(dst.variables.keys())
print('--------------------------------------------------------')
# 查看nc文件中变量的属性名称
print(dst.variables.keys())
for i in dst.variables.keys():
print('%s: %s' % (i, dst.variables[i].ncattrs()))
print('--------------------------------------------------------')
# 查看nc文件中变量的属性的具体信息
print(dst.variables.keys())
for i in dst.variables.keys():
print(dst.variables[i])
print('\n')
print('-------------------------------------------------------')
# 获取维度的sizes信息,获得索引范围
for i in dst.dimensions.keys():
print('%s_sizes: %s' % (i, dst.dimensions[i].size))
print('------------------------------------------------------')
# 循环提取数据
# for i in dst.variables.keys():
# if i not in dst.dimensions.keys():
分开来讲,
1.
print(dst)
它的输出是:
root group (NETCDF4 data model, file format HDF5):
dimensions(sizes): time(288), lon(71), lat(61)
variables(dimensions): int32 time(time), float64 lon(lon), float64 lat(lat), int16 si10_NON_CDM(time,lat,lon), int16 tas(time,lat,lon), int16 blh_NON_CDM(time,lat,lon), int16 ps(time,lat,lon)
groups:
可以看到,.cn数据主要分为:dimensions(sizes),variables(dimensions), 和 groups三类
作为维度(dimensions)的time,lon,lat同时也被视为变量(variables)
2.
print(dst.variables.keys())
for i in dst.variables.keys():
print('%s: %s' % (i, dst.variables[i].ncattrs()))
这段输出结果是:
odict_keys(['time', 'lon', 'lat', 'si10_NON_CDM', 'tas', 'blh_NON_CDM', 'ps'])
odict_keys(['time', 'lon', 'lat', 'si10_NON_CDM', 'tas', 'blh_NON_CDM', 'ps'])
time: ['long_name', 'standard_name', 'axis', 'stored_direction', 'type', 'units', 'calendar']
lon: ['_FillValue', 'units', 'long_name', 'standard_name', 'axis', 'stored_direction', 'type', 'valid_max', 'valid_min']
lat: ['_FillValue', 'units', 'long_name', 'standard_name', 'axis', 'stored_direction', 'type', 'valid_max', 'valid_min']
si10_NON_CDM: ['_FillValue', 'units', 'long_name', 'standard_name', 'Conventions', 'history', 'institution', 'source', 'add_offset', 'scale_factor', 'missing_value']
tas: ['_FillValue', 'units', 'long_name', 'standard_name', 'cell_methods', 'cell_measures', 'comment', 'type', 'Conventions', 'history', 'institution', 'source', 'add_offset', 'scale_factor', 'missing_value']
blh_NON_CDM: ['_FillValue', 'units', 'long_name', 'standard_name', 'Conventions', 'history', 'institution', 'source', 'add_offset', 'scale_factor', 'missing_value']
ps: ['_FillValue', 'units', 'long_name', 'standard_name', 'cell_methods', 'cell_measures', 'comment', 'type', 'Conventions', 'history', 'institution', 'source', 'add_offset', 'scale_factor', 'missing_value']
使用variables.keys()
获取每个变量名,使用variables[i].ncattrs()
获取每个变量的属性名
3.
print(dst.variables.keys())
for i in dst.variables.keys():
print(dst.variables[i])
print('\n')
通过循环使用variables[i]
,输出每个变量的具体信息(包括所有属性信息),输出结果如下:
odict_keys(['time', 'lon', 'lat', 'si10_NON_CDM', 'tas', 'blh_NON_CDM', 'ps'])
int32 time(time)
long_name: time
standard_name: time
axis: T
stored_direction: increasing
type: double
units: hours since 1900-01-01
calendar: gregorian
unlimited dimensions:
current shape = (288,)
filling on, default _FillValue of -2147483647 used
float64 lon(lon)
_FillValue: nan
units: degrees_east
long_name: longitude
standard_name: longitude
axis: X
stored_direction: increasing
type: double
valid_max: 360.0
valid_min: -180.0
unlimited dimensions:
current shape = (71,)
filling on
float64 lat(lat)
_FillValue: nan
units: degrees_north
long_name: latitude
standard_name: latitude
axis: Y
stored_direction: increasing
type: double
valid_max: 90.0
valid_min: -90.0
unlimited dimensions:
current shape = (61,)
filling on
int16 si10_NON_CDM(time, lat, lon)
_FillValue: -32767
units: m s**-1
long_name: 10 metre wind speed
standard_name: si10_NON_CDM_NON_CDM
Conventions: CF-1.6
history: 2019-12-31 02:10:37 GMT by grib_to_netcdf-2.15.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -o /cache/data9/adaptor.mars.internal-1577758219.9776971-27136-7-b6f6b5fe-91ae-4c05-b1c1-c71593f8ce35.nc /cache/tmp/b6f6b5fe-91ae-4c05-b1c1-c71593f8ce35-adaptor.mars.internal-1577758219.9783804-27136-4-tmp.grib
institution: ECMWF
source: ECMWF
add_offset: 2.942976411903907
scale_factor: 6.10416630729552e-05
missing_value: -32767
unlimited dimensions:
current shape = (288, 61, 71)
filling on
int16 tas(time, lat, lon)
_FillValue: -32767
units: K
long_name: Near-Surface Air Temperature
standard_name: air_temperature
cell_methods: area: time: mean
cell_measures: area: areacella
comment: near-surface (usually, 2 meter) air temperature
type: real
Conventions: CF-1.6
history: 2019-12-31 02:11:06 GMT by grib_to_netcdf-2.15.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -o /cache/data5/adaptor.mars.internal-1577758239.0596352-31045-5-da136231-4632-427e-9fb6-4fd02bf48df4.nc /cache/tmp/da136231-4632-427e-9fb6-4fd02bf48df4-adaptor.mars.internal-1577758239.0607688-31045-2-tmp.grib
institution: ECMWF
source: ECMWF
add_offset: 286.02881901386394
scale_factor: 0.0007115816470804786
missing_value: -32767
unlimited dimensions:
current shape = (288, 61, 71)
filling on
int16 blh_NON_CDM(time, lat, lon)
_FillValue: -32767
units: m
long_name: Boundary layer height
standard_name: blh_NON_CDM_NON_CDM
Conventions: CF-1.6
history: 2019-12-31 02:11:39 GMT by grib_to_netcdf-2.15.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -o /cache/data8/adaptor.mars.internal-1577758270.2376764-8594-34-f5c65927-82c4-466a-8231-51fc09989a07.nc /cache/tmp/f5c65927-82c4-466a-8231-51fc09989a07-adaptor.mars.internal-1577758270.238204-8594-13-tmp.grib
institution: ECMWF
source: ECMWF
add_offset: 1226.2842591562364
scale_factor: 0.036645261745811744
missing_value: -32767
unlimited dimensions:
current shape = (288, 61, 71)
filling on
int16 ps(time, lat, lon)
_FillValue: -32767
units: Pa
long_name: Surface Air Pressure
standard_name: surface_air_pressure
cell_methods: area: time: mean
cell_measures: area: areacella
comment: surface pressure (not mean sea-level pressure), 2-D field to calculate the 3-D pressure field from hybrid coordinates
type: real
Conventions: CF-1.6
history: 2019-12-31 02:12:09 GMT by grib_to_netcdf-2.15.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -o /cache/data4/adaptor.mars.internal-1577758301.9568667-12141-21-8f0d4921-f64e-4068-9b05-512b2e4aacbd.nc /cache/tmp/8f0d4921-f64e-4068-9b05-512b2e4aacbd-adaptor.mars.internal-1577758301.9576428-12141-9-tmp.grib
institution: ECMWF
source: ECMWF
add_offset: 93586.2274913021
scale_factor: 0.2950173958158485
missing_value: -32767
unlimited dimensions:
current shape = (288, 61, 71)
filling on
for i in dst.dimensions.keys():
print('%s_sizes: %s' % (i, dst.dimensions[i].size))
这段代码是获取所有维度的size大小并输出,结果如下:
time_sizes: 288
lon_sizes: 71
lat_sizes: 61
其实,以上种种,都可以使用Panoply软件产看,并且生成简单的可视化图,具体操作可以参考:安装并使用Panoply (netCDF, HDF and GRIB Data Viewer),但是在使用python对.nc数据进行进一步处理时,那么获取这些基本信息也是基本的步骤.