netCDF4处理nc⽂件总结
variable used in lambda创建nc⽂件
# 数据类型包括:f4(32位浮点数),f8(64位浮点数),i1(8位符号整型),i2(16位符号整型),i4(32位符号整型),i8(64位符号整型),
#            u1(8位⽆符号整型),u2(16位⽆符号整型),u4(32位⽆符号整型),u8(64位⽆符号整型),S1(单字符字符串)
with nc.Dataset(nc_name, 'w', format='NETCDF4') as fnc:
#                    变量名数据类型维度⼤⼩
lat = ateVariable('lat', 'f4', ('lat',))  # 创建变量
lat.long_name = "latitude"  # 该变量的名称
lat.units = "degrees_north"  # 该变量的单位
lon = ateVariable('lon', 'f4', ('lon',))
lon.long_name = "longitude"
lon.units = "degrees_east"
#                                                  是否压缩数据压缩级别(1-9级)默认初始填充值
alt = ateVariable('alt', 'f4', ('lat', 'lon'), zlib=True, complevel=9, fill_value=-9999)
alt.long_name = 'height above sea level'
alt.units = 'meters'
alt.positive = 'up'  # ⾼度的⽅向  up为向上
PRS = ateVariable('PRS', 'short', ('lat', 'lon'), zlib=True, complevel=9, fill_value=-9999)  # ⽓压
PRS.long_name = 'station pressure'
PRS.units = 'hPa'
PRS.missing_value = -9999  # 默认缺测填充
PRS.add_offset = np.float32(0.0)  # 增加偏移
PRS.scale_factor = np.float32(0.1)  # ⽐例偏移实际值存⼊⾃动扩⼤10倍(除以0.1),取出⾃动缩⼩10倍(乘以0.1)
PRS.valid_min = 5200  # 限制存⼊最⼩值⼩于最⼩为缺测
PRS.valid_max = 10850  # 限制存⼊最⼤值⼤于最⼤为缺测
# 给变量赋值赋值的数据维度--必须和创建变量时指定的维度⼤⼩⼀致
fnc.variables['lat'][:] = np.arange(53.56, 18.16, -0.01)
fnc.variables['lon'][:] = np.arange(73.44, 135.09, 0.01)
fnc.variables['alt'][:] = np.array(data['alt'], dtype='float').reshape(3541, 6165)
fnc.variables['PRS'][:] = np.array(data['PRS'], dtype='float').reshape(3541, 6165)
fnc.author = 'ecoweather'  # 创建⽂件作者
创建有时间变量的nc⽂件
with nc.Dataset(nc_name, 'w', format='NETCDF4') as fnc:
tt = ateVariable('time', 'i4', 'time')
tt.long_name = 'time'
# 起始⼩时时间它会⾃动根据你赋值的数据,从起始⼩时累加
tt.units = 'hours since %s' % datetime.datetime.strptime(start_time, '%Y%m%d%H%M%S').strftime('%Y-%m-%d %H:%M:%S')    # ⽇历(公历)
tt.calendar = "gregorian"
sta = ateVariable('sta', 'i4', ('size',))  # 创建变量
sta.long_name = 'station number'  # 站号
lat = ateVariable('lat', 'f4', ('size',))
lat.long_name = "latitude"
lat.units = "degrees_north"
lon = ateVariable('lon', 'f4', ('size',))
lon.long_name = "longitude"
lon.units = "degrees_east"
PRE_1m = ateVariable('PRE_1m', 'f4', ('time', 'size'), zlib=True, complevel=9, fill_value=-9999)  # ⼀分钟降⽔
PRE_1m.long_name = 'precipitation of 1 min'
PRE_1m.units = 'mm'
PRE_1m.missing_value = -9999
PRE_1m._CoordinateAxes = 'lat lon'
fnc.variables['time'][:] = np.array([i for i in range(1, 25)])
fnc.variables['sta'][:] = np.array(list())
fnc.variables['lat'][:] = np.array(dictknn['lat'].tolist())
fnc.variables['lon'][:] = np.array(dictknn['lon'].tolist())
fnc.variables['PRE_1m'][:] = np.array(data)
读取nc⽂件
def read_nc(ncpath: str):
print('读取 nc ⽂件 %s' % ncpath)
f = nc.Dataset(ncpath, 'r')
# 获取nc⽂件中的变量名
var_key = list(f.variables.keys())
var_len = f.variables['lat'][:].shape
# print(var_key, var_len)
# 创建DataFrame存放数据,⼀列⼀类数据
nc_data = pd.DataFrame(index=range(var_len[0]), columns=var_key[1:], dtype='short')
for e in var_key:
# 获取变量数据
vardata = f.variables[e][:]
size = vardata.shape
# print('{}-{}'.format(e, size))  # (3541, 6165)  3541⾏,每⾏6165个
if len(size) == 1:
if e == 'sta':
# nc_data[e] = vardata
nc_data.index = vardata
else:
nc_data[e] = list(map(lambda s: float('%s' % s), vardata))
else:
nc_data[e] = list(vardata)
# print(f.variables)
f.close()
print(nc_data)
return nc_data
创建组
with nc.Dataset(nc_name, 'w', format='NETCDF4') as fnc:
# 创建⼀个组
group1 = ateGroup('LatLon_1441X2880')
lat1 = ateVariable('lat', 'f4', ('lat',), zlib=True, complevel=9, fill_value=-9999)
lat1.units = "degrees_north"
lon1 = ateVariable('lon', 'f4', ('lon',), zlib=True, complevel=9, fill_value=-9999)
lon1.units = "degrees_east"
group1.variables['lat'][:] = data[1441]['lat']
group1.variables['lon'][:] = data[1441]['lon']
for elem in data[1441]['value']:
d = ateVariable(elem['name'], 'short', ('lat', 'lon'), zlib=True, complevel=9, fill_value=-9999)            d.units = elem['units']
d.missing_value = -9999
d.add_offset = np.float32(0.0)
d.scale_factor = np.float32(0.1)
group1.variables[elem['name']][:] = elem['value']
group1.close  # 关闭组,注意,这⾥没有括号
# 创建⼀个组
group2 = ateGroup('LatLon_281X361')
lat2 = ateVariable('lat', 'f4', ('lat1',), zlib=True, complevel=9, fill_value=-9999)
lat2.units = "degrees_north"
lon2 = ateVariable('lon', 'f4', ('lon1',), zlib=True, complevel=9, fill_value=-9999)
lon2.units = "degrees_east"
group2.variables['lat'][:] = data[281]['lat']
group2.variables['lon'][:] = data[281]['lon']
for elem in data[281]['value']:
d = ateVariable(elem['name'], 'short', ('lat1', 'lon1'), zlib=True, complevel=9, fill_value=-9999)            d.units = elem['units']
d.missing_value = -9999
d.add_offset = np.float32(0.0)
d.scale_factor = np.float32(0.1)
group2.variables[elem['name']][:] = elem['value']
group2.close
fnc.author = 'ecoweather'

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。