将二维数组写入 NetCDF 文件

时间:2021-02-01 15:49:47

标签: python-3.x netcdf4

我有 3 个二维数组,每个数组都有一个形状 (1248,1620):纬度、经度,都从初始 nc 文件中检索,以及我的数据输出。我想将其保存为 .nc 文件以备后用。

print(output.shape)
print(lons.shape)
print(lats.shape)

(1248,1620) (1248,1620) (1248,1620)

import NetCDF4 as nc
filename = './Turbidity.nc'
ds = nc.Dataset(filename, mode='w', format='NETCDF4')
ds.createDimension('Turbidity', len(output))
ds.createDimension('latitude', len(lats))
ds.createDimension('longitude', len(lons))
T = ds.createVariable('T', 'f4', ('latitude', 'longitude'), zlib=True, fill_value=np.nan)
lat = ds.createVariable('lat', 'f4', ('latitude', 'longitude'), zlib=True)
lon = ds.createVariable('lon', 'f4', ('latitude', 'longitude'), zlib=True)

T[:] = output
lat[:] = lats
lon[:] = lons

根据我对 NetCDF4 文档的理解,变量现在应该包含数据,但是,它们是空的。

test = ds.variables['lat'][:]
print(test)

[[-- -- -- ... -- -- --] [-- -- -- ... -- -- --] ... [-- -- -- ... -- -- --] [-- -- -- ... -- -- --]]

我在这里遗漏了什么?

0 个答案:

没有答案